阿布朱朱
阿布朱朱
  • 发布:2024-06-07 09:57
  • 更新:2024-08-14 17:06
  • 阅读:349

接口中使用泛型报错:io.dcloud.uts.UTSJSONObject cannot be cast to uni.UNIA808F62

分类:uts
// 调用报错:Possible Unhandled Promise Rejection: ‍[⁠java.lang.ClassCastException⁠]‍ {cause: null, message: // "io.dcloud.uts.UTSJSONObject cannot be cast to uni.UNIA808F62.GetHomeDataResponse"}  
class ResponseData<T> {  
    code: number | null = null  
    msg: string | null = null  
    time: string | null = null  
    data: T | null = null  
}  

function request<T>(options : RequestOptions<T>): Promise<T> {  
    return new Promise((resolve, reject) => {  
        const token = userState.token  
        const version = deviceState.appVersion  
        const header: UTSJSONObject = {}  
        if (version !== null) header['version'] = version  
        if (token !== null) header['token'] = token  
        const url = `${APP_BASE_URL}${options.url}`  
        uni.request<ResponseData<T>>({  
            url,  
            data: options.data,  
            header,  
            method: options.method,  
            timeout: 6000,  
            success(originResponse : RequestSuccess<ResponseData<T>>) {  
                const originData = originResponse.data // ResponseData<T> | null  
                if (originData != null && originData.code != null) {  
                    // const code: number  
                    // const msg: string | null  
                    // const data: T | null  
                    const { code, msg, data } = originData  
                    if (`${code}` == '1') {  
                        if (data != null) {  
                            // const data: T & {}  
                            resolve((data as T))  
                            console.log('[ResponseData1] = ', data)  
                            return  
                        }  
                    } else {  
                        if (msg != null) {  
                            mShowToast(msg)  
                            reject(null)  
                            return  
                        }  
                    }  
                }  
                console.log('[ResponseData2] = ', originData)  
                mShowToast('网络错误')  
                reject(null)  
            },  
            fail(err : RequestFail) {  
                const { errCode } = err  
                console.log('[ResponseFail] = ', err)  
                mShowToast(`网络错误:${errCode}`)  
                reject(err)  
            }  
        })  
    })  
}  

export function post<T>(url = '', data : null | any = null): Promise<T> {  
    return request<T>({  
        url,  
        data  
    } as RequestOptions<T>)  
}  

export type BannerItem = {  
    title : string  
    file : string  
    type : string  
    type_id : number  
}  
export type ProductItem = {  
    id : number  
    title : string  
}  
export type ProductList = {  
    id : number  
    title : string  
    list : ProductItem[]  
}  
export type GetHomeDataResponse = {  
    banner : BannerItem[]  
    hot_product : ProductList[]  
    hot_casus : ProductList[]  
}  
export const getHomeDataApi = () : Promise<GetHomeDataResponse> => post<GetHomeDataResponse>(`${prefix}/home`)
2024-06-07 09:57 负责人:无 分享
已邀请:
电车

电车

有人找出问题了吗?

DCloud_UNI_BFC

DCloud_UNI_BFC

你好,我这边没有复现这个bug ,升级最新版hbuilderX 或者提供一下可复现的demo

  • 2***@qq.com

    复制代码就能复现这个问题

    2024-09-12 12:34

要回复问题请先登录注册