| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 | 
							- // uni-app请求封装
 
- export default class Request {
 
- 	http(router, data = {}, method, contentType, url) {
 
- 		let that = this;
 
- 		let path = 'https://api.fhhx.runtzh.com';
 
- 		// let path = 'http://x5d7cc68.natappfree.cc';//刘明欣
 
- 		// let path =  'http://192.168.10.122:7014';
 
- 		// let path = 'http://192.168.10.126:7014';
 
- 		// let path = 'https://live.test.ylrztop.com/live-api'; // 余红奇
 
- 		// let path = 'http://192.168.10.166:7014'; // 余红奇
 
- 		let token = "";
 
- 		let type = 0
 
- 		if (url != null) {
 
- 			type = 1
 
- 			let projectCode = uni.getStorageSync('projectCode')
 
- 			path = uni.getStorageSync('addressUrl_' + projectCode)
 
- 			token = uni.getStorageSync('AppTokenmini_MYCourse')
 
- 		} else {
 
- 			type = 0
 
- 			uni.setStorageSync('requestPath', path)
 
- 			token = uni.getStorageSync('AppToken');
 
- 		}
 
- 		if (router.indexOf("/course_uniapp") != -1) {
 
- 			type = 2;
 
- 			router = router.replace('/course_uniapp', '')
 
- 			let projectCode = uni.getStorageSync('projectCode')
 
- 			path = uni.getStorageSync('addressUrl_' + projectCode)
 
- 			token = uni.getStorageSync('TOKEN_WEXIN')
 
- 		}
 
- 		// uni.showLoading({
 
- 		// 	title: '加载中'
 
- 		// });
 
- 		return new Promise((resolve, reject) => {
 
- 			var httpContentType = 'application/x-www-form-urlencoded';
 
- 			if (!path) {
 
- 				uni.showToast({
 
- 					title: '链接有误',
 
- 					icon: 'none'
 
- 				});
 
- 				return
 
- 				reject('链接有误')
 
- 			}
 
- 			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 && type == 0) {
 
- 						uni.setStorageSync('AppToken', res.data.token)
 
- 					}
 
- 					resolve(res.data)
 
- 				},
 
- 				fail: (res) => {
 
- 					//接口调用失败的回调函数
 
- 				},
 
- 				complete: (res) => {
 
- 					//接口调用结束的回调函数(调用成功、失败都会执行)
 
- 					if (res.data.code == 401) {
 
- 						return false
 
- 					}
 
- 					uni.hideLoading();
 
- 				}
 
- 			})
 
- 		})
 
- 	}
 
- }
 
 
  |