<script>
	export default {
		onLaunch: function() {
			let timer = null;
			//socket 
			uni.$on("openSocket",function(res){
				uni.connectSocket({
					url: 'wss://api.lyiyuan.cn/websocket?userId='+uni.getStorageSync('userInfo').id
				});
				uni.onSocketOpen(function (res) {
				  xintiao()
				  uni.sendSocketMessage({data:JSON.stringify({"userId": uni.getStorageSync("userInfo").id,"topic":"heart_beat"})});
				  console.log('WebSocket连接已打开！');
				  uni.setStorageSync("isOpen",true)
				});
				uni.onSocketMessage(function (res) {
					const obj = res.data?JSON.parse(res.data):{};
					console.log(obj)
					if(obj.topic == 'message'){
						if(obj.type>=8){
							if(uni.getStorageSync("userInfo").id == obj.sellerId){
								if(obj.type == 12){
									uni.$emit("watchEvent",obj)
								}
								if(obj.type == 9){
									if(obj.userMsgVoice == 1){
										uni.$emit("playAudio")
									}
									if(obj.userMsgShock == 1){
										uni.vibrateLong({
											complete: function (res) {
												console.log('success',res);
											}
										});
									}
									uni.showModal({
										title: '温馨提示',
										content: '来自'+obj.userNickname+"发起的视频匹配，是否同意",
										cancelText: '拒绝',
										confirmText: '同意',
										success: res => {
											if(res.confirm){
												console.log(obj, uni.getStorageSync("userInfo"))
												uni.sendSocketMessage({data:JSON.stringify({
													"userId": uni.getStorageSync("userInfo").id,
													sellerId: obj.userId, 
													type: 10,
													"topic":"message"
												})})
												if(uni.getStorageSync("userInfo").id == obj.sellerId){
													console.log("toChat",uni.getStorageSync("userInfo").id , obj.userId)
													uni.navigateTo({
														url: "/pages/pipei/shipin/detail?type=2&id="+obj.userId
													})
												}
											}else{
												uni.sendSocketMessage({data:JSON.stringify({
													"userId": uni.getStorageSync("userInfo").id,
													sellerId: obj.userId, 
													type: 11,
													"topic":"message"
												})})
											}
										},
										fail: () => {},
										complete: () => {}
									});
								}
								if(obj.type == 8){
									if(obj.userMsgVoice == 1){
										uni.$emit("playAudio")
									}
									if(obj.userMsgShock == 1){
										uni.vibrateLong({
											complete: function (res) {
												console.log('success',res);
											}
										});
									}
									uni.showModal({
										title: '温馨提示',
										content: '来自'+obj.userNickname+"发起的语音匹配，是否同意",
										cancelText: '拒绝',
										confirmText: '同意',
										success: res => {
											if(res.confirm){
												uni.sendSocketMessage({data:JSON.stringify({
													"userId": uni.getStorageSync("userInfo").id,
													sellerId: data.id, 
													type: 10,
													"topic":"message"
												})})
											}else{
												uni.sendSocketMessage({data:JSON.stringify({
													"userId": uni.getStorageSync("userInfo").id,
													sellerId: data.id, 
													type: 11,
													"topic":"message"
												})})
											}
										},
										fail: () => {},
										complete: () => {}
									});
								}
							}
						}
					}
				});
				uni.onSocketClose(function (res) {
					clearInterval(timer)
					uni.closeSocket();
					uni.removeStorageSync("isOpen")
					console.log('WebSocket 已关闭！');
					setTimeout(()=>{
						if(uni.getStorageSync("userInfo").id){
							uni.$emit("openSocket")
						}
					},1000)
				});
			})
			
			function xintiao() {
				timer = setInterval(()=>{
					uni.setStorageSync("isOpen",true)
					uni.sendSocketMessage({data:JSON.stringify({"userId": uni.getStorageSync("userInfo").id,"topic":"heart_beat"})});
				}, 30000)
				
			}
			
			setTimeout(()=>{
				if(uni.getStorageSync("userInfo").id){
					uni.$emit("openSocket")
				}
			},1000)
			
			const bgAudioManager = uni.getBackgroundAudioManager();
			uni.$on("playAudio",res=>{
				bgAudioManager.src = 'https://api.lyiyuan.cn/profile/upload/static/tishiyin.mp3';
				bgAudioManager.play();
			})
		},
		onShow: function() {
			
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	.permissions_box{
		padding: 50rpx 30rpx;
		font-size: 30rpx;
		line-height: 50rpx;
		background-color: #fff;
	}
	/*每个页面公共css */
	page{
		background-color: #F3F4F5;
	}
</style>
