import { config } from './config.js'
import { tansParams } from "./util.js";

export const apiResquest = (prams) => {
	return new Promise((resolve, reject) => {
		let url = config.base_url + prams.url;
		uni.showLoading({
			title: '加载中',
			mask: true
		});
        // get请求映射params参数
        if (prams.method === 'GET' && prams.data) {
            url = url + '?' + tansParams(prams.data);
            url = url.slice(0, -1);
            prams.data = {};
        }

		return uni.request({
			url: url,
			data: {
				...prams.data
			},
			method: prams.method,
			header: {
			  	// 'content-type': 'application/x-www-form-urlencoded',
			  	'content-type': 'application/json;charset=utf-8',
				'Authorization':uni.getStorageSync('token')
			},
			success: (res) => {
				// 成功
				uni.hideLoading()
				if(res.data.code == 200){
					resolve(res);
				}else if(res.data.code == 401){
					uni.showToast({
						mask:true,
						icon:"none",
						position:"center",
						title: "登录超时,请重新登录~",
						duration: 2000
					});
                    uni.removeStorageSync('token');
                    uni.removeStorageSync('userId');
                    uni.removeStorageSync('userType');
					setTimeout(function(){
						uni.redirectTo({
							url: '/pages/login',
						});
					},2000);
				}else{
					uni.showToast({
						mask:true,
						icon:"none",
						position:"center",
						title: res.data.msg,
						duration: 2000
					});
					resolve(res);
				}
			},
			fail: (err) => {
				// 失败
				uni.hideLoading()
				uni.showToast({
					mask:true,
					icon:"none",
					position:"center",
					title: '出错啦~请联系管理员!',
					duration: 2000
				});
			},
			complete: () => {
				// 完成
			}
		});
	})
}
export const apiResquestForm = (prams) => {
    return new Promise((resolve, reject) => {
        let url = config.base_url + prams.url;
        uni.showLoading({
            title: '加载中',
            mask: true
        });
        // get请求映射params参数
        if (prams.method === 'GET' && prams.data) {
            url = url + '?' + tansParams(prams.data);
            url = url.slice(0, -1);
            prams.data = {};
        }
        return uni.request({
            url: url,
            data: {
                ...prams.data
            },
            method: prams.method,
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Authorization':uni.getStorageSync('token')
            },
            success: (res) => {
                // 成功
                uni.hideLoading()
                if(res.data.code == 200){
                    resolve(res);
                }else if(res.data.code == 401){
                    uni.showToast({
                        mask:true,
                        icon:"none",
                        position:"center",
                        title: "登录超时,请重新登录~",
                        duration: 2000
                    });
                    uni.removeStorageSync('token');
                    uni.removeStorageSync('userId');
                    uni.removeStorageSync('userType');
                    setTimeout(function(){
                        uni.redirectTo({
                            url: '/pages/login',
                        });
                    },2000);
                }else{
                    uni.showToast({
                        mask:true,
                        icon:"none",
                        position:"center",
                        title: res.data.msg,
                        duration: 2000
                    });
                    resolve(res);
                }
            },
            fail: (err) => {
                // 失败
                uni.hideLoading()
                uni.showToast({
                    mask:true,
                    icon:"none",
                    position:"center",
                    title: '出错啦~请联系管理员!',
                    duration: 2000
                });
            },
            complete: () => {
                // 完成
            }
        });
    })
}
export const apiResquestJsonList = (prams) => {
    return new Promise((resolve, reject) => {
        let url = config.base_url + prams.url;
        uni.showLoading({
            title: '加载中',
            mask: true
        });
        // get请求映射params参数
        if (prams.method === 'GET' && prams.data) {
            url = url + '?' + tansParams(prams.data);
            url = url.slice(0, -1);
            prams.data = {};
        }
        return uni.request({
            url: url,
            data: prams.data,
            method: prams.method,
            header: {
                'content-type': 'application/json',
                'Authorization':uni.getStorageSync('token')
            },
            success: (res) => {
                // 成功
                uni.hideLoading()
                if(res.data.code == 200){
                    resolve(res);
                }else if(res.data.code == 401){
                    uni.showToast({
                        mask:true,
                        icon:"none",
                        position:"center",
                        title: "登录超时,请重新登录~",
                        duration: 2000
                    });
                    uni.removeStorageSync('token');
                    uni.removeStorageSync('userId');
                    uni.removeStorageSync('userType');
                    setTimeout(function(){
                        uni.redirectTo({
                            url: '/pages/login',
                        });
                    },2000);
                }else{
                    uni.showToast({
                        mask:true,
                        icon:"none",
                        position:"center",
                        title: res.data.msg,
                        duration: 2000
                    });
                    resolve(res);
                }
            },
            fail: (err) => {
                // 失败
                uni.hideLoading()
                uni.showToast({
                    mask:true,
                    icon:"none",
                    position:"center",
                    title: '出错啦~请联系管理员!',
                    duration: 2000
                });
            },
            complete: () => {
                // 完成
            }
        });
    })
}
export const apiResquestFormVideo = (prams) => {
    return new Promise((resolve, reject) => {
        let url = uni.getStorageSync('cameraExtranetAgent') + prams.url;
        uni.showLoading({
            title: '加载中',
            mask: true
        });
        // get请求映射params参数
        if (prams.method === 'GET' && prams.data) {
            url = url + '?' + tansParams(prams.data);
            url = url.slice(0, -1);
            prams.data = {};
        }
        return uni.request({
            url: url,
            data: {
                ...prams.data
            },
            method: prams.method,
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Authorization':uni.getStorageSync('token')
            },
            success: (res) => {
                // 成功
                uni.hideLoading()
                if(res.data.code == 200){
                    resolve(res);
                }else if(res.data.code == 401){
                    uni.showToast({
                        mask:true,
                        icon:"none",
                        position:"center",
                        title: "登录超时,请重新登录~",
                        duration: 2000
                    });
                    uni.removeStorageSync('token');
                    uni.removeStorageSync('userId');
                    uni.removeStorageSync('userType');
                    setTimeout(function(){
                        uni.redirectTo({
                            url: '/pages/login',
                        });
                    },2000);
                }else{
                    uni.showToast({
                        mask:true,
                        icon:"none",
                        position:"center",
                        title: res.data.msg,
                        duration: 2000
                    });
                    resolve(res);
                }
            },
            fail: (err) => {
                // 失败
                uni.hideLoading()
                uni.showToast({
                    mask:true,
                    icon:"none",
                    position:"center",
                    title: '出错啦~请联系管理员!',
                    duration: 2000
                });
            },
            complete: () => {
                // 完成
            }
        });
    })
}
export const apiResquestTimer = (prams) => {
	return new Promise((resolve, reject) => {
		let url = config.base_url + prams.url;
        // get请求映射params参数
        if (prams.method === 'GET' && prams.data) {
            url = url + '?' + tansParams(prams.data);
            url = url.slice(0, -1);
            prams.data = {};
        }

		return uni.request({
			url: url,
			data: {
				...prams.data
			},
			method: prams.method,
			header: {
			  	// 'content-type': 'application/x-www-form-urlencoded',
			  	'content-type': 'application/json;charset=utf-8',
				'Authorization':uni.getStorageSync('token')
			},
			success: (res) => {
				// 成功
				uni.hideLoading()
				if(res.data.code == 200){
					resolve(res);
				}else if(res.data.code == 401){
					uni.showToast({
						mask:true,
						icon:"none",
						position:"center",
						title: "登录超时,请重新登录~",
						duration: 2000
					});
                    uni.removeStorageSync('token');
                    uni.removeStorageSync('userId');
                    uni.removeStorageSync('userType');
					setTimeout(function(){
						uni.redirectTo({
							url: '/pages/login',
						});
					},2000);
				}else{
					uni.showToast({
						mask:true,
						icon:"none",
						position:"center",
						title: res.data.msg,
						duration: 2000
					});
					resolve(res);
				}
			},
			fail: (err) => {
				// 失败
				uni.hideLoading()
				uni.showToast({
					mask:true,
					icon:"none",
					position:"center",
					title: '出错啦~请联系管理员!',
					duration: 2000
				});
			},
			complete: () => {
				// 完成
			}
		});
	})
}
