index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * request插件地址:https://ext.dcloud.net.cn/plugin?id=822
  3. */
  4. import store from '@/store'
  5. import request from './request'
  6. import Config from '@/core/config'
  7. // 后端api地址
  8. const apiUrl = Config.get('apiUrl')
  9. // 可以new多个request来支持多个域名请求
  10. const $http = new request({
  11. // 接口请求地址
  12. baseUrl: apiUrl,
  13. // 服务器本地上传文件地址
  14. fileUrl: apiUrl,
  15. // 服务器上传图片默认url
  16. defaultUploadUrl: 'upload/image',
  17. // 设置请求头(如果使用报错跨域问题,可能是content-type请求类型和后台那边设置的不一致)
  18. header: {
  19. 'content-type': 'application/json;charset=utf-8'
  20. },
  21. // 请求超时时间, 单位ms(默认15000)
  22. timeout: 15000,
  23. // 默认配置(可不写)
  24. config: {
  25. // 是否自动提示错误
  26. isPrompt: true,
  27. // 是否显示加载动画
  28. load: true,
  29. // 是否使用数据工厂
  30. isFactory: true
  31. }
  32. })
  33. // 当前接口请求数
  34. let requestNum = 0
  35. // 请求开始拦截器
  36. $http.requestStart = options => {
  37. if (options.load) {
  38. if (requestNum <= 0) {
  39. // 打开加载动画
  40. uni.showLoading({
  41. title: '加载中',
  42. mask: true
  43. })
  44. }
  45. requestNum += 1
  46. }
  47. // 图片上传大小限制
  48. if (options.method == "FILE" && options.maxSize) {
  49. // 文件最大字节: options.maxSize 可以在调用方法的时候加入参数
  50. const maxSize = options.maxSize
  51. for (let item of options.files) {
  52. if (item.size > maxSize) {
  53. setTimeout(() => {
  54. uni.showToast({
  55. title: "图片过大,请重新上传",
  56. icon: "none"
  57. })
  58. })
  59. return false
  60. }
  61. }
  62. }
  63. // 请求前加入当前终端
  64. options.header['api-name'] = store.getters.platform
  65. // 请求前加入Token
  66. options.header['token'] = store.getters.token
  67. // return false 表示请求拦截,不会继续请求
  68. return options
  69. }
  70. // 请求结束
  71. $http.requestEnd = options => {
  72. // 判断当前接口是否需要加载动画
  73. if (options.load) {
  74. if (requestNum > 0) {
  75. uni.hideLoading()
  76. }
  77. }
  78. }
  79. // 当前是否显示modal
  80. // let loginModal = false
  81. // 所有接口数据处理(可在接口里设置不调用此方法)
  82. // 此方法需要开发者根据各自的接口返回类型修改,以下只是模板
  83. $http.dataFactory = async res => {
  84. // console.log("接口请求数据", {
  85. // url: res.url,
  86. // resolve: res.response,
  87. // header: res.header,
  88. // data: res.data,
  89. // method: res.method,
  90. // })
  91. requestNum -= 1;
  92. if (requestNum <= 0) {
  93. uni.hideLoading();
  94. }
  95. const httpData = res.response;
  96. if (httpData.code === 0) {
  97. uni.showToast({
  98. title: httpData.msg,
  99. icon: "none",
  100. duration: 2500
  101. });
  102. return false;
  103. } else if (httpData.code == 401) { // 判断是否需要登录
  104. // 401也有可能是后端登录态到期, 所以要清空本地的登录状态
  105. store.dispatch('Logout');
  106. uni.showToast({
  107. title: '登录状态错误或已失效',
  108. icon: "none",
  109. duration: 2500
  110. });
  111. return false;
  112. // return Promise.resolve(httpData.data);
  113. } else if (httpData.code === 200) { // 判断数据是否请求成功
  114. // 返回正确的结果(then接受数据)
  115. return Promise.resolve(httpData)
  116. } else {
  117. uni.showToast({
  118. title: '服务端错误',
  119. icon: "none",
  120. duration: 2500
  121. });
  122. return false;
  123. }
  124. }
  125. // 错误回调
  126. $http.requestError = e => {
  127. if (e.statusCode === 0) {
  128. throw e
  129. } else {
  130. setTimeout(() => showRequestError(e), 10)
  131. }
  132. }
  133. // 显示请求错误信息
  134. const showRequestError = (e) => {
  135. let errMsg = `网络请求出错:${e.errMsg}`
  136. // #ifdef MP-WEIXIN
  137. if (e.errMsg === 'request:fail url not in domain list') {
  138. errMsg = '当前API域名未添加到微信小程序授权名单 ' + e.errMsg
  139. }
  140. // #endif
  141. if (e.errMsg === 'request:fail') {
  142. errMsg = '网络请求错误,请检查您的网络是否正常!'
  143. }
  144. uni.showToast({
  145. title: errMsg,
  146. icon: "none",
  147. duration: 3500
  148. })
  149. }
  150. export default $http