jmsUtil.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import moment from 'moment';
  2. const app = getApp();
  3. const formatTime = date => {
  4. const year = date.getFullYear()
  5. const month = date.getMonth() + 1
  6. const day = date.getDate()
  7. const hour = date.getHours()
  8. const minute = date.getMinutes()
  9. const second = date.getSeconds()
  10. return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  11. }
  12. const getWeekByDate = dates => {
  13. let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
  14. let date = new Date(dates);
  15. date.setDate(date.getDate());
  16. let day = date.getDay();
  17. return show_day[day];
  18. }
  19. const formatNumber = n => {
  20. n = n.toString()
  21. return n[1] ? n : `0${n}`
  22. }
  23. function formatDate(date) {
  24. var year = date.getFullYear();
  25. var month = date.getMonth() + 1;
  26. var day = date.getDate();
  27. if (month < 10)
  28. month = "0" + month;
  29. if (day < 10)
  30. day = "0" + day;
  31. return [year, month, day].map(formatNumber).join('-');
  32. }
  33. //时间戳转日期,falg:true表示只要年月日,part: year month date
  34. function toDate(number, flag, part) {
  35. var n = number;
  36. var date = new Date(parseInt(n) * 1000);
  37. var y = date.getFullYear();
  38. var m = date.getMonth() + 1;
  39. m = m < 10 ? ('0' + m) : m;
  40. var d = date.getDate();
  41. d = d < 10 ? ('0' + d) : d;
  42. var h = date.getHours();
  43. h = h < 10 ? ('0' + h) : h;
  44. var minute = date.getMinutes();
  45. var second = date.getSeconds();
  46. minute = minute < 10 ? ('0' + minute) : minute;
  47. second = second < 10 ? ('0' + second) : second;
  48. if (flag) {
  49. if (part == "year") {
  50. return y;
  51. } else if (part == "month") {
  52. return m;
  53. } else if (part == "date") {
  54. return n;
  55. }
  56. return y + '-' + m + '-' + d;
  57. }
  58. return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
  59. }
  60. //当前日期加减天数,falg:true表示只要年月日
  61. function mathChangeDate(date, method, days, flag) {
  62. //method:'+' || '-'
  63. //ios不解析带'-'的日期格式,要转成'/',不然Nan,切记
  64. var dateVal = date.replace(/-/g, '/');
  65. var timestamp = Date.parse(dateVal);
  66. if (method == '+') {
  67. timestamp = timestamp / 1000 + 24 * 60 * 60 * days;
  68. } else if (method == '-') {
  69. timestamp = timestamp / 1000 - 24 * 60 * 60 * days;
  70. }
  71. return toDate(timestamp, flag);
  72. }
  73. /**
  74. * 封封微信的的request
  75. */
  76. function request(url, data = {}, method = "GET") {
  77. var url=url;
  78. // console.log(app.globalData);
  79. let isUat=wx.getStorageSync('isUat');
  80. if(!isUat){
  81. // url=url.replace('uat.','');
  82. }
  83. return new Promise(function (resolve, reject) {
  84. wx.request({
  85. url: url,
  86. data: data,
  87. method: method,
  88. header: {
  89. 'Content-Type': 'application/json',
  90. 'token': wx.getStorageSync('token')
  91. },
  92. success: function (res) {
  93. if (res.statusCode == 200) {
  94. if (res.data.errno == 501||res.data.errno == 401) {
  95. wx.hideLoading();
  96. // 清除登录相关内容
  97. try {
  98. wx.removeStorageSync('userInfo');
  99. wx.removeStorageSync('token');
  100. } catch (e) {
  101. // Do something when catch error
  102. }
  103. // 切换到登录页面
  104. // wx.redirectTo({
  105. // url: '/pages/temp/login/login'
  106. // });
  107. wx.redirectTo({
  108. url: '/pages/index/index'
  109. });
  110. } else {
  111. resolve(res.data);
  112. }
  113. } else {
  114. reject(res.errMsg);
  115. }
  116. },
  117. fail: function (err) {
  118. reject(err)
  119. }
  120. })
  121. });
  122. }
  123. function showErrorToast(msg) {
  124. wx.showToast({
  125. title: msg,
  126. image: '/static/images/icon_error.png'
  127. })
  128. }
  129. function formatweek(date) {
  130. let week = date.getDay();
  131. let weekArr = '';
  132. switch (week) {
  133. case 1:
  134. weekArr = '周一';
  135. break;
  136. case 2:
  137. weekArr = '周二';
  138. break;
  139. case 3:
  140. weekArr = '周三';
  141. break;
  142. case 4:
  143. weekArr = '周四';
  144. break;
  145. case 5:
  146. weekArr = '周五';
  147. break;
  148. case 6:
  149. weekArr = '周六';
  150. break;
  151. case 0:
  152. weekArr = '周日';
  153. break;
  154. }
  155. return weekArr;
  156. }
  157. function getDateList(date, interval, length) {
  158. let dateList = [];
  159. let len = (length ? length : 7);
  160. for (let i = 0; i < len; i++) {
  161. let datestamp = new Date(date.getTime() + i * 60 * 60 * 1000 * 24 * interval);
  162. let dateItem = {
  163. week: formatweek(datestamp),
  164. date: formetdate(datestamp),
  165. id: new Date(datestamp).getDay()
  166. };
  167. dateList.push(dateItem);
  168. }
  169. return dateList;
  170. }
  171. function dateDifference(end) {
  172. // //比较2个日期
  173. // console.log(start + ",," + end);
  174. // var stime = new Date(start).getTime();
  175. var stime = new Date().getTime();
  176. console.log(end.replace(/-/g, "/"));
  177. var etime = new Date(end.replace(/-/g, "/")).getTime();
  178. var usedTime = etime - stime; //两个时间戳相差的毫秒数
  179. var days = Math.floor(usedTime / (24 * 3600 * 1000));
  180. //计算出小时数
  181. var leave1 = usedTime % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  182. var hours = Math.floor(leave1 / (3600 * 1000));
  183. //计算相差分钟数
  184. var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  185. var minutes = Math.floor(leave2 / (60 * 1000));
  186. if (days == 0 && hours == 0) {
  187. return minutes + "分钟";
  188. }
  189. if (days == 0) {
  190. return hours + "时" + minutes + "分钟";
  191. }
  192. var time = days + "天" + hours + "时" + minutes + "分钟";
  193. // var time = days;
  194. return time;
  195. }
  196. function formetdate(date) {
  197. var year = date.getFullYear();
  198. var month = date.getMonth() + 1;
  199. var day = date.getDate();
  200. return [year, month, day].map(formatNumber).join('-');
  201. }
  202. function getYearMonth(str,method){
  203. let arr = str.split('-');
  204. let year = arr[0]; //获取当前日期的年份
  205. let month = arr[1]; //获取当前日期的月份
  206. let total=0;
  207. if (method == '+') {
  208. total=Number(year*12+Number(month)+1).toFixed(0);
  209. } else if (method == '-') {
  210. total=Number(year*12+Number(month)-1).toFixed(0);
  211. }
  212. let year2=parseInt(total/12);
  213. let month2=total%12;
  214. if(month2==0){
  215. year2=parseInt(total/12)-1;
  216. month2=12;
  217. }
  218. if (month2 < 10) {
  219. month2 = '0' + month2;
  220. }
  221. let str2 = year2 + '-' + month2 ;
  222. return str2;
  223. }
  224. function getWeek(day) {
  225. let week=moment(day).format("W");
  226. return week;
  227. }
  228. function validIdCard(value) {
  229. //15位和18位身份证号码的正则表达式
  230. var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
  231. //如果通过该验证,说明身份证格式正确,但准确性还需计算
  232. if (regIdCard.test(value)) {
  233. if (value.length == 18) {
  234. var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //将前17位加权因子保存在数组里
  235. var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); //这是除以11后,可能产生的11位余数、验证码,也保存成数组
  236. var idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和
  237. for (var i = 0; i < 17; i++) {
  238. idCardWiSum += value.substring(i, i + 1) * idCardWi[i];
  239. }
  240. var idCardMod = idCardWiSum % 11;//计算出校验码所在数组的位置
  241. var idCardLast = value.substring(17);//得到最后一位身份证号码
  242. //如果等于2,则说明校验码是10,身份证号码最后一位应该是X
  243. if (idCardMod == 2) {
  244. if (idCardLast == "X" || idCardLast == "x") {
  245. return true;
  246. } else {
  247. // callback(new Error("身份证格式不正确!"));
  248. return false;
  249. }
  250. } else {
  251. //用计算出的验证码与最后一位身份证号码匹配,如果一致,说明通过,否则是无效的身份证号码
  252. if (idCardLast == idCardY[idCardMod]) {
  253. return true;
  254. } else {
  255. //
  256. return false;
  257. }
  258. }
  259. }
  260. } else {
  261. //alert("身份证格式不正确!");
  262. return false;
  263. }
  264. }
  265. module.exports = {
  266. formatTime,
  267. formatDate,
  268. request,
  269. mathChangeDate,
  270. showErrorToast,
  271. getWeekByDate,
  272. formatweek,
  273. getDateList,
  274. dateDifference,
  275. formetdate,
  276. toDate,
  277. getYearMonth,
  278. getWeek,
  279. validIdCard
  280. }