request.js 1.8 KB
// export const baseUrl = 'http://192.168.1.136:9100'
export const baseUrl = 'http://longsi.t.brotop.cn'
function getHeaders() {
	let header = {
		"token": uni.getStorageSync('token') || '', // 'X-Requested-With': 'XMLHttpRequest',
		'Content-Type': 'application/json'
	}
	return header
}
function getHeader() {
	let header = {
		"token": uni.getStorageSync('token') || '', // 'X-Requested-With': 'XMLHttpRequest',
		'Content-Type': 'application/json',
	}
	return header
}
export default {
	get(url, param) {
		return new Promise((resolve, reject) => {
			uni.request({
				method: 'GET',
				url: baseUrl + url,
				data: param,
				header: getHeaders()
			}).then(res => { 
				if (res[1].data.code == 1) {
					resolve(res[1].data)
				} else if (res[1].data.msg.indexOf('过期') > -1 || res[1].data.msg.indexOf('请登录') > -1) {
					uni.showToast({
						icon: 'none',
						title: "请登录",
						duration: 3000
					})
					uni.navigateTo({
						url:'/pages/index/index'
					})
				}else{
					uni.showToast({
						icon: 'none',
						title: res[1].data.msg,  
						duration: 1300
					})
				}

			}).catch(
				(response) => {
					reject(response)
				}
			)
		})
	},

	post(url, param) {
		return new Promise((resolve, reject) => {
			uni.request({
				method: 'POST',
				url: baseUrl + url,
				data: param,   
				header: getHeader()
			}).then(res => {  
				if (res[1].data.code == 1) {
					resolve(res[1].data)
				} else if(res[1].data.code == 0){
					resolve(res[1].data)
				}
				else if (res[1].data.msg.indexOf('过期') > -1 || res[1].data.msg.indexOf('请登录') > -1) {
					uni.showToast({
						icon: 'none',
						title: "请登录",
						duration: 3000
					})
					uni.navigateTo({
						url:'/pages/index/index'
					})
				}
			}).catch((response) => {
				reject(response)
			})
		})
	},
}