@cdwx/system
Version:
成都文宣业务类服务模块组件库
134 lines (133 loc) • 4.07 kB
JavaScript
var p = Object.defineProperty;
var m = (o, e, t) => e in o ? p(o, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : o[e] = t;
var c = (o, e, t) => m(o, typeof e != "symbol" ? e + "" : e, t);
import "../../stores/src/instance.js";
import "../../stores/src/modules/menu.js";
import "../../stores/src/modules/page-tab.js";
import { useRequestingStore as u } from "../../stores/src/modules/requesting-records.js";
import { useSystemStore as d } from "../../stores/src/modules/system.js";
import "../../stores/src/modules/user.js";
import { sm2Encrypt as l } from "../../utils/src/sm-encrypt.js";
import h from "../../../node_modules/.pnpm/axios@1.7.9/node_modules/axios/lib/axios.js";
import { errorTip as q, responseHandler as g, responseErrorHandler as v } from "./response-handler.js";
class z {
// 记录正在请求的接口
constructor(e) {
// 请求实例
c(this, "service");
this.service = h.create(e), this.requestInterceptors(), this.responseInterceptors();
}
/**
* get请求
* @param url 请求地址
* @param params 请求参数
* @param config
*/
get(e, t, r) {
return this.service.get(e, {
...r,
params: t
});
}
/**
* post请求
* @param url 请求地址
* @param params 请求参数
* @param config 请求配置
* @param encrypt 是否加密
*/
// eslint-disable-next-line style/max-len
post(e, t, r, s = !1) {
const i = s ? l(JSON.stringify(t)) : t;
return this.service.post(e, i, {
...r,
headers: {
"Content-Type": "application/json;charset=UTF-8",
...r == null ? void 0 : r.headers
}
});
}
/**
* delete请求
* @param url 请求地址
* @param params 请求参数
* @param config
*/
delete(e, t, r) {
return this.service.delete(e, {
...r,
params: t
});
}
/**
* 导出文档数据
* @param url 请求地址
* @param params 请求参数
* @param config
*/
exportFile(e, t, r) {
return this.service.post(e, t, {
...r,
headers: {
"Content-Type": "application/json;charset=UTF-8"
},
responseType: "blob"
});
}
/**
* 请求拦截:记录当前请求
* @param config
* @private
*/
recordRequest(e) {
const t = u(), r = new AbortController(), s = t.addRequest({
url: e.url || "",
data: e.data,
method: e.method || "",
params: e.params
}, r);
e.cancel !== !1 && t.cancelAndRemoveSimilarRequest(s), e.signal = r.signal;
}
/**
* 响应拦截:移除请求
* @param signal
* @private
*/
removeRequest(e) {
if (!e)
return;
u().removeRequest(e);
}
// 请求拦截:新增token
requestInterceptors() {
this.service.interceptors.request.use((e) => {
const t = d();
return e.headers.Authorization || (e.headers.Authorization = t == null ? void 0 : t.token), this.recordRequest(e), e;
}, (e) => (q("服务器异常,请联系管理员!"), Promise.reject(e)));
}
// 请求响应拦截
responseInterceptors() {
this.service.interceptors.response.use((e) => {
var i, a, n;
const { data: t, config: { responseType: r } } = e;
if ((i = e == null ? void 0 : e.config) != null && i.signal && this.removeRequest(e.config.signal), r === "blob")
return e;
if (t.code === 0 || t.success)
return Array.isArray((a = t.data) == null ? void 0 : a.records) && (t.page = {
currentPage: t.data.currentPage,
pageSize: t.data.pageSize,
total: t.data.total
}, t.data = t.data.records), t;
let s = "服务器异常,请联系管理员!";
return t.msg && (s = t.msg), g(t, (n = e.config) == null ? void 0 : n.errorTip), Promise.reject(new Error(s));
}, (e) => {
var r, s;
(r = e == null ? void 0 : e.config) != null && r.signal && this.removeRequest(e.config.signal);
const t = v(e, (s = e.config) == null ? void 0 : s.errorTip);
return Promise.reject(new Error(t));
});
}
}
export {
z as default
};