App.vue 10 KB
<!-- * WanlShop - App全局模板配置
 * @author 深圳前海万联科技有限公司 <wanlshop@i36k.com> 
 * @link http://www.wanlshop.com
 * 
 * @本程序受法律保护,著作权©软著登记号2020SR0255711 < 未经版权所有权人书面许可,不能用于商业用途!>
 * 
 * @获取许可
 * 后台下载:https://www.fastadmin.net/store/wanlshop.html
 * 帮助文档:https://doc.fastadmin.net/wanlshop
 * 
 * @stress 本程序仅用作FastAdmin付费插件(WanlShop B2B2C商城)API使用,未经版权所有权人书面许可,不能自行用于商业用途
 * @creationtime  2019年9月10日12:52:20
 * @lasttime 2020年6月9日09:26:07
 * @version 1.0.0
 -->
<script> 
	import Vue from 'vue';
	/*#ifdef APP-PLUS*/
	import Aegis from 'aegis-weex-sdk';
	/*#endif*/
	import config from '@/common/config/config.js'
	import service from '@/service/service.js'
	import API from '@/apis/index.js'
	export default {
		onLaunch() {
			// 检测客户端更新
			this.$store.dispatch('update/update');
			// 仅首次打开一次性加载数据
			this.$store.dispatch('common/init');
			// 同步购物车 本地 & 网络
			this.$store.dispatch('cart/get');
			// 同步 动态数据
			this.$store.dispatch('statistics/get');

			const projectName = 'uniappApiExampleExt';
			uni.$aegis = new Aegis({
				id: 'iHWefAYqWuCkDjhucc',
				spa: true,
				reportApiSpeed: true,
				reportAssetSpeed: true,
				pagePerformance: true,
				hostUrl: 'https://tamaegis.com',
			});
			uni.$uploadToTAM = (eventString, sdkAppId) => {
				uni.$aegis.reportEvent({
					name: eventString.split('#')[0] || '',
					ext1: eventString,
					ext2: projectName,
					ext3: sdkAppId,
				});
			};
			uni.$createTrtcCloudUpload = sdkAppId => uni.$uploadToTAM('createTrtcCloud', sdkAppId);
			uni.$enterRoomSuccessUpload = sdkAppId => uni.$uploadToTAM('enterRoom-success', sdkAppId);
			uni.$enterRoomFailedUpload = (sdkAppId, errorMsg) => uni.$uploadToTAM(`enterRoom-failed#error: ${errorMsg}`,
				sdkAppId);
			uni.$onErrorUpload = (sdkAppId, errorMsg) => uni.$uploadToTAM(`onError-failed#error: ${errorMsg}`, sdkAppId);
			uni.$startLocalPreviewUpload = sdkAppId => uni.$uploadToTAM('startLocalPreview', sdkAppId);
			uni.$startRemoteViewSuccessUpload = sdkAppId => uni.$uploadToTAM('startRemoteView-success', sdkAppId);
			uni.$startRemoteViewFailedUpload = (sdkAppId, errorMsg) => uni.$uploadToTAM(
				`startRemoteView-failed#error: ${errorMsg}`, sdkAppId);
			this.initSocket();
		},
		onShow() {
			// 启动或重启即时通讯
			// this.$store.dispatch('chat/start');
		},
		onHide() {
			// 断开即时通讯
			// this.$store.dispatch('chat/close');
		},
		methods: {
			handleOnMessage(msg) {
				let user = this.$store.state.user;
				if(msg.method == 'initMessage'){
					// this.setTabBarBadge(msg.unreadMessageCount);
				}
				if(msg.messageType == 'messageRead' || msg.messageType == 'text' || msg.messageType == 'voice'){
					getApp().globalData.updateUnReadMessageCount();
				}
				
				if (msg.messageType == 'joinRoom' && msg.intToUserId == user.id && user.user_type.length) {
					console.log('joinRoom', msg);
					uni.showModal({
						content: `用户【${msg.realName}】邀请你视频连线,是否加入房间?`,
						confirmText: '加入房间',
						cancelText: '放弃',
						confirmColor: '#D669BE',
						success({
							confirm
						}) {
							if (confirm) {
								uni.navigateTo({
									url: `/pages/meeting/meeting?roomId=${msg.roomNo}&joinType=seller&customerid=${msg.intUserId}`
								})
							}
						}
					})
				}
				if (msg.messageType == 'joinMultiplayerVideo' && msg.intToUserId == user.id) {
					console.log('joinMultiplayerVideo', msg);
					uni.showModal({
						content: `用户【${msg.realName}】邀请你视频连线,是否加入房间?`,
						confirmText: '加入房间',
						cancelText: '放弃',
						confirmColor: '#D669BE',
						success({
							confirm
						}) {
							if (confirm) {
								uni.navigateTo({
									url: `/pages/meeting/meeting?roomId=${msg.roomNo}&joinType=service&customerid=${msg.intUserId}`
								})
							}
						}
					})
				}
				if (msg.messageType == 'joinVideo' && msg.intToUserId == user.id) {
					uni.showModal({
						content: `用户【${msg.realName}】邀请你视频连线,是否加入房间?`,
						confirmText: '加入',
						cancelText: '放弃',
						confirmColor: '#D669BE',
						success({
							confirm
						}) {
							if (confirm) {
								uni.navigateTo({
									url: `/pages/meeting/serviceMeeting?roomId=${msg.roomNo}&userid=${msg.intUserId}`
								})
							}
						}
					})
				}
			},
			initSocket() {
				let socketTask = uni.connectSocket({
					url: config.socketurl,
					protocols: ['protocol1'],
					fail(err){
						console.log('connectSocket fail',err);
					},
					complete() {},
				});
				uni.onSocketOpen((res) => {
					console.log('WebSocket连接已打开!', res);
				});
				uni.onSocketError((res) => {
					// console.log('WebSocket连接打开失败,请检查!', res);
					this.initSocket();
				});
				this.globalData.socketTask = socketTask;
				socketTask.onOpen((data) => {
					let json = {
						method: 'OnConnection',
						token: service.getTokenChaoge(),
						mobileDevice: true
					};
					console.log('send json', json);
					socketTask.send({
						data: JSON.stringify(json),
						success(res){
							console.log('socketTask.send success',res);
						},
						fail(err) {
							console.log('socketTask.send fail',err);
							this.initSocket();
						}
					})
				});
				socketTask.onClose((res)=>{
					console.log('socketTask.onClose', res)
					// this.initSocket();
				})
				socketTask.onError((error) => {
					// console.log('socketTask.onError', error)
					// this.initSocket();
				});
				socketTask.onMessage((msg) => {
					console.log('socketTask.onMessage', msg);
					msg = JSON.parse(msg.data);
					this.handleOnMessage(msg);
					uni.$emit('socketMessage', msg)
				});
				uni.$on('closeSocket', () => {
					console.log('uni.$on closeSocket',)
					this.globalData.socketTask.close();
				});
				uni.$on('openSocket', () => {
					this.globalData.socketTask.onMessage((msg) => {
						console.log('openSocket.onMessage', msg);
					});
				});
				this.globalData.updateUnReadMessageCount();
			},
		},
		globalData: {
			typeNum: 1,
			socketTask: {},
			handleOnMessage(msg) {
				let user = this.$store.state.user;
				if(msg.method == 'initMessage'){
					// this.setTabBarBadge(msg.unreadMessageCount);
				}
				if(msg.messageType == 'messageRead' || msg.messageType == 'text' || msg.messageType == 'voice'){
					getApp().globalData.updateUnReadMessageCount();
				}
				
				if (msg.messageType == 'joinRoom' && msg.intToUserId == user.id && user.user_type.length) {
					console.log('joinRoom', msg);
					uni.showModal({
						content: `用户【${msg.realName}】邀请你视频连线,是否加入房间?`,
						confirmText: '加入房间',
						cancelText: '放弃',
						confirmColor: '#D669BE',
						success({
							confirm
						}) {
							if (confirm) {
								uni.navigateTo({
									url: `/pages/meeting/meeting?roomId=${msg.roomNo}&joinType=seller&customerid=${msg.intUserId}`
								})
							}
						}
					})
				}
				if (msg.messageType == 'joinMultiplayerVideo' && msg.intToUserId == user.id) {
					console.log('joinMultiplayerVideo', msg);
					uni.showModal({
						content: `用户【${msg.realName}】邀请你视频连线,是否加入房间?`,
						confirmText: '加入房间',
						cancelText: '放弃',
						confirmColor: '#D669BE',
						success({
							confirm
						}) {
							if (confirm) {
								uni.navigateTo({
									url: `/pages/meeting/meeting?roomId=${msg.roomNo}&joinType=service&customerid=${msg.intUserId}`
								})
							}
						}
					})
				}
				if (msg.messageType == 'joinVideo' && msg.intToUserId == user.id) {
					uni.showModal({
						content: `用户【${msg.realName}】邀请你视频连线,是否加入房间?`,
						confirmText: '加入',
						cancelText: '放弃',
						confirmColor: '#D669BE',
						success({
							confirm
						}) {
							if (confirm) {
								uni.navigateTo({
									url: `/pages/meeting/serviceMeeting?roomId=${msg.roomNo}&userid=${msg.intUserId}`
								})
							}
						}
					})
				}
			},
			initSocket(){
				let socketTask = uni.connectSocket({
					url: config.socketurl,
					protocols: ['protocol1'],
					complete() {},
				});
				uni.onSocketOpen((res) => {
					console.log('WebSocket连接已打开!', res);
				});
				uni.onSocketError((res) => {
					console.log('WebSocket连接打开失败,请检查!', res);
					this.initSocket();
				});
				this.socketTask = socketTask;
				socketTask.onOpen((data) => {
					let json = {
						method: 'OnConnection',
						token: service.getTokenChaoge(),
						mobileDevice: true
					};
					console.log('send json', json);
					socketTask.send({
						data: JSON.stringify(json)
					})
				});
				socketTask.onError((error) => {
					console.log('socketTask.onError', error)
					this.initSocket();
				});
				socketTask.onMessage((msg) => {
					console.log('socketTask.onMessage', msg);
					msg = JSON.parse(msg.data);
					this.handleOnMessage(msg);
					uni.$emit('socketMessage', msg)
				});
				uni.$on('closeSocket', () => {
					this.socketTask.close();
				});
				uni.$on('openSocket', () => {
					this.socketTask.onMessage((msg) => {
						console.log('openSocket.onMessage', msg);
					});
				});
				this.updateUnReadMessageCount();
				return socketTask;
			},
			updateUnReadMessageCount(){
				API.message.getUserUnReadMessageCount().then(res=>{
					console.log('getUserUnReadMessageCount',res);
					let badgeNum = res.data;
					if(badgeNum>0){
						uni.setTabBarBadge({
							index: 1,
							text: badgeNum > 99 ? '99+' : badgeNum.toString()
						});
					}else{
						uni.removeTabBarBadge({
							index: 1,
						});
					}
				})
			}
		}
	};
</script>

<style>
	/* #ifndef APP-NVUE */
	@import 'static/style/colorui.css';
	@import 'static/style/wanlshop.css';
	@import 'static/style/iconfont.css';
	@import url("./styles/index.scss");
	/* #endif */
</style>