http.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. var config = require('../config/config.js')
  2. import {
  3. showToast
  4. } from '../utils/tips.js'
  5. //model状态
  6. let model = true;
  7. let loading = true;
  8. //服务器地址
  9. const apiUrl = config.apiUrl;
  10. /**
  11. * 处理请求数据
  12. */
  13. const getData = (data) => {
  14. // 预先取值
  15. const { user_token, user_id, member_id } = {
  16. user_token: wx.getStorageSync('user_token'),
  17. user_id: wx.getStorageSync('user_id'),
  18. member_id: wx.getStorageSync('member_id'),
  19. }
  20. // 删除空值
  21. let tmp_data = { user_token, user_id, member_id }
  22. for (let key in tmp_data) {
  23. if (!tmp_data[key] && tmp_data[key] !== 0) {
  24. delete tmp_data[key]
  25. }
  26. }
  27. // 再次过滤
  28. const _data = Object.assign(data, tmp_data)
  29. for (let key in _data) {
  30. if (!_data[key] && _data[key] !== 0) {
  31. delete _data[key]
  32. }
  33. }
  34. return _data;
  35. }
  36. /**
  37. * 封装http 请求方法
  38. */
  39. const http = (params) => {
  40. //返回promise 对象
  41. return new Promise((resolve, reject) => {
  42. if (loading) {
  43. loading = false;
  44. wx.showLoading({
  45. title: 'loading',
  46. mask: true
  47. });
  48. }
  49. const pages = getCurrentPages();
  50. wx.request({
  51. url: apiUrl + params.url, //服务器url+参数中携带的接口具体地址
  52. data: getData(params.data), //请求参数
  53. header: Object.assign({
  54. "Content-Type": "application/json" //设置后端需要的常用的格式就好,特殊情况调用的时候单独设置
  55. }, params.header || {}),
  56. method: params.method || 'GET', //默认为GET,可以不写,如常用请求格式为POST,可以设置POST为默认请求方式
  57. dataType: params.dataType, //返回的数据格式,默认为JSON,特殊格式可以在调用的时候传入参数
  58. responseType: params.responseType, //响应的数据类型
  59. success: function (res) {
  60. wx.hideLoading({
  61. success: () => {
  62. loading = true
  63. }
  64. });
  65. if (res.statusCode == 200) {
  66. var errorCode = res.data.errcode
  67. if (errorCode == 0) {
  68. return resolve(res.data)
  69. } else if (errorCode == 1014) {
  70. wx.setStorageSync('openId', res.data.data.openId);
  71. wx.setStorageSync('sessionKey', res.data.data.sessionKey);
  72. wx.setStorageSync('unionId', res.data.data.unionId);
  73. //前往绑定手机号
  74. wx.redirectTo({
  75. url: '/pages/bind-phone/index',
  76. })
  77. } else if (errorCode == 4000) {
  78. wx.showModal({
  79. title: '提示',
  80. confirmText: '好的',
  81. content: res.data.errmsg,
  82. showCancel: false,
  83. success: () => {
  84. // wx.setClipboardData({
  85. // data: '4009960099',
  86. // success: (res) => {
  87. // // wx.showToast({
  88. // // icon: 'none',
  89. // // title: '客服电话已复制',
  90. // // })
  91. // },
  92. // })
  93. }
  94. })
  95. return
  96. } else if (errorCode == 1005) {
  97. //未获取到微信登录信息
  98. wx.navigateTo({
  99. url: '/pages/login/login',
  100. })
  101. } else if (errorCode == 2009) {
  102. //用户信息重复,请选择用户信息
  103. wx.setStorageSync('userList', res.data.data.userList);
  104. wx.setStorageSync('currentUser', res.data.data.currentUser);
  105. wx.redirectTo({
  106. url: '/pages/select-info/index',
  107. })
  108. } else if (errorCode == 2008) {
  109. //账号不存在,或token无效
  110. if (model && params.data.task_id !== 10) {
  111. const _type = pages.length === 1 ? 'reLaunch' : 'navigateTo';
  112. wx[_type]({
  113. url: '/pages/login/login',
  114. success: res => {
  115. wx.showModal({
  116. title: '提示',
  117. content: '登录失效请重新登录',
  118. showCancel: false,
  119. success: res => {
  120. if (res.confirm) {
  121. model = true;
  122. }
  123. }
  124. })
  125. }
  126. })
  127. // if (pages.length === 1) {
  128. // wx.reLaunch({
  129. // url: '/pages/login/login',
  130. // success: res => {
  131. // wx.showModal({
  132. // title: '提示',
  133. // content: '登录失效请重新登录',
  134. // showCancel: false,
  135. // success: res => {
  136. // if (res.confirm) {
  137. // model = true;
  138. // }
  139. // }
  140. // })
  141. // }
  142. // })
  143. // } else {
  144. // wx.navigateTo({
  145. // url: '/pages/login/login',
  146. // success: res => {
  147. // wx.showModal({
  148. // title: '提示',
  149. // content: '登录失效请重新登录',
  150. // showCancel: false,
  151. // success: res => {
  152. // if (res.confirm) {
  153. // model = true;
  154. // }
  155. // }
  156. // })
  157. // }
  158. // })
  159. // }
  160. model = false
  161. }
  162. return
  163. } else if (errorCode == 1007) {
  164. //手机号已被绑定
  165. } else if (errorCode == 40005) {
  166. //用户信息和微信信息不匹配
  167. } else if (errorCode == 1015) {
  168. //用户尚未创建简历
  169. return reject(res.data);
  170. } else if (errorCode == 40007) {
  171. //账号类型不符
  172. } else if (errorCode == 2006) {
  173. //后台接口异常
  174. } else if (errorCode == 2010) {
  175. //用户未登录
  176. const _type = pages.length === 1 ? 'reLaunch' : 'navigateTo';
  177. wx[_type]({
  178. url: '/pages/login/login',
  179. success: data => {
  180. wx.showToast({
  181. icon: 'none',
  182. title: '请先登录',
  183. })
  184. }
  185. })
  186. // if (pages.length === 1) {
  187. // wx.reLaunch({
  188. // url: '/pages/login/login',
  189. // success: data => {
  190. // wx.showToast({
  191. // icon: 'none',
  192. // title: '请先登录',
  193. // })
  194. // }
  195. // })
  196. // } else {
  197. // wx.navigateTo({
  198. // url: '/pages/login/login',
  199. // success: data => {
  200. // wx.showToast({
  201. // icon: 'none',
  202. // title: '请先登录',
  203. // })
  204. // }
  205. // })
  206. // }
  207. return
  208. }
  209. params.data.task_id !== 10 && showToast(res.data.errmsg);
  210. reject(res.data);
  211. } else {
  212. //2. 操作不成功返回数据,以toast方式弹出响应信息,如后端未格式化非操作成功异常信息,则可以统一定义异常提示
  213. wx.showToast({
  214. icon: "none",
  215. title: "网络异常",
  216. success: () => {
  217. reject()
  218. }
  219. })
  220. }
  221. },
  222. fail: function (e) {
  223. wx.hideLoading({
  224. success: () => {
  225. loading = true
  226. }
  227. });
  228. wx.showToast({
  229. icon: "none",
  230. title: "网络异常",
  231. success: () => {
  232. loading = true
  233. reject()
  234. }
  235. })
  236. },
  237. complete: (res) => {
  238. wx.stopPullDownRefresh({
  239. success: () => {
  240. console.log(`${pages[pages.length - 1] ? pages[pages.length - 1].route : pages} => stopPullDownRefresh`);
  241. }
  242. });
  243. try {
  244. console.log('当前页面=>', pages[pages.length - 1].route)
  245. console.log("请求参数=>", params);
  246. console.log(`请求状态码=>${res.statusCode}`, `状态信息=>${res.errMsg}`);
  247. console.log("返回结果=>", res.data);
  248. console.log(' ');
  249. } catch (error) {
  250. console.log(error);
  251. }
  252. }
  253. })
  254. })
  255. };
  256. module.exports = {
  257. http
  258. }