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;
...@@ -5,37 +5,64 @@ ...@@ -5,37 +5,64 @@
* @LastEditors: huyue * @LastEditors: huyue
* @LastEditTime: 2024-07-29 15:40:03 * @LastEditTime: 2024-07-29 15:40:03
*/ */
import http from './http' import http from "./http";
import { useUserStore } from '../store/user' import http_file from "./http_file";
const PATH = 'rop-sk-gw/router' import { useUserStore } from "../store/user";
const UPLOADPATH = '/router' const PATH = "rop-sk-gw/router";
const UPLOADPATH = "/router";
// const UPLOADPATH = 'rop-scrm-carlife/router' // const UPLOADPATH = 'rop-scrm-carlife/router'
const req: any = (url: string, data: any = {}, returnAll = false, method = 'POST',) => { const req: any = (
data.method = url url: string,
data: any = {},
returnAll = false,
method = "POST"
) => {
data.method = url;
const options = { const options = {
domain: 'request', domain: "request",
url: PATH, url: PATH,
method, method,
data, data,
returnAll returnAll,
} };
return http(options) return http(options);
} };
const uploadReq = (url: string, data: any = {}, returnAll = false, method = 'POST', noSignAttr = ['file']) => { const uploadReq = (
console.log(noSignAttr) url: string,
data.method = url data: any = {},
returnAll = false,
method = "POST",
noSignAttr = ["file"]
) => {
console.log(noSignAttr);
data.method = url;
const options = { const options = {
domain: 'uploadRequest', domain: "uploadRequest",
url: UPLOADPATH, url: UPLOADPATH,
method, method,
data, data,
returnAll, returnAll,
noSignAttr
}
return http(options)
}
export {
req,
uploadReq
}
\ No newline at end of file \ 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 };
...@@ -5,41 +5,54 @@ ...@@ -5,41 +5,54 @@
* @LastEditors: huyue * @LastEditors: huyue
* @LastEditTime: 2024-07-29 10:07:19 * @LastEditTime: 2024-07-29 10:07:19
*/ */
import sha1 from 'sha1' import sha1 from "sha1";
import qs from 'qs' import qs from "qs";
import md5 from "js-md5";
export const Util = { export const Util = {
createSign: function (params: object, secret: string, noSignAttr = [], way = 'hexSha2') { createSign: function (
delete params.sign params: object,
params.timestamp = new Date().getTime() secret: string,
var strQs = qs.stringify(params, { arrayFormat: 'indices', allowDots: true, encodeValuesOnly: true }) noSignAttr = [],
const objQs = strQs.split('&') 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) => { const collect = objQs.reduce((collect: any, item) => {
let [key, value] = item.split('=') let [key, value] = item.split("=");
if (value === null) { if (value === null) {
value = '' value = "";
} else if (typeof value === 'object') { } else if (typeof value === "object") {
value = JSON.stringify(value) value = JSON.stringify(value);
} }
collect[key] = decodeURIComponent(value) collect[key] = decodeURIComponent(value);
return collect return collect;
}, {}) }, {});
const keys = Object.keys(collect).sort() const keys = Object.keys(collect).sort();
const str = keys.reduce((col, key) => { const str = keys.reduce((col, key) => {
if (!noSignAttr.includes(key)) { if (!noSignAttr.includes(key)) {
col.push(key, collect[key]) col.push(key, collect[key]);
} }
return col return col;
}, []) }, []);
collect.sign = sha1(secret + str.join('') + secret).toUpperCase() collect.sign = { sha1, md5 }
return collect [way](secret + str.join("") + secret)
.toUpperCase();
return collect;
}, },
rgb: function(){//rgb颜色随机 rgb: function () {
const r = Math.floor(Math.random()*256); //rgb颜色随机
const g = Math.floor(Math.random()*256); const r = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random()*256); const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
return `rgb(${r},${g},${b})`; return `rgb(${r},${g},${b})`;
} },
} };
export const createSign = Util.createSign
export default Util
\ No newline at end of file \ 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!