From de3edb50d18b549fc47adfc3dcc4f6833cbe04c4 Mon Sep 17 00:00:00 2001
From: 罗广辉 <guanghui.luo@foxmail.com>
Date: Mon, 17 Nov 2025 13:54:22 +0800
Subject: [PATCH] feat: 接入全局ws,接入腾讯音频通话
---
src/config/env.js | 3
src/api/user/index.js | 11 ++
src/App.vue | 154 +++++++++++-------------------
src/utils/voiceCallByTX/index.js | 36 +++++++
src/hooks/useGlobalWS.js | 70 ++++++++++++++
5 files changed, 176 insertions(+), 98 deletions(-)
diff --git a/src/App.vue b/src/App.vue
index e7028be..83a564a 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -1,116 +1,76 @@
<script setup>
- import {onHide, onLaunch, onShow} from "@dcloudio/uni-app";
- import {useAppStore, useUserStore} from "@/store";
- import {genTestUserSig} from '@/utils/voiceCallByTX/GenerateTestUserSig.js';
+import {onHide, onLaunch, onShow} from "@dcloudio/uni-app";
+import {useAppStore, useUserStore} from "@/store";
+import {useGlobalWS} from "@/hooks/useGlobalWS.js";
+const appStore = useAppStore();
+const userStore = useUserStore();
+useGlobalWS()
- const appStore = useAppStore();
- const userStore = useUserStore();
- const userId = computed(() => userStore?.userInfo?.user_id)
+onShow(() => {
+ console.log("App Show");
+});
+onHide(() => {
+ console.log("App Hide");
+});
-
- onLaunch(() => {
- // 初始化系统信息
- appStore.initSystemInfo();
-
- if (!userStore.userInfo) {
- //不存在则跳转至登录页
- uni.reLaunch({
- url: "/pages/login/index",
- })
- }
- });
-
- // #ifdef APP-PLUS
- import TrtcCloud from '@/utils/voiceCallByTX/TrtcCloud/lib/index';
- import { TRTCAppScene } from '@/utils/voiceCallByTX/TrtcCloud/lib/TrtcDefines';
- const trtcCloud = TrtcCloud.createInstance();
- function enterRoomFun() {
- const {userSig, SDKAppID} = genTestUserSig(userId.value);
- const params = {sdkAppId: SDKAppID, userId: userId.value, roomId: 789456, userSig: userSig};
- trtcCloud.enterRoom(params, TRTCAppScene.TRTCAppSceneVideoCall)
- trtcCloud.startLocalAudio();
+onLaunch(() => {
+ // 初始化系统信息
+ appStore.initSystemInfo();
+ if (!userStore.userInfo) {
+ //不存在则跳转至登录页
+ uni.reLaunch({
+ url: "/pages/login/index",
+ })
}
- // #endif
-
- // #ifdef H5
- import TRTC from 'trtc-sdk-v5';
- const trtc = TRTC.create();
- function enterRoomFunByH5() {
- const {userSig, SDKAppID} = genTestUserSig(userId.value);
- trtc.enterRoom({ sdkAppId:SDKAppID, userId:userId.value, userSig, roomId: 789456 })
- trtc.startLocalAudio()
- }
- // #endif
-
- // 初始化登陆腾讯音频平台
- watch(userId, () => {
- if (!userId.value) return
- // #ifdef APP-PLUS
- // enterRoomFun()
- // #endif
- // #ifdef H5
- // enterRoomFunByH5()
- // #endif
-
- }, {
- immediate: true
- })
-
- onShow(() => {
- console.log("App Show");
- });
-
- onHide(() => {
- console.log("App Hide");
- });
+});
</script>
<style lang="scss">
- /* 每个页面公共css */
- @import "uview-plus/index.scss";
- @import "@/static/styles/common.scss";
+/* 每个页面公共css */
+@import "uview-plus/index.scss";
+@import "@/static/styles/common.scss";
- .TUICallKit-mobile {
- position: fixed;
- left: 0;
- top: 0;
- z-index: 9999;
- }
+.TUICallKit-mobile {
+ position: fixed;
+ left: 0;
+ top: 0;
+ z-index: 9999;
+}
- page {
- background-image: url('~@/static/images/user/bg.png');
- background-size: cover;
- background-position: center;
- background-repeat: no-repeat;
- background-attachment: fixed;
- // min-height: 10vh;
- }
+page {
+ background-image: url('~@/static/images/user/bg.png');
+ background-size: cover;
+ background-position: center;
+ background-repeat: no-repeat;
+ background-attachment: fixed;
+ // min-height: 10vh;
+}
- .uni-tabbar__item:nth-child(4) {
- .uni-tabbar__bd {
- .uni-tabbar__icon {
- width: 60px !important;
- height: 60px !important;
- margin-top: -20px;
- }
+.uni-tabbar__item:nth-child(4) {
+ .uni-tabbar__bd {
+ .uni-tabbar__icon {
+ width: 60px !important;
+ height: 60px !important;
+ margin-top: -20px;
}
}
+}
- .uni-tabbar-border {
- background-color: #f6f6f6 !important;
- }
+.uni-tabbar-border {
+ background-color: #f6f6f6 !important;
+}
- .uni-page-head__title {
- font-family: Source Han Sans CN, Source Han Sans CN !important;
- font-weight: 400 !important;
- font-size: 17px !important;
- color: #222324 !important;
- }
+.uni-page-head__title {
+ font-family: Source Han Sans CN, Source Han Sans CN !important;
+ font-weight: 400 !important;
+ font-size: 17px !important;
+ color: #222324 !important;
+}
- .uni-page-head {
- background: transparent !important;
- }
+.uni-page-head {
+ background: transparent !important;
+}
</style>
diff --git a/src/api/user/index.js b/src/api/user/index.js
index e5392f7..7597dc1 100644
--- a/src/api/user/index.js
+++ b/src/api/user/index.js
@@ -68,4 +68,13 @@
newPassword1,
},
});
-};
\ No newline at end of file
+};
+
+// 发送通话信息
+export const sendVoiceCallApi = (data) => {
+ return request({
+ url: `/drone-device-core/dp/call/sendVoiceCall`,
+ method: 'post',
+ data
+ })
+}
diff --git a/src/config/env.js b/src/config/env.js
index 3de6b92..b9da4ec 100644
--- a/src/config/env.js
+++ b/src/config/env.js
@@ -6,18 +6,21 @@
VITE_APP_WEBVIEW_URL: 'http://localhost:5173/drone-app-web-view/#/webViewWrapper',
VITE_API_BASE_URL: 'https://wrj.shuixiongit.com/api',
// VITE_API_BASE_URL: 'https://aisky.org.cn/api',
+ VITE_APP_WS_API_URL:'wss://wrj.shuixiongit.com/drone-wss/api/v1/ws'
}
const test = {
VITE_APP_ENV:'test',
VITE_APP_WEBVIEW_URL: 'https://wrj.shuixiongit.com/drone-app-web-view/#/webViewWrapper',
VITE_API_BASE_URL: 'https://wrj.shuixiongit.com/api',
+ VITE_APP_WS_API_URL:'wss://wrj.shuixiongit.com/drone-wss/api/v1/ws'
}
const production = {
VITE_APP_ENV:'production',
VITE_APP_WEBVIEW_URL: 'https://aisky.org.cn/drone-app-web-view/#/webViewWrapper',
VITE_API_BASE_URL: 'https://aisky.org.cn/api',
+ VITE_APP_WS_API_URL:'wss://wrj.shuixiongit.com/drone-wss/api/v1/ws'
}
export default {
diff --git a/src/hooks/useGlobalWS.js b/src/hooks/useGlobalWS.js
new file mode 100644
index 0000000..450c29a
--- /dev/null
+++ b/src/hooks/useGlobalWS.js
@@ -0,0 +1,70 @@
+import {useUserStore} from "@/store/index.js";
+import {getEnvObj} from "@/utils/index.js";
+import {enterRoom} from "@/utils/voiceCallByTX/index.js";
+
+let socketTask = null
+
+export function useGlobalWS() {
+ const userStore = useUserStore();
+ const userId = computed(() => userStore?.userInfo?.user_id)
+ const access_token = computed(() => userStore?.userInfo?.access_token)
+ const {VITE_APP_WS_API_URL} = getEnvObj()
+
+ // 消息处理
+ function messageHandler(payload) {
+ switch (payload.biz_code) {
+ case 'JOB_ISREFRESH':
+ break
+ case 'DEVICE_ISREFRESH':
+ break
+ case 'DOWNLOAD_PROGRESS':
+ break
+ case 'LOGOUT_USER':
+ userStore.setUserInfo(null)
+ uni.reLaunch({
+ url: '/pages/login/index'
+ })
+ break
+ case 'VoiceCall':
+ enterRoom(payload, userId.value)
+ break
+ default:
+ break;
+ }
+ }
+
+ // 关闭ws
+ function closeWS() {
+ socketTask?.close({
+ success: () => {
+ console.log('ws关闭连接');
+ },
+ })
+ }
+
+ // 初始化ws
+ function initWS() {
+ // 关闭,再连接ws
+ closeWS()
+ if (!access_token.value) return
+ const url = VITE_APP_WS_API_URL
+ + `?x-auth-token=${encodeURI(access_token?.value)}`
+ + `&model_type=3&workspace-id=${userId.value}`
+ // 创建连接
+ socketTask = uni.connectSocket({
+ url: url,
+ success: () => {
+ console.log('ws连接成功');
+ },
+ fail: (err) => {
+ console.error('ws连接失败:', err);
+ }
+ });
+ // 消息监听
+ socketTask.onMessage((result) => {
+ messageHandler(JSON.parse(result.data))
+ })
+ }
+
+ watch(access_token, initWS, {immediate: true})
+}
diff --git a/src/utils/voiceCallByTX/index.js b/src/utils/voiceCallByTX/index.js
new file mode 100644
index 0000000..4a0fa2a
--- /dev/null
+++ b/src/utils/voiceCallByTX/index.js
@@ -0,0 +1,36 @@
+// #ifdef APP-PLUS
+import TrtcCloud from '@/utils/voiceCallByTX/TrtcCloud/lib/index';
+import {TRTCAppScene} from '@/utils/voiceCallByTX/TrtcCloud/lib/TrtcDefines';
+
+const trtcCloud = TrtcCloud.createInstance();
+// #endif
+
+// #ifdef H5
+import TRTC from 'trtc-sdk-v5';
+
+const trtc = TRTC.create();
+// #endif
+
+import {genTestUserSig} from "@/utils/voiceCallByTX/GenerateTestUserSig.js";
+import {sendVoiceCallApi} from "@/api/user/index.js";
+
+export function enterRoom(payload, userId) {
+ const data = JSON.parse(payload.data.data)
+ const theOtherPartyId = payload.data.caller_id
+ const {userSig, SDKAppID: sdkAppId} = genTestUserSig(userId);
+ const params = {sdkAppId, userId, roomId: data.roomId, userSig};
+// #ifdef APP-PLUS
+ trtcCloud.enterRoom(params, TRTCAppScene.TRTCAppSceneVideoCall)
+ trtcCloud.startLocalAudio();
+// #endif
+// #ifdef H5
+ trtc.enterRoom(params)
+ trtc.startLocalAudio()
+// #endif
+ // demo原因,直接接通来进行验证测试
+ sendVoiceCallApi({
+ caller_id: userId,
+ receiver_id: theOtherPartyId,
+ data: JSON.stringify({state: '接通', roomId: data.roomId}),
+ })
+}
--
Gitblit v1.9.3