util.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. uni.request({
  28. url: url,
  29. data: data,
  30. method: method,
  31. header: {
  32. 'Content-Type': 'application/json',
  33. 'X-JZ-Token': uni.getStorageSync('token')
  34. },
  35. success: function (res) {
  36. if (res.statusCode == 200) {
  37. if (res.data.errno == 501) {
  38. // 清除登录相关内容
  39. try {
  40. uni.removeStorageSync('userInfo');
  41. uni.removeStorageSync('token');
  42. } catch (e) {
  43. console.log('CatchClause', e);
  44. console.log('CatchClause', e);
  45. } // Do something when catch error
  46. // 切换到登录页面
  47. uni.navigateTo({
  48. url: '/pages/auth/login/login'
  49. });
  50. } else {
  51. resolve(res.data);
  52. }
  53. } else {
  54. reject(res.errMsg);
  55. }
  56. },
  57. fail: function (err) {
  58. reject(err);
  59. }
  60. });
  61. });
  62. }
  63. function redirect(url) {
  64. //判断页面是否需要登录
  65. uni.redirectTo({
  66. url: url
  67. });
  68. }
  69. function showErrorToast(msg) {
  70. uni.showToast({
  71. title: msg,
  72. image: '/static/images/icon_error.png'
  73. });
  74. }
  75. function formatweek(date) {
  76. let week = date.getDay();
  77. let weekArr = '';
  78. switch (week) {
  79. case 1:
  80. weekArr = '周一';
  81. break;
  82. case 2:
  83. weekArr = '周二';
  84. break;
  85. case 3:
  86. weekArr = '周三';
  87. break;
  88. case 4:
  89. weekArr = '周四';
  90. break;
  91. case 5:
  92. weekArr = '周五';
  93. break;
  94. case 6:
  95. weekArr = '周六';
  96. break;
  97. case 0:
  98. weekArr = '周日';
  99. break;
  100. }
  101. return weekArr;
  102. }
  103. function getDateList(date, interval, length) {
  104. let dateList = [];
  105. let len = length ? length : 7;
  106. for (let i = 0; i < len; i++) {
  107. let datestamp = new Date(date.getTime() + i * 60 * 60 * 1000 * 24 * interval);
  108. let dateItem = {
  109. week: formatweek(datestamp),
  110. date: formetdate(datestamp),
  111. id: new Date(datestamp).getDay(),
  112. isBook: true,
  113. //是否可约
  114. name: i == 0 ? '今天' : i == 1 ? '明天' : '',
  115. mark: '' //标注是否不可约的原因之类的
  116. };
  117. dateList.push(dateItem);
  118. }
  119. return dateList;
  120. }
  121. function dateDifference(end) {
  122. // //比较2个日期
  123. // console.log(start + ",," + end);
  124. // var stime = new Date(start).getTime();
  125. var stime = new Date().getTime();
  126. console.log(end.replace(/-/g, '/'));
  127. var etime = new Date(end.replace(/-/g, '/')).getTime();
  128. var usedTime = etime - stime; //两个时间戳相差的毫秒数
  129. var days = Math.floor(usedTime / 86400000);
  130. //计算出小时数
  131. var leave1 = usedTime % 86400000; //计算天数后剩余的毫秒数
  132. var hours = Math.floor(leave1 / 3600000);
  133. //计算相差分钟数
  134. var leave2 = leave1 % 3600000; //计算小时数后剩余的毫秒数
  135. var minutes = Math.floor(leave2 / 60000);
  136. if (days == 0 && hours == 0) {
  137. return minutes + '分钟';
  138. }
  139. if (days == 0) {
  140. return hours + '时' + minutes + '分钟';
  141. }
  142. var time = days + '天' + hours + '时' + minutes + '分钟';
  143. // var time = days;
  144. return time;
  145. }
  146. //获取下个月和下下个月的天数
  147. function getNextMonthDays(next) {
  148. const today = new Date();
  149. const currentMonth = today.getMonth(); // 获取当前月份,月份从0开始
  150. const currentYear = today.getFullYear(); // 获取当前年份
  151. if(next==1){
  152. // 计算下个月的第一天
  153. const nextMonth = (currentMonth + 1) % 12;
  154. const nextYear = currentMonth === 11 ? currentYear + 1 : currentYear;
  155. // 如果下个月是当前年的1月,则年份加1
  156. if (nextMonth === 0) {
  157. nextYear++;
  158. }
  159. // 创建下个月第一天的日期对象
  160. const firstDayNextMonth = new Date(nextYear, nextMonth, 1);
  161. // 获取下个月的天数
  162. const daysInNextMonth = new Date(firstDayNextMonth.getFullYear(), firstDayNextMonth.getMonth() + 1, 0).getDate();
  163. return daysInNextMonth;
  164. }else{
  165. const nextMonth = (currentMonth + 2) % 12;
  166. const nextYear = currentMonth === 10 ? currentYear + 1 : currentYear;
  167. if (nextMonth === 0) {
  168. nextYear++;
  169. }
  170. const firstDayNextMonth = new Date(nextYear, nextMonth, 1);
  171. const daysInNextMonth = new Date(firstDayNextMonth.getFullYear(), firstDayNextMonth.getMonth() + 1, 0).getDate();
  172. return daysInNextMonth;
  173. }
  174. }
  175. module.exports = {
  176. formatTime,
  177. formetdate,
  178. request,
  179. redirect,
  180. showErrorToast,
  181. formatweek,
  182. getDateList,
  183. dateDifference,
  184. getNextMonthDays
  185. };