consumer模块完成度95%,准备部署消费者端测试
This commit is contained in:
@@ -369,70 +369,6 @@ export class AkReq {
|
||||
};
|
||||
}
|
||||
|
||||
// 新增:支持类型转换的请求方法
|
||||
static async requestAs<T = any>(options : AkReqOptions, skipRefresh ?: boolean) : Promise<AkReqResponse<T|Array<T>>> {
|
||||
const response = await this.request(options, skipRefresh);
|
||||
|
||||
// 如果原始 data 是 null,直接返回 null
|
||||
// if (response.data == null) {
|
||||
// return {
|
||||
// status: response.status,
|
||||
// data: null,
|
||||
// headers: response.headers,
|
||||
// error: response.error,
|
||||
// total: response.total,
|
||||
// page: response.page,
|
||||
// limit: response.limit,
|
||||
// hasmore: response.hasmore,
|
||||
// origin: response.origin
|
||||
// } as AkReqResponse<T|Array<T>>;
|
||||
// }
|
||||
|
||||
// 尝试类型转换
|
||||
let convertedData: T | null = null;
|
||||
try {
|
||||
// #ifdef APP-ANDROID
|
||||
if (response.data instanceof UTSJSONObject) {
|
||||
convertedData = response.data.parse<T>();
|
||||
} else if (Array.isArray(response.data)) {
|
||||
const convertedArray: Array<any> = [];
|
||||
const dataArray = response.data;
|
||||
for (let i = 0; i < dataArray.length; i++) {
|
||||
const item = dataArray[i];
|
||||
if (item instanceof UTSJSONObject) {
|
||||
const parsed = item.parse<T>();
|
||||
if (parsed != null) {
|
||||
convertedArray.push(parsed);
|
||||
}
|
||||
} else {
|
||||
convertedArray.push(item);
|
||||
}
|
||||
}
|
||||
convertedData = convertedArray as T;
|
||||
}
|
||||
// #endif
|
||||
|
||||
// #ifndef APP-ANDROID
|
||||
convertedData = response.data as T;
|
||||
// #endif
|
||||
} catch (e) {
|
||||
console.warn('类型转换失败,使用原始 UTSJSONObject:', e);
|
||||
// 转换失败时,返回原始 UTSJSONObject
|
||||
convertedData = response.data as T;
|
||||
}
|
||||
const aaa = {
|
||||
status: response.status,
|
||||
data: convertedData!!,
|
||||
headers: response.headers,
|
||||
error: response.error,
|
||||
total: response.total,
|
||||
page: response.page,
|
||||
limit: response.limit,
|
||||
hasmore: response.hasmore,
|
||||
origin: response.origin
|
||||
} ;
|
||||
return aaa
|
||||
}
|
||||
}
|
||||
|
||||
export default AkReq;
|
||||
Reference in New Issue
Block a user