import { DEV_BASE_URL, BASE_URL } from '@/config'

const baseRequest = async (url, method, data = {}, type = 1) => {
	const header = {
		'content-type': 'application/json',
	}
	return new Promise((reslove, reject) => {
		uni.showLoading({
			title: '',
			mask: true
		})
		const baseUrl = type == 1 ? DEV_BASE_URL : BASE_URL
		uni.request({
			url: baseUrl + url,
			method: method || 'GET',
			header,
			timeout: 10000,
			data: data || {},
			success: (successData) => {
				uni.hideLoading();
				const res = successData.data
				if (successData.statusCode == 200) {
					// 业务逻辑，自行修改
					if (res.code === 499) {
						setTimeout(() => {
							uni.clearStorageSync();
							uni.reLaunch({
								url: '/pages/index/index'
							})
						}, 1500)
					} else if (res.code === 200 || res.code === 0 || res.status === 200) {
						reslove(res)
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						reject(res)
					}
				} else {
					uni.showToast({
						title: res.message || '网络连接失败，请稍后重试',
						icon: 'none'
					})
					reject(res)
				}
			},
			fail: (msg) => {
				uni.hideLoading();
				uni.showToast({
					title: res.message || '网络连接失败，请稍后重试',
					icon: 'none'
				})
				reject(message)
			},
			complete() {

			}
		})
	})
}


export {
	baseRequest
}