let serverUrl = "https://broadcast.waityou24.cn";//http://127.0.0.1:15000
let wechatUrl = 'https://broadcast.waityou24.cn';

const getDomain = (uri) => {
    if (uri.startsWith('/wechat/api')) return wechatUrl;
    return serverUrl;
}

const $get = (uri, data) => {
    let app = getApp();
    let baseUrl = getDomain(uri);
    return new Promise((resolve, reject) => {
        wx.request({
            url: `${baseUrl}${uri}`,
            method: "get",
            data,
            header: {},
            success: (res) => toResolve(resolve, res),
            error: (e) => toReject(reject, e),
        });
    });
};
const $post = (uri, data) => {
    let app = getApp();
    let baseUrl = getDomain(uri);
    return new Promise((resolve, reject) => {
        wx.request({
            url: `${baseUrl}${uri}`,
            method: "post",
            data,
            header: {},
            success: (res) => toResolve(resolve, res),
            error: (e) => toReject(reject, e),
        });
    });
};
const $delete = (uri, data) => {
    let app = getApp();
    let baseUrl = getDomain(uri);
    return new Promise((resolve, reject) => {
        wx.request({
            url: `${baseUrl}${uri}`,
            method: "delete",
            data,
            success: (res) => toResolve(resolve, res),
            error: (e) => toReject(reject, e),
        });
    });
};

const toResolve = (resolve, result) => {
    if (result.statusCode === 200) resolve(result.data);
    else {
        console.error("请求失败");
    }
};

const toReject = (reject, result) => {
    console.error(result);
    reject(result);
};

module.exports = {
    $get,
    $post,
    $delete
};