http.js 7.6 KB

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