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
		}))
}

class Https {
	request(url, method, data,loading) {
		var loading=loading||false
		if(!loading){
			uni.showLoading({
				title: '加载中'
			})
		}
		
		var token = uni.getStorageSync('token')
		// var token = "df057579-73c8-4bdd-8312-f64db8efd699"
		return new Promises((resolve, reject) => {
			uni.request({
				// // #ifdef H5
				// url:'/apid'+url,
				// // #endif

				url: host + '/' + url, //
				method: method,
				data: data,
				header: {
					// application/json
					'content-type': 'application/json', // 默认值
					'Authorization': token || ''
				},
				success: function(res) {
					// console.log(res.data.code,98765)
					if(!loading){
						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.clearStorage()
						uni.reLaunch({
							url: '/pages/login/login'
						})
					}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)
		var token = uni.getStorageSync('token')
		let urls = ''
		return new Promises((resolve, reject) => {
			console.log(params.data)
			uni.uploadFile({
				url: url + '/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)
		var 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)
	}
	dele(url, data) {
		return this.request(url, 'delete', data)
	}
	post(url, data,loading) {
		return this.request(url, 'POST', data,loading)
	}
	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()

export default https
  