import $ from "../utils/Tool.js";
import errorRequest from "./errorRequest.js";
const test = "http://192.168.0.47:52015"; //192.168.12.20 172.16.36.245
const API_BASE_URL = "https://duoduoenv.sagacloud.cn";
function get(url, data = {}, testenv) {
    let _url;
    if (testenv) {
        _url = test + url.replace("/server", "");
    } else {
        _url = API_BASE_URL + url;
    }

    data = Object.assign(data, {
        pubname: "sagacare",
        openid: $.storage.get("openId"),
    });
    return new Promise(function (resolve, reject) {
        wx.request({
            url: _url,
            method: "GET",
            data: data,
            header: {
                "content-type": "application/json",
            },
            success: function success(request) {
                if (request.statusCode >= 400) {
                    console.log(
                      'systemLog/save接口请求失败',request
                    );
                    errorRequest({
                        name: _url, // 类型：String  必有字段  备注：接口名字
                        input: JSON.stringify(data) || '', // 类型：String  必有字段  备注：请求输入参数
                        exception: JSON.stringify(request.errMsg) // 类型：String  必有字段  备注：异常信息
                      });
                    reject(request.errMsg);
                  } else{
                    resolve(request.data);
                  }

            },
            fail: function fail(error) {
                wx.showModal({
                    title: "网络错误",
                    content: "网络出错，请刷新重试",
                    showCancel: false,
                });
                errorRequest({
                    name: _url, // 类型：String  必有字段  备注：接口名字
                    input: JSON.stringify(data) || '', // 类型：String  必有字段  备注：请求输入参数
                    exception: JSON.stringify(error), // 类型：String  必有字段  备注：异常信息
                });
                reject(error);
            },
            complete: function complete() {},
        });
    });
}

function post(url, data = {}, testenv) {
    let _url;
    if (testenv) {
        _url = test + url.replace("/server", "");
    } else {
        _url = API_BASE_URL + url;
    }
    data = Object.assign(data, {
        openid: $.storage.get("openId"),
    });
    _url = _url + `?pubname=sagacare&openid=${$.storage.get("openId")}`;
    if (data.flag) {
        _url = _url + `&flag=${data.flag}`;
    }
    return new Promise(function (resolve, reject) {
        wx.request({
            url: _url,
            method: "POST",
            data: data,
            header: {
                "content-type": "application/json",
            },
            success: function success(request) {
                if (request.statusCode >= 400) {
                    console.log(
                      'systemLog/save接口请求失败',request
                    );
                    errorRequest({
                        name: _url, // 类型：String  必有字段  备注：接口名字
                        input: JSON.stringify(data) || '', // 类型：String  必有字段  备注：请求输入参数
                        exception: JSON.stringify(request.errMsg) // 类型：String  必有字段  备注：异常信息
                      });
                    reject(request.errMsg);
                  } else{
                    resolve(request.data);
                  }
            },
            fail: function fail(error) {
                wx.showModal({
                    title: "网络错误",
                    content: "网络出错，请刷新重试",
                    showCancel: false,
                });
                errorRequest({
                    name: _url, // 类型：String  必有字段  备注：接口名字
                    input: JSON.stringify(data) || '', // 类型：String  必有字段  备注：请求输入参数
                    exception: JSON.stringify(error) // 类型：String  必有字段  备注：异常信息
                  });
                reject(error);
            },
            complete: function complete() {},
        });
    });
}
module.exports = {
    http: { get, post },
};
