util.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const app = getApp();
  2. const formatTime = date => {
  3. const year = date.getFullYear()
  4. const month = date.getMonth() + 1
  5. const day = date.getDate()
  6. const hour = date.getHours()
  7. const minute = date.getMinutes()
  8. const second = date.getSeconds()
  9. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  10. };
  11. const loginIntercept = function () {
  12. wx.request({
  13. url: app.globalData.prodUrl + 'web-sso/get_open_id',
  14. data: {
  15. code: wx.getStorageSync('key').code
  16. },
  17. success: function (data) {
  18. wx.setStorage({
  19. key: "key",
  20. data: {
  21. openid: data.data.data.openid,
  22. ydw_token: data.data.ydw_token
  23. }
  24. });
  25. wx.setStorage({
  26. key: "persion",
  27. data: {
  28. persion: data.data.persion
  29. }
  30. })
  31. }
  32. })
  33. };
  34. const formatNumber = n => {
  35. n = n.toString()
  36. return n[1] ? n : '0' + n
  37. }
  38. function formatTimeTwo(number, format) {
  39. var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  40. var returnArr = [];
  41. var date = new Date(number);
  42. returnArr.push(date.getFullYear());
  43. returnArr.push(formatNumber(date.getMonth() + 1));
  44. returnArr.push(formatNumber(date.getDate()));
  45. returnArr.push(formatNumber(date.getHours()));
  46. returnArr.push(formatNumber(date.getMinutes()));
  47. returnArr.push(formatNumber(date.getSeconds()));
  48. for (var i in returnArr) {
  49. format = format.replace(formateArr[i], returnArr[i]);
  50. }
  51. return format;
  52. }
  53. module.exports = {
  54. formatTime: formatTime,
  55. loginIntercept: loginIntercept,
  56. formatTimeTwo: formatTimeTwo // 时间戳转日期
  57. }