import { toast } from '@/utils/fun.js';

export const proxyObj = {
	// 开发版
	development: 'https://apidjk.jitujt.com',
	// development: 'http://192.168.1.62:48080',
	// development: 'http://192.168.1.191:48080',
	// 正式版
	production: 'https://apidjk.jitujt.com',
}

interface shortcutsType {
	url : string
	params : any
	method : any
}



export const http = ({ url, params, method } : shortcutsType) => {
	return new Promise((resolve, reject) => {
		let Authorization : string = uni.getStorageSync('Authorization') || ''
		let header = {
			Authorization,
			"Tenant-id": "134",
		}
		uni.request({
			url: proxyObj[process.env.NODE_ENV || 'development'] + url,
			data: params || {},
			method: method || 'POST',
			header: header,
			success: (res : any) => {
				const data = res.data
				if (data.code == 401 || res.statusCode == 401) {
					uni.removeStorageSync('Authorization');
					uni.removeStorageSync('userInfo');


					uni.showModal({
						title: '提示',
						content: `${Authorization ? '登录失效' : '你还未登录'},是否前往登录?`,
						success: function (res) {
							if (res.confirm) {
								// uni.clearStorageSync();
								uni.navigateTo({
									url: '/pages/login/login'
								})
							} else if (res.cancel) {
								uni.clearStorageSync();
								console.log('用户点击取消');
							}
						}
					});
					reject(data)
					return
				}
				if (data.code === 0) {
					resolve(data.data || null)
				} else {
					toast(data.msg)
					reject(data)
				}
			},
			fail: (err) => {
				reject(err)
			}
		})
	});
};