http.js 7.6 KB

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