| | |
| | | import axios from 'axios'; // 引入axios |
| | | import axios from "axios"; // 引入axios |
| | | //import QS from 'qs'; // 引入qs模块,用来序列化post类型的数据,后面会提到 |
| | | |
| | | import { Toast, Dialog } from 'vant'; |
| | | import { Toast, Dialog } from "vant"; |
| | | import store from "@/store/index"; |
| | | import i18n from '@/i18n'; |
| | | import router from '@/router/router' |
| | | import { signatureGenerate } from "@/utils/signatureUtil" |
| | | import i18n from "@/i18n"; |
| | | import router from "@/router/router"; |
| | | import { signatureGenerate } from "@/utils/signatureUtil"; |
| | | import urlKyc from "./urlKyc"; |
| | | class AxiosRequest { |
| | | /** |
| | | * @description Class AxiosRequest constructor. |
| | |
| | | // alert: Axios default base url param is "baseURL" not is "baseUrl" ! |
| | | baseURL: this.baseUrl, |
| | | headers: this.headers, |
| | | timeout: 10000 |
| | | } |
| | | timeout: 10000, |
| | | }; |
| | | return config; |
| | | } |
| | | |
| | |
| | | interceptors(instance, url) { |
| | | // Request intercept. |
| | | instance.interceptors.request.use( |
| | | config => { |
| | | var token = store.state.user.userInfo.token |
| | | (config) => { |
| | | const arr = urlKyc.filter((item) => item === url); |
| | | |
| | | if (arr.length !== 0 && store.state.user.kyc !== 2) { |
| | | return Toast.fail(i18n.t("请先实名认证!")); |
| | | } |
| | | var token = store.state.user.userInfo.token; |
| | | if (token) { |
| | | if (!config.params) { |
| | | config.params = {} |
| | | config.params = {}; |
| | | } |
| | | config.params.token = token; |
| | | // console.log(config) |
| | |
| | | duration: 0, |
| | | forbidClick: true, |
| | | loadingType: "spinner", |
| | | message: i18n.t('加载中...') |
| | | message: i18n.t("加载中..."), |
| | | }); |
| | | } |
| | | this.queue[url] = true; |
| | | } |
| | | // 获取请求头参数 |
| | | const { timestamp, signature } = signatureGenerate() |
| | | const { timestamp, signature } = signatureGenerate(); |
| | | // 分别将签名、时间戳 至请求头 |
| | | if (signature) config.headers["sign"] = signature |
| | | if (timestamp) config.headers["tissuePaper"] = timestamp |
| | | if (signature) config.headers["sign"] = signature; |
| | | if (timestamp) config.headers["tissuePaper"] = timestamp; |
| | | return config; |
| | | }, |
| | | error => { |
| | | (error) => { |
| | | if (this.isLoading) { |
| | | Toast.clear(); |
| | | } |
| | |
| | | ); |
| | | // Response intercept. |
| | | instance.interceptors.response.use( |
| | | result => { |
| | | (result) => { |
| | | this.destroy(url); |
| | | if (result.data.code == 0||result.data.code == 200) { |
| | | |
| | | if (result.data.code == 0 || result.data.code == 200) { |
| | | return result.data; |
| | | } else if (result.data.code == 401) { |
| | | Dialog.confirm({ |
| | | confirmButtonText: i18n.t('确定'), |
| | | cancelButtonText: i18n.t('取消'), |
| | | title: i18n.t('提示'), |
| | | message: i18n.t('未实名认证,是否认证?'), |
| | | confirmButtonText: i18n.t("确定"), |
| | | cancelButtonText: i18n.t("取消"), |
| | | title: i18n.t("提示"), |
| | | message: i18n.t("未实名认证,是否认证?"), |
| | | beforeClose: (action, done) => { |
| | | if (action === 'confirm') { |
| | | if (action === "confirm") { |
| | | done(); |
| | | router.push({ |
| | | path: '/authentication' |
| | | path: "/authentication", |
| | | }); |
| | | } else { |
| | | done(); |
| | |
| | | return result.data; |
| | | } else if (result.data.code == 403) { |
| | | router.replace({ |
| | | path: '/login', |
| | | path: "/login", |
| | | query: { |
| | | redirect: router.currentRoute.fullPath |
| | | } |
| | | redirect: router.currentRoute.fullPath, |
| | | }, |
| | | }); |
| | | store.commit('user/SET_OUT'); |
| | | store.commit("user/SET_OUT"); |
| | | } else { |
| | | if (result.data.msg != undefined) { Toast(i18n.t(result.data.msg)); } |
| | | if (result.data.msg != undefined) { |
| | | Toast(i18n.t(result.data.msg)); |
| | | } |
| | | console.log(result, "====="); |
| | | return Promise.reject(result.data); |
| | | } |
| | | }, |
| | | error => { |
| | | (error) => { |
| | | if (this.isLoading) { |
| | | Toast.clear(); |
| | | } |
| | | if (error.code == 'ERR_NETWORK') { |
| | | Toast({ message: 'ERR_NETWORK', type: 'fail', duration: 1000 }) |
| | | } else if (error.code == 'ECONNABORTED') { |
| | | if (error.code == "ERR_NETWORK") { |
| | | Toast({ message: "ERR_NETWORK", type: "fail", duration: 1000 }); |
| | | } else if (error.code == "ECONNABORTED") { |
| | | //Toast({ message: 'Network Timeout', type: 'fail', duration: 2000 }) |
| | | return Promise.reject(error); |
| | | } |