util.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. function get(url, data = {}) {
  69. return request(url, data, 'GET')
  70. }
  71. function post(url, data = {}) {
  72. return request(url, data, 'POST')
  73. }
  74. /**
  75. * 检查微信会话是否过期
  76. */
  77. function checkSession() {
  78. return new Promise(function (resolve, reject) {
  79. wx.checkSession({
  80. success: function () {
  81. resolve(true);
  82. },
  83. fail: function () {
  84. reject(false);
  85. }
  86. })
  87. });
  88. }
  89. /**
  90. * 调用微信登录
  91. */
  92. function login() {
  93. return new Promise(function (resolve, reject) {
  94. wx.login({
  95. success: function (res) {
  96. if (res.code) {
  97. resolve(res.code);
  98. } else {
  99. reject(res);
  100. }
  101. },
  102. fail: function (err) {
  103. reject(err);
  104. }
  105. });
  106. });
  107. }
  108. function getUserInfo() {
  109. return new Promise(function (resolve, reject) {
  110. wx.getUserInfo({
  111. withCredentials: true,
  112. success: function (res) {
  113. if (res.detail.errMsg === 'getUserInfo:ok') {
  114. resolve(res);
  115. } else {
  116. reject(res)
  117. }
  118. },
  119. fail: function (err) {
  120. reject(err);
  121. }
  122. })
  123. });
  124. }
  125. function redirect(url) {
  126. //判断页面是否需要登录
  127. if (false) {
  128. wx.redirectTo({
  129. url: '/pages/auth/login/login'
  130. });
  131. return false;
  132. } else {
  133. wx.redirectTo({
  134. url: url
  135. });
  136. }
  137. }
  138. function showErrorToast(msg) {
  139. wx.showToast({
  140. title: msg,
  141. image: '/static/images/icon_error.png'
  142. })
  143. }
  144. module.exports = {
  145. formatTime,
  146. request,
  147. get,
  148. post,
  149. redirect,
  150. showErrorToast,
  151. checkSession,
  152. login,
  153. getUserInfo,
  154. }