import tools from '@/tools/tools.js'
import store from '../store'
let baseUrl = "http://tc.pcxbc.com"
const httpRequest = (url, method = "get", data) => {
	data.token = uni.getStorageSync('token')
	data.aan_id = uni.getStorageSync('aan_id')
	let httpDefaultOpts = {
		url: baseUrl + url,
		data: data,
		method: method,
		header: {
			'X-Requested-With': 'XMLHttpRequest',
			'content-type': 'application/json;charset=UTF-8',
		},
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				uni.hideLoading()
				console.log("request", url, res, data)
				if (res[1].data.code == 20001) {
					uni.clearStorageSync()
					uni.setStorageSync('token', null)
					uni.setStorageSync('aan_id', null)
					setTimeout(function() {
						uni.reLaunch({
							url: "/pages/login/login"
						})
					}, 2000)
					return
				}
				if (res[1].statusCode != 200) {
					tools.msg(res[1].data.msg)
					return
				}
				resolve(res[1].data)
			}
		).catch(
			(response) => {
				uni.hideLoading()
				reject(response)
			}
		)
	})
	return promise

};
const get = (url, data) => {
	return httpRequest(url, 'get', data)
}

const post = (url, data) => {
	return httpRequest(url, 'post', data)
}
export default {
	baseUrl,
	get,
	post
}