http.js 7.7 KB

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