http.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. var config = require("config.js");
  2. //统一的网络请求方法
  3. function request(params, isGetTonken) {
  4. // 全局变量
  5. var globalData = getApp().globalData;
  6. // 如果正在进行登陆,就将非登陆请求放在队列中等待登陆完毕后进行调用
  7. // if (!isGetTonken && globalData.isLanding) {
  8. // globalData.requestQueue.push(params);
  9. // return;
  10. // }
  11. wx.request({
  12. url: config.domain + params.url, //接口请求地址
  13. data: params.data,
  14. header: {
  15. // 'content-type': params.method == "GET" ? 'application/x-www-form-urlencoded' : 'application/json;charset=utf-8',
  16. 'Authorization': params.login ? undefined : wx.getStorageSync('token')
  17. },
  18. method: params.method == undefined ? "POST" : params.method,
  19. dataType: 'json',
  20. responseType: params.responseType == undefined ? 'text' : params.responseType,
  21. success: function(res) {
  22. const responseData = res.data
  23. // 00000 请求成功
  24. if (responseData.code === '00000') {
  25. if (params.callBack) {
  26. params.callBack(responseData.data);
  27. }
  28. return
  29. }
  30. // A00004 未授权
  31. if (responseData.code === 'A00004') {
  32. wx.navigateTo({
  33. url: '/pages/login/login',
  34. })
  35. return
  36. }
  37. // A00005 服务器出了点小差
  38. if (responseData.code === 'A00005') {
  39. console.error('============== 请求异常 ==============')
  40. console.log('接口: ', params.url)
  41. console.log('异常信息: ', responseData)
  42. console.error('============== 请求异常 ==============')
  43. if (params.errCallBack) {
  44. params.errCallBack(responseData)
  45. return
  46. }
  47. wx.showToast({
  48. title: '服务器出了点小差~',
  49. icon: 'none'
  50. })
  51. }
  52. // A00001 用于直接显示提示用户的错误,内容由输入内容决定
  53. if (responseData.code === 'A00001') {
  54. if (params.errCallBack) {
  55. params.errCallBack(responseData)
  56. return
  57. }
  58. wx.showToast({
  59. title: responseData.msg || 'Error',
  60. icon: 'none'
  61. })
  62. return
  63. }
  64. // 其他异常
  65. if (responseData.code !== '00000') {
  66. // console.log('params', params)
  67. wx.hideLoading();
  68. if (params.errCallBack) {
  69. params.errCallBack(responseData)
  70. } else {
  71. console.log(`接口: ${params.url}`)
  72. console.log(`返回信息: `, res)
  73. }
  74. }
  75. if (!globalData.isLanding) {
  76. wx.hideLoading();
  77. }
  78. },
  79. fail: function(err) {
  80. wx.hideLoading();
  81. wx.showToast({
  82. title: "服务器出了点小差",
  83. icon: "none"
  84. });
  85. }
  86. })
  87. }
  88. //通过code获取token,并保存到缓存
  89. var getToken = function() {
  90. wx.login({
  91. success: res => {
  92. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  93. request({
  94. login: true,
  95. url: '/login?grant_type=mini_app',
  96. data: {
  97. principal: res.code
  98. },
  99. callBack: result => {
  100. // 没有获取到用户昵称,说明服务器没有保存用户的昵称,也就是用户授权的信息并没有传到服务器
  101. if (!result.nickName) {
  102. updateUserInfo();
  103. }
  104. if (result.userStutas == 0) {
  105. wx.showModal({
  106. showCancel: false,
  107. title: '提示',
  108. content: '您已被禁用,不能购买,请联系客服'
  109. })
  110. wx.setStorageSync('token', '');
  111. } else {
  112. wx.setStorageSync('token', 'bearer' + result.access_token); //把token存入缓存,请求接口数据时要用
  113. }
  114. var globalData = getApp().globalData;
  115. globalData.isLanding = false;
  116. while (globalData.requestQueue.length) {
  117. request(globalData.requestQueue.pop());
  118. }
  119. }
  120. }, true)
  121. }
  122. })
  123. }
  124. // 更新用户头像昵称
  125. function updateUserInfo() {
  126. wx.getUserInfo({
  127. success: (res) => {
  128. var userInfo = JSON.parse(res.rawData)
  129. request({
  130. url: "/p/user/setUserInfo",
  131. method: "PUT",
  132. data: {
  133. avatarUrl: userInfo.avatarUrl,
  134. nickName: userInfo.nickName
  135. }
  136. });
  137. }
  138. })
  139. }
  140. //获取购物车商品数量
  141. function getCartCount() {
  142. var params = {
  143. url: "/p/shopCart/prodCount",
  144. method: "GET",
  145. data: {},
  146. callBack: function(res) {
  147. if (res > 0) {
  148. wx.setTabBarBadge({
  149. index: 2,
  150. text: res + "",
  151. })
  152. var app = getApp();
  153. app.globalData.totalCartCount = res;
  154. } else {
  155. wx.removeTabBarBadge({
  156. index: 2
  157. })
  158. var app = getApp();
  159. app.globalData.totalCartCount = 0;
  160. }
  161. }
  162. };
  163. request(params);
  164. }
  165. exports.getToken = getToken;
  166. exports.request = request;
  167. exports.getCartCount = getCartCount;
  168. exports.updateUserInfo = updateUserInfo;