const getResponseData = (response) => {
    switch (response.type) {
        case 0:
            return response["data"]; // 操作成功
        default: {
            uni.showToast({
                icon: "none",
                title: response.errorMsg,
            });
            return null;
        }
    }
};

export const ttLockRequest = (url, method, params) => {
    return new Promise(resolve => {
        uni.request({
            url: `https://cnapi.ttlock.com${url}`,
            header: {
                "content-type": "application/x-www-form-urlencoded",
            },
            data: params,
            method: method,
            success: (response) => {
                let result = null;
                switch (response.statusCode) {
                    case 200:
                        if (!!response.data && typeof response.data["errcode"] === "undefined") {
                            result = getResponseData({
                                data: response.data,
                                errorCode: -2,
                                errorMsg: "操作成功",
                                type: 0,
                            });
                        } else {
                            let errMsg = response.data["errcode"] === 0 ? "操作成功" : response.data["errmsg"];
                            result = getResponseData({
                                data: response.data,
                                errorCode: -2,
                                errorMsg: errMsg,
                                type: 0,
                            });
                        }
                        break;
                    default:
                        result = getResponseData({
                            errorCode: -2,
                            errorMsg: `服务器请求失败，状态码：${response.statusCode}`,
                            type: 2,
                        });
                        break;
                }
                resolve(result);
            },
            fail: (err) => {
                let result = getResponseData({
                    errorCode: -1,
                    errorMsg: "服务器请求失败，请检查服务器域名是否已被列入白名单",
                    type: 3,
                });
                resolve(result);
            },
        });
    });
};

export const ttLockRequest2 = (url, method, params) => {
    return new Promise(resolve => {
        uni.request({
            url: `https://api.ttlock.com${url}`,
            header: {
                "content-type": "application/x-www-form-urlencoded",
            },
            data: _makeParams(params),
            method: method,
            dataType: "json",
            success: (response) => {
                let result = null;
                switch (response.statusCode) {
                    case 200:
                        if (!!response.data && typeof response.data["errcode"] === "undefined") {
                            result = getResponseData({
                                data: response.data,
                                errorCode: -2,
                                errorMsg: "操作成功",
                                type: 0,
                            });
                        } else {
                            let errMsg = response.data["errcode"] === 0 ? "操作成功" : response.data["errmsg"];
                            result = getResponseData({
                                data: response.data,
                                errorCode: -2,
                                errorMsg: errMsg,
                                type: 0,
                            });
                        }
                        break;
                    default:
                        result = getResponseData({
                            errorCode: -2,
                            errorMsg: `服务器请求失败，状态码：${response.statusCode}`,
                            type: 2,
                        });
                        break;
                }
                resolve(result);
            },
            fail: (err) => {
                let result = getResponseData({
                    errorCode: -1,
                    errorMsg: "服务器请求失败，请检查服务器域名是否已被列入白名单",
                    type: 3,
                });
                resolve(result);
            },
        });
    });
};

function _generateParams(params) {
    if (!params) return {};
    for (let key of Object.keys(params)) {
        if (params[key] === null) {
            params[key] = undefined;
            continue;
        }
        const type = typeof params[key];
        switch (type) {
            case "function":
                {
                    params[key] = undefined;
                }
                break;
            case "object":
                {
                    params[key] = JSON.stringify(params[key]);
                }
                break;
            case "number":
            case "string":
            case "boolean":
            default:
                break;
        }
    }
    return JSON.parse(JSON.stringify(params));
}

function _makeParams(params) {
    return JSON.parse(
        JSON.stringify({
            ..._generateParams(params),
            clientId: "7946f0d923934a61baefb3303de4d132",
            accessToken: uni.getStorageSync("tt_access_token"),
            date: Date.now(),
        })
    );
}