util.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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){
  110. let dateList=[];
  111. for(let i=0;i<7;i++){
  112. let datestamp=new Date(date.getTime()+i*60*60*1000*24*interval);
  113. let dateItem={
  114. week:formatweek(datestamp),
  115. date:formetdate(datestamp),
  116. id:new Date(datestamp).getDay()
  117. };
  118. dateList.push(dateItem);
  119. }
  120. return dateList;
  121. }
  122. function dateDifference( end) {
  123. // //比较2个日期
  124. // console.log(start + ",," + end);
  125. // var stime = new Date(start).getTime();
  126. var stime = new Date().getTime();
  127. console.log(end.replace(/-/g, "/"));
  128. var etime = new Date(end.replace(/-/g, "/")).getTime();
  129. var usedTime = etime - stime; //两个时间戳相差的毫秒数
  130. var days = Math.floor(usedTime / (24 * 3600 * 1000));
  131. //计算出小时数
  132. var leave1 = usedTime % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  133. var hours = Math.floor(leave1 / (3600 * 1000));
  134. //计算相差分钟数
  135. var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  136. var minutes = Math.floor(leave2 / (60 * 1000));
  137. if (days == 0 && hours == 0) {
  138. return minutes + "分钟";
  139. }
  140. if (days == 0) {
  141. return hours + "时" + minutes + "分钟";
  142. }
  143. var time = days + "天" + hours + "时" + minutes + "分钟";
  144. // var time = days;
  145. return time;
  146. }
  147. module.exports = {
  148. formatTime,
  149. formetdate,
  150. request,
  151. redirect,
  152. showErrorToast,
  153. formatweek,
  154. getDateList,
  155. dateDifference
  156. }