index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 '@/config'
  7. import { isWechat } from '../app';
  8. // 后端api地址
  9. const baseURL = config.apiUrl;
  10. const merchantNo = config.merchantNo;
  11. // 可以new多个request来支持多个域名请求
  12. const $http = new request({
  13. // 接口请求地址
  14. baseUrl: baseURL,
  15. // 服务器本地上传文件地址
  16. fileUrl: baseURL,
  17. // 服务器上传图片默认url
  18. defaultUploadUrl: 'clientApi/file/upload',
  19. // 设置请求头(如果使用报错跨域问题,可能是content-type请求类型和后台那边设置的不一致)
  20. header: {
  21. 'content-type': 'application/json;charset=utf-8'
  22. },
  23. // 请求超时时间, 单位ms(默认300000)
  24. timeout: 300000,
  25. // 默认配置(可不写)
  26. config: {
  27. // 是否自动提示错误
  28. isPrompt: true,
  29. // 是否显示加载动画
  30. load: true,
  31. // 是否使用数据工厂
  32. isFactory: true
  33. }
  34. })
  35. // 当前接口请求数
  36. let requestNum = 0
  37. // 请求开始拦截器
  38. $http.requestStart = options => {
  39. if (options.load) {
  40. if (requestNum <= 0) {
  41. // 打开加载动画
  42. uni.showLoading({
  43. title: '加载中',
  44. mask: true
  45. })
  46. }
  47. requestNum += 1
  48. }
  49. // 图片上传大小限制
  50. if (options.method == "FILE" && options.maxSize) {
  51. // 文件最大字节: options.maxSize 可以在调用方法的时候加入参数
  52. const maxSize = options.maxSize
  53. for (let item of options.files) {
  54. if (item.size > maxSize) {
  55. setTimeout(() => {
  56. uni.showToast({
  57. title: "图片过大,请重新上传",
  58. icon: "none"
  59. })
  60. }, 10)
  61. return false
  62. }
  63. }
  64. }
  65. // 请求前加入当前终端
  66. options.header['platform'] = store.getters.platform ? String(store.getters.platform) : ''
  67. // 请求前加入Token
  68. options.header['Access-Token'] = store.getters.token ? String(store.getters.token) : ''
  69. // 商户号
  70. options.header['merchantNo'] = uni.getStorageSync("merchantNo") ? uni.getStorageSync("merchantNo") : merchantNo;
  71. // 店铺ID
  72. options.header['storeId'] = uni.getStorageSync("storeId") ? uni.getStorageSync("storeId") : 0;
  73. options.header['orderId'] = uni.getStorageSync("orderId") ? uni.getStorageSync("orderId") : 0;
  74. options.header['tableId'] = uni.getStorageSync("tableId") ? uni.getStorageSync("tableId") : 0;
  75. options.header['latitude'] = uni.getStorageSync("latitude") ? uni.getStorageSync("latitude") : '';
  76. options.header['longitude'] = uni.getStorageSync("longitude") ? uni.getStorageSync("longitude") : '';
  77. options.header['isWechat'] = isWechat() ? 'Y' : 'N';
  78. // return false 表示请求拦截,不会继续请求
  79. return options
  80. }
  81. // 请求结束
  82. $http.requestEnd = options => {
  83. // 判断当前接口是否需要加载动画
  84. if (options.load) {
  85. requestNum = requestNum - 1
  86. if (requestNum <= 0) {
  87. uni.hideLoading()
  88. }
  89. }
  90. }
  91. // 登录弹窗次数
  92. let loginPopupNum = 0
  93. // 所有接口数据处理(可在接口里设置不调用此方法)
  94. // 此方法需要开发者根据各自的接口返回类型修改,以下只是模板
  95. $http.dataFactory = async res => {
  96. // console.log("接口请求数据", {
  97. // url: res.url,
  98. // resolve: res.response,
  99. // header: res.header,
  100. // data: res.data,
  101. // method: res.method,
  102. // })
  103. if (!res.response.statusCode || res.response.statusCode != 200) {
  104. // 返回错误的结果(catch接受数据)
  105. return Promise.reject({
  106. statusCode: res.response.statusCode,
  107. // errMsg: "数据工厂验证不通过"
  108. errMsg: 'http状态码错误'
  109. })
  110. }
  111. let httpData = res.response.data
  112. if (typeof httpData == "string") {
  113. try {
  114. httpData = JSON.parse(httpData)
  115. } catch {
  116. httpData = false
  117. }
  118. }
  119. if (httpData === false || typeof httpData !== 'object') {
  120. // 返回错误的结果(catch接受数据)
  121. return Promise.reject({
  122. statusCode: res.response.statusCode,
  123. errMsg: "返回数据验证不通过"
  124. })
  125. }
  126. /*********以下只是模板(及共参考),需要开发者根据各自的接口返回类型修改*********/
  127. // 判断数据是否请求成功
  128. // result.code [ 200正常 5000有错误 1001未登录 4003没有权限访问 ]
  129. // 判断是否需要登录
  130. if (httpData.code == 1001) {
  131. // 1001也有可能是后端登录态到期, 所以要清空本地的登录状态
  132. store.dispatch('Logout')
  133. // 弹窗告诉用户去登录
  134. if (loginPopupNum <= 0) {
  135. loginPopupNum++
  136. uni.showModal({
  137. title: '温馨提示',
  138. content: '此时此刻需要您登录喔~',
  139. confirmText: "去登录",
  140. cancelText: "再逛会",
  141. success: res => {
  142. loginPopupNum--
  143. if (res.confirm) {
  144. uni.navigateTo({
  145. url: "/pages/login/index"
  146. })
  147. }
  148. }
  149. })
  150. }
  151. // 返回错误的结果(catch接受数据)
  152. return Promise.reject({
  153. statusCode: 0,
  154. errMsg: httpData.message,
  155. result: httpData
  156. })
  157. }
  158. // 其他错误提示
  159. else if (httpData.status == 5000) {
  160. if (res.isPrompt) {
  161. setTimeout(() => {
  162. uni.showToast({
  163. title: httpData.message,
  164. icon: "none",
  165. duration: 2500
  166. }, 10)
  167. })
  168. }
  169. // 返回错误的结果(catch接受数据)
  170. return Promise.reject({
  171. statusCode: 0,
  172. errMsg: httpData.message,
  173. result: httpData
  174. })
  175. } else {
  176. // 返回正确的结果(then接受数据)
  177. return Promise.resolve(httpData)
  178. }
  179. /*********以上只是模板(及共参考),需要开发者根据各自的接口返回类型修改*********/
  180. }
  181. // 错误回调
  182. $http.requestError = e => {
  183. if (e.statusCode === 0) {
  184. throw e
  185. } else {
  186. setTimeout(() => {
  187. uni.showToast({
  188. title: `网络请求出错:${e.errMsg}`,
  189. icon: "none",
  190. duration: 2500
  191. })
  192. })
  193. }
  194. }
  195. export default $http