瀏覽代碼

修改看课token问题

XSLu08042 1 周之前
父節點
當前提交
43c41c83cb
共有 12 個文件被更改,包括 283 次插入102 次删除
  1. 4 0
      api/course.js
  2. 3 0
      api/courseLook.js
  3. 6 3
      common/request.js
  4. 115 0
      common/requestOld.js
  5. 2 2
      main.js
  6. 14 16
      pages_course/becomeVIP.vue
  7. 17 0
      pages_course/feedback.vue
  8. 24 15
      pages_course/video.vue
  9. 49 20
      pages_course/videovip.vue
  10. 1 0
      pages_course/webview.vue
  11. 13 33
      store/index.js
  12. 35 13
      utils/common.js

+ 4 - 0
api/course.js

@@ -103,4 +103,8 @@ export function getWxConfig(data) {
  }
  export function getConfigByKey(data) {
  	 return request('/app/common/getConfigByKey',data,'GET','','https://h5api.his.cdwjyyh.com');
+ }
+ 
+ export function checkCourseLogin(data) {
+ 	 return request('/app/course/checkLogin',data,'GET','','https://h5api.his.cdwjyyh.com');
  }

+ 3 - 0
api/courseLook.js

@@ -78,4 +78,7 @@ export function getRealLink(data) {
  //看课获取用户信息
   export function getUserInfoLook(data) {
   	 return request('/course_uniapp/app/user/getUserInfoByUserId',data,'GET','application/json;charset=UTF-8','https://h5api.his.cdwjyyh.com');
+  }
+  export function checkCourseLoginLook(data) {
+  	 return request('/course_uniapp/app/course/checkLogin',data,'GET','','https://h5api.his.cdwjyyh.com');
   }

+ 6 - 3
common/request.js

@@ -1,3 +1,4 @@
+import store from '@/store/index.js'
 // uni-app请求封装
 export default class Request {
 	http(router, data = {}, method, contentType, url) {
@@ -34,9 +35,6 @@ export default class Request {
 		// uni.showLoading({
 		// 	title: '加载中'
 		// });
-		
-		
-		
 		return new Promise((resolve, reject) => {
 			var httpContentType = 'application/x-www-form-urlencoded';
 			if (!path) {
@@ -64,6 +62,11 @@ export default class Request {
 				method: method,
 				success: (res) => {
 					//收到开发者服务器成功返回的回调函数
+					if(type !==0&&(res.data.code == 401 || res.data.code == 4001||res.data.code == 4004)) {
+						store.commit('setCoureLogin', 2);
+						resolve({ code: 401, msg: '处理中', data: null });
+						return
+					}
 					if (res.data.code == 401) { //没有权限直接退出到登录界面
 						let pages = getCurrentPages();
 						pages.forEach(function(element) {

+ 115 - 0
common/requestOld.js

@@ -0,0 +1,115 @@
+// 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();
+				}
+
+			})
+		})
+
+	}
+
+}

+ 2 - 2
main.js

@@ -7,8 +7,7 @@ Vue.config.productionTip = false
 
 import utils from './utils/common.js'
 Vue.prototype.utils = utils;
-import store from './store/index.js' // 确保路径正确
-Vue.prototype.$store = store;
+import store from './store'
 import {isLoginCourse} from './utils/common.js'
 Vue.prototype.$isLoginCourse = isLoginCourse
 import {
@@ -19,6 +18,7 @@ Vue.prototype.setData = setData;
 
 App.mpType = 'app'
 const app = new Vue({
+	store,
 	...App
 })
 app.$mount()

+ 14 - 16
pages_course/becomeVIP.vue

@@ -168,7 +168,7 @@
 			// 	this.userlogo=true
 			// }
 			if(!uni.getStorageSync('userinfos')&&this.$store.state.logoname=='乐氏本源'){
-				await this.$store.dispatch('getWebviewUrl');
+				await this.utils.getConfigKey();
 				uni.navigateTo({
 					url:'/pages_course/webview'
 				})
@@ -207,22 +207,20 @@
 				if(res.code == 200) {
 						this.sendType=uni.getStorageSync('sendType')
 						this.utils.getConfigKey()
+						if(uni.getStorageSync('userInfo')&&uni.getStorageSync('userInfo')!='{}') {
+							this.userInfo = uni.getStorageSync('userInfo')
+						} else {
+							this.userInfo = {}
+						}
+						this.userinfos=uni.getStorageSync('userinfos')
+						let obj=uni.getStorageSync('TOKEN_WEXIN');
+						console.log(uni.getStorageSync('userinfos'))
+						this.isLogin = !!obj;
+						if(this.isLogin&&this.isVip!=1) {
+							this.registerCourse()
+						}
 				}
 			})
-			if(uni.getStorageSync('userInfo')&&uni.getStorageSync('userInfo')!='{}') {
-				this.userInfo = uni.getStorageSync('userInfo')
-			} else {
-				this.userInfo = {}
-			}
-			this.userinfos=uni.getStorageSync('userinfos')
-			let obj=uni.getStorageSync('TOKEN_WEXIN');
-			console.log(uni.getStorageSync('userinfos'))
-			this.isLogin = !!obj;
-			if(this.isLogin&&this.isVip!=1) {
-				this.registerCourse()
-			}
-			
-			
 		},
 		methods: {
 			getWebviewUrl() {
@@ -349,7 +347,7 @@
 				this.userinfos.nickname=e.detail.value
 			},
 			async nato(){
-				await this.$store.dispatch('getWebviewUrl');
+				await this.utils.getConfigKey();
 				uni.navigateTo({
 					url:'/pages_course/webview'
 				})

+ 17 - 0
pages_course/feedback.vue

@@ -48,6 +48,7 @@
 </template>
 
 <script>
+	import { mapGetters } from 'vuex';
 	import{ getTypeTree, complaintRecord,loginByMp } from "@/api/course.js"
 	export default {
 		data() {
@@ -80,6 +81,21 @@
 				code:''
 			};
 		},
+		computed: {
+			...mapGetters(['coureLogin']),
+		},
+		watch: {
+		    coureLogin: {
+		      immediate: true,          // 页面一进入就检查一次
+		      handler(val) {
+		        if (val == 2) {
+					console.log("AppToken失效,请重新登录")
+					this.isLogin = false
+					this.goLogin()
+		        }
+		      }
+		    }
+		},
 		onLoad(option) {
 			this.userId = option.userId || ''
 			this.courseId = option.courseId || ''
@@ -317,6 +333,7 @@
 									loginByMp({code: loginRes.code,encryptedData:infoRes.encryptedData,iv:infoRes.iv,appId:getApp().globalData.appId}).then(res=>{
 										 uni.hideLoading();
 										 if (res.code == 200) {
+											 this.$store.commit('setCoureLogin', 1);
 											uni.setStorageSync(this.utils.TOKEN_KEYAuto, res.token);
 											uni.setStorageSync('auto_userInfo', JSON.stringify(res.user));
 											this.userId = res.user.userId || ''

+ 24 - 15
pages_course/video.vue

@@ -251,6 +251,7 @@
 </template>
 
 <script>
+	import { mapGetters } from 'vuex';
 	import ques from "./components/ques.vue"
 	import descInfo from "./components/descInfo.vue"
 	import descInfoNav from "./components/descInfoNav.vue"
@@ -411,17 +412,30 @@
 				projectCode:''
 			}
 		},
+		computed:{
+			...mapGetters(['coureLogin']),
+		},
+		watch: {
+		    coureLogin: {
+		      immediate: true,          // 页面一进入就检查一次
+		      handler(val) {
+		        if (val == 2&&this.isLogin) {
+					console.log("看课AppToken失效,请重新登录")
+					this.isLogin = false
+					this.isAddKf = 0
+					this.goLogin()
+		        }
+		      }
+		    }
+		},
 		onLoad(option) {
-			console.log("onLoad===",'')
+			console.log("onLoad===")
 			this.urlOption = option.course ? JSON.parse(option.course) : {}
 			this.videoId = this.urlOption.videoId || ''
 			this.qwUserId = this.urlOption.qwUserId || ''
 			this.corpId = this.urlOption.corpId || ''
 			this.linkType = this.urlOption.linkType || 0
 			this.projectCode = this.urlOption.projectCode || ''
-			// if (this.code) {
-			// 	this.loginByMp()
-			// }
 			this.sortLink = this.urlOption.link || ''
 			
 			uni.$on('usercode',(data)=>{
@@ -436,16 +450,15 @@
 			// #endif
 		},
 		onShow() {
+			console.log("==onShow===")
 			this.baseUrl=uni.getStorageSync('requestImagesPath')
 			this.tipsOpen = false
-			// this.isLogin = this.utils.isLoginCourseAuto()
 			this.uuId = this.utils.generateRandomString(16)
 			if(uni.getStorageSync('auto_userInfo') && JSON.stringify(uni.getStorageSync('auto_userInfo'))!='{}') {
 				this.user = JSON.parse(uni.getStorageSync('auto_userInfo'))
 			} else {
 				this.user = {}
 			}
-			console.log("https://wxaurl.cn/skRIquxVTtu==========")
 			if(!this.sortLink||!this.projectCode){
 				uni.showToast({
 					title: '链接有误',
@@ -1084,6 +1097,9 @@
 				})
 			},
 			goLogin(data) {
+				if (this.player) {
+					this.player.pause()
+				}
 				if(!this.sortLink||!this.projectCode){
 					uni.showToast({
 						title: '链接有误',
@@ -1112,6 +1128,7 @@
 									loginByMp({code: loginRes.code,encryptedData:infoRes.encryptedData,iv:infoRes.iv,appId:getApp().globalData.appId}).then(res=>{
 										 uni.hideLoading();
 										 if (res.code == 200) {
+											this.$store.commit('setCoureLogin', 1);
 											uni.setStorageSync(this.utils.TOKEN_KEYAuto, res.token);
 											uni.setStorageSync('auto_userInfo', JSON.stringify(res.user));
 											this.user = res.user
@@ -1223,7 +1240,6 @@
 				}
 			},
 			getScrollTop(res) {
-				console.log(res)
 				if(this.currentTab == 2) {
 					this.scrollTop = res
 				} else {
@@ -1307,20 +1323,13 @@
 			// H5授权绑定关系
 			async loginFsUserWx(data){
 				if(data){
-					console.log('huoqu1222',data)
-					// console.log("loginFsUserWx:",res)
-					// this.userinfos=uni.getStorageSync('userinfos')
 					let token = uni.getStorageSync('TOKEN_WEXIN');
 					let user = uni.getStorageSync('userInfo')
-					// this.userInfo=uni.getStorageSync('userInfo');
-					// this.isLogin = true
-					
-					
+
 					uni.setStorageSync(this.utils.TOKEN_KEYAuto, token);
 					uni.setStorageSync('auto_userInfo', JSON.stringify(user));
 					this.user = user
 					this.isLogin = true
-					this.getIsAddKf()
 				}else{
 					uni.setStorageSync('H5course',{
 						companyId: this.urlOption.companyId,

+ 49 - 20
pages_course/videovip.vue

@@ -256,6 +256,7 @@
 </template>
 
 <script>
+	import { mapGetters } from 'vuex';
 	import {
 		generateRandomString
 	} from "@/utils/common.js"
@@ -449,6 +450,21 @@
 			avataruser() {
 				return this.userinfos.avatar
 			},
+			...mapGetters(['coureLogin']),
+		},
+		watch: {
+		    coureLogin: {
+		      immediate: true,          // 页面一进入就检查一次
+		      handler(val) {
+				  console.log(val ,this.isLogin)
+		        if (val == 2) {
+					console.log("看课AppToken失效,请重新登录")
+					this.isLogin = false
+					this.isAddKf = 0
+					this.goLogin()
+		        }
+		      }
+		    }
 		},
 		onLoad(option) {
 
@@ -495,11 +511,11 @@
 			// this.sortLink = this.urlOption.link || ''
 			this.getMenuButton()
 			// #ifdef MP-WEIXIN
-			// if(this.$store.state.logoname!=='乐氏本源'&&!uni.getStorageSync('userInfos')){
+			// if(this.$store.state.logoname!=='乐氏本源'&&!uni.getStorageSync('userinfos')){
 			// 	this.userlogo=true
 			// }
-			console.log(uni.getStorageSync('userInfos'))
-			// if(!uni.getStorageSync('userInfos')&&this.$store.state.logoname=='乐氏本源'){
+			console.log(uni.getStorageSync('userinfos'))
+			// if(!uni.getStorageSync('userinfos')&&this.$store.state.logoname=='乐氏本源'){
 			// 	uni.navigateTo({
 			// 		url:'/pages_course/webview'
 			// 	})
@@ -509,7 +525,7 @@
 		},
 		onShow() {
 			this.userinfos = uni.getStorageSync('userinfos')
-			this.userinfo = uni.getStorageSync('userinfo')
+			this.userinfo = uni.getStorageSync('userInfo')
 			this.tipsOpen = false
 			this.isExpand = true
 			this.uuId = generateRandomString(16)
@@ -518,17 +534,30 @@
 			}
 			const AppToken = uni.getStorageSync('TOKEN_WEXIN')
 			console.log(AppToken)
-			if (AppToken) {
-				// this.isLogin=true
-				this.getUserInfoLooks()
-				if (this.isAddKf == 1 && this.userinfo.userId) {
-					console.log(1233)
-					this.getH5CourseVideoDetails()
-				} else {
-					// this.checkUserInfoA()
-					this.getIsAddKf()
+			// if (AppToken) {
+			// 	console.log("console.log(AppToken,this.userinfo)===",this.userinfo)
+			// 	// this.isLogin=true
+			// 	this.getUserInfoLooks()
+			// 	if (this.isAddKf == 1 && this.userinfo.userId) {
+			// 		console.log(1233)
+			// 		this.getH5CourseVideoDetails()
+			// 	} else {
+			// 		// this.checkUserInfoA()
+			// 		this.getIsAddKf()
+			// 	}
+			// }
+			this.$isLoginCourse().then(res=>{
+				if(res) {
+					this.getUserInfoLooks()
+					if (this.isAddKf == 1 && this.userinfo.userId) {
+						console.log(1233)
+						this.getH5CourseVideoDetails()
+					} else {
+						// this.checkUserInfoA()
+						this.getIsAddKf()
+					}
 				}
-			}
+			})
 			// if(this.sortLink){
 			// 	this.getLink()
 			// } else {
@@ -579,7 +608,7 @@
 		},
 		methods: {
 			getUserInfoLooks() {
-				getUserInfoLook().then(res => {
+				getUserInfoLook({userId:this.userinfo.userId}).then(res => {
 					if (res.code == 200) {
 						this.userinfos.nickname = res.user.nickname
 						this.userInfo = res.user;
@@ -682,7 +711,7 @@
 					});
 					return
 				}
-				uni.setStorageSync('userInfos', this.userinfos)
+				uni.setStorageSync('userinfos', this.userinfos)
 				this.editUserA()
 				this.userlogo = false
 			},
@@ -744,8 +773,8 @@
 									console.log(loginRes)
 									console.log(e)
 									let code = loginRes.code // 获取开发code
-									this.userinfos = uni.getStorageSync('userInfos') ? uni.getStorageSync(
-										'userInfos') : this.userinfos
+									this.userinfos = uni.getStorageSync('userinfos') ? uni.getStorageSync(
+										'userinfos') : this.userinfos
 									loginByMiniApp({
 											encryptedData: e.mp.detail.encryptedData,
 											iv: e.mp.detail.iv,
@@ -837,8 +866,8 @@
 									console.log(loginRes)
 									console.log(e)
 									let code = loginRes.code // 获取开发code
-									this.userinfos = uni.getStorageSync('userInfos') ? uni.getStorageSync(
-										'userInfos') : this.userinfos
+									this.userinfos = uni.getStorageSync('userinfos') ? uni.getStorageSync(
+										'userinfos') : this.userinfos
 									loginByMiniApp({
 											encryptedData: e.mp.detail.encryptedData,
 											iv: e.mp.detail.iv,

+ 1 - 0
pages_course/webview.vue

@@ -78,6 +78,7 @@ export default {
 		  		uni.hideLoading();
 		  		if (res.code == 200) {
 		  			console.log(res)
+					this.$store.commit('setCoureLogin', 1);
 		  			uni.hideLoading();
 		  			uni.showToast({
 		  				icon:'none',

+ 13 - 33
store/index.js

@@ -3,12 +3,11 @@ import Vue from 'vue';
 import Vuex from 'vuex';
 
 Vue.use(Vuex);
-import {
-	getConfigByKey
-} from '@/api/index.js'
-export default new Vuex.Store({
+// import { getConfigByKey } from '@/api/index.js'
+
+const store = new Vuex.Store({
   state: {
-	 webviewUrl: '',
+	coureLogin: uni.getStorageSync('coureLogin') || 0,
 	 wsDanmuUrl: 'wss://userapp.cqsft.vip', // 弹幕评论接口地址
 	 //红包领取规则:
 	 answerType: 1, //红包领取规则 0:完课且最后一分钟(第二次无需最后一分钟), 1:按完课百分比答题领红包
@@ -18,39 +17,20 @@ export default new Vuex.Store({
 	logoname:'倍力优',
 	appid:'wx503cf8ab31f83dd4',
   },
+  getters: {
+    coureLogin: (state) => state.coureLogin,
+  },
   mutations: {
     setCount(state, value) {
       state.count = value;
     },
-    setUserInfo(state, info) {
-      state.userInfo = info;
-    },
-	setWebviewUrl(state, value) {
-		state.webviewUrl = value;
+	setCoureLogin(state, payload) {
+		uni.setStorageSync('coureLogin', payload);
+		state.coureLogin = payload;
 	}
   },
   actions: {
-    fetchUser({ commit }) {
-      // const res = await uni.request({ url: '/api/user' });
-      commit('setUserInfo', 'noe');
-    },
-	getWebviewUrl({commit}) {
-		var that = this;
-		var data = {
-			key: 'course.config'
-		}
-		return new Promise((resolve, reject) => {
-			getConfigByKey(data).then(res => {
-				if (res.code == 200) {
-					console.log("getConfigByKey====", JSON.parse(res.data))
-					let data = JSON.parse(res.data)
-					commit('setWebviewUrl', data.userCourseAuthDomain);
-					resolve()
-				}
-			}).catch(error => {
-				reject(error)
-			});
-		})
-	}
   }
-});
+});
+
+export default store

+ 35 - 13
utils/common.js

@@ -1,6 +1,7 @@
 import {checkLogin} from '@/api/user'
 import { getCourseDomain } from '@/api/common.js'
-import { getConfigByKey } from '@/api/course.js'
+import { getConfigByKey,checkCourseLogin } from '@/api/course.js'
+import { checkCourseLoginLook } from '@/api/courseLook.js'
 import dayjs from 'dayjs'
 
 let TOKEN_KEYAuto = 'AppTokenmini_MYCourse'
@@ -40,12 +41,22 @@ var isLogin=  function() {
 
 var  isLoginCourseAuto =  function() {
 	return new Promise((resolve, reject) => {
-		let token = uni.getStorageSync(TOKEN_KEYAuto);
-		if (token==null||token==undefined||token=="" ) {
-			resolve(false);
-		} else {
-			resolve(true);
-		}
+		// let token = uni.getStorageSync(TOKEN_KEYAuto);
+		// if (token==null||token==undefined||token=="" ) {
+		// 	resolve(false);
+		// } else {
+		// 	resolve(true);
+		// }
+		checkCourseLogin().then(
+			res => {
+				if(res.code==200){
+					resolve(true);
+				}else{
+					resolve(false);
+				}
+			},
+			rej => { }
+		);
 	}); 
 }
 
@@ -429,14 +440,25 @@ var  getConfigKey =  function() {
 }
 var isLoginCourse=function(){
 	return new Promise((resolve, reject) => {
-		let token = uni.getStorageSync('TOKEN_WEXIN');
-		if (token==null||token==undefined||token=="" ) {
-			resolve(false);
-		} else {
-			resolve(true);
-		}
+		// let token = uni.getStorageSync('TOKEN_WEXIN');
+		// if (token==null||token==undefined||token=="" ) {
+		// 	resolve(false);
+		// } else {
+		// 	resolve(true);
+		// }
+		checkCourseLoginLook().then(
+			res => {
+				if(res.code==200){
+					resolve(true);
+				}else{
+					resolve(false);
+				}
+			},
+			rej => { }
+		);
 	}); 
 }
+
 module.exports = {
 		formatDate:formatDate,
         isEmpty : isEmpty,