1
0

util.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. var api = require('../config/api.js');
  2. var user = require('../services/user.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 formatNumber(n) {
  13. n = n.toString()
  14. return n[1] ? n : '0' + n
  15. }
  16. /**
  17. * 封封微信的的request
  18. */
  19. function request(url, data = {}, method = "GET") {
  20. return new Promise(function (resolve, reject) {
  21. wx.request({
  22. url: url,
  23. data: data,
  24. method: method,
  25. header: {
  26. 'Content-Type': 'application/json',
  27. 'X-Nideshop-Token': wx.getStorageSync('token')
  28. },
  29. success: function (res) {
  30. // console.log("res" + res);
  31. // console.log("res.statusCode" + res.statusCode);
  32. if (res.statusCode == 200) {
  33. if (res.data.errno == 401) {
  34. //需要登录后才可以操作
  35. return user.loginByWeixin().then(loginRes => {
  36. //重新请求
  37. request(url, data, method).then(res => {
  38. if (res.errno === 0) {
  39. resolve(res);
  40. } else {
  41. reject(res);
  42. }
  43. }).catch((err) => {
  44. reject(err);
  45. });
  46. });
  47. } else {
  48. resolve(res.data);
  49. }
  50. } else {
  51. reject(res.errMsg);
  52. }
  53. },
  54. fail: function (err) {
  55. reject(err)
  56. showErrorToast('网络超时');
  57. console.log("failed")
  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 showSuccessToast(msg) {
  82. wx.showToast({
  83. title: msg,
  84. icon: 'success'
  85. })
  86. }
  87. function countdown(weixin, that, name, index) {
  88. if (index == null) {
  89. var EndTime = that.end_time || [];
  90. } else {
  91. var EndTime = that[index].end_time || [];
  92. }
  93. var NowTime = new Date().getTime();
  94. var total_micro_second = EndTime - NowTime || [];
  95. // console.log('剩余时间:' + total_micro_second);
  96. // 渲染倒计时时钟
  97. var para = {};
  98. if (index == null) {
  99. that.dateformat = dateformat(total_micro_second)
  100. para[name] = that
  101. } else {
  102. let str = `${name}[${index}].dateformat`;
  103. para[str] = dateformat(total_micro_second)
  104. }
  105. weixin.setData(para)
  106. if (total_micro_second <= 0) {
  107. if (index == null) {
  108. that.dateformat = {
  109. day: 0,
  110. hr: 0,
  111. min: 0,
  112. sec: 0
  113. }
  114. para[name] = that
  115. } else {
  116. let str = `${name}[${index}].dateformat`;
  117. para[str] = {
  118. day: 0,
  119. hr: 0,
  120. min: 0,
  121. sec: 0
  122. }
  123. }
  124. weixin.setData(para)
  125. return;
  126. }
  127. setTimeout(function () {
  128. total_micro_second -= 1000;
  129. countdown(weixin, that, name, index);
  130. }
  131. , 1000)
  132. }
  133. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  134. function dateformat(micro_second) {
  135. // 总秒数
  136. var second = Math.floor(micro_second / 1000);
  137. // 天数
  138. var day = Math.floor(second / 3600 / 24);
  139. // 小时
  140. var hr = Math.floor(second / 3600 % 24);
  141. // 分钟
  142. var min = Math.floor(second / 60 % 60);
  143. // 秒
  144. var sec = Math.floor(second % 60);
  145. return {
  146. day,
  147. hr: hr < 10 ? '0' + hr : hr,
  148. min: min < 10 ? '0' + min : min,
  149. sec: sec < 10 ? '0' + sec : sec
  150. }
  151. }
  152. function getLocation(callback) {
  153. wx.getLocation({
  154. type: 'gcj02', //返回可以用于wx.openLocation的经纬度
  155. success: function (res) {
  156. var latitude = res.latitude;
  157. var longitude = res.longitude;
  158. callback && callback(longitude, latitude)
  159. }
  160. })
  161. }
  162. function chooseLocation(callback) {
  163. wx.chooseLocation({
  164. success: function (res) {
  165. callback && callback(res)
  166. }
  167. })
  168. }
  169. function getNowTime() {
  170. var now = new Date();
  171. var year = now.getFullYear();
  172. var month = now.getMonth() + 1;
  173. var day = now.getDate();
  174. if (month < 10) {
  175. month = '0' + month;
  176. };
  177. if (day < 10) {
  178. day = '0' + day;
  179. };
  180. // 如果需要时分秒,就放开
  181. // var h = now.getHours();
  182. // var m = now.getMinutes();
  183. // var s = now.getSeconds();
  184. var formatDate = year + '-' + month + '-' + day;
  185. return formatDate;
  186. }
  187. function getDateStr(today, addDayCount) {
  188. var dd;
  189. if (today) {
  190. dd = new Date(today);
  191. } else {
  192. dd = new Date();
  193. }
  194. dd.setDate(dd.getDate() + addDayCount);//获取AddDayCount天后的日期
  195. var y = dd.getFullYear();
  196. var m = dd.getMonth() + 1;//获取当前月份的日期
  197. var d = dd.getDate();
  198. if (m < 10) {
  199. m = '0' + m;
  200. };
  201. if (d < 10) {
  202. d = '0' + d;
  203. };
  204. return y + "-" + m + "-" + d;
  205. }
  206. const timeArray = ['9:00-09:30', '09:30-10:00', '10:30-11:00', '11:00-11:30', '11:30-12:00', '12:00-12:30', '12:30-13:00', '13:00-13:30', '13:30-14:00', '14:00-14:30', '14:30-15:00', '15:00-15:30', '15:30-16:00', '16:00-16:30', '16:30-17:00', '17:00-17:30', '17:30-18:00', '18:00-18:30', '18:30-19:00', '19:00-19:30', '19:30-20:00', '20:00-20:30', '20:30-21:00'];
  207. function getNowTimeArray() {
  208. var now = new Date();
  209. var hh = now.getHours(); //获取当前小时数(0-23)
  210. var mm = now.getMinutes(); //获取当前分钟数(0-59)
  211. var index = hh * 2 + (mm >= 30 ? 1 : 0);
  212. var result = timeArray.slice(0); // 深copy
  213. if (index > 17) {
  214. result = result.splice(index - 17 - 1)
  215. }
  216. var startArray = new Array();
  217. startArray.push('尽快送达');
  218. return startArray.concat(result);
  219. }
  220. function uuid() {
  221. var s = [];
  222. var hexDigits = "0123456789abcdef";
  223. for (var i = 0; i < 36; i++) {
  224. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  225. }
  226. s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  227. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  228. s[8] = s[13] = s[18] = s[23] = "-";
  229. var uuid = s.join("");
  230. return uuid;
  231. }
  232. Array.prototype.contains = function (obj) {
  233. var i = this.length;
  234. while (i--) {
  235. if (this[i] === obj) {
  236. return true;
  237. }
  238. }
  239. return false;
  240. }
  241. module.exports = {
  242. formatTime,
  243. request,
  244. redirect,
  245. showErrorToast,
  246. showSuccessToast,
  247. countdown,
  248. getLocation,
  249. chooseLocation,
  250. getNowTime,
  251. getDateStr,
  252. timeArray,
  253. getNowTimeArray,
  254. uuid
  255. }