211 lines
5.8 KiB
JavaScript
211 lines
5.8 KiB
JavaScript
import l from "vue";
|
|
import O from "axios";
|
|
const h = "STARTLOADING", p = "ENDLOADING", y = {
|
|
namespaced: !0,
|
|
state: () => ({
|
|
loadingStatus: {}
|
|
}),
|
|
mutations: {
|
|
[h](e, t) {
|
|
const { fwbm: o } = t;
|
|
l.set(e.loadingStatus, o, !0);
|
|
},
|
|
[p](e, t) {
|
|
const { fwbm: o } = t;
|
|
l.set(e.loadingStatus, o, !1);
|
|
}
|
|
},
|
|
actions: {
|
|
startLoading(e, t) {
|
|
e.commit(h, t || []);
|
|
},
|
|
endLoading(e, t) {
|
|
e.commit(p, t || []);
|
|
}
|
|
}
|
|
}, r = {
|
|
axiosConfig: {
|
|
timeout: 60 * 1e3,
|
|
withCredentials: !0
|
|
},
|
|
showRequestErr(e) {
|
|
if (this.vue)
|
|
return this.vue.$message.error(e);
|
|
console.error("Vue instance not available:", e);
|
|
},
|
|
logoutHandle() {
|
|
this.vue && this.vue.$store ? this.vue.$store.dispatch("logout") : console.warn("Logout handler called but Vue instance or store not available");
|
|
},
|
|
install(e, { axiosConfig: t }) {
|
|
this.vue = e, this.axiosConfig = { ...this.axiosConfig, ...t }, e.prototype.$request = d;
|
|
}
|
|
}, g = O.create(r.axiosConfig);
|
|
g.interceptors.request.use(
|
|
/**
|
|
* onFulfilled钩子函数处理
|
|
* @param conf {Object} 实例中request的配置函数
|
|
* @returns {Object} request配置函数
|
|
*/
|
|
(e) => {
|
|
const t = e, { url: o } = t;
|
|
if (o.indexOf("?") !== -1 ? t.url = `${o}&t=${(/* @__PURE__ */ new Date()).getTime()}` : t.url = `${o}?t=${(/* @__PURE__ */ new Date()).getTime()}`, t.method === "get" && t.params) {
|
|
let n = `${t.url}&`;
|
|
for (const a of Object.keys(t.params)) {
|
|
const s = t.params[a], c = `${encodeURIComponent(a)}=`;
|
|
if (s !== null && typeof s < "u")
|
|
if (typeof s == "object")
|
|
for (const f of Object.keys(s)) {
|
|
const C = `${a}[${f}]`, D = `${encodeURIComponent(C)}=`;
|
|
n += `${D}${encodeURIComponent(s[f])}&`;
|
|
}
|
|
else
|
|
n += `${c}${encodeURIComponent(s)}&`;
|
|
}
|
|
n = n.slice(0, -1), t.params = {}, t.url = n;
|
|
}
|
|
return t;
|
|
},
|
|
/**
|
|
* onRejected钩子函数处理
|
|
* @param err {ErrorEvent} 前一个拦截器返回的request错误事件
|
|
* @returns {Promise<never>} 必须reject对应的错误事件
|
|
*/
|
|
(e) => (console.log("====>> req err", e), Promise.reject(e))
|
|
);
|
|
g.interceptors.response.use(
|
|
/**
|
|
* onFulfilled钩子函数处理
|
|
* @param res {Object} 为前一个拦截器返回的结果
|
|
* @returns {Object} 必须要return`结果对象`
|
|
*/
|
|
async (e) => {
|
|
const { code: t } = e.data;
|
|
return t === 401 ? j() : t !== 1 && t !== 0 ? (e.config.noTips !== !0 && e.data.msg && r.showRequestErr(e.data.msg), Promise.reject(t)) : e.data;
|
|
},
|
|
/**
|
|
* onRejected钩子函数处理
|
|
* @param err {ErrorEvent} 前一个拦截器返回的错误结果
|
|
* @returns {Promise<never>}
|
|
*/
|
|
(e) => {
|
|
if (e.config.noTips !== !0) {
|
|
let { message: t } = e;
|
|
t === "Network Error" ? t = "后端接口连接异常" : t.includes("timeout") ? t = "系统接口请求超时" : t.includes("Request failed with status code") && (t = `系统接口${t.substr(t.length - 3)}异常`), r.showRequestErr(t);
|
|
}
|
|
return Promise.reject(e);
|
|
}
|
|
);
|
|
function j() {
|
|
return r.showRequestErr("无效的会话,或者会话已过期,请重新登录。").then(() => {
|
|
r.logoutHandle();
|
|
}), Promise.reject("无效的会话,或者会话已过期,请重新登录。");
|
|
}
|
|
const u = (e, t) => g(
|
|
{
|
|
...e
|
|
},
|
|
t
|
|
), i = {
|
|
store: null,
|
|
setStore(e) {
|
|
this.store = e;
|
|
},
|
|
getStore() {
|
|
return this.store;
|
|
},
|
|
dispatch(e, t) {
|
|
this.store && this.store.dispatch(e, t);
|
|
}
|
|
}, m = (e, t) => e.then((o) => {
|
|
console.log("请求返回", o);
|
|
const n = [null, o];
|
|
return t && t(n), n;
|
|
}).catch((o) => {
|
|
console.error("请求出错", o);
|
|
const n = [o, null];
|
|
return t && t(n), n;
|
|
}), d = {
|
|
async post(e, t, o, n) {
|
|
const a = `${e}/${t}`, s = JSON.stringify(o) || "{}";
|
|
await i.dispatch("LoadingManager/startLoading", { fwbm: t }), console.log("发起请求", a, "data:", s);
|
|
const c = u({
|
|
...n,
|
|
url: a,
|
|
data: s,
|
|
method: "post",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
}
|
|
}, n);
|
|
return m(c, () => {
|
|
i.dispatch("LoadingManager/endLoading", { fwbm: t });
|
|
});
|
|
},
|
|
async get(e, t, o, n) {
|
|
const a = `${e}/${t}`;
|
|
await i.dispatch("LoadingManager/startLoading", { fwbm: t });
|
|
const s = u({
|
|
...n,
|
|
url: a,
|
|
params: o,
|
|
method: "get",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
}
|
|
});
|
|
return m(s, () => {
|
|
i.dispatch("LoadingManager/endLoading", { fwbm: t });
|
|
});
|
|
},
|
|
async _fetch(e, t) {
|
|
return console.log("发起请求:", e.method, ",", e.url, ",data:", e.params), u(e, t);
|
|
}
|
|
}, w = {
|
|
install(e, { store: t }) {
|
|
i.setStore(t), t.registerModule("LoadingManager", y);
|
|
}
|
|
}, T = "/config", N = {
|
|
/** 获取config */
|
|
async getDefaultConfig() {
|
|
const [, e] = await d.get(T, "config.json", null, { noTips: !0 });
|
|
return e ? e.data || {} : {};
|
|
}
|
|
}, $ = "LOAD_CONFIG", R = {
|
|
namespaced: !0,
|
|
state: () => ({}),
|
|
mutations: {
|
|
[$](e, t) {
|
|
Object.entries(t).forEach(([o, n]) => {
|
|
l.set(e, o, n);
|
|
});
|
|
}
|
|
},
|
|
actions: {
|
|
loadConfig(e, t) {
|
|
e.commit($, typeof t == "object" ? t : {});
|
|
}
|
|
}
|
|
}, S = {
|
|
install(e, { store: t, extSettings: o }) {
|
|
t.registerModule("settings", R), N.getDefaultConfig().then((n) => {
|
|
const a = { ...o, ...n };
|
|
t.dispatch("settings/loadConfig", a);
|
|
});
|
|
}
|
|
}, q = { request: d }, L = { RequestPlugin: r, LoadingManagerPlugin: w, ExtSettingsPlugin: S }, P = {
|
|
install: function(e, t) {
|
|
Object.values(L).forEach((o) => {
|
|
e.use(o, t);
|
|
});
|
|
},
|
|
...q,
|
|
...L
|
|
};
|
|
export {
|
|
S as ExtSettingsPlugin,
|
|
w as LoadingManagerPlugin,
|
|
r as RequestPlugin,
|
|
P as default,
|
|
d as request
|
|
};
|