jmsUtil.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. 'X-JZ-Admin-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/index/index'
  106. });
  107. } else {
  108. resolve(res.data);
  109. }
  110. } else {
  111. reject(res.errMsg);
  112. }
  113. },
  114. fail: function (err) {
  115. reject(err)
  116. }
  117. })
  118. });
  119. }
  120. function showErrorToast(msg) {
  121. wx.showToast({
  122. title: msg,
  123. image: '/static/images/icon_error.png'
  124. })
  125. }
  126. function formatweek(date) {
  127. let week = date.getDay();
  128. let weekArr = '';
  129. switch (week) {
  130. case 1:
  131. weekArr = '周一';
  132. break;
  133. case 2:
  134. weekArr = '周二';
  135. break;
  136. case 3:
  137. weekArr = '周三';
  138. break;
  139. case 4:
  140. weekArr = '周四';
  141. break;
  142. case 5:
  143. weekArr = '周五';
  144. break;
  145. case 6:
  146. weekArr = '周六';
  147. break;
  148. case 0:
  149. weekArr = '周日';
  150. break;
  151. }
  152. return weekArr;
  153. }
  154. function getDateList(date, interval, length) {
  155. let dateList = [];
  156. let len = (length ? length : 7);
  157. for (let i = 0; i < len; i++) {
  158. let datestamp = new Date(date.getTime() + i * 60 * 60 * 1000 * 24 * interval);
  159. let dateItem = {
  160. week: formatweek(datestamp),
  161. date: formetdate(datestamp),
  162. id: new Date(datestamp).getDay()
  163. };
  164. dateList.push(dateItem);
  165. }
  166. return dateList;
  167. }
  168. function dateDifference(end) {
  169. // //比较2个日期
  170. // console.log(start + ",," + end);
  171. // var stime = new Date(start).getTime();
  172. var stime = new Date().getTime();
  173. console.log(end.replace(/-/g, "/"));
  174. var etime = new Date(end.replace(/-/g, "/")).getTime();
  175. var usedTime = etime - stime; //两个时间戳相差的毫秒数
  176. var days = Math.floor(usedTime / (24 * 3600 * 1000));
  177. //计算出小时数
  178. var leave1 = usedTime % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  179. var hours = Math.floor(leave1 / (3600 * 1000));
  180. //计算相差分钟数
  181. var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  182. var minutes = Math.floor(leave2 / (60 * 1000));
  183. if (days == 0 && hours == 0) {
  184. return minutes + "分钟";
  185. }
  186. if (days == 0) {
  187. return hours + "时" + minutes + "分钟";
  188. }
  189. var time = days + "天" + hours + "时" + minutes + "分钟";
  190. // var time = days;
  191. return time;
  192. }
  193. function formetdate(date) {
  194. var year = date.getFullYear();
  195. var month = date.getMonth() + 1;
  196. var day = date.getDate();
  197. return [year, month, day].map(formatNumber).join('-');
  198. }
  199. function getYearMonth(str,method){
  200. let arr = str.split('-');
  201. let year = arr[0]; //获取当前日期的年份
  202. let month = arr[1]; //获取当前日期的月份
  203. let total=0;
  204. if (method == '+') {
  205. total=Number(year*12+Number(month)+1).toFixed(0);
  206. } else if (method == '-') {
  207. total=Number(year*12+Number(month)-1).toFixed(0);
  208. }
  209. let year2=parseInt(total/12);
  210. let month2=total%12;
  211. if(month2==0){
  212. year2=parseInt(total/12)-1;
  213. month2=12;
  214. }
  215. if (month2 < 10) {
  216. month2 = '0' + month2;
  217. }
  218. let str2 = year2 + '-' + month2 ;
  219. return str2;
  220. }
  221. function getWeek(day) {
  222. let week=moment(day).format("W");
  223. return week;
  224. }
  225. function validIdCard(value) {
  226. //15位和18位身份证号码的正则表达式
  227. 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])$)$/;
  228. //如果通过该验证,说明身份证格式正确,但准确性还需计算
  229. if (regIdCard.test(value)) {
  230. if (value.length == 18) {
  231. var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //将前17位加权因子保存在数组里
  232. var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); //这是除以11后,可能产生的11位余数、验证码,也保存成数组
  233. var idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和
  234. for (var i = 0; i < 17; i++) {
  235. idCardWiSum += value.substring(i, i + 1) * idCardWi[i];
  236. }
  237. var idCardMod = idCardWiSum % 11;//计算出校验码所在数组的位置
  238. var idCardLast = value.substring(17);//得到最后一位身份证号码
  239. //如果等于2,则说明校验码是10,身份证号码最后一位应该是X
  240. if (idCardMod == 2) {
  241. if (idCardLast == "X" || idCardLast == "x") {
  242. return true;
  243. } else {
  244. // callback(new Error("身份证格式不正确!"));
  245. return false;
  246. }
  247. } else {
  248. //用计算出的验证码与最后一位身份证号码匹配,如果一致,说明通过,否则是无效的身份证号码
  249. if (idCardLast == idCardY[idCardMod]) {
  250. return true;
  251. } else {
  252. //
  253. return false;
  254. }
  255. }
  256. }
  257. } else {
  258. //alert("身份证格式不正确!");
  259. return false;
  260. }
  261. }
  262. module.exports = {
  263. formatTime,
  264. formatDate,
  265. request,
  266. mathChangeDate,
  267. showErrorToast,
  268. getWeekByDate,
  269. formatweek,
  270. getDateList,
  271. dateDifference,
  272. formetdate,
  273. toDate,
  274. getYearMonth,
  275. getWeek,
  276. validIdCard
  277. }