import {
    baseUrl,
    baseUrl1,
    baseUrl2,
    baseUrl3,
    baseUrl4,
    baseUrl5,
    baseUrl6,
    baseUrl7,
    baseUrl8
} from './config.js'

const request = {}
const headers = {}

request.globalRequest = (url, method, data, power, type) => {
    // 权限判断 因为有的接口请求头可能需要添加的参数不一样，所以这里做了区分
    // 1 == 不通过access_token校验的接口
    // 2 == 文件下载接口列表
    const userInfo = uni.getStorageSync('CAuserInfo')
    // console.log('缓存userinfo:',userInfo);
    switch (power) {
        case 1:
        case 3:
            headers['Token'] = userInfo.token
			// if(power == 3){
			// 	const deviceInfo = wx.getDeviceInfo()
			// 	let data = {
			// 		model: deviceInfo.model,
			// 		system: deviceInfo.system,
			// 		platform: deviceInfo.platform
			// 	}
			// 	headers['DeviceInfo'] = JSON.stringify(data)
			// }
            break;
        case 2:
            headers['Authorization'] = userInfo.userToken
            break;
        default:
            break;
    }
    let host = "";
    let key = "";
    if (type == 1) {
        host = baseUrl1;
    } else if (type == 2) {
        host = baseUrl2;
    } else if (type == 3) {
        host = baseUrl3;
    } else if (type == 4) {
        host = baseUrl4;
    } else if (type == 5) {
        host = baseUrl5;
    } else if (type == 7) {
        host = baseUrl7;
        key = `?PartnerKey=51xuanxiao&PartnerSecret=mC6XRjDWUzGAdxcCqRBWGb88uR`;
    } else if (type == 8) {
        host = baseUrl8;
        //key = `?AppKey=51xuanxiao&AppSecret=mC6XRjDWUzGAdxcCqRBWGb88uR`;
    } else {
        host = baseUrl;
    }
    return uni.request({
        timeout: 60000,
        url: host + url + key,
        method,
        data: data,
        dataType: 'json',
        header: headers
    }).then(res => {
		//console.log("ressss===="+JSON.stringify(res.data));	
		// console.log("type===="+type);	
        if (type == 7) {
            if (res[1].data.code == 0) {
                return res[1].data
            } else {
                throw res[1].data
            }
        } else if (type == 8) {
            if (res[1].data.Code == 0) {
                return res[1].data
            } else {
                throw res[1].data
            }
        } else {
            if (res.data.Result) {
                return res.data
            } else {
                throw res.data
            }
        }
    }).catch(parmas => {
        switch (parmas.status) {
            case 401:
                uni.showToast({
                    title: parmas.msg,
                    icon: 'none',
                    duration: 3500
                })
                uni.removeStorageSync("CAuserInfo");
                setTimeout(() => {
                    uni.reLaunch({
                        url: "/pages/index/index"
                    })
                }, 2800)
                break
            default:
				console.error(parmas);
                return Promise.reject()
                break
        }
    })
}
export default request

