
let baseURL = ''

if (process.env.NODE_ENV==='development') {
	// baseURL = 'https://admin.zleniao.com'
	baseURL = 'https://app.valuefrontier.cn'
} else{
	// baseURL = 'https://admin.zleniao.com'
	baseURL = 'https://app.valuefrontier.cn'
}

export function getBaseURL() {
	return baseURL
}

/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params){    
    return new Promise((resolve, reject) =>{    
		let token = uni.getStorageSync('token')
        uni.request({
        	url:baseURL+url,
			data:params,
			header:{
				'Accept':'application/json',
				'Content-Type':'application/x-www-form-urlencoded',
				'token':token?token:''
			},
			method:"GET",
			success:(response) =>{
				console.log(response)
				if (response.data.code==-200) {
					uni.navigateTo({
						url:'/pages/login/login',
					})
				} else{
					resolve(response.data)
				}
			},
			fail:(error) =>{
				reject(error.response)
			}
        })
	});
}

/** 
 * post方法，对应post请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function post(url, params) {
    return new Promise((resolve, reject) => {
		let token = uni.getStorageSync('token')
		let isJson = 0
		if(params)
		{
			isJson = params.isJson
		}
        uni.request({
        	url:baseURL+url,
        	data:params,
        	header:{
        		'Accept':'application/json',
        		'Content-Type':isJson?'application/json':'application/x-www-form-urlencoded',
				'token':token?token:''
        	},
        	method:"POST",
        	success:(response) =>{
				console.log(JSON.parse(JSON.stringify(response)))
				if(response.header.token&&!params.isNotSaveToken)
				{
					uni.setStorageSync('token',response.header.token)
				}
				if (params&&params.isNotNeedLogin) {
					
				}else 
				{
					if (response.data.code==-200) {
						uni.removeStorageSync('token')
						uni.navigateTo({
							url:'/pages/login/login'
						})
						reject(response.data)
					} else{
						resolve(response.data)
					}
				}
        	},
        	fail:(error) =>{
				uni.showToast({
					title:'网络请求失败',
					icon:'none'
				})
        		reject(error.response)
        	}
        })
    });
}

