// uni-app请求封装
import {TOKEN_WEXIN} from '@/utils/common.js'
import {TOKEN_KEYAuto} from '@/utils/courseTool.js'
console.log("TOKEN_KEYAuto====",TOKEN_KEYAuto)
export default class Request {
	http(router, data = {}, method,contentType) {
		let that = this;
		//曹丽芹
		// let path = 'http://192.168.10.131:7015'
		//涂小龙
		// let path = 'http://192.168.10.125:7015'
		//刘明欣
		// let path = 'http://192.168.10.150:8080'
		//张玉朋
		// let path = 'http://192.168.10.158:7014'//
		//刘明欣
		// let path = 'http://x8489c8e.natappfree.cc'//
		 
		let path = 'https://userapp.fbylive.com'//福本源
		// let path = 'https://userapp.zkhj6.com'//中康
		// let path = 'https://company.h5.test.ylrztop.com/api'//云融融智
		// let path = 'https://userapp.ashyisheng.com'//蜂巢快药
		// let path = 'https://userapp.whhm.ylrzcloud.com/prod-api'//惠名大药房
		// let path = 'https://userapp.xdtongshuntang.top'//同顺堂
		// let path = 'https://userapp.liangmiaoedu.com'//良苗
		// let path = 'https://usercourse.beliyostore.com'//倍力优
		// let path = 'https://userapp.bainian1000y.cn/prod-api'//百年康城
		// let path = 'https://userapp.drkzyy.cn/prod-api'//青岛市德瑞康
		// let path = 'https://companyapp.gjh2024.com/apis'//金康键
		
		// let path = 'https://userapp.cqsft.vip'//四福堂
		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('AppToken');
		// }
		if(router.indexOf("/course_uniapp")!=-1){
			router =router.replace('/course_uniapp','')
			path = 'https://userapp.fbylive.com'//福本源
			// // path = 'https://scrm.gjh2024.com'//金康键
			//张玉朋
			// let path = 'http://192.168.10.158:7014'//
			// path = 'https://userapp.zkhj6.com'//中康
			// path = 'https://company.h5.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 = 'https://userapp.gjh2024.com/apis'//金康键
			// path = 'http://192.168.10.131:7014'
			//#ifdef MP-WEIXIN
			token =uni.getStorageSync('TOKEN_WEXIN')
			//#endif
			// #ifdef H5
			token =uni.getStorageSync('TOKEN_KEY')
			// #endif
		}
		// 企微看课
		if(router.indexOf("/course_auto")!=-1){
			router =router.replace('/course_auto','')
			// path = 'https://userapp.gjh2024.com/apis'//金康键
			path = 'https://userapp.drkzyy.cn/prod-api'//青岛市德瑞康
			token =uni.getStorageSync(TOKEN_KEYAuto)
			console.log("TOKEN_KEYAuto===",TOKEN_KEYAuto,token)
		}
		if(router.indexOf("/QWcourse")!=-1){
			router =router.replace('/QWcourse','')
			token = uni.getStorageSync('TOKEN_KEYQW');
		}
		return new Promise((resolve, reject) => {
			// let token = uni.getStorageSync('AppToken');
			let usertoken = uni.getStorageSync("TOKEN_WEXIN")
			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,
					// 'UserToken':usertoken
				},
				url: `${path}${router}`,
				data: data,
				method: method,
				success: (res) => {
					//收到开发者服务器成功返回的回调函数
					if(res.data.code==401){//没有权限直接退出到登录界面
						let pages = getCurrentPages();
						let currentPage  = pages[ pages.length - 1]; //当前页页面实例
						
						 // 条件编译判断平台，设置不同登录页路径
						  let loginPage = '';
						  // #ifdef H5
						  loginPage = '/pages/auth/login';
						  // #endif
						  // #ifdef MP-WEIXIN
						  loginPage = '/pages/enterprise/enterprise';
						  // #endif
						  
						//如果登录界面已打开,自动关闭
						if (currentPage.route === loginPage.replace(/^\//, '')) {
						    resolve(res.data);
						    return;
						  }
						uni.reLaunch({
							url:loginPage,
							success: () => {
								uni.hideLoading();
							},
							fail: () => {
								uni.hideLoading();
							}
						})
						return;
					}
					// if (res.token) {
					// 	uni.setStorageSync(TOKEN_KEY,res.data.token)
					// }
					resolve(res.data)
				},
				fail:(res) =>{
					//接口调用失败的回调函数
				},
				complete:(res) =>{
					//接口调用结束的回调函数（调用成功、失败都会执行）
					console.log('1111'+router+'1111',res)
					if (res.data&& res.data.code == 401) {
						return false
					}
					uni.hideLoading();
				}
				
			})
		})
		 
	}
	
}
