<template>
	<view>
		<view class="">
			<web-view style="background-color:deeppink;height: 500rpx;" ref="webview" :src="webUrl"></web-view>
		</view>
	</view>
</template>

<script>
	import api from "@/pages/api/api.js"
	var wv=null;//计划创建的webview
	export default {
		data() {
			return {
				//webUrl: '/hybrid/html/voices/index.html?',
				webUrl: '',
				isCaller:false,
				offer:'',
				cid:'',
				imUser:'',
				userId:'',
				ddmark1:0,
				ddmark2:0,
				ddmark3:0,
				jsthmark:0
			}
		},
		onLoad(option) {
			var language = uni.getStorageSync('language');
			this.webUrl='/hybrid/html/voices/audio.html?language='+language;
			//this.webUrl='/hybrid/html/voices/index.html?';
			plus.camera.getCamera();
			const a = plus.audio.getRecorder();
			a.record();
			a.stop();
			if(option.caller==1){
				this.isCaller=true;
				this.imUser = uni.getStorageSync('imUser');
				this.userId=this.imUser.userId;
				//this.ringoffImCall();
			}
			uni.onPushMessage((res) => {
				console.log('onPushMessage',res);
				var content=res.data.content;
				plus.runtime.setBadgeNumber(0);
				if(content.indexOf("发起通话")!=-1){
					if(that.ddmark1!=0){
						this.getCallmsg();
					}
					return;
				}
				if(content.indexOf("通话已被")!=-1){
					if(that.jsthmark==0){
						that.jsthmark=1;
						if(wv){
							wv.evalJS('duifangguaduan()');
						}
					}
				}
			});
			var that = this;
			console.log('plus.globalEvent.addEventListener')
			plus.globalEvent.addEventListener('plusMessage', function(msg){
				console.log('----',msg.data.args.data.arg)//web传来的参数
				if(msg.data.args.data.arg){
					var type = msg.data.args.data.arg.type;
					if(type==99){//webviewready
						if(that.ddmark1==0){
							that.ddmark1=1;
							setTimeout(function() {
								that.webviewonReady();
							}, 600); //需要延时一下，防抖动
						}
						return;
					}
					if(type==88){
						uni.navigateBack();
						return;
					}
					if(type==2){
						console.log('222',that.ddmark2)
						if(that.ddmark2==0){
							that.ddmark2=1;
							that.upDatapushmsg(that.userId,msg.data.args.data.arg)
						}
					}
					if(type==3){
						console.log('333',that.ddmark3)
						if(that.ddmark3==0){
							that.ddmark3=1;
							setTimeout(function() {
								//that.pushMsg(that.imUser.cid,msg.data.args.data.arg.data);
								that.upDatapushmsg(that.userId,msg.data.args.data.arg)
							}, 600); //需要延时一下，防抖动
						}
					}
				}
			});
		},
		onShow() {
			getApp().globalData.imViewOpen=true;
		},
		onUnload() {
			//plus.globalEvent.removeEventListener('plusMessage');
			this.ringoffImCall();
			getApp().globalData.imViewOpen=false;
			console.log('onUnload');
		},
		methods: {
			webviewonReady(){
				var currentWebview = this.$scope.$getAppWebview() //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效
				if(currentWebview==null){
					return;
				}
				wv = currentWebview.children()[0];
				if(this.isCaller){
					var that = this;
					var obj={
						payload:'',
						type:1
					};
					var str = JSON.stringify(obj);
					console.log(obj);
					wv.evalJS('appAct('+str+')');
				}
				else{
					this.getCallmsg();
				}
			},
			plusMessage(msg){
				
			},
			upDatapushmsg(userID,payload){
				console.log(userID,payload)
				var content={
					userID:userID,
					content:payload
				}
				api('userCall',{
					callId:userID,
					content:JSON.stringify(content)
				},res=>{
					console.log(res)
					
				},failc=>{
					//console.log('getadvertis----',failc)
				})
			},
			getCallmsg(){
				api('getCall',{
				},res=>{
					console.log(res)
					if(res.data.code==200){
						if(this.isCaller){
							var answer = res.data.data.answer;
							this.userId=answer.userID;
							var obj={
								payload:answer.content.data,
								type:answer.content.type
							};
							var str = JSON.stringify(obj);
							wv.evalJS('appAct('+str+')');
							return
						}
						var content = res.data.data.content;
						this.userId=content.userID;
						var obj={
							payload:content.content.data,
							type:content.content.type
						};
						var str = JSON.stringify(obj);
						wv.evalJS('appAct('+str+')');
					}
					
				},failc=>{
					//console.log('getadvertis----',failc)
				})
			},
			ringoffImCall(){
				api('ringoff',{
				},res=>{
					console.log(res)
				},failc=>{
					//console.log('getadvertis----',failc)
				})
			}
		}
	}
</script>

<style>

</style>
