refactor: ♻️ 日期范围查询组件封装避免日期的转换,用户和日志模块优化
This commit is contained in:
@@ -16,7 +16,6 @@ export default function request<T>(options: UniApp.RequestOptions): Promise<T> {
|
||||
...options.header,
|
||||
Authorization: getToken() ? `Bearer ${getToken()}` : "",
|
||||
},
|
||||
data: handleData(options.data, options.method || "GET"),
|
||||
success: (response) => {
|
||||
console.log("success response", response);
|
||||
const resData = response.data as ResponseData<T>;
|
||||
@@ -60,51 +59,3 @@ export default function request<T>(options: UniApp.RequestOptions): Promise<T> {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理请求数据
|
||||
* @param data 请求数据
|
||||
* @returns 处理后的数据
|
||||
*/
|
||||
function handleData(data: any, method: string) {
|
||||
// 非微信小程序,且非GET请求,则不处理数据
|
||||
const appInfo = uni.getAppBaseInfo();
|
||||
if (method !== "GET" && appInfo.hostName !== "WeChat") {
|
||||
return data;
|
||||
}
|
||||
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