util.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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].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. let that = this
  20. let token = wx.getStorageSync('token')
  21. return new Promise(function(resolve, reject) {
  22. wx.request({
  23. url: url,
  24. data: data,
  25. method: method,
  26. header: {
  27. 'Content-Type': 'application/json',
  28. 'Authorization': token
  29. },
  30. success: function(res) {
  31. console.log("success");
  32. if (res.statusCode == 200) {
  33. if (res.data.errno == 3002 || res.data.errno == 3003 || res.data.errno == 3004 || res.data.errno == 3005) {
  34. console.log(res.data.errmsg)
  35. //TOKEN_IS_EMPTY
  36. //需要登录后才可以操作
  37. wx.getSetting({
  38. success: res => {
  39. if (res.authSetting['scope.userInfo']) {
  40. // // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  41. that.getUserInfo().then((res) => {
  42. that.backendLogin(res).then((res) => {
  43. that.request(url, data, method)
  44. console.log('再次请求')
  45. })
  46. })
  47. } else {
  48. wx.navigateTo({
  49. url: '/pages/auth/auth'
  50. })
  51. }
  52. }
  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. wx.login() //重新登录
  79. resolve(true);
  80. }
  81. })
  82. });
  83. }
  84. /**
  85. * 在后端服务器进行登录
  86. */
  87. function backendLogin(detail) {
  88. console.log("在后端服务器进行登录" + detail)
  89. let that = this;
  90. let code = null;
  91. return new Promise(function(resolve, reject) {
  92. return that.login().then((res) => {
  93. code = res.code;
  94. }).then(() => {
  95. //登录远程服务器
  96. that.request(api.AuthLoginByWeixin, {
  97. code: code,
  98. detail: detail
  99. }, 'POST').then(res => {
  100. if (res.errno === 0) {
  101. //存储用户信息
  102. wx.setStorageSync('userInfo', res.data.userInfo);
  103. wx.setStorageSync('token', res.data.token);
  104. resolve(res.data.userInfo);
  105. } else {
  106. reject(res);
  107. }
  108. }).catch((err) => { //request
  109. reject(err);
  110. });
  111. }).catch((err) => { //login
  112. reject(err);
  113. })
  114. });
  115. }
  116. /**
  117. * 调用微信登录,获取jscode
  118. */
  119. function login() {
  120. let that = this
  121. return new Promise(function(resolve, reject) {
  122. that.checkSession().then(() => {
  123. wx.login({
  124. success: function (res) {
  125. if (res.code) {
  126. //登录远程服务器
  127. console.log(res)
  128. resolve(res);
  129. } else {
  130. reject(res);
  131. }
  132. },
  133. fail: function (err) {
  134. reject(err);
  135. }
  136. });
  137. })
  138. });
  139. }
  140. /**
  141. * 获取userInfo,只有已经授权的用户能使用这个接口
  142. */
  143. function getUserInfo() {
  144. return new Promise(function(resolve, reject) {
  145. wx.getUserInfo({
  146. withCredentials: true,
  147. success: function(res) {
  148. console.log(res)
  149. resolve(res);
  150. },
  151. fail: function(err) {
  152. reject(err);
  153. }
  154. })
  155. });
  156. }
  157. function showErrorToast(msg) {
  158. wx.showToast({
  159. title: msg,
  160. image: '/static/images/icon_error.png'
  161. })
  162. }
  163. module.exports = {
  164. formatTime,
  165. request,
  166. showErrorToast,
  167. checkSession,
  168. login,
  169. getUserInfo,
  170. backendLogin,
  171. }