// uni-app请求封装
// import {TOKEN_WEXIN} from '@/utils/common.js'
import {TOKEN_KEYAuto} from '@/utils/courseTool.js'
import store from '@/store/index.js'
export default class Request {
	http(router, data = {}, method,contentType) {
		let that = this;
		let type = 0;
		let path= "https://userapp.jnmyunl.com" // 金牛
		uni.setStorageSync('requestPath',path)
		// uni.showLoading({
		// 	  title: '加载中'
		// });
		let token = uni.getStorageSync('AppToken');
		if(router.indexOf("/companyapp")!=-1){
			type = 1;
			// router =router.replace('/companyapp','')
			// path = 'https://companyapp.ljhehualu.com'//济南联志
			 token = uni.getStorageSync('ManageToken');
		}
		if(router.indexOf("/course_uniapp")!=-1){
			type = 2;
			router =router.replace('/course_uniapp','')
			token =uni.getStorageSync('TOKEN_WEXIN')
		}
		// 企微看课
		if(router.indexOf("/course_auto")!=-1){
			type = 3;
			router =router.replace('/course_auto','')
			// path = 'http://a9e258d6.natappfree.cc'//百年康成本地
			token =uni.getStorageSync(TOKEN_KEYAuto)
		}
		return new Promise((resolve, reject) => {
			// let token = uni.getStorageSync('AppToken');
			let usertoken = uni.getStorageSync("UserToken")
			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(type !==0&&(res.data.code == 401 || res.data.code == 4001||res.data.code == 4004)) {
						store.commit('setCoureLogin', 2);
						uni.removeStorageSync("userinfos")
						// uni.removeStorageSync('userInfo');
						uni.removeStorageSync('TOKEN_WEXIN');
						if(type==2) {
							uni.removeStorageSync(TOKEN_KEYAuto)
						}
						if(type==1) {
							uni.removeStorageSync('ManageToken');
							uni.navigateTo({
								url:'/pages_manage/login'
							})
						}
						resolve({ code: 401, data: null });
						return
					}
					//收到开发者服务器成功返回的回调函数
					if(res.data.code==401){//没有权限直接退出到登录界面
						let pages = getCurrentPages();
						let currentPage  = pages[ pages.length - 1]; //当前页页面实例
						
						 // 条件编译判断平台，设置不同登录页路径
						  let loginPage = '';
						  loginPage = '/pages/enterprise/enterprise';
						  
						//如果登录界面已打开,自动关闭
						if (currentPage.route === loginPage.replace(/^\//, '')) {
						    resolve(res.data);
						    return;
						  }
						uni.reLaunch({
							url:loginPage,
							success: () => {
								uni.hideLoading();
							},
							fail: () => {
								uni.hideLoading();
							}
						})
						return;
					}
					if (res.token&&type==0) {
						uni.setStorageSync(TOKEN_KEY,res.data.token)
					}
					resolve(res.data)
				},
				fail:(res) =>{
					//接口调用失败的回调函数
				},
				complete:(res) =>{
					//接口调用结束的回调函数（调用成功、失败都会执行）
					if (res.data&& res.data.code == 401) {
						return false
					}
					uni.hideLoading();
				}
				
			})
		})
		 
	}
	
}
