Newer
Older
XiaoGanWXMini / utils / request.js
@zhangdeliang zhangdeliang on 29 Jul 1 KB 迁移
/*
 * 全局请求封装
 * @param path 请求路径
 * @param method 请求类型(GET/POST/DELETE等)
 * @oaram data 请求体数据
 * baseUrl h5需要设置代理,小程序需要直连,就是接口地址需要拼接处理
 */

// baseUrl设置 
let baseUrl = '';
/* #ifdef MP-WEIXIN */
// baseUrl = "http://192.168.20.49:7200" //谢斌斌
baseUrl = "https://server1.wh-nf.cn:8131/prod-api"
/* #endif */
/* #ifdef H5 */
baseUrl = ''
/* #endif */

// 发送请求函数
function requestBody(path, method, data = {},
	contType) {
	uni.showLoading({
		title: "加载中",
		mask: true
	});
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + path,
			method: method,
			header: {
				'Content-Type': contType ?
					contType :
					'application/json;charset=utf-8',
			},
			data: data,
			timeout: 6000 *
				10, //超时时间
			success(response) {
				// console.log('%c响应拦截:', ' background:green', response);
				uni.hideLoading();
				if (response.data
					.code !== 200
				) {
					uni.showToast({
						icon: "none",
						duration: 2000,
						title: response
							.data
							.msg ||
							'请求失败,请检查网络'
					});
					return false;
				} else {
					// console.log(response.data)
					resolve(response
						.data);
				}
			},
			fail(err) {
				uni.hideLoading();
				uni.showToast({
					icon: "none",
					title: '服务响应失败'
				});
				console.error(
					err);
				reject(err);
			},
			complete() {
				// uni.hideLoading();
			}
		});
	});
}
export default requestBody