api.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 { handleLoginFailure } from '@/utils'
  13. import { isWeixin } from '@/utils/util'
  14. import { VUE_APP_API_URL,TENANT_ID } from '@/config'
  15. import cookie from '@/utils/cookie'
  16. import { replace } from '@/utils/router'
  17. const fly = new Fly()
  18. fly.config.baseURL = VUE_APP_API_URL
  19. fly.interceptors.response.use(
  20. response => {
  21. console.log('response0099991:',response)
  22. // 定时刷新access-token
  23. return response
  24. },
  25. error => {
  26. console.log('response008:',error)
  27. if (error.toString() == 'Error: Network Error') {
  28. handleLoginFailure()
  29. return Promise.reject({ msg: '未登录', toLogin: true })
  30. }
  31. if (error.status == 401) {
  32. handleLoginFailure()
  33. return Promise.reject({ msg: '未登录', toLogin: true })
  34. }
  35. if (error.response.data.status == 5109) {
  36. uni.showToast({
  37. title: error.response.data.msg,
  38. icon: 'none',
  39. duration: 2000,
  40. })
  41. }
  42. return Promise.reject(error)
  43. }
  44. )
  45. const defaultOpt = { login: true }
  46. function baseRequest(options) {
  47. const token = cookie.get('accessToken')
  48. // console.log('--> % token % token:\n', token)
  49. options.headers = {
  50. ...options.headers,
  51. }
  52. // if (options.login === true) {
  53. options.headers = {
  54. ...options.headers,
  55. Authorization: 'Bearer ' + token,
  56. 'tenant-id': TENANT_ID,
  57. }
  58. // }
  59. // 结构请求需要的参数
  60. const { url, params, data, login, ...option } = options
  61. // 发起请求
  62. return fly
  63. .request(url, params || data, {
  64. ...option,
  65. })
  66. .then(res => {
  67. const data = res.data || {}
  68. //console.log('res.status:',res)
  69. console.log('res.code:',res.code)
  70. // #ifdef H5
  71. if (res.data.code == 1004004002) {
  72. if(isWeixin()){
  73. const url = cookie.get('index_url')
  74. //console.log('redirect_uri:',url)
  75. //const url = `${location.origin}/h5/#/pages/index/index`
  76. location.href = url
  77. return
  78. }
  79. }
  80. // #endif
  81. if (res.status !== 200) {
  82. return Promise.reject({ msg: '请求失败', res, data })
  83. }
  84. if (data.code == 401) {
  85. uni.hideLoading()
  86. handleLoginFailure()
  87. uni.showToast({
  88. title: data.msg,
  89. icon: 'none',
  90. duration: 2000,
  91. })
  92. return Promise.reject({ msg: data.msg, res, data })
  93. }
  94. if (data.code != 0) {
  95. uni.showToast({
  96. title: data.msg,
  97. icon: 'none',
  98. duration: 2000,
  99. })
  100. return Promise.reject({ data, res })
  101. }
  102. return Promise.resolve(data.data, res)
  103. // if ([401, 403].indexOf(data.status) !== -1) {
  104. // handleLoginFailure()
  105. // return Promise.reject({ msg: res.data.msg, res, data, toLogin: true })
  106. // } else if (data.status === 200) {
  107. // return Promise.resolve(data, res)
  108. // } else if (data.status == 5101) {
  109. // return Promise.reject({ msg: res.data.msg, res, data })
  110. // } else {
  111. // return Promise.reject({ msg: res.data.msg, res, data })
  112. // }
  113. })
  114. }
  115. /**
  116. * http 请求基础类
  117. * 参考文档 https://www.kancloud.cn/yunye/axios/234845
  118. *
  119. */
  120. const request = ['post', 'put', 'patch'].reduce((request, method) => {
  121. /**
  122. *
  123. * @param url string 接口地址
  124. * @param data object get参数
  125. * @param options object axios 配置项
  126. * @returns {AxiosPromise}
  127. */
  128. request[method] = (url, data = {}, options = {}) => {
  129. console.log(url, data)
  130. return baseRequest(Object.assign({ url, data, method }, defaultOpt, options))
  131. }
  132. return request
  133. }, {})
  134. ;['get', 'delete', 'head'].forEach(method => {
  135. /**
  136. *
  137. * @param url string 接口地址
  138. * @param params object get参数
  139. * @param options object axios 配置项
  140. * @returns {AxiosPromise}
  141. */
  142. request[method] = (url, params = {}, options = {}) => {
  143. return baseRequest(Object.assign({ url, params, method }, defaultOpt, options))
  144. }
  145. })
  146. export default request