const ip = 'http://192.168.88.34:8080'; //线下
//const ip = 'http://114.118.9.159:8080/prod-api'; //线上
/**
 * 封装的http请求
 */
const request = (opt) => {
	opt = opt || {};
	opt.url = ip + opt.url || '';
	opt.data = opt.data || null;
	opt.method = opt.method || 'GET';
	opt.contentType = opt.contentType || 'application/x-www-form-urlencoded'
	opt.header = opt.header || {
		"Content-Type": opt.contentType,
		"satoken": uni.getStorageSync('token')
	};
	opt.loading = opt.loading || 'true';
	opt.success = opt.success || function() {};
	opt.fail = opt.fail || function() {};
	if (process.env.NODE_ENV) {
		console.log(
			"**************************************参数调式***************************************************");
		console.log("请求地址:" + opt.url + " 请求参数:" + JSON.stringify(opt.data));
		console.log(
			"************************************************************************************************");
	}
	if (opt.loading == 'true') {
		uni.showLoading({
			title: '正在加载',
			mask: true
		});
	}
	uni.request({
		url: opt.url,
		data: opt.data,
		method: opt.method,
		header: opt.header,
		dataType: 'json',
		responseType: opt.responseType,
		success: res => {
			setTimeout(() => {
				uni.hideLoading();
			}, 500)
			/*******************未授权***************************/
			if (res.data.code === 401) {
				uni.removeStorageSync('info');
				uni.redirectTo({url:'/pages/login/login'})
				return;
			}
			/*******************未授权***************************/
			if (res.data.code === 403) {
				uni.showModal({
					content: res.data.msg,
					showCancel: false
				});
				return;
			}
			/*******************系统内部错误***************************/
			if (res.data.code === 500) {
				uni.showModal({
					content: res.data.msg,
					showCancel: false
				});
				opt.fail(res);
				return;
			}
			opt.success(res);
		},
		fail: e => {
			uni.hideLoading();
			uni.getNetworkType({
				success: res => {
					if (res.networkType == 'none') {
						uni.showModal({
							content: '当前网络不可用，请检查网络稍后重试',
							showCancel: false
						});
					} else {
						uni.showModal({
							content: '服务异常，请稍后重试',
							showCancel: false
						})
					}
				}
			});
		}
	})
}
module.exports = {
	ip,
	request
};