class Http{
	get(baseUrl= '', url, data= {}, header= {}){
		return new Promise((resolve, reject)=>{
			uni.request({
				url: baseUrl + url,
				data: data,
				method: "GET",
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'Authorization': uni.getStorageSync('token') || '',
					...header
				},
				// 成功的回调
				success(res) {
					resolve(res)
				},
				fail(err) {
					reject(err)
				}
			})
		})
	}

	post(baseUrl= '', url, data= {}, header= {}){
		return new Promise((resolve, reject)=>{
			uni.request({
				url: baseUrl + url,
				data: data,
				method: "POST",
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'Authorization': uni.getStorageSync('token') || '',
					...header
				},
				// 成功的回调
				success(res) {
					resolve(res)
				},
				fail(err) {
					// console.log(err);
					reject(err)
				}
			})
		})
	}

	put(baseUrl= '', url, data= {}, header= {}){
		return new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl + url,
				data: data,
				method: "PUT",
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'Authorization': uni.getStorageSync('token') || '',
					...header
				},
				// 成功的回调
				success(res) {
					resolve(res)
				},
				fail(err) {
					reject(err)
				}
			})
		})
	}

	delete(baseUrl= '', url, data= {}, header= {}){
		return new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl + url,
				data: data,
				method: "DELETE",
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'Authorization': uni.getStorageSync('token') || '',
					...header
				},
				// 成功的回调
				success(res) {
					resolve(res)
				},
				fail(err) {
					reject(err)
				}
			})
		})
	}

	upload(baseUrl= '', url, data= {}, header= {}){
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: baseUrl + url,
				filePath: data,
				header: {
					'Authorization': uni.getStorageSync('token') || '',
					...header
				},
				name: 'file',
				// 成功的回调
				success(res) {
					resolve(res)
				},
				fail(err) {
					reject(err)
				}
			})
		})
	}
}

export default new Http()
