const server = 'http://127.0.0.1:8080';
// const server = 'http://192.168.88.34:8080';



import common from '../common/js/common.js';

function get(url, data) {
	return uni.request({
		method: 'get',
		url: server + url,
		data: data || {},
		header: {
			'Content-type': 'application/x-www-form-urlencoded',
			'satoken': uni.getStorageSync('token')
		}
	}).then(data => {
		common.hidingLoading();
		var [err, res] = data
		if (err) {
			return err
		} else {
			let resp=res.data;
			if(resp.code==401){
				uni.removeStorageSync('token');
				common.to('/pages/login/login');
				return ;
			}
			return res.data
		}
	})
}

function postForm(url, data) {
	common.showLoading('正在提交...')
	return uni.request({
		method: 'post',
		url: server + url,
		data: data || {},
		dataType: 'json',
		header: {
			'Content-type': 'application/x-www-form-urlencoded',
			'satoken': uni.getStorageSync('token')
		}
	}).then(data => {
		console.log(url)
		common.hidingLoading();
		var [err, res] = data
		if (err) {
			return err
		} else {
			let reData = res.data;
			if (reData.code !== 200) {
				uni.showToast({
					"icon": 'none',
					"title": reData.msg,
					"mask": false,
					"duration": 3000,
					"position": 'bottom'
				})
				return res.data;
			}
			return res.data
		}
	})
}

function postJson(url, data) {
	common.showLoading('正在提交...')
	return uni.request({
		method: 'post',
		url: server + url,
		data: JSON.stringify(data),
		dataType: 'json',
		header: {
			'Content-type': 'application/json; charset=utf-8',
			'satoken': uni.getStorageSync('token')
		}
	}).then(data => {
		common.hidingLoading();
		var [err, res] = data
		if (err) {
			return err
		} else {
			let reData = res.data;
			if (reData.code !== 200) {
				uni.showToast({
					"icon": 'none',
					"title": reData.msg,
					"mask": false,
					"duration": 3000,
					"position": 'bottom'
				})
				return res.data;
			}
			return res.data
		}
	})
}
function postJsonNotLoading(url, data) {
	return uni.request({
		method: 'post',
		url: server + url,
		data: JSON.stringify(data),
		dataType: 'json',
		header: {
			'Content-type': 'application/json; charset=utf-8',
			'satoken': uni.getStorageSync('token')
		}
	}).then(data => {
		common.hidingLoading();
		var [err, res] = data
		if (err) {
			return err
		} else {
			let reData = res.data;
			if (reData.code !== 200) {
				uni.showToast({
					"icon": 'none',
					"title": reData.msg,
					"mask": false,
					"duration": 3000,
					"position": 'bottom'
				})
				return res.data;
			}
			return res.data
		}
	})
}

function deleteFn(url, data) {
	common.showLoading('正在删除...')
	return uni.request({
		method: 'delete',
		url: server + url,
		data: data || {},
		dataType: 'json',
		header: {
			'Content-type': 'application/x-www-form-urlencoded',
			'satoken': uni.getStorageSync('token')
		}
	}).then(data => {
		common.hidingLoading();
		var [err, res] = data
		if (err) {
			return err
		} else {
			return res.data
		}
	})
}

function put(url, data) {
	common.showLoading('正在提交...')
	return uni.request({
		method: 'put',
		url: server + url,
		data: JSON.stringify(data),
		dataType: 'json',
		header: {
			'Content-type': 'application/json; charset=utf-8',
			'satoken': uni.getStorageSync('token')
		}
	}).then(data => {
		common.hidingLoading();
		var [err, res] = data
		if (err) {
			return err
		} else {
			return res.data
		}
	})
}

export default {
	get,
	deleteFn,
	put,
	postForm,
	postJson,
	postJsonNotLoading,
	server
}
