Initial Commit

This commit is contained in:
Rajuahamedkst 2025-09-12 16:08:17 +08:00
commit 1d71a02738
237 changed files with 64293 additions and 0 deletions

View file

@ -0,0 +1,321 @@
// 🎤 语音消息组件逻辑
const voiceMessageManager = require('../../utils/voice-message-manager.js');
Component({
properties: {
// 语音消息数据
voiceData: {
type: Object,
value: {},
observer: 'onVoiceDataChange'
},
// 是否为自己发送的消息
isSelf: {
type: Boolean,
value: false
},
// 消息ID
messageId: {
type: String,
value: ''
}
},
data: {
// 播放状态
isPlaying: false,
isLoading: false,
hasError: false,
// 播放进度
currentTime: 0,
duration: 0,
playProgress: 0,
// 波形数据
waveformData: [],
currentWaveIndex: 0,
// 语音信息
voiceUrl: '',
voiceDuration: 0
},
lifetimes: {
attached() {
console.log('🎤 语音消息组件加载');
this.initComponent();
},
detached() {
console.log('🎤 语音消息组件卸载');
this.cleanup();
}
},
methods: {
// 初始化组件
initComponent() {
// 注册语音管理器事件
this.registerVoiceEvents();
// 生成波形数据
this.generateWaveform();
// 检查当前播放状态
this.checkPlayingState();
},
// 语音数据变化处理
onVoiceDataChange(newData, oldData) {
if (!newData || JSON.stringify(newData) === JSON.stringify(oldData)) {
return;
}
console.log('🎤 语音数据更新:', newData);
this.setData({
voiceUrl: newData.url || '',
voiceDuration: newData.duration || 0,
duration: newData.duration || 0
});
// 重新生成波形
this.generateWaveform();
// 检查播放状态
this.checkPlayingState();
},
// 注册语音管理器事件
registerVoiceEvents() {
// 播放开始事件
voiceMessageManager.on('playStart', () => {
this.checkPlayingState();
});
// 播放结束事件
voiceMessageManager.on('playEnd', () => {
this.setData({
isPlaying: false,
currentTime: 0,
playProgress: 0,
currentWaveIndex: 0
});
});
// 播放进度更新事件
voiceMessageManager.on('playTimeUpdate', (data) => {
if (this.isCurrentMessage()) {
this.updatePlayProgress(data.currentTime, data.duration);
}
});
// 播放错误事件
voiceMessageManager.on('playError', (error) => {
if (this.isCurrentMessage()) {
console.error('🎤 语音播放错误:', error);
this.setData({
isPlaying: false,
isLoading: false,
hasError: true
});
}
});
// 播放可以开始事件
voiceMessageManager.on('playCanplay', () => {
if (this.isCurrentMessage()) {
this.setData({
isLoading: false,
hasError: false
});
}
});
},
// 检查是否为当前播放的消息
isCurrentMessage() {
const currentMessageId = voiceMessageManager.getCurrentPlayingMessageId();
return currentMessageId === this.properties.messageId;
},
// 检查播放状态
checkPlayingState() {
const isCurrentlyPlaying = this.isCurrentMessage() && voiceMessageManager.isPlaying();
this.setData({
isPlaying: isCurrentlyPlaying
});
},
// 切换播放状态
async togglePlay() {
if (this.data.hasError) {
return this.retryPlay();
}
if (this.data.isLoading) {
return;
}
try {
if (this.data.isPlaying) {
// 暂停播放
voiceMessageManager.pausePlaying();
this.setData({ isPlaying: false });
} else {
// 开始播放
await this.startPlay();
}
} catch (error) {
console.error('🎤 切换播放状态失败:', error);
this.setData({
hasError: true,
isLoading: false,
isPlaying: false
});
}
},
// 开始播放
async startPlay() {
if (!this.data.voiceUrl) {
console.error('🎤 语音URL为空');
return;
}
try {
this.setData({
isLoading: true,
hasError: false
});
// 播放语音消息
await voiceMessageManager.playVoiceMessage(
this.data.voiceUrl,
this.properties.messageId
);
this.setData({
isPlaying: true,
isLoading: false
});
console.log('🎤 开始播放语音消息');
} catch (error) {
console.error('🎤 播放语音消息失败:', error);
this.setData({
hasError: true,
isLoading: false,
isPlaying: false
});
}
},
// 重试播放
async retryPlay() {
console.log('🎤 重试播放语音消息');
this.setData({
hasError: false,
isLoading: false,
isPlaying: false
});
await this.startPlay();
},
// 更新播放进度
updatePlayProgress(currentTime, duration) {
if (!duration || duration <= 0) return;
const progress = (currentTime / duration) * 100;
const waveIndex = Math.floor((currentTime / duration) * this.data.waveformData.length);
this.setData({
currentTime: currentTime,
duration: duration,
playProgress: progress,
currentWaveIndex: Math.max(0, waveIndex)
});
},
// 生成波形数据
generateWaveform() {
const duration = this.data.voiceDuration || this.data.duration || 1000;
const barCount = Math.min(Math.max(Math.floor(duration / 200), 8), 30); // 8-30个波形条
const waveformData = [];
for (let i = 0; i < barCount; i++) {
// 生成随机高度,模拟真实波形
const height = Math.random() * 60 + 20; // 20-80%的高度
waveformData.push(height);
}
this.setData({
waveformData: waveformData,
currentWaveIndex: 0
});
console.log('🌊 生成波形数据:', waveformData.length, '个波形条');
},
// 格式化时长显示
formatDuration(duration) {
if (!duration || duration <= 0) return '0"';
const seconds = Math.floor(duration / 1000);
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
if (minutes > 0) {
return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}`;
} else {
return `${remainingSeconds}"`;
}
},
// 格式化时间显示
formatTime(time) {
if (!time || time <= 0) return '0:00';
const totalSeconds = Math.floor(time);
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${minutes}:${seconds.toString().padStart(2, '0')}`;
},
// 获取语音文件大小描述
getFileSizeDescription(fileSize) {
if (!fileSize || fileSize <= 0) return '';
if (fileSize < 1024) {
return `${fileSize}B`;
} else if (fileSize < 1024 * 1024) {
return `${(fileSize / 1024).toFixed(1)}KB`;
} else {
return `${(fileSize / (1024 * 1024)).toFixed(1)}MB`;
}
},
// 清理资源
cleanup() {
// 如果当前正在播放这个消息,停止播放
if (this.isCurrentMessage() && voiceMessageManager.isPlaying()) {
voiceMessageManager.stopPlaying();
}
// 移除事件监听器
voiceMessageManager.off('playStart');
voiceMessageManager.off('playEnd');
voiceMessageManager.off('playTimeUpdate');
voiceMessageManager.off('playError');
voiceMessageManager.off('playCanplay');
}
}
});

View file

@ -0,0 +1,4 @@
{
"component": true,
"usingComponents": {}
}

View file

@ -0,0 +1,53 @@
<!-- 🎤 语音消息组件 -->
<view class="voice-message-container {{isSelf ? 'self' : 'other'}} {{isPlaying ? 'playing' : ''}}">
<!-- 语音消息气泡 -->
<view class="voice-bubble" bindtap="togglePlay">
<!-- 播放按钮 -->
<view class="play-button">
<view class="play-icon {{isPlaying ? 'pause' : 'play'}}">
<text wx:if="{{!isPlaying}}" class="icon">▶</text>
<text wx:else class="icon">⏸</text>
</view>
</view>
<!-- 语音波形 -->
<view class="voice-waveform">
<view class="waveform-container">
<view wx:for="{{waveformData}}"
wx:key="index"
class="wave-bar {{index <= currentWaveIndex ? 'active' : ''}}"
style="height: {{item}}%;">
</view>
</view>
</view>
<!-- 语音时长 -->
<view class="voice-duration">
<text class="duration-text">{{formatDuration(duration)}}</text>
</view>
</view>
<!-- 播放进度条 -->
<view wx:if="{{isPlaying}}" class="progress-container">
<view class="progress-bar">
<view class="progress-fill" style="width: {{playProgress}}%;"></view>
</view>
<view class="progress-time">
<text class="current-time">{{formatTime(currentTime)}}</text>
<text class="total-time">{{formatTime(duration)}}</text>
</view>
</view>
<!-- 加载状态 -->
<view wx:if="{{isLoading}}" class="loading-container">
<view class="loading-spinner"></view>
<text class="loading-text">加载中...</text>
</view>
<!-- 错误状态 -->
<view wx:if="{{hasError}}" class="error-container">
<text class="error-icon">⚠️</text>
<text class="error-text">播放失败</text>
<text class="retry-button" bindtap="retryPlay">重试</text>
</view>
</view>

View file

@ -0,0 +1,443 @@
/* 🎤 语音消息组件样式 */
/* CSS变量定义 */
.voice-message-container {
--primary-color: #007AFF;
--primary-light: #5AC8FA;
--success-color: #34C759;
--warning-color: #FF9500;
--danger-color: #FF3B30;
--background-light: #F2F2F7;
--background-dark: #1C1C1E;
--text-primary: #000000;
--text-secondary: #8E8E93;
--border-color: #E5E5EA;
--shadow-light: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
--radius-medium: 12rpx;
--radius-large: 20rpx;
}
/* 🌙 深色模式支持 */
@media (prefers-color-scheme: dark) {
.voice-message-container {
--primary-color: #0A84FF;
--background-light: #2C2C2E;
--background-dark: #1C1C1E;
--text-primary: #FFFFFF;
--text-secondary: #8E8E93;
--border-color: #38383A;
--shadow-light: 0 2rpx 8rpx rgba(0, 0, 0, 0.3);
}
}
.voice-message-container {
max-width: 480rpx;
margin: 8rpx 0;
position: relative;
}
/* 🎨 语音气泡 */
.voice-bubble {
display: flex;
align-items: center;
padding: 24rpx;
border-radius: var(--radius-large);
box-shadow: var(--shadow-light);
transition: all 0.3s ease;
min-width: 200rpx;
position: relative;
overflow: hidden;
}
/* 自己发送的消息 */
.voice-message-container.self .voice-bubble {
background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-light) 100%);
color: white;
}
/* 他人发送的消息 */
.voice-message-container.other .voice-bubble {
background: var(--background-light);
color: var(--text-primary);
border: 1rpx solid var(--border-color);
}
/* 播放状态 */
.voice-message-container.playing .voice-bubble {
transform: scale(1.02);
}
.voice-message-container.self.playing .voice-bubble {
background: linear-gradient(135deg, var(--success-color) 0%, var(--primary-light) 100%);
}
.voice-message-container.other.playing .voice-bubble {
background: rgba(0, 122, 255, 0.1);
border-color: var(--primary-color);
}
/* 🎵 播放按钮 */
.play-button {
width: 80rpx;
height: 80rpx;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin-right: 24rpx;
transition: all 0.3s ease;
position: relative;
box-sizing: border-box;
overflow: hidden;
flex-shrink: 0; /* 不允许在flex布局中被压缩 */
flex: 0 0 auto; /* 宽高由自身决定 */
min-width: 80rpx; /* 保底宽度,维持正圆 */
}
.voice-message-container.self .play-button {
background: rgba(255, 255, 255, 0.2);
}
.voice-message-container.other .play-button {
background: var(--primary-color);
}
.play-button:active {
transform: scale(0.95);
}
.play-icon {
display: flex;
align-items: center;
justify-content: center;
transition: all 0.3s ease;
}
.play-icon .icon {
font-size: 32rpx;
font-weight: bold;
}
.voice-message-container.self .play-icon .icon {
color: white;
}
.voice-message-container.other .play-icon .icon {
color: white;
}
/* 播放动画 */
.play-icon.play .icon {
animation: pulse 2s infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.7; }
}
/* 🌊 语音波形 */
.voice-waveform {
flex: 1;
margin-right: 24rpx;
height: 60rpx;
display: flex;
align-items: center;
}
.waveform-container {
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
height: 100%;
gap: 4rpx;
}
.wave-bar {
width: 6rpx;
border-radius: 3rpx;
transition: all 0.3s ease;
min-height: 8rpx;
}
.voice-message-container.self .wave-bar {
background: rgba(255, 255, 255, 0.4);
}
.voice-message-container.other .wave-bar {
background: var(--border-color);
}
.voice-message-container.self .wave-bar.active {
background: white;
transform: scaleY(1.2);
}
.voice-message-container.other .wave-bar.active {
background: var(--primary-color);
transform: scaleY(1.2);
}
/* 波形动画 */
.voice-message-container.playing .wave-bar.active {
animation: waveAnimation 1.5s ease-in-out infinite;
}
@keyframes waveAnimation {
0%, 100% { transform: scaleY(1); }
50% { transform: scaleY(1.5); }
}
/* ⏱️ 语音时长 */
.voice-duration {
min-width: 60rpx;
text-align: right;
}
.duration-text {
font-size: 24rpx;
font-weight: 500;
}
.voice-message-container.self .duration-text {
color: rgba(255, 255, 255, 0.9);
}
.voice-message-container.other .duration-text {
color: var(--text-secondary);
}
/* 📊 播放进度条 */
.progress-container {
margin-top: 16rpx;
padding: 0 24rpx;
}
.progress-bar {
height: 4rpx;
background: var(--border-color);
border-radius: 2rpx;
overflow: hidden;
margin-bottom: 8rpx;
}
.progress-fill {
height: 100%;
background: var(--primary-color);
border-radius: 2rpx;
transition: width 0.1s linear;
}
.progress-time {
display: flex;
justify-content: space-between;
align-items: center;
}
.current-time,
.total-time {
font-size: 20rpx;
color: var(--text-secondary);
}
/* 🔄 加载状态 */
.loading-container {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
align-items: center;
justify-content: center;
background: rgba(0, 0, 0, 0.1);
border-radius: var(--radius-large);
backdrop-filter: blur(10rpx);
}
.loading-spinner {
width: 40rpx;
height: 40rpx;
border: 3rpx solid rgba(255, 255, 255, 0.3);
border-top: 3rpx solid white;
border-radius: 50%;
animation: spin 1s linear infinite;
margin-right: 16rpx;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.loading-text {
font-size: 24rpx;
color: white;
}
/* ⚠️ 错误状态 */
.error-container {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
align-items: center;
justify-content: center;
background: rgba(255, 59, 48, 0.1);
border-radius: var(--radius-large);
backdrop-filter: blur(10rpx);
gap: 12rpx;
}
.error-icon {
font-size: 32rpx;
}
.error-text {
font-size: 24rpx;
color: var(--danger-color);
}
.retry-button {
font-size: 24rpx;
color: var(--primary-color);
text-decoration: underline;
transition: all 0.2s ease;
}
.retry-button:active {
opacity: 0.7;
}
/* 🎨 特殊效果 */
.voice-bubble::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(45deg, transparent 30%, rgba(255, 255, 255, 0.1) 50%, transparent 70%);
opacity: 0;
transition: opacity 0.3s ease;
pointer-events: none;
}
.voice-bubble:active::before {
opacity: 1;
}
/* 📱 响应式设计 */
@media screen and (max-width: 375px) {
.voice-message-container {
max-width: 400rpx;
}
.voice-bubble {
padding: 20rpx;
}
.play-button {
width: 70rpx;
height: 70rpx;
margin-right: 20rpx;
min-width: 70rpx;
}
.play-icon .icon {
font-size: 28rpx;
}
.voice-waveform {
height: 50rpx;
margin-right: 20rpx;
}
.wave-bar {
width: 5rpx;
}
}
@media screen and (min-width: 414px) {
.voice-message-container {
max-width: 520rpx;
}
.voice-bubble {
padding: 28rpx;
}
.play-button {
width: 90rpx;
height: 90rpx;
margin-right: 28rpx;
min-width: 90rpx;
}
.play-icon .icon {
font-size: 36rpx;
}
.voice-waveform {
height: 70rpx;
margin-right: 28rpx;
}
.wave-bar {
width: 7rpx;
}
}
/* 🎭 动画增强 */
.voice-message-container {
animation: slideIn 0.3s ease-out;
}
@keyframes slideIn {
from {
opacity: 0;
transform: translateY(20rpx);
}
to {
opacity: 1;
transform: translateY(0);
}
}
/* 长按效果 */
.voice-bubble {
user-select: none;
-webkit-user-select: none;
}
.voice-bubble:active {
transform: scale(0.98);
}
/* 可访问性 */
.voice-bubble[aria-pressed="true"] {
outline: 2rpx solid var(--primary-color);
outline-offset: 4rpx;
}
/* 状态指示器 */
.voice-message-container::after {
content: '';
position: absolute;
top: -4rpx;
right: -4rpx;
width: 16rpx;
height: 16rpx;
border-radius: 8rpx;
opacity: 0;
transition: all 0.3s ease;
}
.voice-message-container.playing::after {
opacity: 1;
background: var(--success-color);
animation: pulse 2s infinite;
}