import {errCodeMsgKV} from "@/utils/fetch/errCode";

/**
 * 处理状态码不为200的响应
 */
export function handleHttpError(err) {
    const msg = errCodeMsgKV[err.code] ? errCodeMsgKV[err.code] + ':' + err.errMsg : err.errMsg;
    uni.showToast({
        title: msg,
        icon: "none",
        duration: 2000,
    });
    return Promise.reject(msg);
}


/**
 * 处理http响应码为200的响应
 * @return {Promise<unknown>}
 */
export function handleNormalResponse(resList) {
    const res = resList[0]
    return res && new Promise((resolve, reject) => {
        if (res.code || res.code === 0) {
            switch (res.code) {
                case 200:
                    resolve(res);
                    break;
                case 0:
                    resolve(res);
                    break;
                default:
                    reject(res);
                    uni.showToast({
                        title: res.msg,
                        icon: "none",
                        duration: 2000,
                    });
            }
        } else {
            reject(res);
            uni.showToast({
                title: "服务异常",
                icon: "none",
                duration: 2000,
            });
        }
    });
}