吉安感知网项目-前端
罗广辉
6 days ago 1d552a3bad95caae4af15322df28e6240b797693
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!-- 登录页 -->
<template>
  <WebViewPlus
    ref="sWebViewRef"
    :src="`${viewUrl}`"
    @webMessage="onPostMessage"
  />
</template>
 
<script setup>
import { useUserStore } from "@/store/index.js"
import { getWebViewUrl } from "@/utils/index.js";
import { onLoad } from "@dcloudio/uni-app";
import {cancelIncomingCallNotification } from '@/uni_modules/lgh-dialog'
 
const userStore = useUserStore()
const userParams = userStore?.userInfo ? JSON.stringify(userStore.userInfo) : '{}'
const sWebViewRef = ref(null);
const viewUrl = ref("");
const typeSign = ref('')
// #ifdef APP-PLUS
/**
 * 请求 Android 麦克风权限
 * @returns {Promise<boolean>} 权限是否授予
 */
async function requestAndroidMicPermission() {
  try {
    // 获取麦克风权限
    return await new Promise((resolve) => {
      plus.android.requestPermissions(
        ['android.permission.RECORD_AUDIO'],
        (result) => {
          if (result.granted.length > 0) {
            console.log('麦克风权限已获取')
            resolve(true)
            return
          }
          if (result.deniedAlways.length > 0) {
            uni.showModal({
              title: '提示',
              content: '需要麦克风权限才能正常使用语音功能,请前往系统设置开启',
              success(res) {
                if (res.confirm) {
                  plus.runtime.openURL('app-settings:')
                }
              },
            })
          }
          resolve(false)
        },
        (error) => {
          console.error('请求麦克风权限失败', error)
          resolve(false)
        }
      )
    })
  } catch (error) {
    console.error('❌ 请求麦克风权限失败', error)
    return false
  }
}
 
// #endif
 
onLoad(async (options) => {
  // #ifdef APP-PLUS
  // 在加载 WebView 前先请求麦克风权限
  console.log('📞 语音通话页面加载,请求麦克风权限')
  const hasPermission = await requestAndroidMicPermission()
  if (!hasPermission) {
    console.warn('⚠️ 未获得麦克风权限,通话功能可能无法使用')
  }
  // #endif
 
  // 解析传递过来的contact参数
  const contact = options.contact ? JSON.parse(decodeURIComponent(options.contact)) : null;
  // 构建viewUrl,将contact参数拼接到URL中
  const contactParam = contact ? encodeURIComponent(JSON.stringify(contact)) : '';
  viewUrl.value = getWebViewUrl("/voiceCallDetail", { contact: options.contact ,voiceparams: options.voiceparams});
});
 
function onPostMessage(data) {
typeSign.value = data?.sign || ''
if(typeSign.value === 'closeBox'){
    setTimeout(() => {
    // 关闭弹窗
    cancelIncomingCallNotification()
    }, 1000)
 
}
    // 将WebView消息传递给全局WebSocket处理
    if (typeof uni !== 'undefined' && uni.$emit) {
        uni.$emit('webViewMessage', data);
    }
}
 
// 监听离开
onUnload(() => {
    // #ifdef APP-PLUS
    cancelIncomingCallNotification()
    // #endif
})
 
</script>
 
<style lang="scss" scoped>
 
</style>