Commit 46007c33 by 前端

上传文件

1 parent e5022f66
/*
* @Description:
* @Author: huyue
* @Date: 2022-08-23 11:04:56
* @LastEditors: huyue
* @LastEditTime: 2024-07-29 16:14:07
*/
import axios, {
AxiosInstance,
AxiosRequestConfig,
AxiosResponse,
AxiosError,
} from "axios";
// import { Toast } from 'vant';
import { Message } from "@arco-design/web-vue";
import qs from "qs";
import { createSign } from "./util";
const options: any = localStorage.getItem("config");
interface Config {
response: {
code_key: string;
code_success_value: 0;
data_key: string;
errId_logout_value: string;
errId?: string;
code_error_value: string;
};
request: {
baseURL: string;
};
secret: string;
sign: object;
sign_file: object;
vconsole: boolean;
noSignAttr: Array<string>;
}
const mockConfig: Config = JSON.parse(options);
const { response, request, sign_file, secret } = mockConfig;
axios.defaults.baseURL = request.baseURL;
// 配置新建一个axios 实例
const service: AxiosInstance = axios.create({
timeout: 50000,
withCredentials: true,
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
},
});
// 添加请求拦截器
service.interceptors.request.use(
(config: AxiosRequestConfig) => {
let { data = {}, domain, noSignAttr = [] } = config;
data = createSign(
Object.assign({}, sign_file, data),
secret,
noSignAttr,
"md5"
);
config.baseURL = mockConfig[domain].baseURL;
if (config.method) {
const method = config.method.toLowerCase();
if (method == "get") {
config.params = data;
} else if (method == "post") {
config.data = qs.stringify(data);
}
}
return config;
},
(error: AxiosError) => {
Message.error(error);
return Promise.reject(error);
}
);
// 添加响应拦截器
service.interceptors.response.use(
(r: AxiosResponse) => {
const {
data: res,
config: { returnAll },
} = r;
console.log(r, "===");
const {
code_key,
code_success_value,
data_key = "data",
errId = "errId",
code_error_value,
errId_logout_value,
} = response;
let codekey = Array.isArray(code_key)
? code_key.find((key) => res.hasOwnProperty(key))
: code_key;
let code = res[codekey];
let errValue = res[errId];
// 特殊的code处理,可以增加
switch (code + "") {
case code_success_value + "":
if (returnAll) return res;
break;
case code_error_value + "":
if (errValue && errValue == errId_logout_value) {
localStorage.removeItem("sessionId");
const [href] = window.location.href.split("/#/");
location.href = `${href}/#/login`;
}
// Message(res.message)
break;
default:
Message.error(res.message || res.msg || "出错了,请联系管理员");
// Toast(res.message || res.msg || '出错了,请联系管理员')
break;
}
if (returnAll) return res;
return res[data_key];
},
(error) => {
// Toast(error.message)
Message.error(error || "出错了,请联系管理员");
return Promise.reject(error);
}
);
export default service;
/*
* @Description:
* @Description:
* @Author: huyue
* @Date: 2022-12-06 16:17:36
* @LastEditors: huyue
* @LastEditTime: 2024-07-29 15:40:03
*/
import http from './http'
import { useUserStore } from '../store/user'
const PATH = 'rop-sk-gw/router'
const UPLOADPATH = '/router'
import http from "./http";
import http_file from "./http_file";
import { useUserStore } from "../store/user";
const PATH = "rop-sk-gw/router";
const UPLOADPATH = "/router";
// const UPLOADPATH = 'rop-scrm-carlife/router'
const req: any = (url: string, data: any = {}, returnAll = false, method = 'POST',) => {
data.method = url
const req: any = (
url: string,
data: any = {},
returnAll = false,
method = "POST"
) => {
data.method = url;
const options = {
domain: 'request',
domain: "request",
url: PATH,
method,
data,
returnAll
}
return http(options)
}
const uploadReq = (url: string, data: any = {}, returnAll = false, method = 'POST', noSignAttr = ['file']) => {
console.log(noSignAttr)
data.method = url
returnAll,
};
return http(options);
};
const uploadReq = (
url: string,
data: any = {},
returnAll = false,
method = "POST",
noSignAttr = ["file"]
) => {
console.log(noSignAttr);
data.method = url;
const options = {
domain: 'uploadRequest',
domain: "uploadRequest",
url: UPLOADPATH,
method,
data,
returnAll,
noSignAttr
}
return http(options)
}
export {
req,
uploadReq
}
\ No newline at end of file
noSignAttr,
};
return http(options);
};
const uploadFile = (
url: "",
data: any = {},
returnAll = false,
method = "POST",
noSignAttr = ["file"]
) => {
console.log(noSignAttr);
data.method = url;
const options = {
domain: "uploadRequest",
url: "/router?method=glsx.accounts.biz.common.pictureUpload",
method,
data,
returnAll,
noSignAttr,
};
return http_file(options);
};
export { req, uploadReq, uploadFile };
/*
* @Description:
* @Description:
* @Author: huyue
* @Date: 2022-10-10 10:54:14
* @LastEditors: huyue
* @LastEditTime: 2024-07-29 10:07:19
*/
import sha1 from 'sha1'
import qs from 'qs'
import sha1 from "sha1";
import qs from "qs";
import md5 from "js-md5";
export const Util = {
createSign: function (params: object, secret: string, noSignAttr = [], way = 'hexSha2') {
delete params.sign
params.timestamp = new Date().getTime()
var strQs = qs.stringify(params, { arrayFormat: 'indices', allowDots: true, encodeValuesOnly: true })
const objQs = strQs.split('&')
createSign: function (
params: object,
secret: string,
noSignAttr = [],
way = "sha1"
) {
delete params.sign;
params.timestamp = new Date().getTime();
var strQs = qs.stringify(params, {
arrayFormat: "indices",
allowDots: true,
encodeValuesOnly: true,
});
const objQs = strQs.split("&");
const collect = objQs.reduce((collect: any, item) => {
let [key, value] = item.split('=')
let [key, value] = item.split("=");
if (value === null) {
value = ''
} else if (typeof value === 'object') {
value = JSON.stringify(value)
value = "";
} else if (typeof value === "object") {
value = JSON.stringify(value);
}
collect[key] = decodeURIComponent(value)
return collect
}, {})
const keys = Object.keys(collect).sort()
collect[key] = decodeURIComponent(value);
return collect;
}, {});
const keys = Object.keys(collect).sort();
const str = keys.reduce((col, key) => {
if (!noSignAttr.includes(key)) {
col.push(key, collect[key])
col.push(key, collect[key]);
}
return col
}, [])
collect.sign = sha1(secret + str.join('') + secret).toUpperCase()
return collect
return col;
}, []);
collect.sign = { sha1, md5 }
[way](secret + str.join("") + secret)
.toUpperCase();
return collect;
},
rgb: function(){//rgb颜色随机
const r = Math.floor(Math.random()*256);
const g = Math.floor(Math.random()*256);
const b = Math.floor(Math.random()*256);
rgb: function () {
//rgb颜色随机
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
return `rgb(${r},${g},${b})`;
}
}
},
};
export const createSign = Util.createSign
export default Util
\ No newline at end of file
export const createSign = Util.createSign;
export default Util;
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!