// uni-app请求封装
export default class Request {
	http(router, data = {}, method,contentType) {
		let that = this;
		//let path = 'http://localhost:7014';
		// let path = 'https://test.userapp.store.cdwjyyh.com';
		// let path = 'https://user.test.ylrztop.com/api';
		let path = 'https://userapp.zkhj6.com'//中康
		uni.setStorageSync('requestPath',path)
		// uni.showLoading({
		// 	title: '加载中'
		// });
		let token = uni.getStorageSync('AppToken');
		if(router.indexOf("/companyapp")!=-1){
			// router =router.replace('/companyapp','')
			 // path = 'http://192.168.10.158:7015'//
			 token = uni.getStorageSync('ManageToken');
		}
		if(router.indexOf("/course_uniapp")!=-1){
			router =router.replace('/course_uniapp','')
			//张玉朋
			// let path = 'http://192.168.10.158:7014'//
			// path = 'https://userapp.zkhj6.com'//中康
			// path = 'https://user.test.ylrztop.com/api'//云融融智
			// path = 'https://userapp.ashyisheng.com'//蜂巢快药（爱上嘉园）
			// path = 'https://userapp.liangmiaoedu.com'//良苗
			// path = 'https://usercourse.beliyostore.com'//倍力优
			// path = 'https://userapp.bainian1000y.cn/prod-api'//百年康城
			// path = 'https://userapp.whhm.ylrzcloud.com/prod-api'//惠名大药房
			// path = 'https://userapp.cqsft.vip'//四福堂
			// path = 'https://userapp.drkzyy.cn/prod-api'//青岛市德瑞康
			// path = 'http://192.168.10.131:7014'
			token =uni.getStorageSync('TOKEN_WEXIN')
		}
		return new Promise((resolve, reject) => {
			// token = uni.getStorageSync('AppToken');
			var httpContentType='application/x-www-form-urlencoded';
			if(contentType!=undefined){
				//application/json;charset=UTF-8
				httpContentType=contentType;
			}
			var routers=router;
			// 请求
			uni.request({
				header: {
					// 'Content-Type': 'application/x-www-form-urlencoded',
					'Content-Type': httpContentType,
					'AppToken': token
				},
				url: `${path}${router}`,
				data: data,
				method: method,
				success: (res) => {
					//收到开发者服务器成功返回的回调函数
					if(res.data.code==401){//没有权限直接退出到登录界面
						let pages = getCurrentPages();
						pages.forEach(function(element) {
						  if(element!=undefined&&element.route=="pages/auth/login"){
						  	resolve(res.data)
						  	return;
						  }
						});
						// let url = pages[ pages.length - 1]; //当前页页面实例
						// //如果登录界面已打开,自动关闭
						// if(url!=undefined&&url.route=="pages/auth/login"){
						//  	resolve(res.data)
						// 	return;
						// }
						uni.navigateTo({
							url:'/pages/auth/login',
							success: () => {
								uni.hideLoading();
								
							},
							fail: () => {
								uni.hideLoading();
							}
						})
						return;
					}
					if (res.data.token) {
						uni.setStorageSync('AppToken',res.data.token)
					}
					resolve(res.data)
				},
				fail:(res) =>{
					//接口调用失败的回调函数
				},
				complete:(res) =>{
					//接口调用结束的回调函数（调用成功、失败都会执行）
					if (res.data.code == 401) {
						return false
					}
					uni.hideLoading();
				}
				
			})
		})
		 
	}
	
}
