|
@@ -0,0 +1,232 @@
|
|
|
+import Axios, { AxiosInstance, AxiosRequestConfig } from "axios";
|
|
|
+import qs from "qs";
|
|
|
+import NProgress from "../progress";
|
|
|
+import { getToken, getRelaComNo } from "/@/utils/auth";
|
|
|
+import md5 from "md5";
|
|
|
+import { store } from "/@/store";
|
|
|
+import { trigger } from "/@/plugins/errorHooks/trigger";
|
|
|
+
|
|
|
+import {
|
|
|
+ PureHttpError,
|
|
|
+ RequestMethods,
|
|
|
+ PureHttpResponse,
|
|
|
+ PureHttpRequestConfig
|
|
|
+} from "./types.d";
|
|
|
+import { func } from "vue-types";
|
|
|
+
|
|
|
+function generateRandomString(length) {
|
|
|
+ const str = Math.random()
|
|
|
+ .toString(36)
|
|
|
+ .substring(2, length + 2);
|
|
|
+ return str;
|
|
|
+}
|
|
|
+function aa(): AxiosRequestConfig {
|
|
|
+ const noce = generateRandomString(10);
|
|
|
+ const origin = window.location.origin;
|
|
|
+ const timestamp = new Date().valueOf() + "";
|
|
|
+ const sign = md5(origin + noce + timestamp);
|
|
|
+ const defaultConfig: AxiosRequestConfig = {
|
|
|
+ baseURL: "",
|
|
|
+ timeout: 10000,
|
|
|
+ headers: {
|
|
|
+ Accept: "application/json, text/plain, */*",
|
|
|
+ "Content-Type": "application/json",
|
|
|
+ "X-Requested-With": "XMLHttpRequest",
|
|
|
+ "Web-Auth": "1",
|
|
|
+ Noce: noce,
|
|
|
+ Sign: sign,
|
|
|
+ Timestamp: timestamp
|
|
|
+ },
|
|
|
+ // 数组格式参数序列化
|
|
|
+ paramsSerializer: params => qs.stringify(params, { indices: false })
|
|
|
+ };
|
|
|
+ return defaultConfig;
|
|
|
+}
|
|
|
+// // 相关配置请参考:www.axios-js.com/zh-cn/docs/#axios-request-config-1
|
|
|
+// const defaultConfig: AxiosRequestConfig = {
|
|
|
+// baseURL: "",
|
|
|
+// timeout: 10000,
|
|
|
+// headers: {
|
|
|
+// Accept: "application/json, text/plain, */*",
|
|
|
+// "Content-Type": "application/json",
|
|
|
+// "X-Requested-With": "XMLHttpRequest",
|
|
|
+// "Web-Auth": "1"
|
|
|
+// },
|
|
|
+// // 数组格式参数序列化
|
|
|
+// paramsSerializer: params => qs.stringify(params, { indices: false })
|
|
|
+// };
|
|
|
+
|
|
|
+class PureHttp {
|
|
|
+ constructor() {
|
|
|
+ this.httpInterceptorsRequest();
|
|
|
+ this.httpInterceptorsResponse();
|
|
|
+ }
|
|
|
+ // 初始化配置对象
|
|
|
+ private static initConfig: PureHttpRequestConfig = {};
|
|
|
+
|
|
|
+ // 保存当前Axios实例对象
|
|
|
+ private static axiosInstance: AxiosInstance = Axios.create(aa());
|
|
|
+
|
|
|
+ // 请求拦截
|
|
|
+ private httpInterceptorsRequest(): void {
|
|
|
+ PureHttp.axiosInstance.interceptors.response.use(response => {
|
|
|
+ const { data } = response;
|
|
|
+ // 在发送请求之前做些什么
|
|
|
+ // let token = window.vm.$getStorage("token"); // 令牌
|
|
|
+ // let tid = window.vm.$getStorage("tid"); // 租户id
|
|
|
+ // let envName = window.vm.$getStorage("envName"); // 环境变量
|
|
|
+ // let currentOrgId = window.vm.$getStorage("currentOrgId"); // 区域id
|
|
|
+ // let data = { token, tid, envName, currentOrgId };
|
|
|
+ // if (!config.data || JSON.stringify(config.data) === "{}") {
|
|
|
+ // config.data = data;
|
|
|
+ // } else if (!config.data.hasOwnProperty("currentOrgId")) {
|
|
|
+ // Object.assign(config.data, data);
|
|
|
+ // }
|
|
|
+ // config.headers.appId = window.vm.$encrypt(
|
|
|
+ // "520f066da8e7c4736abb20afdd29289a",
|
|
|
+ // "TFEUyxO9PDJlyyg7"
|
|
|
+ // );
|
|
|
+ // // 加密
|
|
|
+ // config.headers.Authentication = window.vm.$encrypt(
|
|
|
+ // "520f066da8e7c4736abb20afdd29289a#nU-#q1PwD!1573801531190",
|
|
|
+ // "TFEUyxO9PDJlyyg7"
|
|
|
+ // );
|
|
|
+ // 解密
|
|
|
+ // config.headers.Authentication = window.vm.$decrypt(this.parmValue, "TFEUyxO9PDJlyyg7");
|
|
|
+ // console.log(config);
|
|
|
+ // response.headers["web-auth"] = "1";
|
|
|
+
|
|
|
+ console.log(response);
|
|
|
+ const { code, message } = data;
|
|
|
+ //响应code 检查登出
|
|
|
+ if (
|
|
|
+ Number(code) >= 101 &&
|
|
|
+ Number(code) <= 140 &&
|
|
|
+ message !== "账户已禁用"
|
|
|
+ ) {
|
|
|
+ trigger("userLogout");
|
|
|
+ }
|
|
|
+
|
|
|
+ return response;
|
|
|
+ });
|
|
|
+
|
|
|
+ PureHttp.axiosInstance.interceptors.request.use(
|
|
|
+ (config: PureHttpRequestConfig) => {
|
|
|
+ const $config = config;
|
|
|
+ // console.log($config);
|
|
|
+ // 开启进度条动画
|
|
|
+ NProgress.start();
|
|
|
+ // 优先判断post/get等方法是否传入回掉,否则执行初始化设置等回掉
|
|
|
+ if (typeof config.beforeRequestCallback === "function") {
|
|
|
+ config.beforeRequestCallback($config);
|
|
|
+ return $config;
|
|
|
+ }
|
|
|
+ if (PureHttp.initConfig.beforeRequestCallback) {
|
|
|
+ PureHttp.initConfig.beforeRequestCallback($config);
|
|
|
+ return $config;
|
|
|
+ }
|
|
|
+ const token = getToken();
|
|
|
+ const relaComNo = getRelaComNo();
|
|
|
+
|
|
|
+ const isSuper = store.state.value["pure-user"].isSuperUser;
|
|
|
+
|
|
|
+ if (token && config && config.data) {
|
|
|
+ config.data["token"] = token;
|
|
|
+
|
|
|
+ config.data["relaComNo"] = config.data["relaComNo"]
|
|
|
+ ? config.data["relaComNo"]
|
|
|
+ : relaComNo;
|
|
|
+
|
|
|
+ if (config.data["noRela"] || isSuper) {
|
|
|
+ config.data["relaComNo"] = "";
|
|
|
+ delete config.data["noRela"];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $config;
|
|
|
+ } else {
|
|
|
+ return $config;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ error => {
|
|
|
+ return Promise.reject(error);
|
|
|
+ }
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ // 响应拦截
|
|
|
+ private httpInterceptorsResponse(): void {
|
|
|
+ const instance = PureHttp.axiosInstance;
|
|
|
+ instance.interceptors.response.use(
|
|
|
+ (response: PureHttpResponse) => {
|
|
|
+ const $config = response.config;
|
|
|
+ // 关闭进度条动画
|
|
|
+ NProgress.done();
|
|
|
+ // 优先判断post/get等方法是否传入回掉,否则执行初始化设置等回掉
|
|
|
+ if (typeof $config.beforeResponseCallback === "function") {
|
|
|
+ $config.beforeResponseCallback(response);
|
|
|
+ return response.data;
|
|
|
+ }
|
|
|
+ if (PureHttp.initConfig.beforeResponseCallback) {
|
|
|
+ PureHttp.initConfig.beforeResponseCallback(response);
|
|
|
+ return response.data;
|
|
|
+ }
|
|
|
+ return response.data;
|
|
|
+ },
|
|
|
+ (error: PureHttpError) => {
|
|
|
+ const $error = error;
|
|
|
+ $error.isCancelRequest = Axios.isCancel($error);
|
|
|
+ // 关闭进度条动画
|
|
|
+ NProgress.done();
|
|
|
+ // 所有的响应异常 区分来源为取消请求/非取消请求
|
|
|
+ return Promise.reject($error);
|
|
|
+ }
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ // 通用请求工具函数
|
|
|
+ public request<T>(
|
|
|
+ method: RequestMethods,
|
|
|
+ url: string,
|
|
|
+ param?: AxiosRequestConfig,
|
|
|
+ axiosConfig?: PureHttpRequestConfig
|
|
|
+ ): Promise<T> {
|
|
|
+ const config = {
|
|
|
+ method,
|
|
|
+ url,
|
|
|
+ ...param,
|
|
|
+ ...axiosConfig
|
|
|
+ } as PureHttpRequestConfig;
|
|
|
+
|
|
|
+ // 单独处理自定义请求/响应回掉
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ PureHttp.axiosInstance
|
|
|
+ .request(config)
|
|
|
+ .then((response: undefined) => {
|
|
|
+ resolve(response);
|
|
|
+ })
|
|
|
+ .catch(error => {
|
|
|
+ reject(error);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 单独抽离的post工具函数
|
|
|
+ public post<T, P>(
|
|
|
+ url: string,
|
|
|
+ params?: T,
|
|
|
+ config?: PureHttpRequestConfig
|
|
|
+ ): Promise<P> {
|
|
|
+ return this.request<P>("post", url, params, config);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 单独抽离的get工具函数
|
|
|
+ public get<T, P>(
|
|
|
+ url: string,
|
|
|
+ params?: T,
|
|
|
+ config?: PureHttpRequestConfig
|
|
|
+ ): Promise<P> {
|
|
|
+ return this.request<P>("get", url, params, config);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export const http = new PureHttp();
|