util.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. function formatTime(time) {
  2. if (typeof time !== 'number' || time < 0) {
  3. return time
  4. }
  5. var hour = parseInt(time / 3600)
  6. time = time % 3600
  7. var minute = parseInt(time / 60)
  8. time = time % 60
  9. var second = time
  10. return ([hour, minute, second]).map(function (n) {
  11. n = n.toString()
  12. return n[1] ? n : '0' + n
  13. }).join(':')
  14. }
  15. function formatLocation(longitude, latitude) {
  16. if (typeof longitude === 'string' && typeof latitude === 'string') {
  17. longitude = parseFloat(longitude)
  18. latitude = parseFloat(latitude)
  19. }
  20. longitude = longitude.toFixed(2)
  21. latitude = latitude.toFixed(2)
  22. return {
  23. longitude: longitude.toString().split('.'),
  24. latitude: latitude.toString().split('.')
  25. }
  26. }
  27. var dateUtils = {
  28. UNITS: {
  29. '年': 31557600000,
  30. '月': 2629800000,
  31. '天': 86400000,
  32. '小时': 3600000,
  33. '分钟': 60000,
  34. '秒': 1000
  35. },
  36. humanize: function (milliseconds) {
  37. var humanize = '';
  38. for (var key in this.UNITS) {
  39. if (milliseconds >= this.UNITS[key]) {
  40. humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
  41. break;
  42. }
  43. }
  44. return humanize || '刚刚';
  45. },
  46. format: function (dateStr) {
  47. var date = this.parse(dateStr)
  48. var diff = Date.now() - date.getTime();
  49. if (diff < this.UNITS['天']) {
  50. return this.humanize(diff);
  51. }
  52. var _format = function (number) {
  53. return (number < 10 ? ('0' + number) : number);
  54. };
  55. return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
  56. _format(date.getHours()) + ':' + _format(date.getMinutes());
  57. },
  58. parse: function (str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
  59. var a = str.split(/[^0-9]/);
  60. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  61. }
  62. };
  63. var getQuery=function(name){
  64. return decodeURIComponent((new RegExp('[?|&]'+name+'='+'([^&;]+?)(&|#|;|$)').exec(location.href)||[,""])[1].replace(/\+/g,'%20'))||null;
  65. }
  66. /* 登录判断 */
  67. var persistLogin=function(_this) {
  68. let storageState = uni.getStorageSync('storage_state');
  69. //console.info('_this.$store',_this.$store);
  70. if(_this.$store.state){
  71. if (!_this.$store.state.isLogin) { //缓存中有值,重新赋值
  72. if (storageState.isLogin) {
  73. console.info("缓存中storageState isLogin为真有值重新给状态赋值");
  74. setState(_this,storageState);
  75. } else { //缓存中没值,跳转到登录页面
  76. //console.info("当前页面路径:" + _this.route);
  77. console.info("缓存中storageState isLogin为假");
  78. if (_this.route !== "/" && _this.route.toLocaleLowerCase() !== "/pages/login/login") {
  79. console.info('util重新登录');
  80. uni.navigateTo({
  81. url: '/'
  82. });
  83. }
  84. }
  85. } else {
  86. console.info("_this.$store.state.isLogin为真")
  87. setStateFromStorage(_this,storageState);
  88. }
  89. }
  90. else{
  91. console.info('_this.$store.state=null');
  92. setStateFromStorage(_this);
  93. }
  94. }
  95. function setStateFromStorage(_this,storageState){
  96. if (storageState) {
  97. let obj = _this.$store.state;
  98. for (let key in obj) {
  99. if (obj[key]) {
  100. //设置状态中的各种值
  101. if (key == 'token') {
  102. _this.$store.commit('setToken', storageState.token);
  103. }
  104. if (key == 'avatar') {
  105. _this.$store.commit('setAvatar', storageState.avatar);
  106. }
  107. if (key == 'name') {
  108. _this.$store.commit('setName', storageState.name);
  109. }
  110. if (key == 'introduction') {
  111. _this.$store.commit('setIntroduction', storageState.introduction);
  112. }
  113. if (key == 'roles') {
  114. _this.$store.commit('setRoles', storageState.roles);
  115. }
  116. if (key == 'user') {
  117. _this.$store.commit('setUser', storageState.user);
  118. }
  119. if (key == 'stationName') {
  120. _this.$store.commit('setStationName', storageState.stationName);
  121. }
  122. if (key == 'stationCode') {
  123. _this.$store.commit('setStationCode', storageState.stationCode);
  124. }
  125. if (key == 'departmentName') {
  126. _this.$store.commit('setDepartmentName', storageState.departmentName);
  127. }
  128. if (key == 'departmentCode') {
  129. _this.$store.commit('setDepartmentCode', storageState.departmentCode);
  130. }
  131. if (key == 'projectCode') {
  132. _this.$store.commit('setProjectCode', storageState.projectCode);
  133. }
  134. }
  135. }
  136. }
  137. else{
  138. //console.info("当前页面路径:" + _this.route);
  139. if (_this.route !== "/" && _this.route.toLocaleLowerCase() !== "/pages/login/login") {
  140. console.info('util重新登录');
  141. uni.navigateTo({
  142. url: '/'
  143. });
  144. }
  145. }
  146. }
  147. /* 设置状态值 */
  148. function setState(_this,storageState){
  149. if(storageState){
  150. //设置状态中的各种值
  151. _this.$store.commit('setIsLogin', storageState.isLogin);
  152. _this.$store.commit('setToken', storageState.token);
  153. _this.$store.commit('setAvatar', storageState.avatar);
  154. _this.$store.commit('setName', storageState.name);
  155. _this.$store.commit('setIntroduction', storageState.introduction);
  156. _this.$store.commit('setRoles', storageState.roles);
  157. _this.$store.commit('setUser', storageState.user);
  158. _this.$store.commit('setStationName', storageState.stationName);
  159. _this.$store.commit('setStationCode', storageState.stationCode);
  160. _this.$store.commit('setDepartmentName', storageState.departmentName);
  161. _this.$store.commit('setDepartmentCode', storageState.departmentCode);
  162. _this.$store.commit('setProjectCode', storageState.projectCode);
  163. }
  164. }
  165. /* 获取状态值 */
  166. function getState(_this,key){
  167. let storageState=uni.getStorageSync("storage_state");
  168. if(key==='isLogin'){
  169. if(!_this.$store.state.isLogin){
  170. if(storageState){
  171. _this.$store.commit('setIsLogin', storageState.isLogin);
  172. }
  173. }
  174. return _this.$store.state.isLogin;
  175. }
  176. if(key==='userCode'){
  177. if(!_this.$store.state.user||!_this.$store.state.user.userCode){
  178. if(storageState){
  179. _this.$store.commit('setUser', storageState.user);
  180. }
  181. }
  182. return _this.$store.state.user.userCode;
  183. }
  184. if (key == 'projectCode') {
  185. if(!_this.$store.state.projectCode){
  186. if(storageState){
  187. _this.$store.commit('setProjectCode', storageState.projectCode);
  188. }
  189. }
  190. return _this.$store.state.projectCode;
  191. }
  192. return "";
  193. }
  194. /* 日期格式 */
  195. var getDate=function(type) {
  196. const date = new Date();
  197. let year = date.getFullYear();
  198. let month = date.getMonth() + 1;
  199. let day = date.getDate();
  200. if (type === 'start') {
  201. year = year - 60;
  202. } else if (type === 'end') {
  203. year = year + 2;
  204. }
  205. month = month > 9 ? month : '0' + month;;
  206. day = day > 9 ? day : '0' + day;
  207. return `${year}-${month}-${day}`;
  208. }
  209. module.exports = {
  210. formatTime: formatTime,
  211. formatLocation: formatLocation,
  212. dateUtils: dateUtils,
  213. getQuery:getQuery , //获取url的某个参数
  214. persistLogin:persistLogin,
  215. getDate:getDate,
  216. setState:setState, //自storage中获取状态值,重新给状态赋值
  217. getState:getState
  218. }