refactor: ♻️ 更改了部分命名规则,调整部分内容
增加请求参数对于数组的判断和处理,编辑使用popup,页面优化
This commit is contained in:
@@ -16,6 +16,7 @@ export default function request<T>(options: UniApp.RequestOptions): Promise<T> {
|
||||
...options.header,
|
||||
Authorization: getToken() ? `Bearer ${getToken()}` : "",
|
||||
},
|
||||
data: handleData(options.data),
|
||||
success: (response) => {
|
||||
console.log("success response", response);
|
||||
const resData = response.data as ResponseData<T>;
|
||||
@@ -59,3 +60,46 @@ export default function request<T>(options: UniApp.RequestOptions): Promise<T> {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理请求数据
|
||||
* @param data 请求数据
|
||||
* @returns 处理后的数据
|
||||
*/
|
||||
function handleData(data: any) {
|
||||
if (!data) return data;
|
||||
|
||||
// 如果是对象,遍历处理每个属性
|
||||
if (typeof data === "object") {
|
||||
const result: Record<string, any> = {};
|
||||
for (const key in data) {
|
||||
const value = data[key];
|
||||
if (Array.isArray(value)) {
|
||||
let res = handleArray(value);
|
||||
if (res) {
|
||||
result[key] = res;
|
||||
}
|
||||
} else {
|
||||
result[key] = value;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理数组
|
||||
* @param value 数组
|
||||
* @returns 逗号分隔字符串
|
||||
*/
|
||||
function handleArray(value: any[]) {
|
||||
let str = "";
|
||||
for (const item of value) {
|
||||
if (item != 0 && item) {
|
||||
str += `${item},`;
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user