// let apiUrl = 'https://test.api.zanxiangnet.com/miniapp'
let apiUrl = 'https://api.zanxiangnet.com/miniapp'



let isopen = false  // 控制loading
const openLoading = () => {
    if (!isopen) {
        isopen = true
        uni.showLoading({
            title: '加载中'
        });
    }
}
const hiddenLoading = () => {
    isopen = false
    uni.hideLoading()
}
export function request(props) {
	let {
		url,
		data,
		config,
		method,
		backspace = false,
		isLoading = true,
		isToast = true
	} = props
	let _loading = false
	let reqTimer = null
	if (isLoading) {
		reqTimer = setTimeout(() => {
			openLoading()
			_loading = true
		}, 500)
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${apiUrl}${url}`,
			data,
			method: method || 'GET',
			success: (res) => {
				console.log("请求返回数据---》", res)
				reqTimer && clearTimeout(reqTimer)
				if (_loading) {
					hiddenLoading()
				}
				if (res.data.code === 500) {
					uni.showToast({
					    title: res.data.msg || `${apiUrl}${url}请求错误`,
						icon: 'none',
					    duration: 2000
					})
					return
				}
				resolve(res.data)
			},
			fail: (err) => {
				reqTimer && clearTimeout(reqTimer)
				if (_loading) {
					hiddenLoading()
				}
				reject(`${url}接口调用失败${err}`)
			}
		})
	})
}
