123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- import axios from 'axios';
- import cookies from 'vue-cookies'
- import { userStore } from '@/store/userInfo'
- import { Message } from 'element-ui'
- //关闭断网时toast
- let msg = {
- close() { }
- }
- // 创建请求实例
- const instance = axios.create({
- baseURL: import.meta.env.VITE_BASE_URL,
- // 指定请求超时的毫秒数
- timeout: 10000,
- // 表示跨域请求时是否需要使用凭证
- withCredentials: false,
- });
- // 前置拦截器(发起请求之前的拦截)
- instance.interceptors.request.use(
- (config) => {
- /**
- * 在这里一般会携带前台的参数发送给后台,比如下面这段代码:
- * const token = getToken()
- * if (token) {
- * config.headers.token = token
- * }
- */
- msg.close()
- if (!(config.url.indexOf("pdf-tech/vppMember/create") !== -1 || config.url.indexOf("/pdf-tech/login") !== -1)) {
- const token = cookies.get('accessToken')
- config.headers.Authorization = 'Bearer ' + token
- }
- return config;
- },
- (error) => {
- return Promise.reject(error);
- },
- );
- // 后置拦截器(获取到响应时的拦截)
- instance.interceptors.response.use(
- (response) => {
- /**
- * 根据你的项目实际情况来对 response 和 error 做处理
- * 这里对 response 和 error 不做任何处理,直接返回
- */
- if (response.data.msg === "当前用户不具备操作权限" || response.data.msg === "Insufficient permissions") {
- cookies.remove('accessToken')
- userStore().clearUserInfo()
- window.location.href = '/non-admin-user'
- // window.location.href = '/product?type=isNoAdmin'
- }
- return response;
- },
- (error) => {
- const { response } = error;
- msg.close()
- if (response && response.data) {
- //没有登录或者登录过期跳到登录页面
- if (response.data.code === 310 && response.data.msg === "无效的token或者token已过期") {
- const token = cookies.get('accessToken')
- if (token) {
- cookies.set('accessToken', 'expired')
- }
- userStore().clearUserInfo()
- window.location.href = '/login'
- }
- return Promise.reject(error);
- }
- const { message } = error;
- //断网情况下,弹出框
- if (error.request) {
- msg = Message({
- duration: 5000,
- showClose: true,
- message: 'Internet Connection Error',
- type: "error",
- })
- }
- console.error(message);
- return Promise.reject(error);
- },
- );
- // 导出常用函数
- /**
- * @param {string} url
- * @param {object} data
- * @param {object} params
- */
- export function post(url, data = {}, params = {}) {
- return instance({
- method: 'post',
- url,
- data,
- params,
- });
- }
- /**
- * @param {string} url
- * @param {object} params
- */
- export function get(url, params = {}) {
- return instance({
- method: 'get',
- url,
- params,
- });
- }
- export function downLoad(url, params = {}) {
- return instance({
- method: 'get',
- url,
- params,
- responseType: "arraybuffer"
- });
- }
- /**
- * @param {string} url
- * @param {object} data
- * @param {object} params
- */
- export function put(url, data = {}, params = {}) {
- return instance({
- method: 'put',
- url,
- params,
- data,
- });
- }
- /**
- * @param {string} url
- * @param {object} params
- */
- export function _delete(url, params = {}) {
- return instance({
- method: 'delete',
- url,
- params,
- });
- }
- /**
- * @param {string} url
- * @param {object} data
- */
- export function postWithHeader(url, data = {}) {
- return instance({
- method: 'post',
- url,
- data,
- headers: {
- 'Content-Type': 'application/json'
- },
- dataType: "json",
- });
- }
- /**
- * @param {string} url
- * @param {object} data
- */
- export function putWithHeader(url, data = {}) {
- return instance({
- method: 'put',
- url,
- data,
- headers: {
- 'Content-Type': 'application/json'
- },
- dataType: "json",
- });
- }
- export default instance;
|