util.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. function formatDates(dates) {
  23. // 将日期字符串分割为年、月、日
  24. const [year, month, day] = dates.split('-');
  25. // 返回格式化后的日期字符串,不包含年份
  26. return bookdate = `${month}-${day}`
  27. }
  28. /**
  29. * 封封微信的的request
  30. */
  31. function request(url, data = {}, method = 'GET') {
  32. return new Promise(function(resolve, reject) {
  33. uni.request({
  34. url: url,
  35. data: data,
  36. method: method,
  37. header: {
  38. 'Content-Type': 'application/json',
  39. 'X-DOUYIN-Token': uni.getStorageSync('token'),
  40. 'app-id': api.appid
  41. },
  42. success: function(res) {
  43. if (res.statusCode == 200) {
  44. if (res.data.errno == 501) {
  45. // 清除登录相关内容
  46. try {
  47. uni.removeStorageSync('userInfo');
  48. uni.removeStorageSync('token');
  49. uni.removeStorageSync('openId');
  50. } catch (e) {
  51. console.log('CatchClause', e);
  52. console.log('CatchClause', e);
  53. } // Do something when catch error
  54. // 切换到登录页面
  55. uni.navigateTo({
  56. url: '/pages/auth/login/login'
  57. });
  58. } else {
  59. resolve(res.data);
  60. }
  61. } else {
  62. reject(res.errMsg);
  63. }
  64. },
  65. fail: function(err) {
  66. reject(err);
  67. }
  68. });
  69. });
  70. }
  71. function redirect(url) {
  72. //判断页面是否需要登录
  73. uni.redirectTo({
  74. url: url
  75. });
  76. }
  77. function showErrorToast(msg) {
  78. uni.showToast({
  79. title: msg,
  80. image: '/static/images/icon_error.png'
  81. });
  82. }
  83. function formatweek(date) {
  84. let week = date.getDay();
  85. let weekArr = '';
  86. switch (week) {
  87. case 1:
  88. weekArr = '周一';
  89. break;
  90. case 2:
  91. weekArr = '周二';
  92. break;
  93. case 3:
  94. weekArr = '周三';
  95. break;
  96. case 4:
  97. weekArr = '周四';
  98. break;
  99. case 5:
  100. weekArr = '周五';
  101. break;
  102. case 6:
  103. weekArr = '周六';
  104. break;
  105. case 0:
  106. weekArr = '周日';
  107. break;
  108. }
  109. return weekArr;
  110. }
  111. function getDateList(date, interval, length) {
  112. let dateList = [];
  113. let len = length ? length : 7;
  114. for (let i = 0; i < len; i++) {
  115. let datestamp = new Date(date.getTime() + i * 60 * 60 * 1000 * 24 * interval);
  116. let dateItem = {
  117. week: formatweek(datestamp),
  118. date: formetdate(datestamp),
  119. id: new Date(datestamp).getDay(),
  120. isBook: true,
  121. //是否可约
  122. name: i == 0 ? '今天' : i == 1 ? '明天' : '',
  123. mark: '' //标注是否不可约的原因之类的
  124. };
  125. //如果当前时间+4大于18点 不返回当日可约时间
  126. if (new Date().getHours() >= 14 && i == 0) {
  127. continue;
  128. }
  129. dateList.push(dateItem);
  130. }
  131. return dateList;
  132. }
  133. function dateDifference(end) {
  134. // //比较2个日期
  135. // console.log(start + ",," + end);
  136. // var stime = new Date(start).getTime();
  137. var stime = new Date().getTime();
  138. console.log(end.replace(/-/g, '/'));
  139. var etime = new Date(end.replace(/-/g, '/')).getTime();
  140. var usedTime = etime - stime; //两个时间戳相差的毫秒数
  141. var days = Math.floor(usedTime / 86400000);
  142. //计算出小时数
  143. var leave1 = usedTime % 86400000; //计算天数后剩余的毫秒数
  144. var hours = Math.floor(leave1 / 3600000);
  145. //计算相差分钟数
  146. var leave2 = leave1 % 3600000; //计算小时数后剩余的毫秒数
  147. var minutes = Math.floor(leave2 / 60000);
  148. if (days == 0 && hours == 0) {
  149. return minutes + '分钟';
  150. }
  151. if (days == 0) {
  152. return hours + '时' + minutes + '分钟';
  153. }
  154. var time = days + '天' + hours + '时' + minutes + '分钟';
  155. // var time = days;
  156. return time;
  157. }
  158. function getBeginTimeList(time, type, dateName) {
  159. let timeList = [];
  160. if (time && time != null) {
  161. let startTime = 8;
  162. let endTime = '';
  163. if (type == 1) {
  164. startTime = 8;
  165. endTime = 21 - time > 12 ? 12 : 21 - time;
  166. } else {
  167. startTime = 12;
  168. endTime = 21 - time > 12 ? 21 - time : 12;
  169. }
  170. //控制当天的日期返回:当天可约时间 不能小于12点或当前时间+4小时 过14点 今日全天不可约
  171. if (dateName == '今天') {
  172. let date = new Date();
  173. let hour = date.getHours() + 4;
  174. if (date.getMinutes() > 30) {
  175. hour = hour + 1;
  176. }
  177. startTime = hour;
  178. if (hour > 18 || type == 1) {
  179. return [];
  180. } else if (hour < 12 && type == 2) {
  181. startTime = 12;
  182. }
  183. }
  184. //如果是今天20点后 预约明天 也是不可以约12点前
  185. if (dateName == '明天') {
  186. let date = new Date();
  187. if (date.getHours() >= 20 && type == 1) {
  188. return [];
  189. }
  190. }
  191. for (let i = startTime; i <= endTime; i++) {
  192. if (type == 2 && i == 12) {
  193. let halfTime = i < 10 ? '0' + i + ':30' : i + ':30';
  194. timeList.push({
  195. time: halfTime,
  196. value: i + 0.5
  197. });
  198. } else if (i < endTime) {
  199. let intTime = i < 10 ? '0' + i + ':00' : i + ':00';
  200. let halfTime = i < 10 ? '0' + i + ':30' : i + ':30';
  201. timeList.push({
  202. time: intTime,
  203. value: i
  204. });
  205. timeList.push({
  206. time: halfTime,
  207. value: i + 0.5
  208. });
  209. } else {
  210. let intTime = i < 10 ? '0' + i + ':00' : i + ':00';
  211. timeList.push({
  212. time: intTime
  213. });
  214. }
  215. }
  216. }
  217. return timeList;
  218. }
  219. function creadUuid() {
  220. var s = [];
  221. var hexDigits = "0123456789abcdef";
  222. for (var i = 0; i < 36; i++) {
  223. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  224. }
  225. s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  226. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  227. s[8] = s[13] = s[18] = s[23] = "-";
  228. let uuid = s.join("");
  229. return uuid;
  230. };
  231. //
  232. function addLog(inorout, pageId, eventId, parms) {
  233. var pages = getCurrentPages();
  234. var prePageUrl = "";
  235. if (pages.length >= 2) {
  236. var prevPage = pages[pages.length - 2]; //取上个页面
  237. prePageUrl = prevPage.route; //上个页面路径
  238. }
  239. var app = getApp();
  240. let userInfo = uni.getStorageSync('userInfo');
  241. request(api.addLog, {
  242. "uunid": app.globalData.uuid,
  243. "userOpenid": app.globalData.openId,
  244. "userId": userInfo.id ? userInfo.id : null,
  245. "inorout": inorout ? inorout : 1,
  246. "pageId": pageId,
  247. "prePageUrl": prePageUrl,
  248. "eventId": eventId,
  249. "parms": parms ? JSON.stringify(parms) : null,
  250. // #ifdef MP-TOUTIAO
  251. "ofSys": 1,
  252. // #endif
  253. // #ifdef MP-ALIPAY
  254. "ofSys": 3,
  255. //#endif
  256. }, 'POST').then((res) => {
  257. if (res.errno === 0) {
  258. console.log('addLog' + res);
  259. }
  260. })
  261. }
  262. function getNextSevenDaysFromToday() {
  263. const today = new Date(); // 获取今天的日期
  264. const dayOfMonth = today.getDate(); // 获取今天的“日”部分
  265. const month = today.getMonth(); // 获取今天的“月”部分
  266. const year = today.getFullYear(); // 获取今年的年份
  267. // 判断是否需要跨年
  268. const isLeapYear = (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
  269. const daysInMonth = month === 1 ? (isLeapYear ? 29 : 28) : (month + 1) % 12 === 0 ? 31 : 30;
  270. let nextDays = [];
  271. for (let i = 1; i <= 7; i++) {
  272. const nextDay = new Date(year, month, dayOfMonth + i);
  273. nextDays.push(nextDay.getDate());
  274. }
  275. // 移除今天
  276. nextDays = nextDays.filter(day => day !== dayOfMonth);
  277. return nextDays;
  278. }
  279. // 用于存储之前改变的两个元素的索引
  280. let changedIndexes = [];
  281. // 函数用于随机改变两个元素的 dateType 值,并恢复上一次改变的元素
  282. function changeRandomTwoElementsDateType(array) {
  283. const indexes = array.map((_, index) => index);
  284. let randomIndexes;
  285. // 如果之前有改变的元素,先恢复它们
  286. if (changedIndexes.length === 2) {
  287. array[changedIndexes[0]].dateType = 0;
  288. array[changedIndexes[1]].dateType = 0;
  289. changedIndexes = []; // 清空上一次的索引
  290. }
  291. // 随机选择两个不重复的索引
  292. randomIndexes = [];
  293. while (randomIndexes.length < 2) {
  294. const index = indexes[Math.floor(Math.random() * indexes.length)];
  295. if (!randomIndexes.includes(index)) {
  296. randomIndexes.push(index);
  297. }
  298. }
  299. // 改变这两个元素的 dateType 值并保存索引
  300. changedIndexes = randomIndexes;
  301. array[changedIndexes[0]].dateType = 1;
  302. array[changedIndexes[1]].dateType = 1;
  303. return array;
  304. }
  305. module.exports = {
  306. changeRandomTwoElementsDateType,
  307. getNextSevenDaysFromToday,
  308. formatTime,
  309. formetdate,
  310. request,
  311. redirect,
  312. showErrorToast,
  313. formatweek,
  314. getDateList,
  315. dateDifference,
  316. getBeginTimeList,
  317. creadUuid,
  318. addLog,
  319. formatDates
  320. };