拦截器处理

@/utils/request.js

引入+创建axios实例

import axios from 'axios'
import { Notification, MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'//获取token方法
import errorCode from '@/utils/errorCode'

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'

const service = axios.create({
  // axios中请求配置有baseURL选项,表示请求URL公共部分
  baseURL: process.env.VUE_APP_BASE_API,
  // 超时
  timeout: 10000
})

请求拦截器(service.interceptors.request)

service.interceptors.request.use(config => {
  // 是否需要设置 token
  const noToken = (config.headers || {}).isToken === false
  if (getToken() && !noToken ) {
    config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  }
  // get请求映射params参数
  if (config.method === 'get' && config.params) {
    let url = config.url + '?';
    for (const propName of Object.keys(config.params)) {
      const value = config.params[propName];
      var part = encodeURIComponent(propName) + "=";
      if (value && typeof(value) !== "undefined") {
        if (typeof value === 'object') {
          for (const key of Object.keys(value)) {
            let params = propName + '[' + key + ']';
            var subPart = encodeURIComponent(params) + "=";
            url += subPart + encodeURIComponent(value[key]) + "&";
          }
        } else {
          url += part + encodeURIComponent(value) + "&";
        }
      }
    }
    url = url.slice(0, -1);
    config.params = {};
    config.url = url;
  }
  return config
}, error => {
    console.log(error)
    Promise.reject(error)
})

响应拦截器(service.interceptors.response)

service.interceptors.response.use(res => {
    // 未设置状态码则默认成功状态
    const code = res.data.code || 200;
    // 获取错误信息
    const msg = errorCode[code] || res.data.msg || errorCode['default']
    if (code === 401) {
      MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
          confirmButtonText: '重新登录',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        store.dispatch('LogOut').then(() => {
          location.href = '/index';
          // this.$router.push('/index')
        })
      })
    } else if (code === 500) {
      Message({
        message: msg,
        type: 'error'
      })
      return Promise.reject(new Error(msg))
    } else if (code !== 200) {
      Notification.error({
        title: msg
      })
      return Promise.reject('error')
    } else {
      return res.data
    }
  },
  error => {
    console.log('err' + error)
    let { message } = error;
    if (message == "Network Error") {
      message = "后端接口连接异常";
    }
    else if (message.includes("timeout")) {
      message = "系统接口请求超时";
    }
    else if (message.includes("Request failed with status code")) {
      message = "系统接口" + message.substr(message.length - 3) + "异常";
    }
    Message({
      message: message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

暴露实例

export default service

@/utils/errorCode.js

export default {
  '401': '认证失败,无法访问系统资源',
  '403': '当前操作没有权限',
  '404': '访问资源不存在',
  'default': '系统未知错误,请反馈给管理员'
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Mybatis拦截器是一个强大的工具,可以用于拦截和修改执行的SQL语句。在使用Mybatis拦截器处理mysql和达梦的SQL动态转换时,可以按照以下步骤进行实现: 1. 实现自定义拦截器类 首先,需要实现一个自定义的拦截器类,该类需要实现Mybatis的Interceptor接口,其中包括intercept()方法和plugin()方法。 intercept()方法用于拦截SQL语句的执行,并在执行前后做一些自定义的处理。在该方法中,可以通过参数Invocation对象获取到执行的SQL语句以及执行的目标对象。 plugin()方法用于创建代理对象,将拦截器链中的拦截器和目标对象进行绑定。 2. 配置拦截器 在Mybatis的配置文件中,需要对自定义的拦截器进行配置。可以通过在配置文件中添加<plugins>标签来配置拦截器,具体如下所示: ``` <plugins> <plugin interceptor="com.example.MyInterceptor"/> </plugins> ``` 其中,interceptor属性值为自定义的拦截器类的全限定名。 3. 实现SQL语句的动态转换 在自定义的拦截器类中,可以通过对SQL语句的解析和修改,实现SQL语句的动态转换。具体实现方式可以根据不同的需求进行选择,例如可以使用正则表达式对SQL语句进行匹配和替换,也可以通过字符串拼接的方式实现SQL语句的修改。 4. 测试拦截器 最后,可以通过在代码中执行SQL语句的方式来测试自定义的拦截器是否生效。可以在执行SQL语句前后打印出SQL语句的内容,以验证是否已经成功实现了SQL语句的动态转换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值