@infly/libs 2.0.26 → 2.0.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.js +29 -21
- package/build/build-dist/index.js +258 -61
- package/build/webpack5/remove-legacy-assets-plugin.js +84 -0
- package/build/webpack5/webpack.base.js +228 -20
- package/build/webpack5/webpack.base.test.js +59 -0
- package/index.js +10 -0
- package/module/Permission.js +121 -55
- package/module/REST.js +136 -26
- package/module/Router.js +15 -0
- package/module/Uts.js +380 -331
- package/module/cjs/deep-merge.cjs +38 -0
- package/module/cjs/page-config.cjs +119 -0
- package/module/cjs/request-url-rules.cjs +55 -0
- package/package.json +11 -10
- package/script/build/command.js +48 -0
- package/script/build/env.js +28 -0
- package/script/build/git.js +252 -0
- package/script/build/preview.js +75 -0
- package/script/build/webhook.js +118 -0
- package/script/git-automation/check-packages.js +11 -8
- package/script/git-automation/git-utils.js +67 -0
- package/script/git-automation/index.js +378 -106
- package/script/index.js +8 -8
- package/script/pts/cloud-scenes.mjs +65 -0
- package/script/pts/cloud.js +151 -0
- package/script/pts/generate-cloud-params.mjs +210 -0
- package/script/pts/generate-cloud-params.test.mjs +67 -0
- package/script/webhook/webhook.js +75 -4
- package/store/modules/user.js +111 -9
- package/tools/auto-export.js +56 -0
- package/tools/file-export.js +32 -9
- package/tools/file-process.js +3 -0
- package/tools/project-preview.js +110 -97
- package/dataInit/commonTypeMap.js +0 -31
- package/dataInit/marketingActivitiesMap.js +0 -214
- package/dataInit/orderMap.js +0 -13
- package/dataInit/personalMap.js +0 -19
- package/dataInit/settlementMap.js +0 -17
- package/types/unused.index.d.ts +0 -71
package/module/REST.js
CHANGED
|
@@ -1,27 +1,106 @@
|
|
|
1
1
|
import axios from "axios";
|
|
2
2
|
import Uts from "./Uts.js";
|
|
3
3
|
import TokenService from "./TokenService.js";
|
|
4
|
+
import { resolveInternalServiceURL } from "./cjs/request-url-rules.cjs";
|
|
4
5
|
|
|
5
6
|
const baseURL = process.env.VUE_APP_BASE_API;
|
|
6
7
|
const { env } = Uts.getEnv() || {};
|
|
7
8
|
const isTestEnv = Uts.contain(["localhost", "infly-test"], env);
|
|
8
|
-
|
|
9
|
+
|
|
10
|
+
// 若接口地址是 localhost/127.0.0.1,则自动替换为当前页面访问主机。
|
|
11
|
+
// 协议、端口、路径保持不变,便于局域网联调时免改环境变量。
|
|
12
|
+
function remapLocalhostToCurrentHost(url) {
|
|
13
|
+
const normalized = Uts.normalizeEnvUrl(url);
|
|
14
|
+
if (!normalized) {
|
|
15
|
+
return normalized;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (typeof window === "undefined" || !window.location || !window.location.hostname) {
|
|
19
|
+
return normalized;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
const parsed = new URL(normalized);
|
|
24
|
+
const isLocalHost = ["localhost", "127.0.0.1"].includes(parsed.hostname);
|
|
25
|
+
if (!isLocalHost) {
|
|
26
|
+
return normalized;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
parsed.hostname = window.location.hostname;
|
|
30
|
+
return parsed.toString();
|
|
31
|
+
} catch (e) {
|
|
32
|
+
return normalized;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 解析 axios 最终使用的 baseURL。
|
|
37
|
+
// 优先级:config.baseURL > process.env.VUE_APP_BASE_API,再执行 localhost 主机替换。
|
|
38
|
+
function resolveServiceBaseURL(customBaseURL) {
|
|
39
|
+
const targetBaseURL = Uts.normalizeEnvUrl(customBaseURL || baseURL);
|
|
40
|
+
return remapLocalhostToCurrentHost(targetBaseURL);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 对任意通知函数加去重包装
|
|
45
|
+
* @param {Function} fn - 原始通知函数,接受字符串或 { message, type, ... }
|
|
46
|
+
* @param {Number} ttl - 去重时间窗(ms),默认 500
|
|
47
|
+
* @returns {Function} 去重后的通知函数
|
|
48
|
+
*/
|
|
49
|
+
export function createNotifyDedup(fn, ttl = 500) {
|
|
50
|
+
const recentMsgs = new Set();
|
|
51
|
+
return function dedupedNotify(msgOrOptions) {
|
|
52
|
+
const message = typeof msgOrOptions === "string" ? msgOrOptions : msgOrOptions?.message;
|
|
53
|
+
if (message && recentMsgs.has(message)) return;
|
|
54
|
+
if (message) {
|
|
55
|
+
recentMsgs.add(message);
|
|
56
|
+
setTimeout(() => recentMsgs.delete(message), ttl);
|
|
57
|
+
}
|
|
58
|
+
return fn(msgOrOptions);
|
|
59
|
+
};
|
|
60
|
+
}
|
|
9
61
|
|
|
10
62
|
/**
|
|
11
63
|
* 创建封装的 axios 实例
|
|
12
64
|
* @param {Object} config 配置项
|
|
13
|
-
* @param {
|
|
65
|
+
* @param {String} config.getTokenFunc 获取 token 的 TokenService 方法名,默认 "getToken"
|
|
14
66
|
* @param {Function} config.onLogout 登出回调
|
|
15
|
-
* @param {
|
|
67
|
+
* @param {Object} config.notify Message-like 对象(如 ElementUI Message),传入后自动应用去重并构建 onNotify
|
|
68
|
+
* @param {Function} config.onNotify 自定义通知回调;若传入 Message-like 对象也可自动识别
|
|
16
69
|
* @returns {AxiosInstance}
|
|
17
70
|
*/
|
|
18
71
|
export function createService(config) {
|
|
19
72
|
const {
|
|
73
|
+
baseURL: customBaseURL,
|
|
74
|
+
defaultField = "admin",
|
|
75
|
+
serviceField = defaultField,
|
|
20
76
|
logoutAction = "user/logout", // 退出登录的action方法
|
|
21
77
|
onLogout,
|
|
22
|
-
|
|
78
|
+
notify,
|
|
79
|
+
onNotify: rawOnNotify,
|
|
80
|
+
getTokenFunc = "getToken",
|
|
81
|
+
internalServiceURLRules = []
|
|
23
82
|
} = config || {};
|
|
24
|
-
|
|
83
|
+
// 统一在 REST 内部处理 baseURL,避免业务项目重复实现主机替换逻辑。
|
|
84
|
+
const resolvedBaseURL = resolveServiceBaseURL(customBaseURL);
|
|
85
|
+
const isProdAPI = Uts.contain(["https://api.sutpay.com"], resolvedBaseURL);
|
|
86
|
+
const service = axios.create({ baseURL: resolvedBaseURL });
|
|
87
|
+
|
|
88
|
+
// 统一处理 notify 与 onNotify:
|
|
89
|
+
// - 优先使用显式传入的 notify
|
|
90
|
+
// - 若 onNotify 是 Message-like 对象(带 .error/.success 等类型方法),也当作 notify 处理
|
|
91
|
+
// 两种写法都自动应用去重,拦截器与调用方 .catch() 共用同一个去重包装
|
|
92
|
+
const notifyTarget = notify || (rawOnNotify && typeof rawOnNotify["error"] === "function" ? rawOnNotify : null);
|
|
93
|
+
let onNotify = notifyTarget ? null : rawOnNotify;
|
|
94
|
+
|
|
95
|
+
if (notifyTarget) {
|
|
96
|
+
["success", "warning", "info", "error"].forEach((type) => {
|
|
97
|
+
if (typeof notifyTarget[type] === "function") {
|
|
98
|
+
notifyTarget[type] = createNotifyDedup(notifyTarget[type].bind(notifyTarget));
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
onNotify = ({ message, type = "error", duration }) =>
|
|
102
|
+
notifyTarget[type]?.(message) ?? notifyTarget({ message, type, duration });
|
|
103
|
+
}
|
|
25
104
|
|
|
26
105
|
/**
|
|
27
106
|
* 发送通知
|
|
@@ -37,13 +116,13 @@ export function createService(config) {
|
|
|
37
116
|
// 涉及到store和request循环依赖,导致store为undefined,所以不要引入store来处理而是通过闭包形式
|
|
38
117
|
const handleLogout = () => {
|
|
39
118
|
if (Uts.isFunction(onLogout)) {
|
|
40
|
-
onLogout(logoutAction);
|
|
119
|
+
onLogout(logoutAction, { redirectLogin: true });
|
|
41
120
|
}
|
|
42
121
|
};
|
|
43
122
|
|
|
44
123
|
service.interceptors.request.use(
|
|
45
|
-
config => {
|
|
46
|
-
const Token = TokenService
|
|
124
|
+
(config) => {
|
|
125
|
+
const Token = TokenService[getTokenFunc]();
|
|
47
126
|
|
|
48
127
|
if (Token) {
|
|
49
128
|
config.headers["Authorization"] = " JWT " + Token;
|
|
@@ -57,33 +136,64 @@ export function createService(config) {
|
|
|
57
136
|
});
|
|
58
137
|
}
|
|
59
138
|
|
|
139
|
+
const internalServiceURL = resolveInternalServiceURL(config.url, internalServiceURLRules);
|
|
140
|
+
if (internalServiceURL) {
|
|
141
|
+
config.url = internalServiceURL.url;
|
|
142
|
+
config.baseURL = resolveServiceBaseURL(internalServiceURL.baseURL);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (serviceField !== defaultField) {
|
|
146
|
+
config.url = config.url.replace(`/${defaultField}/`, `/${serviceField}/`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
Uts.filterCommaFields(config?.data);
|
|
150
|
+
|
|
151
|
+
Uts.filterCommaFields(config?.params);
|
|
152
|
+
|
|
60
153
|
return config;
|
|
61
154
|
},
|
|
62
|
-
error => {
|
|
155
|
+
(error) => {
|
|
63
156
|
return Promise.reject(error);
|
|
64
157
|
}
|
|
65
158
|
);
|
|
66
159
|
|
|
67
160
|
service.interceptors.response.use(
|
|
68
|
-
response => {
|
|
69
|
-
|
|
161
|
+
(response) => {
|
|
162
|
+
const { data } = response || {};
|
|
163
|
+
return Uts.transArrayBufferToObj(data);
|
|
70
164
|
},
|
|
71
|
-
error => {
|
|
72
|
-
const { response } = error || {};
|
|
73
|
-
const { status } = response || {};
|
|
165
|
+
(error) => {
|
|
166
|
+
const { response, request } = error || {};
|
|
167
|
+
const { status, data } = response || {};
|
|
168
|
+
const { error: bufferError } = Uts.transArrayBufferToObj(data) ?? {};
|
|
169
|
+
const { error: dataError, detail, message } = data || {};
|
|
170
|
+
const errorMsg = bufferError || dataError || detail || message || `请求失败(${status})`;
|
|
74
171
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
172
|
+
// 添加标识,表示错误已被拦截器处理
|
|
173
|
+
error.handledByInterceptor = true;
|
|
174
|
+
|
|
175
|
+
// HTTP 状态码 → 固定提示语映射(400 直接用后端返回的 errorMsg)
|
|
176
|
+
const STATUS_MESSAGES = {
|
|
177
|
+
400: errorMsg,
|
|
178
|
+
401: "登录信息已过期,请重新登录",
|
|
179
|
+
403: "您没有权限访问,请联系管理员",
|
|
180
|
+
500: errorMsg || "服务器内部错误,请联系开发人员",
|
|
181
|
+
502: "网关错误,请稍后重试或联系开发人员",
|
|
182
|
+
503: "服务暂时不可用,请稍后重试",
|
|
183
|
+
504: "网关超时,请稍后重试或联系开发人员"
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// 按优先级确定最终提示语,合并为一次 handleNotify 调用
|
|
187
|
+
const notifyMsg =
|
|
188
|
+
STATUS_MESSAGES[status] ??
|
|
189
|
+
(response ? errorMsg : null) ??
|
|
190
|
+
(request ? "网络请求失败,请检查网络连接" : null) ??
|
|
191
|
+
(error.message || "请求配置错误");
|
|
192
|
+
|
|
193
|
+
handleNotify({ message: notifyMsg, type: "error" });
|
|
194
|
+
|
|
195
|
+
// 401 副作用:自动登出(不受弹窗逻辑影响)
|
|
196
|
+
if (status === 401) handleLogout();
|
|
87
197
|
|
|
88
198
|
return Promise.reject(error);
|
|
89
199
|
}
|
package/module/Router.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const resetRouter = (RouterInstance, router, constantRoutes) => {
|
|
2
|
+
const newRouter = createRouter(RouterInstance, constantRoutes);
|
|
3
|
+
router.matcher = newRouter.matcher; // reset router
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export const updateRouter = resetRouter
|
|
7
|
+
|
|
8
|
+
export const createRouter = (RouterInstance, constantRoutes) =>
|
|
9
|
+
new RouterInstance({
|
|
10
|
+
// mode: 'history', // require service support
|
|
11
|
+
scrollBehavior: () => ({
|
|
12
|
+
y: 0
|
|
13
|
+
}),
|
|
14
|
+
routes: constantRoutes || []
|
|
15
|
+
});
|