123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- 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<Interface>
- >(
- 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<Interface>
- >(
- 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<Interface>
- >(
- 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<Interface>
- >(
- 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 = <K extends AxiosStatic<Interface>>(
- baseAxios: K,
- urls: typeof URL.codeURLS
- ) =>
- baseAxios.addIntercept({
- reqHandler() {
- return {
- paths: { sceneCode: params.m },
- };
- },
- urls,
- });
- const uploadUrls = [URL.uploadFile] as const;
- // 文件上传处理
- export const attachUpload = <K extends AxiosStatic<Interface>>(
- 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<Interface>) => {
- 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;
- };
|