小程序端工程代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.9 KiB

  1. // #ifdef H5
  2. // h5端
  3. import Fly from 'flyio/dist/npm/fly'
  4. // #endif
  5. // #ifdef APP-PLUS
  6. // app端
  7. import Fly from 'flyio/dist/npm/wx'
  8. // #endif
  9. // #ifdef MP-WEIXIN
  10. import Fly from 'flyio/dist/npm/wx'
  11. // #endif
  12. import store from '../store'
  13. import { handleLoginFailure } from '@/utils'
  14. import { VUE_APP_API_URL } from '@/config'
  15. import cookie from '@/utils/store/cookie'
  16. const fly = new Fly()
  17. fly.config.baseURL = VUE_APP_API_URL
  18. // 小程序请求域名
  19. // #ifdef MP-WEIXIN
  20. fly.config.baseURL = 'http://347i13244b.zicp.vip/api'
  21. // #endif
  22. // #ifdef APP-PLUS
  23. // app端
  24. fly.config.baseURL = 'http://347i13244b.zicp.vip/api'
  25. // #endif
  26. fly.interceptors.response.use(
  27. response => {
  28. // console.log(response)
  29. // 定时刷新access-token
  30. return response
  31. },
  32. error => {
  33. console.log(error)
  34. if (error.toString() == 'Error: Network Error') {
  35. console.log('————————')
  36. console.log('请求失败', error)
  37. console.log('————————')
  38. handleLoginFailure()
  39. return Promise.reject({ msg: '未登录', toLogin: true })
  40. }
  41. if (error.status == 401) {
  42. console.log('————————')
  43. console.log('登录失效 401', error)
  44. console.log('————————')
  45. handleLoginFailure()
  46. return Promise.reject({ msg: '未登录', toLogin: true })
  47. }
  48. if (error.response.data.status == 5109) {
  49. uni.showToast({
  50. title: error.response.data.msg,
  51. icon: 'none',
  52. duration: 2000,
  53. })
  54. }
  55. return Promise.reject(error)
  56. }
  57. )
  58. const defaultOpt = { login: true }
  59. function baseRequest(options) {
  60. // 从缓存中获取 token 防止 token 失效后还会继续请求的情况
  61. const token = cookie.get('login_status')
  62. // 合并传参过来的 headers
  63. // 如果接口需要登录,携带 token 去请求
  64. options.headers = {
  65. ...options.headers,
  66. }
  67. if (options.login === true) {
  68. options.headers = {
  69. ...options.headers,
  70. Authorization: 'Bearer ' + token,
  71. }
  72. }
  73. // // 如果需要登录才可访问的接口没有拿到 token 视为登录失效
  74. // if (options.login === true && !token) {
  75. // // 跳转到登录或授权页面
  76. // handleLoginFailure()
  77. // // 提示错误信息
  78. // return Promise.reject({ msg: '未登录', toLogin: true })
  79. // }
  80. // 结构请求需要的参数
  81. const { url, params, data, login, ...option } = options
  82. // 发起请求
  83. return fly
  84. .request(url, params || data, {
  85. ...option,
  86. })
  87. .then(res => {
  88. const data = res.data || {}
  89. if (res.status !== 200) {
  90. return Promise.reject({ msg: '请求失败', res, data })
  91. }
  92. console.log(data)
  93. if ([401, 403].indexOf(data.status) !== -1) {
  94. handleLoginFailure()
  95. return Promise.reject({ msg: res.data.msg, res, data, toLogin: true })
  96. } else if (data.status === 200) {
  97. return Promise.resolve(data, res)
  98. } else if (data.status == 5101) {
  99. return Promise.reject({ msg: res.data.msg, res, data })
  100. } else {
  101. return Promise.reject({ msg: res.data.msg, res, data })
  102. }
  103. })
  104. }
  105. /**
  106. * http 请求基础类
  107. * 参考文档 https://www.kancloud.cn/yunye/axios/234845
  108. *
  109. */
  110. const request = ['post', 'put', 'patch'].reduce((request, method) => {
  111. /**
  112. *
  113. * @param url string 接口地址
  114. * @param data object get参数
  115. * @param options object axios 配置项
  116. * @returns {AxiosPromise}
  117. */
  118. request[method] = (url, data = {}, options = {}) => {
  119. return baseRequest(Object.assign({ url, data, method }, defaultOpt, options))
  120. }
  121. return request
  122. }, {});
  123. ['get', 'delete', 'head'].forEach(method => {
  124. /**
  125. *
  126. * @param url string 接口地址
  127. * @param params object get参数
  128. * @param options object axios 配置项
  129. * @returns {AxiosPromise}
  130. */
  131. request[method] = (url, params = {}, options = {}) => {
  132. return baseRequest(Object.assign({ url, params, method }, defaultOpt, options))
  133. }
  134. })
  135. export default request