utils.ts
5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* 环境检测工具函数
*/
/**
* 环境类型枚举
*/
export enum EnvironmentType {
WEB = 'web',
MINIPROGRAM = 'miniprogram',
UNIAPP = 'uniapp'
}
/**
* 检测当前运行环境
* 使用IIFE模式,确保只执行一次环境检测
*/
export const detectEnvironment = (() => {
// 缓存检测结果
let detectedEnv: EnvironmentType | null = null;
// 返回实际的检测函数
return (): EnvironmentType => {
// 如果已经检测过,直接返回缓存结果
if (detectedEnv !== null) {
return detectedEnv;
}
// 执行环境检测
if (typeof wx !== 'undefined' && wx && 'request' in wx) {
// 小程序环境
detectedEnv = EnvironmentType.MINIPROGRAM;
} else if (typeof uni !== 'undefined' && uni && 'request' in uni) {
// uniapp环境
detectedEnv = EnvironmentType.UNIAPP;
} else {
// Web环境
detectedEnv = EnvironmentType.WEB;
}
return detectedEnv;
};
})();
/**
* 数组处理模式枚举
*/
export enum ArrayProcessMode {
/** JSON 格式:将数组转换为 JSON 字符串 */
JSON = 'json',
/** 索引格式:将数组转换为索引键值对 */
INDEX = 'index',
/** 对象索引格式:将对象数组转换为扁平化的索引键值对,适用于Java后端 */
OBJECT_INDEX = 'object_index'
}
/**
* 将参数处理为JSON格式,用于签名计算
* @param params 原始参数对象
* @returns 处理后的参数对象,数组和对象转换为JSON字符串
*/
export function processParamsAsJson(params: Record<string, any>): Record<string, string> {
const result: Record<string, string> = {};
for (const [key, value] of Object.entries(params)) {
if (value === null || value === undefined) {
result[key] = '';
} else if (Array.isArray(value)) {
// 数组直接转换为 JSON 字符串
result[key] = JSON.stringify(value);
} else if (typeof value === 'object') {
// 对象转换为 JSON 字符串
result[key] = JSON.stringify(value);
} else {
// 基本类型直接使用
result[key] = String(value);
}
}
return result;
}
/**
* 将参数处理为索引格式,用于URL查询参数
* @param params 原始参数对象
* @returns 键值对数组,数组使用索引格式 key[0]=value, key[1]=value
*/
export function processParamsAsIndex(params: Record<string, any>): Array<{key: string, value: string}> {
const result: Array<{key: string, value: string}> = [];
for (const [key, value] of Object.entries(params)) {
if (value === null || value === undefined) {
result.push({ key, value: '' });
} else if (Array.isArray(value)) {
// 数组使用索引格式 key[0]=value, key[1]=value
value.forEach((item, index) => {
result.push({
key: `${key}[${index}]`,
value: String(item)
});
});
} else if (typeof value === 'object') {
// 对象转换为 JSON 字符串
result.push({ key, value: JSON.stringify(value) });
} else {
// 基本类型直接使用
result.push({ key, value: String(value) });
}
}
return result;
}
/**
* 将参数处理为对象索引格式,将对象数组扁平化为Java后端期望的格式
* @param params 原始参数对象
* @returns 键值对数组,对象数组扁平化为 dogs[0].name=value, dogs[0].age=value 格式
*/
export function processParamsAsObjectIndex(params: Record<string, any>): Array<{key: string, value: string}> {
const result: Array<{key: string, value: string}> = [];
for (const [key, value] of Object.entries(params)) {
if (value === null || value === undefined) {
result.push({ key, value: '' });
} else if (Array.isArray(value)) {
// 数组中的每个元素
value.forEach((item, index) => {
if (typeof item === 'object' && item !== null) {
// 对象数组:dogs[0].name=value, dogs[0].age=value
for (const [objKey, objValue] of Object.entries(item)) {
result.push({
key: `${key}[${index}].${objKey}`,
value: String(objValue)
});
}
} else {
// 基本类型数组:key[0]=value, key[1]=value
result.push({
key: `${key}[${index}]`,
value: String(item)
});
}
});
} else if (typeof value === 'object') {
// 单个对象:展开为多个键值对
for (const [objKey, objValue] of Object.entries(value)) {
result.push({
key: `${key}.${objKey}`,
value: String(objValue)
});
}
} else {
// 基本类型直接使用
result.push({ key, value: String(value) });
}
}
return result;
}
/**
* 处理GET请求的签名参数,将数组展开为 key[index]=value 格式
* @param params 原始参数对象
* @returns 处理后的参数对象
*/
export function processGetSignatureParams(params: Record<string, any>): Record<string, string> {
const processedResult = processParamsAsObjectIndex(params);
// OBJECT_INDEX 模式返回的是数组,需要转换为对象
const result: Record<string, string> = {};
(processedResult as Array<{key: string, value: string}>).forEach(item => {
result[item.key] = item.value;
});
return result;
}
/**
* 处理POST请求的签名参数,将数组转换为JSON字符串格式
* @param params 原始参数对象
* @returns 处理后的参数对象
*/
export function processPostSignatureParams(params: Record<string, any>): Record<string, string> {
const processedResult = processParamsAsJson(params);
// JSON 模式直接返回对象
return processedResult as Record<string, string>;
}