import { AxiosStatic } from "./setup"; import Interface from "./interfaces"; import * as URL from "./url"; import { Loading } from "@kankan/components/index"; import { useParams } from "@/hook/useParams"; import { encodePassword } from "@/utils"; import { Code, errTip } from "./code"; import { status, StatusEum } from "@/store/setup"; import axios from "axios"; const params = useParams(); type URLAll = { [key in keyof Interface]: { [index in keyof Interface[key]]: "url" extends keyof Interface[key][index] ? Interface[key][index]["url"] : never; }[keyof Interface[key]]; }[keyof Interface]; // 密码加密 export const attachPwdencode = < T extends readonly URLAll[], K extends AxiosStatic >( baseAxios: K, urls: T ) => baseAxios.addIntercept({ reqHandler(data: any) { const ret: any = {}; if (data.data.password) { ret.password = encodePassword(data.data.password); } if (data.data.confirmPwd) { ret.confirmPwd = ret.password; } return { data: ret, }; }, urls, }); // loadding export const attachLoadding = < T extends readonly URLAll[], K extends AxiosStatic >( baseAxios: K, urls: T ) => baseAxios.addIntercept({ reqHandler() { console.log("open loadding"); Loading.show(); }, resHandler(data) { console.log("close loadding"); setTimeout(() => Loading.hide()); return data; }, errHandler() { console.log("close loadding"); setTimeout(() => Loading.hide()); }, urls, }); // 错误提示 export const attachError = < T extends readonly URLAll[], K extends AxiosStatic >( baseAxios: K, urls: T ) => // 错误提示 baseAxios.addIntercept({ resHandler(res: any) { if (res.code !== Code.SUSSESS) { errTip(res.code, res.msg); return null; } return res; }, errHandler(res) { if (res.status === -1) { status.value = StatusEum.disconnect; } else { status.value = StatusEum.serverErr; } }, urls, }); // 结构拆解 export const attachAnalysis = < T extends readonly URLAll[], K extends AxiosStatic >( baseAxios: K, urls: T ) => baseAxios.addIntercept({ resHandler(res: any) { if (!res) { throw new Error("请求错误"); } if (res.code !== Code.SUSSESS) { throw new Error(res.msg); } else { return res.data; } }, urls: urls, }); // sceneCode附加 export const attachSceneCode = >( baseAxios: K, urls: typeof URL.codeURLS ) => baseAxios.addIntercept({ reqHandler() { return { paths: { sceneCode: params.m }, }; }, urls, }); const uploadUrls = [URL.uploadFile] as const; // 文件上传处理 export const attachUpload = >( baseAxios: K, urls: typeof uploadUrls ) => baseAxios.addIntercept({ reqHandler(config) { const form = new FormData(); if (config.data instanceof Blob) { form.append("file", config.data); } else { for (const key in config.data as any) { form.append(key, config.data[key]); } } return { headers: { "Content-Type": "application/x-www-form-urlencoded;charset:UTF-8", }, data: form, }; }, urls, }); export default (baseAxios: AxiosStatic) => { attachLoadding(baseAxios, URL.loadURLS as any); attachError(baseAxios, URL.errorTipURLS as any); attachAnalysis(baseAxios, URL.disassembleURLS as any); // attachUploadDown(baseAxios, uploadDownUrls) // token校验拦截 const axios = baseAxios .addIntercept({ reqHandler() { return { paths: { sceneCode: params.m }, }; }, urls: URL.codeURLS, }) .addIntercept({ reqHandler(config) { const form = new FormData(); if (config.data instanceof Blob) { form.append("file", config.data); } else { for (const key in config.data as any) { form.append(key, config.data[key]); } } return { headers: { "Content-Type": "application/x-www-form-urlencoded;charset:UTF-8", }, data: form, }; }, urls: [URL.uploadFile] as const, }); return axios; };