http.js 7.2 KB

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