var common = require('./common.js');
var storage = require('./storage.js');
var config = require('./config.js');
var helpConfig = require('./help_config.js');

// 检查token是否失效 如失效 清除缓存 提示重新登录
// 402: 解析token出错  403: token格式错误  404: 公共声明中缺少必要字段  405: token验证失败
function _checkTokenValid(res) {
    if (
        res.data.code === 402 ||
        res.data.code === 403 ||
        res.data.code === 404 ||
        res.data.code === 405 ||
        res.data.code === 300 ||
        res.data.code === 302 ||
        res.data.code === 303 ||
        res.data.code === 304 ||
        res.data.code === 305 ||
        res.data.code === 306
    ) {
        uni.clearStorageSync();
        uni.reLaunch({
            url: '/pages/login/login'
        });
        return false;
    }
    return true;
}
function getRequest(url, data, successCallBack, failCallBack) {
    if (!failCallBack) {
        failCallBack = function () {};
    }
    if (url.indexOf('?') > 0) {
        url = url + '&' + common.obj2UrlQuery(data);
    } else {
        url = '?' + common.obj2UrlQuery(data);
    }
    uni.request({
        url: url + '?token=' + storage.getApiToken(),
        method: 'GET',
        success: function (res) {
            uni.hideLoading();
            if (_checkTokenValid(res)) {
                successCallBack(res);
            }
        },
        fail: function (res) {
            uni.hideLoading();
            uni.showModal({
                title: '错误',
                content: '网络错误',
                showCancel: false
            });
            if (failCallBack) {
                failCallBack(res);
            }
        }
    });
}
function getHelpRequest(url, data, successCallBack, failCallBack) {
    if (!failCallBack) {
        failCallBack = function () {};
    }
    if (url.indexOf('?') > 0) {
        url = url + '&' + common.obj2UrlQuery(data);
    } else {
        url = '?' + common.obj2UrlQuery(data);
    }
    uni.request({
        url: url + '&token=' + storage.getApiToken(),
        method: 'GET',
        success: function (res) {
            uni.hideLoading();
            if (_checkTokenValid(res)) {
                successCallBack(res);
            }
        },
        fail: function (res) {
            uni.hideLoading();
            uni.showModal({
                title: '错误',
                content: '网络错误',
                showCancel: false
            });
            if (failCallBack) {
                failCallBack(res);
            }
        }
    });
}
function postRequest(url, data, successCallBack, failCallBack) {
    if (!failCallBack) {
        failCallBack = function () {};
    }
    if (url.indexOf('?') > 0) {
        if (storage.getApiToken() != '') {
            url = url + '&token=' + storage.getApiToken();
        }
    } else {
        console.log('nnnnn');
        if (storage.getApiToken() != '') {
            url = url + '?token=' + storage.getApiToken();
        }
        console.log(url);
    }
    uni.request({
        url: url,
        data: data,
        header: {
            'content-type': 'application/json',
            'X-Token': storage.getApiToken()
        },
        method: 'POST',
        success: function (res) {
            //wx.hideLoading();
            if (_checkTokenValid(res)) {
                successCallBack(res);
            }
        },
        fail: function (res) {
            uni.hideLoading();
            failCallBack(res);
        }
    });
}
function postHelpRequest(url, data, successCallBack, failCallBack) {
    if (!failCallBack) {
        failCallBack = function () {};
    }
    if (url.indexOf('?') > 0) {
        url = url + '&token=' + storage.getApiToken();
    } else {
        url = url + '?token=' + storage.getApiToken();
    }
    uni.request({
        url: url,
        data: data,
        header: {
            'content-type': 'application/json'
        },
        method: 'POST',
        success: function (res) {
            //wx.hideLoading();
            if (_checkTokenValid(res)) {
                successCallBack(res);
            }
        },
        fail: function (res) {
            uni.hideLoading();
            failCallBack(res);
        }
    });
}
function getApi(url, data, successCallBack, failCallBack) {
    getRequest(url, data, successCallBack, failCallBack);
}
function postApi(url, data, successCallBack, failCallBack) {
    postRequest(url, data, successCallBack, failCallBack);
}
function getAppConfig(callback = () => {}) {
    console.log('getAppConfig ...');
    //const accountInfo = uni.getAccountInfoSync();
    const pData = {
        appid: "wxddbcc3709026525e",
        terminal: 'wx_app'
    };
    getApi(helpConfig.API_INDEX_APP_CONFIG, pData, function (resp) {
        if (resp.data.code === 200) {
            const appConfig = resp.data.data.appConfig;
            storage.setAppConfig(appConfig);
            // wx.setNavigationBarTitle({
            //   title: appConfig.app_name,
            // })
            callback(appConfig);
        }
    });
}

/**
 * 上报formid
 */
function reportFormId(formId) {
    //const accountInfo = uni.getAccountInfoSync();
    var postData = {
        formId: formId,
        appid: "wxddbcc3709026525e"
    };
    postApi(config.API_FORMID_REPORT, postData, function () {});
}
module.exports = {
    getAppConfig: getAppConfig,
    getRequest: getRequest,
    getHelpRequest: getHelpRequest,
    postHelpRequest: postHelpRequest,
    postRequest: postRequest,
    getApi: getApi,
    postApi: postApi,
    reportFormId: reportFormId
};
