import config from '@/utils/config.js'

module.exports = (param) => {
	var url = param.url;
	var method = param.method;
	var header = param.header || {};
	var data = param.data || {};
	
	// 请求方式: GET POST 
	if(method){
		method = method.toUpperCase(); // 小写转成大写
		header = {'content-type':'application/x-www-form-urlencoded'}
	}
	
	// 发起请求 加载动画
	if(!param.hideLoading){
		uni.showLoading({title:"加载中..."})
	}
	
	// 发起网络请求
	uni.request({
		url: url,
		method:method || "GET",
		header:header,
		data: data,
		success: res => {
			// console.log('success', res)
			if(res.statusCode && res.statusCode != 200){ // api错误
				uni.showModal({
					content:res.statusCode + res.data
				})
				return;
			}
			if(res.data.code !== config.SUCCESS) {
				uni.showToast({
					icon:'none',
					title:res.data.msg,
					duration:2000
				})
			}
			typeof param.success == "function" && param.success(res.data);
		},
		fail: (e) => {
			console.log('fail', e)
			uni.showModal({
				content: e.errMsg,
				showCancel:false
			})
			typeof param.fail == "function" && param.fail(e.data);
		},
		complete: () => {
			// console.log("网络请求complete");
			uni.hideLoading();
			typeof param.complete == "function" && param.complete(e.data);
			return;
		}
	})
}