util.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. var api = require('../config/api.js');
  2. var app = getApp();
  3. function formatTime(date) {
  4. var year = date.getFullYear()
  5. var month = date.getMonth() + 1
  6. var day = date.getDate()
  7. var hour = date.getHours()
  8. var minute = date.getMinutes()
  9. var second = date.getSeconds()
  10. return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  11. }
  12. function formetdate(date){
  13. var year = date.getFullYear();
  14. var month = date.getMonth() + 1;
  15. var day = date.getDate();
  16. return [year, month, day].map(formatNumber).join('-') ;
  17. }
  18. function formatNumber(n) {
  19. n = n.toString()
  20. return n[1] ? n : '0' + n
  21. }
  22. /**
  23. * 封封微信的的request
  24. */
  25. function request(url, data = {}, method = "GET") {
  26. return new Promise(function(resolve, reject) {
  27. wx.request({
  28. url: url,
  29. data: data,
  30. method: method,
  31. header: {
  32. 'Content-Type': 'application/json',
  33. 'X-JZ-Token': wx.getStorageSync('token')
  34. },
  35. success: function(res) {
  36. if (res.statusCode == 200) {
  37. if (res.data.errno == 501) {
  38. // 清除登录相关内容
  39. try {
  40. wx.removeStorageSync('userInfo');
  41. wx.removeStorageSync('token');
  42. } catch (e) {
  43. // Do something when catch error
  44. }
  45. // 切换到登录页面
  46. wx.navigateTo({
  47. url: '/pages/auth/login/login'
  48. });
  49. } else {
  50. resolve(res.data);
  51. }
  52. } else {
  53. reject(res.errMsg);
  54. }
  55. },
  56. fail: function(err) {
  57. reject(err)
  58. }
  59. })
  60. });
  61. }
  62. function redirect(url) {
  63. //判断页面是否需要登录
  64. if (false) {
  65. wx.redirectTo({
  66. url: '/pages/auth/login/login'
  67. });
  68. return false;
  69. } else {
  70. wx.redirectTo({
  71. url: url
  72. });
  73. }
  74. }
  75. function showErrorToast(msg) {
  76. wx.showToast({
  77. title: msg,
  78. image: '/static/images/icon_error.png'
  79. })
  80. }
  81. function formatweek(date){
  82. let week=date.getDay();
  83. let weekArr='';
  84. switch (week){
  85. case 1:
  86. weekArr='周一';
  87. break;
  88. case 2:
  89. weekArr='周二';
  90. break;
  91. case 3:
  92. weekArr='周三';
  93. break;
  94. case 4:
  95. weekArr='周四';
  96. break;
  97. case 5:
  98. weekArr='周五';
  99. break;
  100. case 6:
  101. weekArr='周六';
  102. break;
  103. case 0:
  104. weekArr='周日';
  105. break;
  106. }
  107. return weekArr;
  108. }
  109. function getDateList(date,interval,length){
  110. let dateList=[];
  111. let len=(length?length:7);
  112. for(let i=0;i<len;i++){
  113. let datestamp=new Date(date.getTime()+i*60*60*1000*24*interval);
  114. let dateItem={
  115. week:formatweek(datestamp),
  116. date:formetdate(datestamp),
  117. id:new Date(datestamp).getDay()
  118. };
  119. dateList.push(dateItem);
  120. }
  121. return dateList;
  122. }
  123. function dateDifference( end) {
  124. // //比较2个日期
  125. // console.log(start + ",," + end);
  126. // var stime = new Date(start).getTime();
  127. var stime = new Date().getTime();
  128. console.log(end.replace(/-/g, "/"));
  129. var etime = new Date(end.replace(/-/g, "/")).getTime();
  130. var usedTime = etime - stime; //两个时间戳相差的毫秒数
  131. var days = Math.floor(usedTime / (24 * 3600 * 1000));
  132. //计算出小时数
  133. var leave1 = usedTime % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  134. var hours = Math.floor(leave1 / (3600 * 1000));
  135. //计算相差分钟数
  136. var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  137. var minutes = Math.floor(leave2 / (60 * 1000));
  138. if (days == 0 && hours == 0) {
  139. return minutes + "分钟";
  140. }
  141. if (days == 0) {
  142. return hours + "时" + minutes + "分钟";
  143. }
  144. var time = days + "天" + hours + "时" + minutes + "分钟";
  145. // var time = days;
  146. return time;
  147. }
  148. module.exports = {
  149. formatTime,
  150. formetdate,
  151. request,
  152. redirect,
  153. showErrorToast,
  154. formatweek,
  155. getDateList,
  156. dateDifference
  157. }