http.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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') || wx.getStorageSync('tempToken')
  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. // console.log(res);
  23. if (res.statusCode == 200) {
  24. //如果有定义了params.callBack,则调用 params.callBack(res.data)
  25. if (params.callBack) {
  26. params.callBack(res.data);
  27. }
  28. } else if (res.statusCode == 500) {
  29. wx.hideLoading()
  30. setTimeout(() => {
  31. wx.showToast({
  32. title: res.data || res.data.message,
  33. icon: "none"
  34. })
  35. }, 1)
  36. } else if (res.statusCode == 401) {
  37. if (!params.dontTrunLogin) {
  38. if (!wx.getStorageSync('token')) {
  39. // 添加到请求队列
  40. globalData.requestQueue.push(params);
  41. // 是否正在登陆
  42. if (!globalData.isLanding) {
  43. globalData.isLanding = true
  44. //重新获取token,再次请求接口
  45. getToken();
  46. }
  47. } else {
  48. wx.removeStorageSync('token')
  49. wx.removeStorageSync('loginResult')
  50. wx.hideLoading();
  51. wx.showModal({
  52. title: '提示',
  53. content: '登录已过期,请重新登录~',
  54. showCancel: false,
  55. success: modalRes => {
  56. if (modalRes.confirm) {
  57. wx.redirectTo({
  58. url: '/pages/login/login',
  59. })
  60. }
  61. }
  62. })
  63. }
  64. }
  65. } else if (res.statusCode == 400 && params.errCallBack) {
  66. if (params.errCallBack) {
  67. params.errCallBack(res);
  68. }
  69. if (!globalData.isLanding) {
  70. wx.hideLoading();
  71. }
  72. } else if (res.statusCode == 400 && !params.errCallBack) {
  73. wx.hideLoading()
  74. setTimeout(() => {
  75. wx.showToast({
  76. title: res.data,
  77. icon: "none"
  78. })
  79. }, 1)
  80. } else {
  81. //如果有定义了params.errCallBack,则调用 params.errCallBack(res.data)
  82. if (params.errCallBack) {
  83. params.errCallBack(res);
  84. }
  85. if (!globalData.isLanding) {
  86. wx.hideLoading();
  87. }
  88. }
  89. },
  90. fail: function (err) {
  91. wx.hideLoading();
  92. setTimeout(() => {
  93. wx.showToast({
  94. title: err.message || "服务器出了点小差",
  95. icon: "none"
  96. });
  97. }, 1)
  98. }
  99. })
  100. }
  101. //上传文件统一接口
  102. function upload(params) {
  103. wx.uploadFile({
  104. url: config.domain + params.url,
  105. filePath: params.filePath,
  106. name: params.name,
  107. header: {
  108. 'Content-Type': 'multipart/form-data',
  109. 'Authorization': params.login ? undefined : wx.getStorageSync('token')
  110. },
  111. dataType: 'json',
  112. responseType: params.responseType == undefined ? 'json' : params.responseType,
  113. success: function (res) {
  114. console.log(res);
  115. //console.log(res);
  116. if (res.statusCode == 200) {
  117. //如果有定义了params.callBack,则调用 params.callBack(res.data)
  118. if (params.callBack) {
  119. params.callBack(res.data);
  120. }
  121. } else {
  122. wx.showToast({
  123. title: res.message || "服务器出了点小差",
  124. icon: "none"
  125. });
  126. }
  127. },
  128. fail: function (err) {
  129. wx.hideLoading();
  130. }
  131. })
  132. }
  133. //通过code获取token,并保存到缓存
  134. var getToken = function (fn) {
  135. wx.login({
  136. success: res => {
  137. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  138. console.log(res);
  139. request({
  140. login: true,
  141. url: '/appLogin',
  142. data: {
  143. principal: res.code,
  144. appType: 1
  145. },
  146. callBack: result => {
  147. loginSuccess(result, fn)
  148. }
  149. }, true)
  150. }
  151. })
  152. }
  153. function loginSuccess(result, fn) {
  154. // 保存登陆信息
  155. wx.setStorageSync('loginResult', result)
  156. // 没有获取到用户昵称,说明服务器没有保存用户的昵称,也就是用户授权的信息并没有传到服务器
  157. if (!result.pic) {
  158. updateUserInfo();
  159. }
  160. if (!result.enabled) {
  161. wx.showModal({
  162. showCancel: false,
  163. title: '提示',
  164. content: '您已被禁用,不能购买,请联系客服'
  165. })
  166. wx.setStorageSync('token', '');
  167. } else {
  168. wx.setStorageSync('token', 'bearer' + result.access_token); //把token存入缓存,请求接口数据时要用
  169. }
  170. var globalData = getApp().globalData;
  171. globalData.isLanding = false;
  172. while (globalData.requestQueue.length) {
  173. request(globalData.requestQueue.pop());
  174. }
  175. if (fn) {
  176. fn()
  177. }
  178. }
  179. // 更新用户头像昵称
  180. function updateUserInfo() {
  181. wx.getUserInfo({
  182. success: (res) => {
  183. var userInfo = JSON.parse(res.rawData)
  184. request({
  185. url: "/p/user/setUserInfo",
  186. method: "PUT",
  187. data: {
  188. avatarUrl: userInfo.avatarUrl,
  189. nickName: userInfo.nickName
  190. }
  191. });
  192. }
  193. })
  194. }
  195. //获取购物车商品数量
  196. function getCartCount() {
  197. var params = {
  198. url: "/p/shopCart/prodCount",
  199. method: "GET",
  200. dontTrunLogin: true,
  201. data: {},
  202. callBack: function (res) {
  203. if (res > 0) {
  204. wx.setTabBarBadge({
  205. index: 2,
  206. text: res + "",
  207. })
  208. var app = getApp();
  209. app.globalData.totalCartCount = res;
  210. } else {
  211. wx.removeTabBarBadge({
  212. index: 2
  213. })
  214. var app = getApp();
  215. app.globalData.totalCartCount = 0;
  216. }
  217. }
  218. };
  219. request(params);
  220. }
  221. // 查看是否授权
  222. function isUserAuthInfo() {
  223. // 查看是否授权
  224. wx.getSetting({
  225. success(res) {
  226. if (res.authSetting['scope.userInfo']) {
  227. // 已经授权,可以直接调用 getUserInfo 获取头像昵称
  228. wx.getUserInfo({
  229. success: function (res) {
  230. console.log(res.userInfo)
  231. }
  232. })
  233. }
  234. }
  235. })
  236. }
  237. exports.getToken = getToken;
  238. exports.request = request;
  239. exports.getCartCount = getCartCount;
  240. exports.updateUserInfo = updateUserInfo;
  241. exports.upload = upload;
  242. exports.loginSuccess = loginSuccess;