// 维修工
export default {
	common:{
		header:{
			'token':'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDY5OTAzNzMsInVzZXJJZCI6IjEwMDAifQ.ImEj4qgkj0vYFc07ztJQIO9ESs3RHAmcYRQYrEOH6kk',
		},
		method:'POST',
		token:false
	},
	request(options={}){
		options.url = options.url
		options.header = options.header || this.common.header
		options.method = options.method || this.common.method
		options.token = options.token === true ? true : this.common.token
		
		return new Promise((res,rej)=>{
			if (options.token){
				let token =uni.getStorageSync('access_token')
				if (!token) {
					uni.showToast({
						title:'请先登录',
						icon:'none'
					});
					uni.navigateTo({
						url:"/pages/login/login.vue"
					})
					return rej("请先登录")
				}
				options.header.access_token = token
			}
			uni.request({
				...options,
				success: (result) => {
					let data = result.data;
					if (!data) {
						uni.showToast({
							title:data.msg || '服务端失败',
							icon:'none' 
						});
						return rej(data)
					} else {
						res(data)
					
					}
				},
				fail: (error) => {
					uni.showToast({
						title:error.errMsg ||'请求失败',
						icon: 'none'
					});
					return rej(error)
				}
			});
		})
	},
	post(url,data = {},options = {}){
		options.url = url
		options.method = 'POST'
		return this.request(options)
	}
}