import refreshToken from "../api/refreshToken";

// let requestPath = 'http://192.168.0.102:8059/xgf_gwj_2.0/'; // 后台请求地址
let requestPath = 'http://127.0.0.1:8059/xgf_gwj_2.0/'; // 后台请求地址
// let requestPath = 'https://skqhdg.porthebei.com:9006/qa-prevention-xgf/'; // 后台请求地址
// let requestPath = 'https://qgxgf.qhdsafety.com/qa-prevention-xgf/'; // 外网地址
import store from '../store/index'
import {setRefreshToken} from "../api/api";
import {decryptAes} from "@/utils/aes_secret";

function post(url, data) {
    return new Promise((resolve, reject) => {
        if (data && data.loading !== false) {
            uni.showLoading({
                title: '加载中'
            });
        }
        uni.request({
            url: requestPath + url,
            data: {
                USER_ID: store.state.userInfo.USER_ID || '',
                CORPINFO_ID: store.state.userInfo.CORPINFO_ID || '',
                ...data
            },
            method: 'POST',
            header: {
                'Content-type': data?.postMethod || 'application/x-www-form-urlencoded',
                'token': store.state.userInfo.token  || ''
            },
            success: (res) => {
                res.data = decryptAes(res.data);
				if (res.statusCode != 200){
				    uni.showToast({
				        title: '网络错误请重试',
				        icon: 'error',
				        duration: 2000
				    });
				    reject(res)
				}
                if (data && data.loading !== false) {
                    uni.hideLoading();
                }
                if (res.data.result === 'success') {
                    resolve(res.data)
                } else {
                    uni.showToast({
                        title: res.data.msg || '系统开小差了',
                        icon: 'none',
                        duration: 2000
                    });
                    reject(res.data)
                }
            },
            fail: (err) => {
                if (data && data.loading !== false) {
                    uni.hideLoading();
                }
                uni.showToast({
                    title: '网络错误请重试',
                    icon: 'none',
                    duration: 2000
                });
                reject(err)
            }
        });
    })
}

function upload(url, data) {
    return new Promise((resolve, reject) => {
        if (data && data.loading !== false) {
            uni.showLoading({
                title: '加载中'
            });
        }
        uni.uploadFile({
            url: requestPath + url,
            filePath: data.filePath,
            name: data.name || 'file',
            formData: data.formData || {},
            header: {
                'token': store.state.userInfo.token || ''
            },
            success: (res) => {
                res.data = decryptAes(res.data);
                uni.hideLoading();
                if (JSON.parse(res.data).result === 'success') {
                    resolve(JSON.parse(res.data))
                } else {
                    uni.showToast({
                        title: JSON.parse(res.data).msg || '系统开小差了',
                        icon: 'none',
                        duration: 2000
                    });
                    reject(JSON.parse(res.data))
                }
            },
            fail: (err) => {
                uni.hideLoading();
                uni.showToast({
                    title: '网络错误请重试',
                    icon: 'none',
                    duration: 2000
                });
                reject(err)
            }
        });
    })
}

function uploads(url, data) {
    return new Promise((resolve, reject) => {
        if (data && data.loading !== false) {
            uni.showLoading({
                title: '加载中'
            });
        }
        uni.uploadFile({
            url: requestPath + url,
            files: data.files,
            formData: data.formData || {},
            header: {
                'token': store.state.userInfo.token || ''
            },
            success: (res) => {
                res.data = decryptAes(res.data);
                uni.hideLoading();
                if (JSON.parse(res.data).result === 'success') {
                    resolve(JSON.parse(res.data))
                } else {
                    uni.showToast({
                        title: JSON.parse(res.data).msg || '系统开小差了',
                        icon: 'none',
                        duration: 2000
                    });
                    reject(JSON.parse(res.data))
                }
            },
            fail: (err) => {
                uni.hideLoading();
                uni.showToast({
                    title: '网络错误请重试',
                    icon: 'none',
                    duration: 2000
                });
                reject(err)
            }
        });
    })
}

export {post, upload, uploads}
