util.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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-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. //如果当前时间+4大于18点 不返回当日可约时间
  120. if (new Date().getHours() >= 14 && i == 0) {
  121. continue;
  122. }
  123. dateList.push(dateItem);
  124. }
  125. return dateList;
  126. }
  127. function dateDifference(end) {
  128. // //比较2个日期
  129. // console.log(start + ",," + end);
  130. // var stime = new Date(start).getTime();
  131. var stime = new Date().getTime();
  132. console.log(end.replace(/-/g, '/'));
  133. var etime = new Date(end.replace(/-/g, '/')).getTime();
  134. var usedTime = etime - stime; //两个时间戳相差的毫秒数
  135. var days = Math.floor(usedTime / 86400000);
  136. //计算出小时数
  137. var leave1 = usedTime % 86400000; //计算天数后剩余的毫秒数
  138. var hours = Math.floor(leave1 / 3600000);
  139. //计算相差分钟数
  140. var leave2 = leave1 % 3600000; //计算小时数后剩余的毫秒数
  141. var minutes = Math.floor(leave2 / 60000);
  142. if (days == 0 && hours == 0) {
  143. return minutes + '分钟';
  144. }
  145. if (days == 0) {
  146. return hours + '时' + minutes + '分钟';
  147. }
  148. var time = days + '天' + hours + '时' + minutes + '分钟';
  149. // var time = days;
  150. return time;
  151. }
  152. function getBeginTimeList(time, type, dateName) {
  153. let timeList = [];
  154. if (time && time != null) {
  155. let startTime = 8;
  156. let endTime = '';
  157. if (type == 1) {
  158. startTime = 8;
  159. endTime = 21 - time > 12 ? 12 : 21 - time;
  160. } else {
  161. startTime = 12;
  162. endTime = 21 - time > 12 ? 21 - time : 12;
  163. }
  164. //控制当天的日期返回:当天可约时间 不能小于12点或当前时间+4小时 过14点 今日全天不可约
  165. if (dateName == '今天') {
  166. let date = new Date();
  167. let hour = date.getHours() + 4;
  168. if (date.getMinutes() > 30) {
  169. hour = hour + 1;
  170. }
  171. startTime = hour;
  172. if (hour > 18 || type == 1) {
  173. return [];
  174. } else if (hour < 12 && type == 2) {
  175. startTime = 12;
  176. }
  177. }
  178. //如果是今天20点后 预约明天 也是不可以约12点前
  179. if (dateName == '明天') {
  180. let date = new Date();
  181. if (date.getHours() >= 20 && type == 1) {
  182. return [];
  183. }
  184. }
  185. for (let i = startTime; i <= endTime; i++) {
  186. if (type == 2 && i == 12) {
  187. let halfTime = i < 10 ? '0' + i + ':30' : i + ':30';
  188. timeList.push({
  189. time: halfTime,
  190. value: i + 0.5
  191. });
  192. } else if (i < endTime) {
  193. let intTime = i < 10 ? '0' + i + ':00' : i + ':00';
  194. let halfTime = i < 10 ? '0' + i + ':30' : i + ':30';
  195. timeList.push({
  196. time: intTime,
  197. value: i
  198. });
  199. timeList.push({
  200. time: halfTime,
  201. value: i + 0.5
  202. });
  203. } else {
  204. let intTime = i < 10 ? '0' + i + ':00' : i + ':00';
  205. timeList.push({
  206. time: intTime
  207. });
  208. }
  209. }
  210. }
  211. return timeList;
  212. }
  213. function creadUuid(){
  214. var s = [];
  215. var hexDigits = "0123456789abcdef";
  216. for (var i = 0; i < 36; i++) {
  217. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  218. }
  219. s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  220. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  221. s[8] = s[13] = s[18] = s[23] = "-";
  222. let uuid = s.join("");
  223. return uuid;
  224. };
  225. //
  226. function addLog(inorout,pageId,eventId,parms){
  227. var pages = getCurrentPages();
  228. var prePageUrl="";
  229. if(pages.length>=2){
  230. var prevPage = pages[pages.length - 2];//取上个页面
  231. prePageUrl=prevPage.route;//上个页面路径
  232. }
  233. var app = getApp();
  234. let userInfo = uni.getStorageSync('userInfo');
  235. request(api.addLog,{
  236. "uunid":app.globalData.uuid,
  237. "userOpenid":app.globalData.openId,
  238. "userId":userInfo.id?userInfo.id:null,
  239. "inorout":inorout?inorout:1,
  240. "pageId":pageId,
  241. "prePageUrl":prePageUrl,
  242. "eventId":eventId,
  243. "parms":parms?JSON.stringify(parms):null,
  244. // #ifdef MP-TOUTIAO
  245. "ofSys":1,
  246. // #endif
  247. // #ifdef MP-ALIPAY
  248. "ofSys":3,
  249. //#endif
  250. },'POST').then((res) => {
  251. if (res.errno === 0) {
  252. console.log('addLog'+res);
  253. }
  254. })
  255. }
  256. module.exports = {
  257. formatTime,
  258. formetdate,
  259. request,
  260. redirect,
  261. showErrorToast,
  262. formatweek,
  263. getDateList,
  264. dateDifference,
  265. getBeginTimeList,
  266. creadUuid,
  267. addLog
  268. };