// index.uvue
getShopList(data).then((res : HttpData<GoodsDetailData>) => {
const data = res.rows! // rows提示为(property)rows ?: GoodsDetailData[] | undefined
// const data = JSON.parse<GoodsDetailData[]>(JSON.stringify(res.rows!)!)!
this.newGoods = data.map((item : GoodsDetailData) => { // 报错io.dcloud.uts.UTSJSONObject cannot be cast to uni.UNI666B1AD.GoodsDetailData
item.priceMin = (item.ggList as GoodsSpecData[]).map((item) => item.price).sort((a : number, b : number) => a - b)[0]
return item
})
})
// api.uts
export function getShopList(data : GoodsListQuery) : Promise<HttpData<GoodsDetailData>> {
return http.getRequest<GoodsDetailData>("/webApi/nologin/getProudctList", data, false, null)
}
// request.uts
async request<T>(
url : string,
method : RequestMethod,
option ?: any,
showLoading : boolean = false,
header ?: UTSJSONObject
) : Promise<HttpData<T>> {
if (showLoading) {
uni.showLoading({
title: '',
mask: true
});
}
const optionsData = JSON.parseObject(JSON.stringify(option)!)
return new Promise<HttpData<T>>((resolve, reject) => {
uni.request<HttpData<T>>({
url: this.baseUrl + url,
data: optionsData,
method: method,
header: {
'Content-Type': 'application/json;charset=UTF-8',
'Authorization': this.token != '' ? this.token : uni.getStorageSync('Authorization'),
...(header != null ? header : {})
},
timeout: 10000,
success: (res : RequestSuccess<HttpData<T>>) => {
// console.log(res.data)
if (showLoading) {
uni.hideLoading();
}
if (res.statusCode === 200) {
const responseData = JSON.parse<HttpData<T>>(JSON.stringify(res.data!))!;
if (this.debug) {
console.log(url, '-->', responseData);
}
if (responseData === null) {
reject({
code: 500,
msg: "Response data is null"
})
return
}
if (responseData.code != null && responseData.code === 200) {
resolve(responseData)
} else {
if (responseData.code != null && responseData.code === 401) {
// 如果需要登录,统一处理
// debounceModal({
// title: '提示',
// content: '您的登录状态以过期,请登录',
// confirmText: '去登录',
// success(rs : any) {
// // console.log(res)
// }
// })
}
reject(responseData)
}
} else {
reject({
code: res.statusCode,
msg: `HTTP Error: ${res.statusCode}`
})
}
},
fail: (err : RequestFail) => {
reject({
code: err.errCode,
mas: "Request Fail",
data: err.cause
})
}
});
});
}
async getRequest<T>(url : string, option ?: any, showLoading : boolean = true, header ?: UTSJSONObject) : Promise<HttpData<T>> {
return this.request<T>(url, 'GET', option, showLoading, header);
}
// type.uts
export type HttpData<T> = {
code : number
msg : string | null
rows ?: Array<T>
total ?: number
data ?: T | string | number | null,
url ?: string
}

- 发布:2025-10-10 16:09
- 更新:2025-10-10 18:17
- 阅读:35

这个看上去是泛型传递丢失的问题:
https://doc.dcloud.net.cn/uni-app-x/plugin/uts-for-android.html#lost-generics
可以参考文档中的方法进行规避。
如果还是不行,需要提供一下可以复现问题的最简示例。我确认一下

m***@163.com (作者)
解决了,封装为class类用不了@UTSAndroid注解,哪怕是在外面new className()再单独导出类里的方法也不行,必须是常规的js写法,而且不用每个导出的方法都写一遍,只用在所有方法的最前面写一次就能生效,多个反而不生效,附件是我封装好的,有需要的自取
m***@163.com (作者)
根据文档我应该在getRequest方法上添加注解,但我在方法上添加注解后提示类型UTSAndroid上不存在keyword属性
2025-10-10 16:46
m***@163.com (作者)
是因为我将封装的uni.request作为一个类导出的原因吗
2025-10-10 16:49