
let baseURL = ''

if (process.env.NODE_ENV==='development') {
	// baseURL = 'https://app.valuefrontier.cn:5002'
	baseURL = 'https://api.valuefrontier.cn:5002'
	// baseURL = 'http://43.143.189.195:5002'
} else{
	// baseURL = 'https://app.valuefrontier.cn:5002'
	baseURL = 'https://api.valuefrontier.cn:5002'
	// baseURL = 'http://43.143.189.195:5002'
}

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.showLoading({
			title:'加载中'
		})
		let baseUrl = baseURL
		if(url.indexOf('concept-api')>-1||url.indexOf('news-api')>-1||url.indexOf('report-api')>-1)
		{
			baseUrl = 'https://api.valuefrontier.cn'
		}
        uni.request({
        	url:baseUrl+url,
			data:params,
			header:{
				'Accept':'application/json',
				'Content-Type':'application/x-www-form-urlencoded',
				'Authorization':token?('Bearer '+token):''
			},
			method:"GET",
			success:(response) =>{
				console.log(response)
				uni.hideLoading()
				if (response.data.code==401) {
					uni.removeStorageSync('token')
					uni.navigateTo({
						url:'/pages/login/login',
					})
				} else{
					resolve(response.data)
				}
			},
			fail:(error) =>{
				uni.hideLoading()
				uni.showToast({
					title:'请求失败',
					icon:'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
		let isFile = 0
		if(params)
		{
			isJson = params.isJson
		}
		if(params)
		{
			isFile = params.isFile
		}
		uni.showLoading({
			title:'加载中'
		})
		if(isFile)
		{
			let token = uni.getStorageSync('token')
			uni.uploadFile({
				url: baseURL+url,
				filePath: params.avatar,
				name: 'avatar',
				header:{
					'Authorization':token?('Bearer '+token):''
				},
				formData:params,
				success: (response) => {
					console.log(response)
					uni.hideLoading()
					if (response.data.code==401) {
						uni.removeStorageSync('token')
						uni.navigateTo({
							url:'/pages/login/login'
						})
						reject(response.data)
					} else{
						resolve(response.data)
					}
				},
				fail: (error) => {
					console.log('上传失败'+error.errMsg)
					uni.hideLoading()
					uni.showToast({
						title:'请求失败',
						icon:'error'
					})
					reject()
				}
			});
		}else
		{
			let baseUrl = baseURL
			if(url.indexOf('concept-api')>-1)
			{
				baseUrl = 'https://api.valuefrontier.cn'
			}
			uni.request({
				url:baseUrl+url,
				data:params,
				header:{
					'Accept':'application/json',
					'Content-Type':isJson?'application/json':(isFile?'multipart/form-data':'application/x-www-form-urlencoded'),
					'Authorization':token?('Bearer '+token):''
				},
				method:"POST",
				success:(response) =>{
					uni.hideLoading()
					if (params&&params.isNotNeedLogin) {
						
					}else 
					{
						console.log(response.data)
						if (response.data.code==401) {
							uni.removeStorageSync('token')
							uni.navigateTo({
								url:'/pages/login/login'
							})
							reject(response.data)
						} else{
							resolve(response.data)
						}
					}
				},
				fail:(error) =>{
					uni.hideLoading()
					uni.showToast({
						title:'请求失败',
						icon:'error'
					})
					reject(error.response)
				}
			})
		}
    });
}

