// uni-app请求封装
import store from '@/store/index.js'
import {TOKEN_KEYAuto} from '@/utils/courseTool.js'
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'//中康
		// let path = 'http://e6dde68c.natappfree.cc/store'//本地接口
		let path = 'https://userapp.moonxiang.com/store'//北京存在文化商城
		// let path = 'http://pa87de27.natappfree.cc/store'//本地（测试）
		
		// let path = 'https://user.test.ylrztop.com/prod-api/store'//云联融智优选
		let type = 0
		uni.setStorageSync('requestPath',path)
		// uni.showLoading({
		// 	title: '加载中'
		// });
		let token = uni.getStorageSync('AppToken');
		if(router.indexOf("/companyapp")!=-1){
			// router =router.replace('/companyapp','')
			 // path = 'http://aa5ae96f.natappfree.cc'//存在本地
			 path = 'https://userapp.moonxiang.com'//存在文化
			 type = 1
			 token = uni.getStorageSync('ManageToken');
		}
		// 企微看课
		if(router.indexOf("/course_auto")!=-1){
			type = 2
			router =router.replace('/course_auto','')
			// path = 'https://userapp.gjh2024.com/apis'//金康键
			// path = 'https://userapp.moonxiang.com/prod-api'//青岛市德瑞康
			path = 'https://userapp.moonxiang.com'//存在
			// path = 'https://user.test.ylrztop.com/prod-api'//云联融智优选
			token =uni.getStorageSync(TOKEN_KEYAuto)
			console.log("TOKEN_KEYAuto===",TOKEN_KEYAuto,token)
		}
		if(router.indexOf("/course_uniapp")!=-1){
			type = 3
			router =router.replace('/course_uniapp','')
			//张玉朋
			// let path = 'http://192.168.10.158:7014'//
			// path = 'http://192.168.10.131:7014'
			path = 'https://userapp.moonxiang.com'//存在
			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) => {
					//收到开发者服务器成功返回的回调函数
					console.log(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)
						}
						resolve({ code: 401, data: null });
						return
					}
					if(res.data.code==401){//没有权限直接退出到登录界面
						let pages = getCurrentPages();
						pages.forEach(function(element) {
						  if(element!=undefined&&element.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&&type==0) {
						uni.setStorageSync('AppToken',res.data.token)
					}
					resolve(res.data)
				},
				fail:(res) =>{
					//接口调用失败的回调函数
				},
				complete:(res) =>{
					//接口调用结束的回调函数（调用成功、失败都会执行）
					if (res.data.code == 401) {
						return false
					}
					uni.hideLoading();
				}
				
			})
		})
		 
	}
	
}
