util.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import moment from 'moment';
  2. const app = getApp();
  3. const formatTime = date => {
  4. const year = date.getFullYear()
  5. const month = date.getMonth() + 1
  6. const day = date.getDate()
  7. const hour = date.getHours()
  8. const minute = date.getMinutes()
  9. const second = date.getSeconds()
  10. return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  11. }
  12. const getWeekByDate = dates => {
  13. let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
  14. let date = new Date(dates);
  15. date.setDate(date.getDate());
  16. let day = date.getDay();
  17. return show_day[day];
  18. }
  19. const formatNumber = n => {
  20. n = n.toString()
  21. return n[1] ? n : `0${n}`
  22. }
  23. function formatDate(date) {
  24. var year = date.getFullYear();
  25. var month = date.getMonth() + 1;
  26. var day = date.getDate();
  27. if (month < 10)
  28. month = "0" + month;
  29. if (day < 10)
  30. day = "0" + day;
  31. return [year, month, day].map(formatNumber).join('-');
  32. }
  33. //时间戳转日期,falg:true表示只要年月日,part: year month date
  34. function toDate(number, flag, part) {
  35. var n = number;
  36. var date = new Date(parseInt(n) * 1000);
  37. var y = date.getFullYear();
  38. var m = date.getMonth() + 1;
  39. m = m < 10 ? ('0' + m) : m;
  40. var d = date.getDate();
  41. d = d < 10 ? ('0' + d) : d;
  42. var h = date.getHours();
  43. h = h < 10 ? ('0' + h) : h;
  44. var minute = date.getMinutes();
  45. var second = date.getSeconds();
  46. minute = minute < 10 ? ('0' + minute) : minute;
  47. second = second < 10 ? ('0' + second) : second;
  48. if (flag) {
  49. if (part == "year") {
  50. return y;
  51. } else if (part == "month") {
  52. return m;
  53. } else if (part == "date") {
  54. return n;
  55. }
  56. return y + '-' + m + '-' + d;
  57. }
  58. return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
  59. }
  60. //当前日期加减天数,falg:true表示只要年月日
  61. function mathChangeDate(date, method, days, flag) {
  62. //method:'+' || '-'
  63. //ios不解析带'-'的日期格式,要转成'/',不然Nan,切记
  64. var dateVal = date.replace(/-/g, '/');
  65. var timestamp = Date.parse(dateVal);
  66. if (method == '+') {
  67. timestamp = timestamp / 1000 + 24 * 60 * 60 * days;
  68. } else if (method == '-') {
  69. timestamp = timestamp / 1000 - 24 * 60 * 60 * days;
  70. }
  71. return toDate(timestamp, flag);
  72. }
  73. /**
  74. * 封封微信的的request
  75. */
  76. function request(url, data = {}, method = "GET") {
  77. var url=url;
  78. // console.log(app.globalData);
  79. let isUat=wx.getStorageSync('isUat');
  80. if(!isUat){
  81. url=url.replace('uat.','')
  82. }
  83. return new Promise(function (resolve, reject) {
  84. wx.request({
  85. url: url,
  86. data: data,
  87. method: method,
  88. header: {
  89. 'Content-Type': 'application/json',
  90. 'X-JZ-Token': wx.getStorageSync('token')
  91. },
  92. success: function (res) {
  93. if (res.statusCode == 200) {
  94. if (res.data.errno == 501||res.data.errno == 401) {
  95. // 清除登录相关内容
  96. try {
  97. wx.removeStorageSync('userInfo');
  98. wx.removeStorageSync('token');
  99. } catch (e) {
  100. // Do something when catch error
  101. }
  102. // 切换到登录页面
  103. wx.redirectTo({
  104. url: '/pages/index/index'
  105. });
  106. } else {
  107. resolve(res.data);
  108. }
  109. } else {
  110. reject(res.errMsg);
  111. }
  112. },
  113. fail: function (err) {
  114. reject(err)
  115. }
  116. })
  117. });
  118. }
  119. function showErrorToast(msg) {
  120. wx.showToast({
  121. title: msg,
  122. image: '/static/images/icon_error.png'
  123. })
  124. }
  125. function formatweek(date) {
  126. let week = date.getDay();
  127. let weekArr = '';
  128. switch (week) {
  129. case 1:
  130. weekArr = '周一';
  131. break;
  132. case 2:
  133. weekArr = '周二';
  134. break;
  135. case 3:
  136. weekArr = '周三';
  137. break;
  138. case 4:
  139. weekArr = '周四';
  140. break;
  141. case 5:
  142. weekArr = '周五';
  143. break;
  144. case 6:
  145. weekArr = '周六';
  146. break;
  147. case 0:
  148. weekArr = '周日';
  149. break;
  150. }
  151. return weekArr;
  152. }
  153. function getDateList(date, interval, length) {
  154. let dateList = [];
  155. let len = (length ? length : 7);
  156. for (let i = 0; i < len; i++) {
  157. let datestamp = new Date(date.getTime() + i * 60 * 60 * 1000 * 24 * interval);
  158. let dateItem = {
  159. week: formatweek(datestamp),
  160. date: formetdate(datestamp),
  161. id: new Date(datestamp).getDay()
  162. };
  163. dateList.push(dateItem);
  164. }
  165. return dateList;
  166. }
  167. function dateDifference(end) {
  168. // //比较2个日期
  169. // console.log(start + ",," + end);
  170. // var stime = new Date(start).getTime();
  171. var stime = new Date().getTime();
  172. console.log(end.replace(/-/g, "/"));
  173. var etime = new Date(end.replace(/-/g, "/")).getTime();
  174. var usedTime = etime - stime; //两个时间戳相差的毫秒数
  175. var days = Math.floor(usedTime / (24 * 3600 * 1000));
  176. //计算出小时数
  177. var leave1 = usedTime % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  178. var hours = Math.floor(leave1 / (3600 * 1000));
  179. //计算相差分钟数
  180. var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  181. var minutes = Math.floor(leave2 / (60 * 1000));
  182. if (days == 0 && hours == 0) {
  183. return minutes + "分钟";
  184. }
  185. if (days == 0) {
  186. return hours + "时" + minutes + "分钟";
  187. }
  188. var time = days + "天" + hours + "时" + minutes + "分钟";
  189. // var time = days;
  190. return time;
  191. }
  192. function formetdate(date) {
  193. var year = date.getFullYear();
  194. var month = date.getMonth() + 1;
  195. var day = date.getDate();
  196. return [year, month, day].map(formatNumber).join('-');
  197. }
  198. function getYearMonth(str,method){
  199. let arr = str.split('-');
  200. let year = arr[0]; //获取当前日期的年份
  201. let month = arr[1]; //获取当前日期的月份
  202. let total=0;
  203. if (method == '+') {
  204. total=Number(year*12+Number(month)+1).toFixed(0);
  205. } else if (method == '-') {
  206. total=Number(year*12+Number(month)-1).toFixed(0);
  207. }
  208. let year2=parseInt(total/12);
  209. let month2=total%12;
  210. if(month2==0){
  211. year2=parseInt(total/12)-1;
  212. month2=12;
  213. }
  214. if (month2 < 10) {
  215. month2 = '0' + month2;
  216. }
  217. let str2 = year2 + '-' + month2 ;
  218. return str2;
  219. }
  220. function getWeek(day) {
  221. let week=moment(day).format("W");
  222. return week;
  223. }
  224. function chineseDate(day) {
  225. let date=moment(day).format("YYYY年MM月DD日");
  226. return date;
  227. }
  228. function forMatTime(array){
  229. console.log("array",array);
  230. for(let i=0;i<array.length;i++){
  231. let newString = array[i].createTime.replace('T', ' ');
  232. newString = newString.slice(0,16)
  233. array[i].createTime=newString
  234. }
  235. return array
  236. }
  237. module.exports = {
  238. formatTime,
  239. formatDate,
  240. request,
  241. mathChangeDate,
  242. showErrorToast,
  243. getWeekByDate,
  244. formatweek,
  245. getDateList,
  246. dateDifference,
  247. formetdate,
  248. toDate,
  249. getYearMonth,
  250. getWeek,
  251. chineseDate,
  252. forMatTime
  253. }