util.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. if(storageState){
  151. //设置状态中的各种值
  152. _this.$store.commit('setIsLogin', storageState.isLogin);
  153. _this.$store.commit('setToken', storageState.token);
  154. _this.$store.commit('setAvatar', storageState.avatar);
  155. _this.$store.commit('setName', storageState.name);
  156. _this.$store.commit('setIntroduction', storageState.introduction);
  157. _this.$store.commit('setRoles', storageState.roles);
  158. _this.$store.commit('setUser', storageState.user);
  159. _this.$store.commit('setStationName', storageState.stationName);
  160. _this.$store.commit('setStationCode', storageState.stationCode);
  161. _this.$store.commit('setDepartmentName', storageState.departmentName);
  162. _this.$store.commit('setDepartmentCode', storageState.departmentCode);
  163. _this.$store.commit('setProjectCode', storageState.projectCode);
  164. }
  165. }
  166. /* 获取状态值 */
  167. function getState(_this,key){
  168. let storageState=uni.getStorageSync("storage_state");
  169. if(key==='isLogin'){
  170. if(!_this.$store.state.isLogin){
  171. if(storageState){
  172. _this.$store.commit('setIsLogin', storageState.isLogin);
  173. }
  174. }
  175. return _this.$store.state.isLogin;
  176. }
  177. if(key==='userCode'){
  178. if(!_this.$store.state.user||!_this.$store.state.user.userCode){
  179. if(storageState){
  180. _this.$store.commit('setUser', storageState.user);
  181. }
  182. }
  183. return _this.$store.state.user.userCode;
  184. }
  185. if (key == 'projectCode') {
  186. if(!_this.$store.state.projectCode){
  187. if(storageState){
  188. _this.$store.commit('setProjectCode', storageState.projectCode);
  189. }
  190. }
  191. return _this.$store.state.projectCode;
  192. }
  193. return "";
  194. }
  195. /* 日期格式 */
  196. var getDate=function(type) {
  197. const date = new Date();
  198. let year = date.getFullYear();
  199. let month = date.getMonth() + 1;
  200. let day = date.getDate();
  201. if (type === 'start') {
  202. year = year - 60;
  203. } else if (type === 'end') {
  204. year = year + 2;
  205. }
  206. month = month > 9 ? month : '0' + month;;
  207. day = day > 9 ? day : '0' + day;
  208. return `${year}-${month}-${day}`;
  209. }
  210. /* 禁用左右翻页 */
  211. function disableTurnPage(){
  212. document.addEventListener("touchstart",function(e){
  213. startX = e.touches[0].pageX;
  214. startY = e.touches[0].pageY;
  215. });
  216. document.addEventListener('touchmove', function(event) {
  217. //console.info('左右滑动',event);
  218. //当屏幕有多个touch或者页面被缩放过,就不执行move操作
  219. if (event.targetTouches.length > 1 || event.scale && event.scale !== 1) return;
  220. var touch = event.targetTouches[0];
  221. endPos = {
  222. x: touch.pageX - startX,
  223. y: touch.pageY - startY
  224. };
  225. isScrolling = Math.abs(endPos.x) < Math.abs(endPos.y) ? 1 : 0; //isScrolling为1时,表示纵向滑动,0为横向滑动
  226. if (isScrolling === 0) { //横向滑动
  227. // alert(0);
  228. event.preventDefault(); //阻止触摸事件的默认行为,即阻止滚屏
  229. }
  230. }, { passive: false });
  231. }
  232. /**
  233. * 判断变量是否为空,
  234. * @param {[type]} param 变量
  235. * @return {Boolean} 为空返回true,否则返回false。
  236. */
  237. function isEmpty(param){
  238. if(param){
  239. var param_type = typeof(param);
  240. if(param_type == 'object'){
  241. //要判断的是【对象】或【数组】或【null】等
  242. if(typeof(param.length) == 'undefined'){
  243. if(JSON.stringify(param) == "{}"){
  244. return true;//空值,空对象
  245. }
  246. }else if(param.length == 0){
  247. return true;//空值,空数组
  248. }
  249. }else if(param_type == 'string'){
  250. //如果要过滤空格等字符
  251. var new_param = param.trim();
  252. if(new_param.length == 0){
  253. //空值,例如:带有空格的字符串" "。
  254. return true;
  255. }
  256. }else if(param_type == 'boolean'){
  257. if(!param){
  258. return true;
  259. }
  260. }else if(param_type== 'number'){
  261. if(!param){
  262. return true;
  263. }
  264. }
  265. return false;//非空值
  266. }else{
  267. //空值,例如:
  268. //(1)null
  269. //(2)可能使用了js的内置的名称,例如:var name=[],这个打印类型是字符串类型。
  270. //(3)空字符串''、""。
  271. //(4)数字0、00等,如果可以只输入0,则需要另外判断。
  272. return true;
  273. }
  274. }
  275. module.exports = {
  276. formatTime: formatTime,
  277. formatLocation: formatLocation,
  278. dateUtils: dateUtils,
  279. getQuery:getQuery , //获取url的某个参数
  280. persistLogin:persistLogin,
  281. getDate:getDate,
  282. setState:setState, //自storage中获取状态值,重新给状态赋值
  283. getState:getState,
  284. isEmpty:isEmpty
  285. }