import {
	baseUrl
} from "@/config"
import platform from "@/utils/platform"
import getEnv from "@/utils/env" // 获取环境变量
import store from "@/store"
import {
	wipeNulish
} from '@/utils/utils.js'

function requestHandle(obj) {
	let header = obj.header || {}
	const reqData = wipeNulish(obj.data) || {}
	const accessToken = uni.getStorageSync('accessToken') || ''
	const tokenType = uni.getStorageSync('tokenType') || ''
	const Authorization = uni.getStorageSync('Authorization') || ''
	const customRes = obj.customRes
	const plat = platform()
	const env = getEnv()
	let base = (plat === 'H5' && env === 'dev') ? '/api' : baseUrl
	const BASE_URL = obj.url.indexOf("http") > -1 || obj.url.indexOf("https://") > -1 ? obj.url : base + obj.url;
	Object.assign(header, {
		Authorization: Authorization,
		accessToken:accessToken,
		platform: plat
	})
	if (plat === 'APP-PLUS') {
		Object.assign(header, store.state.info.versionInfo)
	}
	header.channel = 2 // 渠道-提货卡
	if (obj.loading) {
		uni.showLoading({
			mask: true
		})
	}
	return new Promise((resolve, reject) => {
		uni.request({
			method: obj.method,
			url: BASE_URL,
			data: reqData,
			header: header,
			dataType: "json"
		}).then(response => {
			uni.hideLoading()
			let [error, {
				data: {
					data,
					code,
					msg
				}
			}] = response
			if (error) {
				uni.showToast({
					title: "服务器炸了",
					icon: 'none'
				})
			}
			if (customRes) {
				resolve(response[1].data)
			} else if (code === 5001) { // 登录过期
				uni.removeStorageSync('accessToken')
				uni.removeStorageSync('tokenType')
				uni.removeStorageSync('userInfo')
				uni.removeStorageSync('currentBuyerAddr')
				// uni.removeStorageSync('payOpenId')
				const pages = getCurrentPages()
				if (pages[pages.length - 1].$page.fullPath.includes('login')) {
					return
				}
				uni.showToast({
					title: msg || '登录已过期',
					icon: 'none',
					mask: true
				})
				setTimeout(() => {
					uni.reLaunch({
						url: '/pages/login/index'
					})
				}, 1000)
			} else if (code !== 200) {
				uni.showToast({
					title: msg || '服务器开小差',
					icon: 'none'
				})
			} else {
				resolve(data)
			}
		}).catch(error => {
			uni.hideLoading()
			/**获取网络状态 */
			uni.getNetworkType({
				success: function(res) {
					if (res.networkType === 'none') {
						console.log('网络未连接，请检查网络状态')
						uni.showToast({
							title: '网络未连接，请检查网络状态',
							mask: false,
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: '网络连接超时',
							icon: 'none'
						})
					}
				}
			})
			reject(error)
		})
	})
}
// customRes 用于自定义反参状态处理，凡是未加此参数 都当作反参code是0处理
const request = {
	get: (obj) => {
		return new Promise((resolve, reject) => {
			requestHandle({
					method: 'GET',
					url: obj.url,
					data: obj.data,
					header: obj.header,
					customRes: obj.customRes,
					loading: obj.loading
				})
				.then((res) => {
					resolve(res)
				})
				.catch((err) => {
					reject(err)
				})
		});
	},
	post: (obj) => {
		return new Promise((resolve, reject) => {
			requestHandle({
					method: 'POST',
					url: obj.url,
					data: obj.data,
					header: obj.header,
					customRes: obj.customRes,
					loading: obj.loading
				})
				.then((res) => {
					resolve(res)
				})
				.catch((err) => {
					reject(err)
				})
		})
	},
	put: (obj) => {
		return new Promise((resolve, reject) => {
			requestHandle({
					method: 'PUT',
					url: obj.url,
					data: obj.data,
					header: obj.header,
					customRes: obj.customRes,
					loading: obj.loading
				})
				.then((res) => {
					resolve(res)
				})
				.catch((err) => {
					reject(err)
				})
		})
	},
	del: (obj) => {
		return new Promise((resolve, reject) => {
			requestHandle({
					method: 'DELETE',
					url: obj.url,
					data: obj.data,
					header: obj.header,
					customRes: obj.customRes,
					loading: obj.loading
				})
				.then((res) => {
					resolve(res)
				})
				.catch((err) => {
					reject(err)
				})
		})
	}
}

export default request