util.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. var api = require('../config/api.js');
  2. function formatTime(date) {
  3. var year = date.getFullYear()
  4. var month = date.getMonth() + 1
  5. var day = date.getDate()
  6. var hour = date.getHours()
  7. var minute = date.getMinutes()
  8. var second = date.getSeconds()
  9. return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  10. }
  11. function formatNumber(n) {
  12. n = n.toString()
  13. return n[1] ? n : '0' + n
  14. }
  15. /**
  16. * 封封微信的的request
  17. */
  18. function request(url, data = {}, method = "GET") {
  19. return new Promise(function (resolve, reject) {
  20. wx.request({
  21. url: url,
  22. data: data,
  23. method: method,
  24. header: {
  25. 'Content-Type': 'application/json',
  26. 'X-Nideshop-Token': wx.getStorageSync('token')
  27. },
  28. success: function (res) {
  29. console.log("success");
  30. if (res.statusCode == 200) {
  31. if (res.data.errno == 401) {
  32. //需要登录后才可以操作
  33. let code = null;
  34. return login().then((res) => {
  35. code = res.code;
  36. return getUserInfo();
  37. }).then((userInfo) => {
  38. //登录远程服务器
  39. request(api.AuthLoginByWeixin, { code: code, userInfo: userInfo }, 'POST').then(res => {
  40. if (res.errno === 0) {
  41. //存储用户信息
  42. wx.setStorageSync('userInfo', res.data.userInfo);
  43. wx.setStorageSync('token', res.data.token);
  44. resolve(res);
  45. } else {
  46. reject(res);
  47. }
  48. }).catch((err) => {
  49. reject(err);
  50. });
  51. }).catch((err) => {
  52. reject(err);
  53. })
  54. } else {
  55. resolve(res.data);
  56. }
  57. } else {
  58. reject(res.errMsg);
  59. }
  60. },
  61. fail: function (err) {
  62. reject(err)
  63. console.log("failed")
  64. }
  65. })
  66. });
  67. }
  68. /**
  69. * 检查微信会话是否过期
  70. */
  71. function checkSession() {
  72. return new Promise(function (resolve, reject) {
  73. wx.checkSession({
  74. success: function () {
  75. resolve(true);
  76. },
  77. fail: function () {
  78. reject(false);
  79. }
  80. })
  81. });
  82. }
  83. /**
  84. * 调用微信登录
  85. */
  86. function login() {
  87. return new Promise(function (resolve, reject) {
  88. wx.login({
  89. success: function (res) {
  90. if (res.code) {
  91. //登录远程服务器
  92. console.log(res)
  93. resolve(res);
  94. } else {
  95. reject(res);
  96. }
  97. },
  98. fail: function (err) {
  99. reject(err);
  100. }
  101. });
  102. });
  103. }
  104. function getUserInfo() {
  105. return new Promise(function (resolve, reject) {
  106. wx.getUserInfo({
  107. withCredentials: true,
  108. success: function (res) {
  109. console.log(res)
  110. resolve(res);
  111. },
  112. fail: function (err) {
  113. reject(err);
  114. }
  115. })
  116. });
  117. }
  118. function redirect(url) {
  119. //判断页面是否需要登录
  120. if (false) {
  121. wx.redirectTo({
  122. url: '/pages/auth/login/login'
  123. });
  124. return false;
  125. } else {
  126. wx.redirectTo({
  127. url: url
  128. });
  129. }
  130. }
  131. function showErrorToast(msg) {
  132. wx.showToast({
  133. title: msg,
  134. image: '/static/images/icon_error.png'
  135. })
  136. }
  137. module.exports = {
  138. formatTime,
  139. request,
  140. redirect,
  141. showErrorToast,
  142. checkSession,
  143. login,
  144. getUserInfo,
  145. }