<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:'',
				ddmark:0
			}
		},
		onLoad(option) {
			var language = uni.getStorageSync('language');
			this.webUrl='/hybrid/html/voices/audio.html?language='+language;
			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){
					this.getCallmsg();
				}
			});
			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.ddmark==0){
							that.ddmark=1;
							setTimeout(function() {
								that.webviewonReady();
								that.ddmark=0;
							}, 600); //需要延时一下，防抖动
						}
						
						return;
					}
					if(type==88){
						uni.navigateBack();
						return;
					}
					if(type==2||type==3){
						if(that.ddmark==0){
							that.ddmark=1;
							setTimeout(function() {
								//that.pushMsg(that.imUser.cid,msg.data.args.data.arg.data);
								that.upDatapushmsg(that.userId,msg.data.args.data.arg)
								that.ddmark=0;
							}, 600); //需要延时一下，防抖动
						}
					}
				}
			    
			})
		},
		onShow() {
			getApp().globalData.imViewOpen=true;
		},
		onUnload() {
			getApp().globalData.imViewOpen=false;
		},
		methods: {
			webviewonReady(){
				var currentWebview = this.$scope.$getAppWebview() //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效
				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();
				}
			},
			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>
