123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- import Vue from "vue";
- import axios from "axios";
- import { loadProgressBar } from "./axiosProgress";
- import cachingGet from "./axiosCache";
- import router from "../router";
- import { getKeyToken, removeKeyToken } from "../auth/auth";
- // Full config: https://github.com/axios/axios#request-config
- // axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
- // axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
- // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
- let config = {
- // baseURL: process.env.baseURL || process.env.apiUrl || ""
- timeout: 60 * 1000, // Timeout
- withCredentials: true // Check cross-site Access-Control
- };
- const _axiosCommonService = axios.create(config);
- /**
- * B. new token lifecycle
- * 1. 任何接口地址都尝试加上 key & token (第三方请求,单独做新的$http)
- * 2. 如果没有 key & token,就当做这是无需认证的api
- * 3. 做好token失效的处理,在interceptors.response 统一删除
- * 4. key & token 由外部管理,不在此插件管理。一般是在登录成功后设置。
- * xx. 额外:通过js-cookie将key等存入cookie
- */
- _axiosCommonService.interceptors.request.use(
- function(config) {
- if (config.url.includes("/login") === false) {
- let [wk_token, wk_key] = getKeyToken();
- if (wk_token) {
- config.headers.common["token"] = wk_token;
- config.headers.common["key"] = wk_key;
- }
- }
- return config;
- },
- function(error) {
- // Do something with request error
- Vue.prototype.$notify({
- showClose: true,
- message: error,
- type: "error"
- });
- return Promise.reject(error);
- }
- );
- // Add a response interceptor
- _axiosCommonService.interceptors.response.use(
- response => {
- return response;
- },
- error => {
- if (!error.response) {
- // "Network Error" 网络不通,直接返回
- Vue.prototype.$notify({
- showClose: true,
- message: "网络连接异常,请检查网络设置。",
- type: "error"
- });
- return Promise.reject(error);
- }
- // 这里是返回状态码不为200时候的错误处理
- let status = error.response.status;
- // 登录失效 跳转登录页面
- if (status == 403 || status == 401) {
- Vue.prototype.$alert("登录失效,请重新登录!", "提示", {
- confirmButtonText: "确定",
- callback: () => {
- removeKeyToken();
- router.push("/login/");
- }
- });
- return Promise.reject(error);
- } else if (status == 405) {
- Vue.prototype.$notify({
- showClose: true,
- message: "没有权限!",
- type: "error"
- });
- return Promise.reject(error);
- }
- if (status != 200) {
- const data = error.response.data;
- if (data && data.desc) {
- Vue.prototype.$notify({
- showClose: true,
- message: data.desc,
- type: "error"
- });
- } else {
- Vue.prototype.$notify({
- showClose: true,
- message: "未定义异常: " + JSON.stringify(data, 2),
- type: "error"
- });
- }
- return Promise.reject(error);
- }
- }
- );
- _axiosCommonService.get = cachingGet(_axiosCommonService.get, []);
- loadProgressBar(_axiosCommonService);
- Plugin.install = function(Vue) {
- Vue.$httpCommonService = _axiosCommonService;
- Object.defineProperties(Vue.prototype, {
- $httpCommonService: {
- get() {
- return _axiosCommonService;
- }
- }
- });
- };
- Vue.use(Plugin);
- export default Plugin;
- export const httpCommonService = _axiosCommonService;
|