123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- /**
- * request插件地址:https://ext.dcloud.net.cn/plugin?id=822
- */
- import store from '@/store'
- import request from './request'
- import Config from '@/core/config'
- // 后端api地址
- const apiUrl = Config.get('apiUrl')
- // 可以new多个request来支持多个域名请求
- const $http = new request({
- // 接口请求地址
- baseUrl: apiUrl,
- // 服务器本地上传文件地址
- fileUrl: apiUrl,
- // 服务器上传图片默认url
- defaultUploadUrl: 'upload/image',
- // 设置请求头(如果使用报错跨域问题,可能是content-type请求类型和后台那边设置的不一致)
- header: {
- 'content-type': 'application/json;charset=utf-8'
- },
- // 请求超时时间, 单位ms(默认15000)
- timeout: 15000,
- // 默认配置(可不写)
- config: {
- // 是否自动提示错误
- isPrompt: true,
- // 是否显示加载动画
- load: true,
- // 是否使用数据工厂
- isFactory: true
- }
- })
- // 当前接口请求数
- let requestNum = 0
- // 请求开始拦截器
- $http.requestStart = options => {
- if (options.load) {
- if (requestNum <= 0) {
- // 打开加载动画
- uni.showLoading({
- title: '加载中',
- mask: true
- })
- }
- requestNum += 1
- }
- // 图片上传大小限制
- if (options.method == "FILE" && options.maxSize) {
- // 文件最大字节: options.maxSize 可以在调用方法的时候加入参数
- const maxSize = options.maxSize
- for (let item of options.files) {
- if (item.size > maxSize) {
- setTimeout(() => {
- uni.showToast({
- title: "图片过大,请重新上传",
- icon: "none"
- })
- })
- return false
- }
- }
- }
- // 请求前加入当前终端
- options.header['api-name'] = store.getters.platform
- // 请求前加入Token
- options.header['token'] = store.getters.token
- // return false 表示请求拦截,不会继续请求
- return options
- }
- // 请求结束
- $http.requestEnd = options => {
- // 判断当前接口是否需要加载动画
- if (options.load) {
- if (requestNum > 0) {
- uni.hideLoading()
- }
- }
- }
- // 当前是否显示modal
- // let loginModal = false
- // 所有接口数据处理(可在接口里设置不调用此方法)
- // 此方法需要开发者根据各自的接口返回类型修改,以下只是模板
- $http.dataFactory = async res => {
- // console.log("接口请求数据", {
- // url: res.url,
- // resolve: res.response,
- // header: res.header,
- // data: res.data,
- // method: res.method,
- // })
- requestNum -= 1;
- if (requestNum <= 0) {
- uni.hideLoading();
- }
- const httpData = res.response;
- if (httpData.code === 0) {
- uni.showToast({
- title: httpData.msg,
- icon: "none",
- duration: 2500
- });
- return false;
- } else if (httpData.code == 401) { // 判断是否需要登录
- // 401也有可能是后端登录态到期, 所以要清空本地的登录状态
- store.dispatch('Logout');
- uni.showToast({
- title: '登录状态错误或已失效',
- icon: "none",
- duration: 2500
- });
- return false;
- // return Promise.resolve(httpData.data);
- } else if (httpData.code === 200) { // 判断数据是否请求成功
- // 返回正确的结果(then接受数据)
- return Promise.resolve(httpData)
- } else {
- uni.showToast({
- title: '服务端错误',
- icon: "none",
- duration: 2500
- });
- return false;
- }
- }
- // 错误回调
- $http.requestError = e => {
- if (e.statusCode === 0) {
- throw e
- } else {
- setTimeout(() => showRequestError(e), 10)
- }
- }
- // 显示请求错误信息
- const showRequestError = (e) => {
- let errMsg = `网络请求出错:${e.errMsg}`
- // #ifdef MP-WEIXIN
- if (e.errMsg === 'request:fail url not in domain list') {
- errMsg = '当前API域名未添加到微信小程序授权名单 ' + e.errMsg
- }
- // #endif
- if (e.errMsg === 'request:fail') {
- errMsg = '网络请求错误,请检查您的网络是否正常!'
- }
- uni.showToast({
- title: errMsg,
- icon: "none",
- duration: 3500
- })
- }
- export default $http
|