Files
aistream-test/tra-app/src/pages/test/testChat2vue
T
2025-12-12 21:33:37 +08:00

762 lines
16 KiB
Plaintext

<template>
<view class="page-container">
<!-- 头部标题栏 -->
<view class="header">
<text class="title">摄像头预览</text>
</view>
<!-- 主要内容区域 -->
<view class="main-content">
<!-- 预览窗口容器 -->
<view class="preview-section" v-if="isPreviewing">
<view class="section-title">
<text>摄像头预览</text>
<text class="preview-status">● 预览中</text>
</view>
<!-- 预览窗口 -->
<view class="preview-box">
<live-pusher
id="livePusher"
ref="livePusher"
class="live-preview"
:url="''"
mode="SD"
:autopush="false"
:muted="true"
:enable-camera="true"
:auto-focus="true"
:orientation="orientation"
:beauty="beautyLevel"
:whiteness="whiteLevel"
:aspect="aspectRatio"
:min-bitrate="300"
:max-bitrate="1000"
@statechange="onStateChange"
@error="onError"
@netstatus="onNetStatus"
/>
<!-- 预览窗口边框装饰 -->
<view class="preview-frame"></view>
</view>
<!-- 预览控制按钮 -->
<view class="preview-controls">
<button
class="control-btn switch-btn"
@click="switchCamera"
:disabled="!isPreviewing"
>
🔄 切换摄像头
</button>
<button
class="control-btn close-btn"
@click="closePreview"
>
❌ 关闭预览
</button>
</view>
</view>
<!-- 未预览时的占位区域 -->
<view class="placeholder-section" v-else>
<view class="section-title">
<text>预览功能</text>
</view>
<view class="placeholder-box">
<text class="placeholder-icon">📷</text>
<text class="placeholder-text">点击开始预览体验摄像头功能</text>
</view>
<button class="start-btn" @click="startPreview">
🎥 开始预览
</button>
</view>
<!-- 设置面板 -->
<view class="settings-section" v-if="showSettings">
<view class="section-title">
<text>预览设置</text>
<text class="close-settings" @click="closeSettings">✕</text>
</view>
<view class="settings-content">
<view class="setting-group">
<text class="group-title">显示设置</text>
<view class="setting-item">
<text class="setting-label">方向模式</text>
<picker
@change="onOrientationChange"
:value="orientationIndex"
:range="orientationOptions"
class="setting-picker"
>
<view class="picker-display">
{{ orientationOptions[orientationIndex] }}
<text class="arrow">▼</text>
</view>
</picker>
</view>
<view class="setting-item">
<text class="setting-label">画面比例</text>
<picker
@change="onAspectChange"
:value="aspectIndex"
:range="aspectOptions"
class="setting-picker"
>
<view class="picker-display">
{{ aspectOptions[aspectIndex] }}
<text class="arrow">▼</text>
</view>
</picker>
</view>
</view>
<view class="setting-group">
<text class="group-title">美化设置</text>
<view class="setting-item">
<text class="setting-label">美颜等级: {{ beautyLevel }}</text>
<slider
:value="beautyLevel"
min="0"
max="9"
step="1"
@change="onBeautyChange"
activeColor="#007AFF"
class="setting-slider"
/>
</view>
<view class="setting-item">
<text class="setting-label">美白等级: {{ whiteLevel }}</text>
<slider
:value="whiteLevel"
min="0"
max="9"
step="1"
@change="onWhiteChange"
activeColor="#007AFF"
class="setting-slider"
/>
</view>
</view>
<button class="apply-btn" @click="applySettings">应用设置</button>
</view>
</view>
</view>
<!-- 底部功能区 -->
<view class="bottom-section">
<!-- 快捷设置按钮 -->
<view class="quick-actions" v-if="isPreviewing">
<button class="action-btn settings-action" @click="openSettings">
⚙️ 设置
</button>
<button class="action-btn capture-action" @click="captureFrame">
📸 截图
</button>
</view>
<!-- 功能说明 -->
<view class="info-section">
<text class="info-text">
💡 提示:预览功能仅用于查看摄像头画面,不会产生任何推流或录制
</text>
</view>
</view>
<!-- 遮罩层 -->
<view
class="overlay"
v-if="showSettings"
@click="closeSettings"
></view>
</view>
</template>
<script>
export default {
data() {
return {
// 预览状态
isPreviewing: false,
showSettings: false,
sssssssss: true,
// 预览配置
orientation: 'vertical',
orientationOptions: ['竖屏', '横屏'],
orientationIndex: 0,
aspectRatio: '9:16',
aspectOptions: ['9:16', '3:4', '1:1'],
aspectIndex: 0,
beautyLevel: 0,
whiteLevel: 0,
// LivePusher 上下文
livePusherCtx: null
}
},
methods: {
// 开始预览
async startPreview() {
try {
console.log('开始启动预览...');
// 创建 live-pusher 上下文
this.livePusherCtx = uni.createLivePusherContext('livePusher', this);
// 显示预览界面
this.isPreviewing = true;
// 启动预览(不推流)
this.$nextTick(() => {
if (this.livePusherCtx) {
this.livePusherCtx.startPreview({
success: () => {
console.log('预览启动成功');
uni.showToast({
title: '预览已开始',
icon: 'success'
});
},
fail: (err) => {
console.error('预览启动失败:', err);
this.handlePreviewError(err);
}
});
}
});
} catch (error) {
console.error('启动预览异常:', error);
uni.showToast({
title: '预览启动失败',
icon: 'error'
});
}
},
// 关闭预览
closePreview() {
uni.showModal({
title: '确认关闭',
content: '确定要关闭摄像头预览吗?',
success: (res) => {
if (res.confirm) {
this.doClosePreview();
}
}
});
},
// 执行关闭预览
doClosePreview() {
try {
console.log('正在关闭预览...');
// 停止预览
if (this.livePusherCtx) {
this.livePusherCtx.stopPreview({
success: () => {
console.log('预览已停止');
},
fail: (err) => {
console.error('停止预览失败:', err);
}
});
}
// 重置状态
this.isPreviewing = false;
this.showSettings = false;
uni.showToast({
title: '预览已关闭1',
icon: 'success'
});
// this.sssssssss = false;
} catch (error) {
console.error('关闭预览异常:', error);
// 强制更新状态
this.isPreviewing = false;
this.showSettings = false;
}
},
// 切换摄像头
switchCamera() {
if (!this.livePusherCtx || !this.isPreviewing) return;
uni.showLoading({
title: '切换中...'
});
this.livePusherCtx.switchCamera({
success: () => {
console.log('摄像头切换成功');
uni.hideLoading();
uni.showToast({
title: '摄像头已切换',
icon: 'success'
});
},
fail: (err) => {
console.error('摄像头切换失败:', err);
uni.hideLoading();
uni.showToast({
title: '切换失败',
icon: 'error'
});
}
});
},
// 打开设置面板
openSettings() {
this.showSettings = true;
},
// 关闭设置面板
closeSettings() {
this.showSettings = false;
},
// 应用设置
applySettings() {
uni.showToast({
title: '设置已应用',
icon: 'success'
});
this.closeSettings();
},
// 方向改变
onOrientationChange(e) {
this.orientationIndex = e.detail.value;
this.orientation = this.orientationIndex === 0 ? 'vertical' : 'horizontal';
},
// 宽高比改变
onAspectChange(e) {
this.aspectIndex = e.detail.value;
this.aspectRatio = this.aspectOptions[this.aspectIndex];
},
// 美颜等级改变
onBeautyChange(e) {
this.beautyLevel = e.detail.value;
},
// 美白等级改变
onWhiteChange(e) {
this.whiteLevel = e.detail.value;
},
// 截图功能(可选)
captureFrame() {
uni.showToast({
title: '截图功能开发中',
icon: 'none'
});
},
// 预览状态变化
onStateChange(e) {
console.log('预览状态变化:', e.detail);
},
// 错误处理
onError(e) {
console.error('预览错误:', e.detail);
const error = e.detail;
let errorMsg = '预览出现错误';
switch(error.errCode) {
case 1001:
errorMsg = '摄像头权限被拒绝';
break;
case 1002:
errorMsg = '麦克风权限被拒绝';
break;
case 1003:
errorMsg = '摄像头被其他应用占用';
break;
case 1004:
errorMsg = '设备不支持';
break;
}
uni.showModal({
title: '预览错误',
content: errorMsg,
showCancel: false,
success: () => {
this.closePreview();
}
});
},
// 网络状态
onNetStatus(e) {
console.log('预览网络状态:', e.detail);
},
// 处理预览错误
handlePreviewError(err) {
uni.showModal({
title: '预览启动失败',
content: '请检查摄像头权限或稍后重试',
showCancel: false,
success: () => {
this.isPreviewing = false;
}
});
}
},
beforeDestroy() {
if (this.isPreviewing) {
this.doClosePreview();
}
}
}
</script>
<style scoped>
.page-container {
min-height: 100vh;
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
display: flex;
flex-direction: column;
}
/* 头部样式 */
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 20px;
text-align: center;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.title {
color: white;
font-size: 20px;
font-weight: bold;
}
/* 主要内容区域 */
.main-content {
flex: 1;
padding: 20px;
display: flex;
flex-direction: column;
gap: 20px;
}
/* 区域标题 */
.section-title {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
padding: 0 10px;
}
.section-title text:first-child {
font-size: 18px;
font-weight: bold;
color: #333;
}
.preview-status {
font-size: 14px;
color: #4CAF50;
}
.close-settings {
font-size: 20px;
color: #999;
padding: 5px 10px;
cursor: pointer;
}
/* 预览区域 */
.preview-section, .placeholder-section {
background: white;
border-radius: 15px;
padding: 20px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}
/* 预览窗口盒子 */
.preview-box {
position: relative;
width: 280px;
height: 373px; /* 9:16 比例 */
margin: 0 auto 20px;
border-radius: 12px;
overflow: hidden;
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}
.live-preview {
width: 100%;
height: 100%;
background-color: #000;
}
/* 预览窗口边框装饰 */
.preview-frame {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
border: 3px solid rgba(0, 122, 255, 0.3);
border-radius: 12px;
pointer-events: none;
}
/* 占位符样式 */
.placeholder-box {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 200px;
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
border-radius: 12px;
margin-bottom: 20px;
}
.placeholder-icon {
font-size: 48px;
margin-bottom: 15px;
}
.placeholder-text {
color: white;
font-size: 16px;
text-align: center;
}
/* 按钮样式 */
.control-btn, .start-btn {
padding: 12px 24px;
border: none;
border-radius: 25px;
font-size: 16px;
font-weight: 500;
cursor: pointer;
transition: all 0.3s ease;
}
.control-btn {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
margin: 0 10px;
}
.control-btn:disabled {
background: #ccc;
cursor: not-allowed;
}
.close-btn {
background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%);
color: white;
}
.start-btn {
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
color: white;
width: 200px;
}
.preview-controls {
display: flex;
justify-content: center;
align-items: center;
}
/* 设置区域 */
.settings-section {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
border-radius: 20px;
padding: 25px;
width: 90%;
max-width: 450px;
max-height: 80vh;
overflow-y: auto;
z-index: 1000;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.settings-content {
margin-top: 15px;
}
.setting-group {
margin-bottom: 25px;
}
.group-title {
display: block;
font-size: 16px;
font-weight: bold;
color: #333;
margin-bottom: 15px;
padding-bottom: 8px;
border-bottom: 2px solid #f0f0f0;
}
.setting-item {
margin-bottom: 20px;
}
.setting-label {
display: block;
font-size: 14px;
color: #555;
margin-bottom: 8px;
font-weight: 500;
}
.setting-picker {
width: 100%;
}
.picker-display {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 15px;
background: #f8f9fa;
border: 1px solid #e9ecef;
border-radius: 8px;
font-size: 14px;
}
.arrow {
color: #999;
font-size: 12px;
}
.setting-slider {
width: 100%;
margin: 10px 0;
}
.apply-btn {
width: 100%;
padding: 15px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 10px;
font-size: 16px;
font-weight: 500;
margin-top: 10px;
}
/* 底部区域 */
.bottom-section {
background: white;
padding: 20px;
box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
}
.quick-actions {
display: flex;
justify-content: center;
gap: 20px;
margin-bottom: 20px;
}
.action-btn {
padding: 10px 20px;
border: none;
border-radius: 20px;
font-size: 14px;
cursor: pointer;
transition: all 0.3s ease;
}
.settings-action {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.capture-action {
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
color: white;
}
.info-section {
text-align: center;
padding: 15px;
background: #f8f9fa;
border-radius: 10px;
}
.info-text {
font-size: 13px;
color: #666;
line-height: 1.5;
}
/* 遮罩层 */
.overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
z-index: 999;
}
/* 响应式设计 */
@media (max-width: 480px) {
.main-content {
padding: 15px;
}
.preview-box {
width: 240px;
height: 320px;
}
.preview-controls {
flex-direction: column;
gap: 10px;
}
.control-btn {
margin: 5px 0;
}
.quick-actions {
flex-direction: column;
align-items: center;
}
.action-btn {
width: 200px;
}
}
</style>