util.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. disableTurnPage();
  69. let storageState = uni.getStorageSync('storage_state');
  70. //console.info('_this.$store',_this.$store);
  71. if(_this.$store.state){
  72. if (!_this.$store.state.isLogin) { //缓存中有值,重新赋值
  73. if (storageState.isLogin) {
  74. console.info("缓存中storageState isLogin为真有值重新给状态赋值");
  75. setState(_this,storageState);
  76. } else { //缓存中没值,跳转到登录页面
  77. //console.info("当前页面路径:" + _this.route);
  78. console.info("缓存中storageState isLogin为假");
  79. if (_this.route !== "/" && _this.route.toLocaleLowerCase() !== "/pages/login/login") {
  80. console.info('util重新登录');
  81. uni.navigateTo({
  82. url: '/'
  83. });
  84. }
  85. }
  86. } else {
  87. console.info("_this.$store.state.isLogin为真")
  88. setStateFromStorage(_this,storageState);
  89. }
  90. }
  91. else{
  92. console.info('_this.$store.state=null');
  93. setStateFromStorage(_this);
  94. }
  95. }
  96. function setStateFromStorage(_this,storageState){
  97. if (storageState) {
  98. let obj = _this.$store.state;
  99. for (let key in obj) {
  100. if (obj[key]) {
  101. //设置状态中的各种值
  102. if (key == 'token') {
  103. _this.$store.commit('setToken', storageState.token);
  104. }
  105. if (key == 'avatar') {
  106. _this.$store.commit('setAvatar', storageState.avatar);
  107. }
  108. if (key == 'name') {
  109. _this.$store.commit('setName', storageState.name);
  110. }
  111. if (key == 'introduction') {
  112. _this.$store.commit('setIntroduction', storageState.introduction);
  113. }
  114. if (key == 'roles') {
  115. _this.$store.commit('setRoles', storageState.roles);
  116. }
  117. if (key == 'user') {
  118. _this.$store.commit('setUser', storageState.user);
  119. }
  120. if (key == 'stationName') {
  121. _this.$store.commit('setStationName', storageState.stationName);
  122. }
  123. if (key == 'stationCode') {
  124. _this.$store.commit('setStationCode', storageState.stationCode);
  125. }
  126. if (key == 'departmentName') {
  127. _this.$store.commit('setDepartmentName', storageState.departmentName);
  128. }
  129. if (key == 'departmentCode') {
  130. _this.$store.commit('setDepartmentCode', storageState.departmentCode);
  131. }
  132. if (key == 'projectCode') {
  133. _this.$store.commit('setProjectCode', storageState.projectCode);
  134. }
  135. }
  136. }
  137. }
  138. else{
  139. //console.info("当前页面路径:" + _this.route);
  140. if (_this.route !== "/" && _this.route.toLocaleLowerCase() !== "/pages/login/login") {
  141. console.info('util重新登录');
  142. uni.navigateTo({
  143. url: '/'
  144. });
  145. }
  146. }
  147. }
  148. /* 设置状态值 */
  149. function setState(_this,storageState){
  150. //设置状态中的各种值
  151. _this.$store.commit('setIsLogin', storageState.isLogin);
  152. _this.$store.commit('setToken', storageState.tnToken);
  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. function getState(_this,key){
  166. let storageState=uni.getStorageSync("storage_state");
  167. console.info('storageState',storageState);
  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. /* 禁用左右翻页 */
  210. function disableTurnPage(){
  211. document.addEventListener("touchstart",function(e){
  212. startX = e.touches[0].pageX;
  213. startY = e.touches[0].pageY;
  214. });
  215. document.addEventListener('touchmove', function(event) {
  216. //console.info('左右滑动',event);
  217. //当屏幕有多个touch或者页面被缩放过,就不执行move操作
  218. if (event.targetTouches.length > 1 || event.scale && event.scale !== 1) return;
  219. var touch = event.targetTouches[0];
  220. endPos = {
  221. x: touch.pageX - startX,
  222. y: touch.pageY - startY
  223. };
  224. isScrolling = Math.abs(endPos.x) < Math.abs(endPos.y) ? 1 : 0; //isScrolling为1时,表示纵向滑动,0为横向滑动
  225. if (isScrolling === 0) { //横向滑动
  226. // alert(0);
  227. event.preventDefault(); //阻止触摸事件的默认行为,即阻止滚屏
  228. }
  229. }, { passive: false });
  230. }
  231. /**
  232. * 判断变量是否为空,
  233. * @param {[type]} param 变量
  234. * @return {Boolean} 为空返回true,否则返回false。
  235. */
  236. function isEmpty(param){
  237. if(param){
  238. var param_type = typeof(param);
  239. if(param_type == 'object'){
  240. //要判断的是【对象】或【数组】或【null】等
  241. if(typeof(param.length) == 'undefined'){
  242. if(JSON.stringify(param) == "{}"){
  243. return true;//空值,空对象
  244. }
  245. }else if(param.length == 0){
  246. return true;//空值,空数组
  247. }
  248. }else if(param_type == 'string'){
  249. //如果要过滤空格等字符
  250. var new_param = param.trim();
  251. if(new_param.length == 0){
  252. //空值,例如:带有空格的字符串" "。
  253. return true;
  254. }
  255. }else if(param_type == 'boolean'){
  256. if(!param){
  257. return true;
  258. }
  259. }else if(param_type== 'number'){
  260. if(!param){
  261. return true;
  262. }
  263. }
  264. return false;//非空值
  265. }else{
  266. //空值,例如:
  267. //(1)null
  268. //(2)可能使用了js的内置的名称,例如:var name=[],这个打印类型是字符串类型。
  269. //(3)空字符串''、""。
  270. //(4)数字0、00等,如果可以只输入0,则需要另外判断。
  271. return true;
  272. }
  273. }
  274. module.exports = {
  275. formatTime: formatTime,
  276. formatLocation: formatLocation,
  277. dateUtils: dateUtils,
  278. getQuery:getQuery , //获取url的某个参数
  279. persistLogin:persistLogin,
  280. getDate:getDate,
  281. setState:setState, //自storage中获取状态值,重新给状态赋值
  282. getState:getState,
  283. isEmpty:isEmpty
  284. }