封装post

import axios from "axios";
import Vue from 'vue'
import { storage, isEmpty } from "@/utils";
import store from "@/store";
// import { noAuthHttp } from './index'

const that = store;
Vue.prototype.$store = that;
const _this = Vue.prototype; // vue上挂载store

axios.defaults.timeout = process.env.VUE_APP_TIMEOUT;

const noAuthAxios = axios.create();
const authedAxios = axios.create();

// dev
authedAxios.defaults.baseURL = process.env.VUE_APP_BASE_URL;
noAuthAxios.defaults.baseURL = process.env.VUE_APP_NO_AUTH_BASE_URL;
// build
// authedAxios.defaults.baseURL = window.location.protocol + '//' + window.location.host + '/fssc';
// noAuthAxios.defaults.baseURL = window.location.protocol + '//' + window.location.host + '/fssc';

// 添加请求拦截器
authedAxios.interceptors.request.use((request) => {
  // let res = checkSession()
  // 添加自定义请求头 - TODO
  const extraHeader = {
    // Host: "10.107.213.21:15030",
    // Origin: "http://10.107.213.21:15030"
  };
  Object.keys(extraHeader).map((item) => {
    request.headers[item] = extraHeader[item];
  });
  if (request.body instanceof FormData) {
    // request.headers["Content-Type"] = "multipart/form-data";
  } else {
    // request.body is not FormData
    request.headers["Content-Type"] = "application/json; charset=utf-8";
  }
  let token = storage.get("user-token");
  if (!token) {
    // token
    const userInfo = _this.$store.getters.getUserInfo;
    request.headers.common.Authorization = `${userInfo.tokenType} ${userInfo.token}`;
  }
  Object.keys(extraHeader).map((item) => {
    request.headers[item] = extraHeader[item];
  });
  // if (res.code === '-1' || res.code === '0') {  // 没有token或者token失效,需要重新获取token
  //     console.log('未获取到token,先获取token')
  //     return noAuthHttp.login().then(() => {
  //         let token = storage.get('user-token')
  //         request.headers['userId'] = token.userId
  //         return request
  //     })
  // }
  // let token = storage.get('user-token')
  // request.headers['Authorization'] = `Bearer ${token}`
  return request;
});

// 添加响应拦截器
authedAxios.interceptors.response.use(
  (response) => {
    // 只将请求结果的data字段返回
    let result = response;
    if (result.data.success || result.success) {
      return Promise.resolve(result);
    }
    // toast.show(result.message);
    return Promise.reject(result);
  },
  (err) => {
    // 发生网络错误后会走到这里
    if (err) {
      console.log(err);
      // utils.showAlert(err.message)
      // toast.show(err.message);
      return Promise.reject(err);
    }
  },
);

noAuthAxios.interceptors.request.use((request) => {
  // 添加自定义请求头 - TODO
  const extraHeader = {};
  Object.keys(extraHeader).map((item) => {
    request.headers[item] = extraHeader[item];
  });
  return request;
});

// 添加响应拦截器
noAuthAxios.interceptors.response.use(
  (response) => {
    // 只将请求结果的data字段返回
    let result = response;
    if (result) {
      return Promise.resolve(result);
    }
    // toast.show(result.message);
    return Promise.reject(result);
  },
  (err) => {
    // 发生网络错误后会走到这里
    if (err) {
      console.log(err);
      // utils.showAlert(err.message)
      // toast.show(err.message);
      return Promise.reject(err);
    }
  },
);

/**
 * 查询当前session有效
 * resolve 0 没有登录,-1 登录失效,1 登录有效
 * */
const checkSession = () => {
  let token = storage.get("user-token");
  if (isEmpty(token)) {
    return {
      code: '0',
    };
  }
  let tokenExpireTimeRemaining = new Date() - new Date(token.requestTime);
  if (tokenExpireTimeRemaining > 23 * 60 * 60 * 1000) {
    // token已使用时间大于23个小时后,准备开始重新获取token
    return {
      code: '-1',
    };
  }
  return {
    code: '1',
  };
};

export { noAuthAxios, authedAxios, checkSession };

你可能感兴趣的:(积少成多,量变到质变)