let Promises = Promise
import url from "@/util/url";
let {host} = url

Promises.prototype.finally = function(callback) {
	let P = this.constructor
	return this.then(
		value => P.resolve(callback()).then(() => value),
		reason => P.resolve(callback()).then(() => {
			throw reason
		}))
}
const _GET={
	 clear:true
 }
class Https {
	request(url, method, data,type) {
		uni.showLoading({
			title: '加载中'
		})
		const token = uni.getStorageSync('token')
		// application/json
		var contentType='application/json'
		
		type=type||false
		
		if(type){
			contentType='application/x-www-form-urlencoded'
		}
		return new Promises((resolve, reject) => {
			uni.request({
				url: host + '/' + url, //
				method: method,
				data: data,
				header: {
					// application/json
					'content-type': contentType, // 默认值
					'Authorization': token || ''
				},
				success: function(res) {
					// console.log(res.data.code,98765)
					uni.hideLoading()
					if (res.statusCode == 401) {
						// uni.navigateTo({
						// 	url: '/pages/load/roles'
						// })
					}else if(res.data.code == '00040000'){
						uni.showToast({
							title: res.data.message,
							duration: 1000,
							icon: 'none'
						});
					}else if(res.code == 1002){
						uni.navigateTo({
							url: '/pages/load/roles'
						})
					}else if(res.data.code == 1002){
						uni.navigateTo({
							url: '/pages/load/roles'
						})
					}else if(res.data.code == 401){
						// uni.navigateTo({
						// 	url: '/pages/load/roles'
						// })
					}else if(res.code == 401){
						// uni.navigateTo({
						// 	url: '/pages/load/roles'
						// })
					}
					else{
						resolve(res.data)
					}
				},
				fail: function(error) {
					uni.hideLoading()
					reject(error)
				}
			})
		})
	}
	upLoad(params) {
		console.log(params)
		const token = uni.getStorageSync('token')
		let urls = ''
		return new Promises((resolve, reject) => {
			console.log(params.data)
			uni.uploadFile({
				// https://qsxyakm.qs163.cn
				// https://yxxy.qs163.cn
				url: 'https://qsxyakm.qs163.cn/prod-api/common/upload', // 
				filePath: params.path,
				data: params,
				header: {
					'content-type': 'multipart/form-data',
					'Authorization': token
				},
				formData: {
					'token': token
				},
				name: params.name || 'file',
				success: function(res) {
					console.log(res.data,9875)
					urls = JSON.parse(res.data); 
					let urliu = urls.url
					console.log(urliu)
					let url = urls.url
					resolve(url)
				},
				fail: function(error) {
					reject(error)
				}
			})
		})
	}
	upDown(params) {
		console.log(params)
		const token = uni.getStorageSync('token')
		return new Promises((resolve, reject) => {
			uni.uploadFile({
				url: host + '/common/download', // 
				header: {
					'Authorization': token
				},
				filePath: params.path,
				name: params.name || 'img',
				success: function(res) {
					console.log(res,4)
					resolve(res.data)
				},
				fail: function(error) {
					reject(error)
				}
			})
		})
	}
	get(url, data) {
		return this.request(url, 'GET', data)
	}
	post(url, data) {
		return this.request(url, 'POST', data)
	}
	posta(url, data) {
		return this.request(url, 'POST', data,true)
	}
	put(url, data) {
		return this.request(url, 'put', data)
	}
	uniApi(params, data) {
		return new Promises((resolve, reject) => {
			params.events({
				...data,
				success(res) {
					resolve(res)
				},
				fail(err) {
					reject(err)
				}
			})
		})
	}
}

const https = new Https()
const self={
	_GET,https
}
export default self
