import {TOKEN_KEY} from '@/utils/common.js'

// uni-app请求封装
export default class Request {
	http(router, data = {}, method,contentType,type) {
		let that = this;
		// let path ='http://h5api.muyi88.com';//server
		// let path = 'http://h5api.ylrztop.com'
		// let path = 'http://h5.baohetangjinyue.cn'
		// let path = 'http://h5api.jiuzhouzaixian.com'
		// let path = 'http://h5api.xintaihlw.cn'
		// let path = 'http://h5api.hbhdt.top'
		// let path = "http://h5api.xintaihlwyy.cn"
		// let path = 'http://h5api.yizhenylfw.com'
		// let path = 'http://h5api.ylrztop.com'
		let path = "http://h5api.cqsft.vip"
		// let path = 'https://h5api.his.cdwjyyh.com'
		// uni.setStorageSync('requestPath',path);
		// uni.showLoading({
		// 	  title: '加载中'
		// });
		return new Promise((resolve, reject) => {
			let token = uni.getStorageSync(TOKEN_KEY);
			
			var httpContentType='application/x-www-form-urlencoded';
			if(contentType!=undefined){
				//application/json;charset=UTF-8
				httpContentType=contentType;
			}
			var routers=type == 1 ? '/app/course/h5'+ router : type == 3? router: '/app/course'+router;
			// console.log("---qxj request url:"+`${path}${router}`+" params:"+JSON.stringify(data));
			// console.log(" token:"+token);
			// 请求
			uni.request({
				header: {
					'Content-Type': httpContentType,
					'AppToken': type == 1 ? '':token,
				},
				
				url: `${path}${routers}`,
				data: data,
				method: method,
				success: (res) => {
					//收到开发者服务器成功返回的回调函数
					if(res.data.code==401){ //没有权限直接退出到登录界面
						let pages = getCurrentPages();
						let url = pages[ pages.length - 1]; //当前页页面实例
						//如果登录界面已打开,自动关闭
						if(url!=undefined&&url.route=="pages/auth/login"){
							resolve(res.data)
							return;
						}
					}
					if(res.token) {
						uni.setStorageSync(TOKEN_KEY,res.token)
					}
					resolve(res.data)
				},
				fail:(res) =>{
					//接口调用失败的回调函数
				},
				complete:(res) =>{
					//接口调用结束的回调函数（调用成功、失败都会执行）
					if (res.data.code == 401) {
						return false
					}
					uni.hideLoading();
				}
				
			})
		})
		 
	}
	
}
