const tt = (e) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve({ timeout: e });
        }, e);
    });
};

const api = {
    get(url, data, timeout = 60000) {
        return new Promise((resolve, reject) => {
            const controller = new AbortController();
            const { signal } = controller;
            const param = Object.keys(data).map(x => `${x}=${encodeURIComponent(data[x])}`).join('&');
            Promise.race([tt(timeout), fetch(`${import.meta.env.VITE_API_URL}${url}?${param}`, {
                headers: {
                    'token': localStorage.getItem('token') || '',
                },
                signal: signal,
            })]).then(rep => {
                if (rep.timeout) {
                    controller.abort();
                    reject({ errcode: -1, errmsg: '网络超时' });
                }
                else if (rep.status == 404)
                    reject({ errcode: -1, errmsg: '接口不存在' });

                rep.json().then(data => {
                    if (data.errcode) {
                        if ([41001, 40001, 40002].includes(data.errcode)) {
                            localStorage.removeItem('token');
                            location.replace('#/Login');
                            location.reload();
                        }
                        reject(data);
                    }
                    else
                        resolve(data.data);
                }).catch(err => {
                    reject({ errcode: -1, errmsg: '网络异常' });
                });
            }).catch(err => {
                reject({ errcode: -1, errmsg: '网络异常' });
            });
        });
    },
    post(url, data, index, size, timeout = 60000) {
        return new Promise((resolve, reject) => {
            const controller = new AbortController();
            const { signal } = controller;
            const postData = {
                data: data,
                ts: new Date() - 0,
            };
            if (index)
                postData.PageIndex = index;
            if (size)
                postData.PageSize = size;
            Promise.race([tt(timeout), fetch(`${import.meta.env.VITE_API_URL}${url}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Accept': 'application/json',
                    'token': localStorage.getItem('token') || '',
                },
                body: JSON.stringify(postData),
                signal: signal,
            })]).then(rep => {
                if (rep.timeout) {
                    controller.abort();
                    reject({ errcode: -1, errmsg: '网络超时' });
                }
                else if (rep.status == 404)
                    reject({ errcode: -1, errmsg: '接口不存在' });

                rep.json().then(data => {
                    if (data.errcode) {
                        if ([41001, 40001, 40002].includes(data.errcode)) {
                            localStorage.removeItem('token');
                            location.replace('#/Login');
                            location.reload();
                        }
                        reject(data);
                    }
                    else
                        resolve(data.data);
                }).catch(err => {
                    reject({ errcode: -1, errmsg: '网络异常' });
                });
            }).catch(err => {
                reject({ errcode: -1, errmsg: '网络异常' });
            });
        });
    },
}
export default api;