<template>
	<view>
		<view class="" @tap='goroom'>
			跳转跳转跳转跳转跳转跳转跳转跳转跳转跳转跳转跳转跳转跳转跳转跳转跳转跳转跳转跳转跳转
		</view>
	</view>
</template>

<script>
	import {
		getUserSig
	} from '@/api/pagesB/solution/solution.js'
	export default {
		data() {
			return {
				src: '',
				userSig: '',
				item: {},
				roomId: '',
				authorizeMic: '',
				authorizeCamera: '',
				hasOpenDeviceAuthorizeModal: '',
			};
		},
		methods: {
			goroom() {
				this.checkDeviceAuthorize().then((result) => {
						console.log('授权成功', result)
						uni.navigateTo({
							url: `/pagesmeeting/room/room?sdkAppID=1600006944&localVideo=true&localAudio=true&userID=${this.item.patientId}&userName=${this.item.receiver}&userSig=${this.userSig}&roomID=${this.roomId}`
						})
					})
					.catch((error) => {
						console.log('没有授权', error)
					})
			},
			checkDeviceAuthorize() {
				this.hasOpenDeviceAuthorizeModal = false
				return new Promise((resolve, reject) => {
					if (!wx.getSetting || !wx.getSetting()) {
						// 微信测试版 获取授权API异常，目前只能即使没授权也可以通过
						resolve()
					}
					wx.getSetting().then((result) => {
						console.log('getSetting', result)
						this.authorizeMic = result.authSetting['scope.record']
						this.authorizeCamera = result.authSetting['scope.camera']
						if (result.authSetting['scope.camera'] && result.authSetting['scope.record']) {
							// 授权成功
							resolve()
						} else {
							// 没有授权，弹出授权窗口
							// 注意： wx.authorize 只有首次调用会弹框，之后调用只返回结果，如果没有授权需要自行弹框提示处理
							console.log('getSetting 没有授权，弹出授权窗口', result)
							wx.authorize({
									scope: 'scope.record',
								}).then((res) => {
									console.log('authorize mic', res)
									this.authorizeMic = true
									if (this.authorizeCamera) {
										resolve()
									}
								})
								.catch((error) => {
									console.log('authorize mic error', error)
									this.authorizeMic = false
								})
							wx.authorize({
									scope: 'scope.camera',
								}).then((res) => {
									console.log('authorize camera', res)
									this.authorizeCamera = true
									if (this.authorizeMic) {
										resolve()
									} else {
										this.openConfirm()
										reject(new Error('authorize fail'))
									}
								})
								.catch((error) => {
									console.log('authorize camera error', error)
									this.authorizeCamera = false
									this.openConfirm()
									reject(new Error('authorize fail'))
								})
						}
					})
				})
			},
			openConfirm() {
				if (this.hasOpenDeviceAuthorizeModal) {
					return
				}
				this.hasOpenDeviceAuthorizeModal = true
				return uni.showModal({
					content: '您没有打开麦克风和摄像头的权限，是否去设置打开？',
					confirmText: '确认',
					cancelText: '取消',
					success: (res) => {
						this.hasOpenDeviceAuthorizeModal = false
						console.log(res)
						// 点击“确认”时打开设置页面
						if (res.confirm) {
							console.log('用户点击确认')
							wx.openSetting({
								success: (res) => {},
							})
						} else {
							console.log('用户点击取消')
						}
					},
				})
			},
			info() {
				getUserSig(this.item.patientId).then(res => {
					this.userSig = res.data
				})
			},
		},
		onLoad(options) {
			this.item = JSON.parse(options.item)
			this.roomId = options.roomId
			this.info()
		}
	}
</script>

<style lang="scss">

</style>
