chenxiong 3 yıl önce
ebeveyn
işleme
8bb78f9487

+ 11 - 0
common/methodTool.js

@@ -301,4 +301,15 @@ export default {
          }
          return result;
      },
+	/**
+	 * @param {Object} length  长度
+	 * 获取随机字符串
+	 */
+	getRandomString(length) {
+	   var str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
+	   var result = '';
+	   for (var i = length; i > 0; --i) 
+		 result += str[Math.floor(Math.random() * str.length)];
+	   return result;
+	 }
 }

+ 4 - 0
common/request.js

@@ -11,6 +11,10 @@ export const BASE_URL = 'http://42.192.164.187:19005'    //test
  //图片上传api
 // export const BASE_IMG_URL = 'https://file.xyyxt.net/'     //release
 export const BASE_IMG_URL = 'https://file-dev.xyyxt.net/'  //test
+
+export const socket_url = 'ws://42.192.164.187:19005/webSocket/'  //test
+
+
 export const tenantId = '867735392558919680' 
 export const myRequest = (options) => {
 	if (store.state.allowLoading && !options.noLoading) {

+ 135 - 0
common/socket.js

@@ -0,0 +1,135 @@
+import store from '@/store/index.js'
+import method from '@/common/methodTool'
+let isSocketClose = false; // 是否关闭socket
+let heartbeatInterval = null; // 心跳定时器
+let socketTask = null; // websocket对象
+let againTimer = null; //断线重连定时器
+
+
+let url = null;
+
+/**
+ * sockeUrl:websocet的地址
+ * */
+const sokcet = (sockeUrl) => {
+	url = sockeUrl;
+	isSocketClose = false;
+	//判断是否有websocet对象,有的话清空
+	if (socketTask) {
+		socketTask.close();
+		socketTask = null;
+		clearInterval(heartbeatInterval);
+	}
+
+	// 连接
+	console.log(url)
+	socketTask = uni.connectSocket({
+		url: url,
+		success(data) {
+			console.log("websocket连接成功");
+			clearInterval(againTimer) //断线重连定时器
+		},
+		fail: (err) => {
+			console.log("报错", err);
+		}
+	});
+	// 连接打开
+	socketTask.onOpen((res) => {
+		console.log('WebSocket打开');
+		clearInterval(againTimer) //断线重连定时器
+		clearInterval(heartbeatInterval);
+		// 10秒发送一次心跳
+		heartbeatInterval = setInterval(() => {
+			sendMsg('心跳ing')
+		}, 10000)
+	})
+	// 监听连接失败
+	socketTask.onError((err) => {
+		console.log('WebSocket连接打开失败,请检查', err);
+		//停止发送心跳
+		clearInterval(heartbeatInterval)
+		//如果不是人为关闭的话,进行重连
+		if (!isSocketClose) {
+			reconnect(url)
+		}
+	})
+
+	// // 监听连接关闭 -
+	socketTask.onClose((e) => {
+		console.log('WebSocket连接关闭!',e);
+		clearInterval(heartbeatInterval)
+		if (!isSocketClose) {
+			reconnect(url)
+		}
+	})
+
+	// 监听收到信息
+	socketTask.onMessage((res) => {
+		console.log(res, 'res监听收到信息')
+		// stop();
+		// uni.setStorageSync('needToLogin','1')
+		// uni.showToast({
+		// 	icon:'none',
+		// 	title:'用户在其他终端登录,强制下线',
+		// 	duration:2000,
+		// })
+		// setTimeout(() => {
+		// 	method.exit();
+		// },2000)
+		
+	});
+
+
+}
+
+const reconnect = (url) => {
+	console.log('进入断线重连', isSocketClose);
+	clearInterval(againTimer) //断线重连定时器
+	clearInterval(heartbeatInterval);
+	console.log(socketTask)
+	socketTask && socketTask.close(); // 确保已经关闭后再重新打开
+	socketTask = null;
+	// 连接  重新调用创建websocet方法
+	againTimer = setInterval(() => {
+		sokcet(url)
+		console.log('在重新连接中...');
+	}, 30000)
+
+
+}
+
+const sendMsg = (msg) => { //向后端发送命令
+	console.log(msg)
+	try {
+		//通过 WebSocket 连接发送数据
+		socketTask.send({
+			data:msg
+		});
+	} catch (e) {
+		console.log(e,'msg')
+		if (isSocketClose) {
+			return
+		} else {
+			reconnect(url)
+		}
+
+	}
+}
+
+const stop = () => {
+	isSocketClose = true
+	clearInterval(heartbeatInterval);
+	clearInterval(againTimer) //断线重连定时器
+	socketTask && socketTask.close(); // 确保已经关闭后再重新打开
+	socketTask = null;
+}
+
+
+
+
+
+export const websocket = {
+	sokcet,
+	stop,
+	sendMsg
+};

+ 4 - 1
components/course/courseModule.vue

@@ -7,7 +7,7 @@
 		</view>
 		<view v-show="!down">
 			<view v-for="(itemM,indexM) in list" :key="indexM">
-				<courseChapter :preItem="list[indexM - 1] || preItem" :needOpen="needOpen" @toDo="toDo($event)" :courseId="courseId" :learningOrder="learningOrder" :goodsId="goodsId" :isBuy="isBuy" :gradeId="gradeId" :isRebuild="isRebuild"  :menuItem="itemM" :levelId="levelId+'-'+itemM.chapterId"></courseChapter>
+				<courseChapter :preItem="list[indexM - 1] || preItem" :needOpen="needOpen" @playEnd="playEnd($event)" @toDo="toDo($event)" :courseId="courseId" :learningOrder="learningOrder" :goodsId="goodsId" :isBuy="isBuy" :gradeId="gradeId" :isRebuild="isRebuild"  :menuItem="itemM" :levelId="levelId+'-'+itemM.chapterId"></courseChapter>
 				<u-line v-if="indexM<list.length-1"></u-line>
 			</view>
 		</view>
@@ -79,6 +79,9 @@ export default {
 		}
 	},
 	methods: {
+		playEnd(isRebuild) {
+			this.$emit('playEnd',{isRebuild:isRebuild.isRebuild})
+		},
 		toDo(item) {
 			this.$emit('toDo',item)
 		},

+ 10 - 0
pages/index/index.vue

@@ -133,6 +133,8 @@
 
 <script>
 import { mapGetters, mapActions } from 'vuex';
+import { websocket } from '@/common/socket.js';
+import {socket_url} from '@/common/request.js';
 export default {
 	components: {},
 	data() {
@@ -189,10 +191,18 @@ export default {
 		if(this.$method.isLogin()) {
 			try  {
 				await this.getUserInfo()
+				websocket.sokcet(socket_url+this.userInfo.userId+'-'+this.$method.getRandomString(6))
 			}catch(err) {
 				
 			}
 			
+		} else {
+			if(uni.getStorageSync('needToLogin')) {
+				uni.removeStorageSync('needToLogin');
+				uni.navigateTo({
+					url:'/pages/login/login'
+				})
+			}
 		}
 		
 		this.init()

+ 2 - 1
pages/wd/index.vue

@@ -96,6 +96,7 @@
 </template>
 
 <script>
+import { websocket } from '@/common/socket.js';
 import { mapGetters } from 'vuex';
 export default {
 	components: {},
@@ -131,7 +132,7 @@ export default {
 			});
 		},
 		logout() {
-			console.log(343);
+			websocket.stop()
 			this.$method.exit();
 			this.isLogin = this.$method.isLogin();
 			

+ 1 - 0
pages2/bank/detail.vue

@@ -93,6 +93,7 @@
 </template>
 
 <script>
+import { websocket } from '@/common/socket.js';
 import { mapGetters } from 'vuex';
 export default {
 	data() {

+ 19 - 5
pages2/order/confirm_pay.vue

@@ -44,7 +44,7 @@
 		</view>
 		<button class="bottomBtn" @click="pay()" :disabled="btnNo">确认支付</button>
 		
-		<u-modal v-model="showModal" confirm-text="返回购物车" cancel-text="知道了" cancel-color="#666666" confirm-color="rgba(0, 122, 255, 1);" :show-confirm-button="showConfirmButton" :show-cancel-button="true" :content="modalMsg" @cancel="modalCancel()" @confirm="modalConfirm()" ref="uModal" ></u-modal>
+		<u-modal v-model="showModal" :confirm-text="confirmText" cancel-text="知道了" cancel-color="#666666" confirm-color="rgba(0, 122, 255, 1);" :show-confirm-button="showConfirmButton" :show-cancel-button="true" :content="modalMsg" @cancel="modalCancel()" @confirm="modalConfirm()" ref="uModal" ></u-modal>
 	</view>
 	
 </template>
@@ -77,7 +77,8 @@ export default {
 			fromCart:'',
 			hasPaying:false,
 			showConfirmButton:false,
-			totalPrice:0
+			totalPrice:0,
+			confirmText:''
 		};
 	},
 	onPullDownRefresh(){
@@ -125,6 +126,7 @@ export default {
 			let data = {goodsList:list}
 			
 			this.$api.placeSmallOrder(data).then(res => {
+				console.log(res,'res')
 				if(res.data.code==200){
 					uni.setStorageSync('updateCart',1) //提醒刷新购物车
 					let data = res.data.data
@@ -136,6 +138,7 @@ export default {
 					    paySign: data.sign,
 						timeStamp: String(data.timeStamp),
 					    success: function (res) {
+							
 							self.btnNo = false
 							uni.redirectTo({
 							    url: `/pages2/order/confirm_success?sn=${data.orderSn}&isBk=${self.isBK}`
@@ -153,12 +156,15 @@ export default {
 					this.modalMsg = res.data.msg;
 					this.showConfirmButton = true;
 					this.confirmText = "跳转到【我的订单】\n查看未支付订单";
+					this.showModal = true;
 				} else if(res.data.code == 511) { //511 重复购买
 					self.btnNo = false
 					this.modalMsg = res.data.msg;
 					this.showConfirmButton = true;
+					this.showModal = true;
 					
 					if(this.fromCart) {
+						console.log(this.fromCart)
 						this.confirmText = "返回购物车";
 					} else {
 						this.confirmText = "继续选课";
@@ -176,9 +182,17 @@ export default {
 			this.showModal = false;
 		},
 		modalConfirm() {
-			uni.switchTab({
-				url:'/pages/shopping/shoppingCart'
-			})
+			if(this.fromCart) {
+				
+				uni.switchTab({
+					url:'/pages/shopping/shoppingCart'
+				})
+			} else {
+				
+				uni.switchTab({
+					url:'/pages/course/index'
+				})
+			}
 		},
 		getOpenid(code){
 			let self = this