util.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. var app = getApp();
  2. var api = require('../config/api.js');
  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-DOUYIN-VERIFY-Token': uni.getStorageSync('token'),
  34. 'app-id':api.appid
  35. },
  36. success: function (res) {
  37. if (res.statusCode == 200) {
  38. if (res.data.errno == 501) {
  39. // 清除登录相关内容
  40. try {
  41. uni.removeStorageSync('userInfo');
  42. uni.removeStorageSync('token');
  43. uni.removeStorageSync('openId');
  44. } catch (e) {
  45. console.log('CatchClause', e);
  46. console.log('CatchClause', e);
  47. } // Do something when catch error
  48. // 切换到登录页面
  49. uni.navigateTo({
  50. url: '/pages/auth/login/login'
  51. });
  52. } else {
  53. resolve(res.data);
  54. }
  55. } else {
  56. reject(res.errMsg);
  57. }
  58. },
  59. fail: function (err) {
  60. reject(err);
  61. }
  62. });
  63. });
  64. }
  65. function redirect(url) {
  66. //判断页面是否需要登录
  67. uni.redirectTo({
  68. url: url
  69. });
  70. }
  71. function showErrorToast(msg) {
  72. uni.showToast({
  73. title: msg,
  74. image: '/static/images/icon_error.png'
  75. });
  76. }
  77. function formatweek(date) {
  78. let week = date.getDay();
  79. let weekArr = '';
  80. switch (week) {
  81. case 1:
  82. weekArr = '周一';
  83. break;
  84. case 2:
  85. weekArr = '周二';
  86. break;
  87. case 3:
  88. weekArr = '周三';
  89. break;
  90. case 4:
  91. weekArr = '周四';
  92. break;
  93. case 5:
  94. weekArr = '周五';
  95. break;
  96. case 6:
  97. weekArr = '周六';
  98. break;
  99. case 0:
  100. weekArr = '周日';
  101. break;
  102. }
  103. return weekArr;
  104. }
  105. function getDateList(date, interval, length) {
  106. let dateList = [];
  107. let len = length ? length : 7;
  108. for (let i = 0; i < len; i++) {
  109. let datestamp = new Date(date.getTime() + i * 60 * 60 * 1000 * 24 * interval);
  110. let dateItem = {
  111. week: formatweek(datestamp),
  112. date: formetdate(datestamp),
  113. id: new Date(datestamp).getDay(),
  114. isBook: true,
  115. //是否可约
  116. name: i == 0 ? '今天' : i == 1 ? '明天' : '',
  117. mark: '' //标注是否不可约的原因之类的
  118. };
  119. if (new Date().getHours() >= 14 && i == 0) {
  120. continue;
  121. }
  122. dateList.push(dateItem);
  123. }
  124. return dateList;
  125. }
  126. function dateDifference(end) {
  127. // //比较2个日期
  128. // console.log(start + ",," + end);
  129. // var stime = new Date(start).getTime();
  130. var stime = new Date().getTime();
  131. console.log(end.replace(/-/g, '/'));
  132. var etime = new Date(end.replace(/-/g, '/')).getTime();
  133. var usedTime = etime - stime; //两个时间戳相差的毫秒数
  134. var days = Math.floor(usedTime / 86400000);
  135. //计算出小时数
  136. var leave1 = usedTime % 86400000; //计算天数后剩余的毫秒数
  137. var hours = Math.floor(leave1 / 3600000);
  138. //计算相差分钟数
  139. var leave2 = leave1 % 3600000; //计算小时数后剩余的毫秒数
  140. var minutes = Math.floor(leave2 / 60000);
  141. if (days == 0 && hours == 0) {
  142. return minutes + '分钟';
  143. }
  144. if (days == 0) {
  145. return hours + '时' + minutes + '分钟';
  146. }
  147. var time = days + '天' + hours + '时' + minutes + '分钟';
  148. // var time = days;
  149. return time;
  150. }
  151. function getBeginTimeList(time, type, dateName) {
  152. let timeList = [];
  153. if (time && time != null) {
  154. let startTime = 8;
  155. let endTime = '';
  156. if (type == 1) {
  157. startTime = 8;
  158. endTime = 21 - time > 12 ? 12 : 21 - time;
  159. } else {
  160. startTime = 12;
  161. endTime = 21 - time > 12 ? 21 - time : 12;
  162. }
  163. //控制当天的日期返回:当天可约时间 不能小于12点或当前时间+4小时 过14点 今日全天不可约
  164. if (dateName == '今天') {
  165. let date = new Date();
  166. let hour = date.getHours() + 4;
  167. if (date.getMinutes() > 30) {
  168. hour = hour + 1;
  169. }
  170. startTime = hour;
  171. if (hour > 18 || type == 1) {
  172. return [];
  173. } else if (hour < 12 && type == 2) {
  174. startTime = 12;
  175. }
  176. }
  177. //如果是今天20点后 预约明天 也是不可以约12点前
  178. if (dateName == '明天') {
  179. let date = new Date();
  180. if (date.getHours() >= 20 && type == 1) {
  181. return [];
  182. }
  183. }
  184. for (let i = startTime; i <= endTime; i++) {
  185. if (type == 2 && i == 12) {
  186. let halfTime = i < 10 ? '0' + i + ':30' : i + ':30';
  187. timeList.push({
  188. time: halfTime,
  189. value: i + 0.5
  190. });
  191. } else if (i < endTime) {
  192. let intTime = i < 10 ? '0' + i + ':00' : i + ':00';
  193. let halfTime = i < 10 ? '0' + i + ':30' : i + ':30';
  194. timeList.push({
  195. time: intTime,
  196. value: i
  197. });
  198. timeList.push({
  199. time: halfTime,
  200. value: i + 0.5
  201. });
  202. } else {
  203. let intTime = i < 10 ? '0' + i + ':00' : i + ':00';
  204. timeList.push({
  205. time: intTime
  206. });
  207. }
  208. }
  209. }
  210. return timeList;
  211. }
  212. module.exports = {
  213. formatTime,
  214. formetdate,
  215. request,
  216. redirect,
  217. showErrorToast,
  218. formatweek,
  219. getDateList,
  220. dateDifference,
  221. getBeginTimeList
  222. };