mirror of
				https://github.com/yangjian102621/geekai.git
				synced 2025-11-04 08:13:43 +08:00 
			
		
		
		
	优化充值产品定价逻辑,确保手机端和PC端显示的价格一致
This commit is contained in:
		@@ -5,6 +5,7 @@
 | 
			
		||||
* 功能新增:管理后台增加AI绘图任务管理,可在管理后台浏览和删除用户的绘图任务
 | 
			
		||||
* 功能新增:管理后台增加Suno和Luma任务管理功能
 | 
			
		||||
* Bug修复:修复管理后台删除兑换码报 404 错误
 | 
			
		||||
* 功能优化:优化充值产品定价逻辑,可以设置原价和优惠价,**升级当前版本之后请务必要到管理后台去重新设置一下产品价格,以免造成损失!!!**,**升级当前版本之后请务必要到管理后台去重新设置一下产品价格,以免造成损失!!!**,**升级当前版本之后请务必要到管理后台去重新设置一下产品价格,以免造成损失!!!**。
 | 
			
		||||
 | 
			
		||||
## v4.1.5
 | 
			
		||||
* 功能优化:重构 websocket 组件,减少 websocket 连接数,全站共享一个 websocket 连接
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,6 @@ import (
 | 
			
		||||
	"geekai/store/model"
 | 
			
		||||
	"geekai/utils"
 | 
			
		||||
	"geekai/utils/resp"
 | 
			
		||||
	"github.com/shopspring/decimal"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
@@ -105,7 +104,7 @@ func (h *PaymentHandler) Pay(c *gin.Context) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	amount, _ := decimal.NewFromFloat(product.Price).Sub(decimal.NewFromFloat(product.Discount)).Float64()
 | 
			
		||||
	amount := product.Discount
 | 
			
		||||
	var payURL, returnURL, notifyURL string
 | 
			
		||||
	switch data.PayWay {
 | 
			
		||||
	case "alipay":
 | 
			
		||||
 
 | 
			
		||||
@@ -112,8 +112,9 @@
 | 
			
		||||
                justify-content right
 | 
			
		||||
              }
 | 
			
		||||
 | 
			
		||||
              .price {
 | 
			
		||||
              .discount {
 | 
			
		||||
                color #f56c6c
 | 
			
		||||
                font-size 20px
 | 
			
		||||
              }
 | 
			
		||||
 | 
			
		||||
              .expire {
 | 
			
		||||
 
 | 
			
		||||
@@ -138,8 +138,8 @@
 | 
			
		||||
 | 
			
		||||
              <div class="input-box">
 | 
			
		||||
                <div class="input-box-inner">
 | 
			
		||||
                  <span class="tool-item" @click="ElMessage.info('暂时不支持语音输入')">
 | 
			
		||||
                    <el-tooltip class="box-item" effect="dark" content="语音输入">
 | 
			
		||||
                  <span class="tool-item" @click="realtimeChat">
 | 
			
		||||
                    <el-tooltip class="box-item" effect="dark" content="实时语音对话">
 | 
			
		||||
                      <i class="iconfont icon-mic-bold"></i>
 | 
			
		||||
                    </el-tooltip>
 | 
			
		||||
                  </span>
 | 
			
		||||
@@ -208,6 +208,10 @@
 | 
			
		||||
    </el-dialog>
 | 
			
		||||
 | 
			
		||||
    <ChatSetting :show="showChatSetting" @hide="showChatSetting = false"/>
 | 
			
		||||
 | 
			
		||||
    <el-dialog v-model="showConversationDialog" title="实时语音通话" :before-close="hangUp">
 | 
			
		||||
      <realtime-conversation  @close="showConversationDialog = false" ref="conversationRef" :height="dialogHeight+'px'" />
 | 
			
		||||
    </el-dialog>
 | 
			
		||||
  </div>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -236,6 +240,7 @@ import FileList from "@/components/FileList.vue";
 | 
			
		||||
import ChatSetting from "@/components/ChatSetting.vue";
 | 
			
		||||
import BackTop from "@/components/BackTop.vue";
 | 
			
		||||
import {showMessageError} from "@/utils/dialog";
 | 
			
		||||
import RealtimeConversation from "@/components/RealtimeConversation.vue";
 | 
			
		||||
 | 
			
		||||
const title = ref('GeekAI-智能助手');
 | 
			
		||||
const models = ref([])
 | 
			
		||||
@@ -891,6 +896,21 @@ const insertFile = (file) => {
 | 
			
		||||
const removeFile = (file) => {
 | 
			
		||||
  files.value = removeArrayItem(files.value, file, (v1,v2) => v1.url===v2.url)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 实时语音对话
 | 
			
		||||
const showConversationDialog = ref(false)
 | 
			
		||||
const conversationRef = ref(null)
 | 
			
		||||
const dialogHeight = ref(window.innerHeight - 75);
 | 
			
		||||
const realtimeChat = () => {
 | 
			
		||||
  showConversationDialog.value = true;
 | 
			
		||||
  nextTick(() => {
 | 
			
		||||
    conversationRef.value.connect()
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
const hangUp = () => {
 | 
			
		||||
  showConversationDialog.value = false;
 | 
			
		||||
  conversationRef.value.hangUp()
 | 
			
		||||
}
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<style scoped lang="stylus">
 | 
			
		||||
 
 | 
			
		||||
@@ -44,11 +44,11 @@
 | 
			
		||||
                <div class="product-info">
 | 
			
		||||
                  <div class="info-line">
 | 
			
		||||
                    <span class="label">商品原价:</span>
 | 
			
		||||
                    <span class="price">¥{{ item.price }}</span>
 | 
			
		||||
                    <span class="price"><del>¥{{ item.price }}</del></span>
 | 
			
		||||
                  </div>
 | 
			
		||||
                  <div class="info-line">
 | 
			
		||||
                    <span class="label">促销立减:</span>
 | 
			
		||||
                    <span class="price">¥{{ item.discount }}</span>
 | 
			
		||||
                    <span class="label">优惠价:</span>
 | 
			
		||||
                    <span class="discount">¥{{ item.discount }}</span>
 | 
			
		||||
                  </div>
 | 
			
		||||
                  <div class="info-line">
 | 
			
		||||
                    <span class="label">有效期:</span>
 | 
			
		||||
@@ -93,7 +93,7 @@
 | 
			
		||||
          <h2 class="headline">消费账单</h2>
 | 
			
		||||
 | 
			
		||||
          <div class="user-order">
 | 
			
		||||
            <user-order v-if="isLogin"/>
 | 
			
		||||
            <user-order v-if="isLogin" :key="userOrderKey"/>
 | 
			
		||||
          </div>
 | 
			
		||||
        </div>
 | 
			
		||||
      </div>
 | 
			
		||||
@@ -154,6 +154,7 @@ const payWays = ref([])
 | 
			
		||||
const vipInfoText = ref("")
 | 
			
		||||
const store = useSharedStore()
 | 
			
		||||
const profileKey = ref(0)
 | 
			
		||||
const userOrderKey = ref(0)
 | 
			
		||||
const showDialog = ref(false)
 | 
			
		||||
const qrImg = ref("")
 | 
			
		||||
const price = ref(0)
 | 
			
		||||
@@ -215,7 +216,7 @@ const pay = (product, payWay) => {
 | 
			
		||||
    showDialog.value = true
 | 
			
		||||
    loading.value = false
 | 
			
		||||
    if (payWay.pay_way === 'wechat') {
 | 
			
		||||
      price.value = Number((product.price - product.discount).toFixed(2))
 | 
			
		||||
      price.value = Number(product.discount)
 | 
			
		||||
      QRCode.toDataURL(res.data, {width: 300, height: 300, margin: 2}, (error, url) => {
 | 
			
		||||
        if (error) {
 | 
			
		||||
          console.error(error)
 | 
			
		||||
@@ -245,6 +246,7 @@ const payCallback = (success) => {
 | 
			
		||||
  showDialog.value = false
 | 
			
		||||
  if (success) {
 | 
			
		||||
    profileKey.value += 1
 | 
			
		||||
    userOrderKey.value += 1
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,29 +1,12 @@
 | 
			
		||||
<template>
 | 
			
		||||
  <div class="audio-chat-page">
 | 
			
		||||
    <el-button style="margin: 20px" type="primary" size="large" @click="connect()">开始语音对话</el-button>
 | 
			
		||||
 | 
			
		||||
    <el-dialog v-model="showDialog" title="语音通话" :before-close="close">
 | 
			
		||||
      <realtime-conversation  @close="showDialog = false" ref="conversationRef" :height="dialogHeight+'px'" />
 | 
			
		||||
    </el-dialog>
 | 
			
		||||
  </div>
 | 
			
		||||
</template>
 | 
			
		||||
 | 
			
		||||
<script setup>
 | 
			
		||||
import {nextTick, ref} from 'vue';
 | 
			
		||||
import RealtimeConversation from "@/components/RealtimeConversation.vue";
 | 
			
		||||
 | 
			
		||||
const showDialog = ref(false);
 | 
			
		||||
const dialogHeight = ref(window.innerHeight - 75);
 | 
			
		||||
const conversationRef = ref(null);
 | 
			
		||||
const connect = () => {
 | 
			
		||||
  showDialog.value = true;
 | 
			
		||||
  nextTick(() => {
 | 
			
		||||
    conversationRef.value.connect()
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
const close = () => {
 | 
			
		||||
  showDialog.value = false;
 | 
			
		||||
  conversationRef.value.hangUp()
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,112 +0,0 @@
 | 
			
		||||
<template>
 | 
			
		||||
  <div>
 | 
			
		||||
   {{data}}
 | 
			
		||||
    <div>
 | 
			
		||||
      <canvas ref="clientCanvasRef" />
 | 
			
		||||
    </div>
 | 
			
		||||
    <el-button type="primary" @click="sendMessage">连接电话</el-button>
 | 
			
		||||
  </div>
 | 
			
		||||
</template>
 | 
			
		||||
 | 
			
		||||
<script setup>
 | 
			
		||||
import {onMounted, ref} from "vue";
 | 
			
		||||
 | 
			
		||||
const data = ref('abc')
 | 
			
		||||
import { RealtimeClient } from '@openai/realtime-api-beta';
 | 
			
		||||
 | 
			
		||||
const client = new RealtimeClient({
 | 
			
		||||
  url: "wss://api.geekai.pro/v1/realtime",
 | 
			
		||||
  apiKey: "sk-Gc5cEzDzGQLIqxWA9d62089350F3454bB359C4A3Fa21B3E4",
 | 
			
		||||
  dangerouslyAllowAPIKeyInBrowser: true,
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// Can set parameters ahead of connecting, either separately or all at once
 | 
			
		||||
client.updateSession({ instructions: 'You are a great, upbeat friend.' });
 | 
			
		||||
client.updateSession({ voice: 'alloy' });
 | 
			
		||||
client.updateSession({
 | 
			
		||||
  turn_detection: 'disabled', // or 'server_vad'
 | 
			
		||||
  input_audio_transcription: { model: 'whisper-1' },
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// Set up event handling
 | 
			
		||||
client.on('conversation.updated', ({ item, delta }) => {
 | 
			
		||||
  console.info('conversation.updated', item, delta)
 | 
			
		||||
  switch (item.type) {
 | 
			
		||||
    case 'message':
 | 
			
		||||
      // system, user, or assistant message (item.role)
 | 
			
		||||
      localStorage.setItem("chat_data", JSON.stringify(Array.from(item.formatted.audio)))
 | 
			
		||||
        console.log("语言消息")
 | 
			
		||||
      break;
 | 
			
		||||
    case 'function_call':
 | 
			
		||||
      // always a function call from the model
 | 
			
		||||
      break;
 | 
			
		||||
    case 'function_call_output':
 | 
			
		||||
      // always a response from the user / application
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
  if (delta) {
 | 
			
		||||
     console.info(delta.audio)
 | 
			
		||||
    //localStorage.setItem("chat_data", JSON.stringify(Array.from(delta.audio)))
 | 
			
		||||
    playPCM16(delta.audio, 24000);
 | 
			
		||||
    // Only one of the following will be populated for any given event
 | 
			
		||||
    // delta.audio = Int16Array, audio added
 | 
			
		||||
    // delta.transcript = string, transcript added
 | 
			
		||||
    // delta.arguments = string, function arguments added
 | 
			
		||||
  }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
client.on('conversation.item.appended', ({ item }) => {
 | 
			
		||||
  if (item.role === 'assistant') {
 | 
			
		||||
    playPCM16(item.formatted.audio, 24000);
 | 
			
		||||
  }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
const speaker = ref(null)
 | 
			
		||||
// 假设 PCM16 数据已经存储在一个 Int16Array 中
 | 
			
		||||
function playPCM16(pcm16Array, sampleRate = 44100) {
 | 
			
		||||
  // 创建 AudioContext
 | 
			
		||||
  const audioContext = new (window.AudioContext || window.webkitAudioContext)();
 | 
			
		||||
 | 
			
		||||
  // 将 Int16Array 转换为 Float32Array (Web Audio API 使用 Float32)
 | 
			
		||||
  let float32Array = new Float32Array(pcm16Array.length);
 | 
			
		||||
  for (let i = 0; i < pcm16Array.length; i++) {
 | 
			
		||||
    float32Array[i] = pcm16Array[i] / 32768; // Int16 转换为 Float32
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // 创建 AudioBuffer
 | 
			
		||||
  const audioBuffer = audioContext.createBuffer(1, float32Array.length, sampleRate); // 单声道
 | 
			
		||||
  audioBuffer.getChannelData(0).set(float32Array); // 设置音频数据
 | 
			
		||||
 | 
			
		||||
  // 创建 AudioBufferSourceNode 并播放音频
 | 
			
		||||
  const source = audioContext.createBufferSource();
 | 
			
		||||
  source.buffer = audioBuffer;
 | 
			
		||||
  source.connect(audioContext.destination); // 连接到扬声器
 | 
			
		||||
  source.start(); // 播放
 | 
			
		||||
  source.onended = () => {
 | 
			
		||||
    console.log("播放结束")
 | 
			
		||||
  }
 | 
			
		||||
  speaker.value = source
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
onMounted(() => {
 | 
			
		||||
  // Connect to Realtime API
 | 
			
		||||
  // client.connect().then(res => {
 | 
			
		||||
  //   if (res) {
 | 
			
		||||
  //     console.log("连接成功!")
 | 
			
		||||
  //   }
 | 
			
		||||
  // }).catch(e => {
 | 
			
		||||
  //   console.log(e)
 | 
			
		||||
  // })
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
const sendMessage = () => {
 | 
			
		||||
  const data = JSON.parse(localStorage.getItem("chat_data"))
 | 
			
		||||
  playPCM16(data, 24000)
 | 
			
		||||
  setTimeout(() => {speaker.value.stop()}, 5000)
 | 
			
		||||
  // client.sendUserMessageContent([{ type: 'input_text', text: `你好,请用四川话给我讲一个笑话?` }]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
</script>
 | 
			
		||||
@@ -1,263 +0,0 @@
 | 
			
		||||
<template>
 | 
			
		||||
  <div class="video-call-container">
 | 
			
		||||
    <div class="wave-container">
 | 
			
		||||
      <div class="wave-animation">
 | 
			
		||||
        <div v-for="i in 5" :key="i" class="wave-ellipse"></div>
 | 
			
		||||
      </div>
 | 
			
		||||
    </div>
 | 
			
		||||
    <!-- 其余部分保持不变 -->
 | 
			
		||||
    <div class="voice-indicators">
 | 
			
		||||
      <div class="voice-indicator left">
 | 
			
		||||
        <canvas ref="canvasClientRef" width="600" height="200"></canvas>
 | 
			
		||||
      </div>
 | 
			
		||||
      <div class="voice-indicator right">
 | 
			
		||||
        <canvas ref="canvasServerRef" width="600" height="200"></canvas>
 | 
			
		||||
      </div>
 | 
			
		||||
    </div>
 | 
			
		||||
    <div class="call-controls">
 | 
			
		||||
      <button class="call-button hangup" @click="hangUp">
 | 
			
		||||
        <i class="iconfont icon-hung-up"></i>
 | 
			
		||||
      </button>
 | 
			
		||||
      <button class="call-button answer" @click="answer">
 | 
			
		||||
        <i class="iconfont icon-call"></i>
 | 
			
		||||
      </button>
 | 
			
		||||
    </div>
 | 
			
		||||
  </div>
 | 
			
		||||
</template>
 | 
			
		||||
 | 
			
		||||
<script setup>
 | 
			
		||||
// Script 部分保持不变
 | 
			
		||||
import {ref, onMounted, onUnmounted} from 'vue';
 | 
			
		||||
 | 
			
		||||
const leftVoiceActive = ref(false);
 | 
			
		||||
const rightVoiceActive = ref(false);
 | 
			
		||||
 | 
			
		||||
const animateVoice = () => {
 | 
			
		||||
  leftVoiceActive.value = Math.random() > 0.5;
 | 
			
		||||
  rightVoiceActive.value = Math.random() > 0.5;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
let voiceInterval;
 | 
			
		||||
const canvasClientRef = ref(null);
 | 
			
		||||
const canvasServerRef = ref(null);
 | 
			
		||||
 | 
			
		||||
onMounted(() => {
 | 
			
		||||
  voiceInterval = setInterval(animateVoice, 500);
 | 
			
		||||
 | 
			
		||||
  async function setupAudioProcessing(canvas, color) {
 | 
			
		||||
    try {
 | 
			
		||||
      const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
 | 
			
		||||
      const audioContext = new (window.AudioContext || window.webkitAudioContext)();
 | 
			
		||||
      const analyser = audioContext.createAnalyser();
 | 
			
		||||
      const source = audioContext.createMediaStreamSource(stream);
 | 
			
		||||
      source.connect(analyser);
 | 
			
		||||
      analyser.fftSize = 256;
 | 
			
		||||
      const bufferLength = analyser.frequencyBinCount;
 | 
			
		||||
      const dataArray = new Uint8Array(bufferLength);
 | 
			
		||||
      const ctx = canvas.getContext('2d')
 | 
			
		||||
 | 
			
		||||
      const draw = () => {
 | 
			
		||||
          analyser.getByteFrequencyData(dataArray);
 | 
			
		||||
 | 
			
		||||
          // 检查音量是否安静
 | 
			
		||||
          const maxVolume = Math.max(...dataArray);
 | 
			
		||||
          if (maxVolume < 100) {
 | 
			
		||||
            // 如果音量很小,则停止绘制
 | 
			
		||||
            ctx.clearRect(0, 0, canvas.width, canvas.height);
 | 
			
		||||
            requestAnimationFrame(draw);
 | 
			
		||||
            return;
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
        ctx.clearRect(0, 0, canvas.width, canvas.height);
 | 
			
		||||
 | 
			
		||||
          const barWidth = (canvas.width / bufferLength) * 2.5;
 | 
			
		||||
          let x = 0;
 | 
			
		||||
 | 
			
		||||
          for (let i = 0; i < bufferLength; i++) {
 | 
			
		||||
            const barHeight = dataArray[i] / 2;
 | 
			
		||||
 | 
			
		||||
            ctx.fillStyle = color; // 淡蓝色
 | 
			
		||||
            ctx.fillRect(x, canvas.height - barHeight, barWidth, barHeight);
 | 
			
		||||
 | 
			
		||||
            x += barWidth + 2;
 | 
			
		||||
          }
 | 
			
		||||
        //requestAnimationFrame(draw);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      draw();
 | 
			
		||||
    } catch (err) {
 | 
			
		||||
      console.error('获取麦克风权限失败:', err);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 // const data = JSON.parse(localStorage.getItem("chat_data"))
 | 
			
		||||
 // setupPCMProcessing(canvasClientRef.value, '#2ecc71', data, 24000);
 | 
			
		||||
  setupAudioProcessing(canvasServerRef.value, '#2ecc71');
 | 
			
		||||
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
onUnmounted(() => {
 | 
			
		||||
  clearInterval(voiceInterval);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
const hangUp = () => {
 | 
			
		||||
  console.log('Call hung up');
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const answer = () => {
 | 
			
		||||
  console.log('Call answered');
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<style scoped lang="stylus">
 | 
			
		||||
.video-call-container {
 | 
			
		||||
  background: linear-gradient(to right, #2c3e50, #4a5568, #6b46c1);
 | 
			
		||||
  height: 100vh;
 | 
			
		||||
  display: flex;
 | 
			
		||||
  flex-direction: column;
 | 
			
		||||
  justify-content: space-between;
 | 
			
		||||
  align-items: center;
 | 
			
		||||
  padding: 0;
 | 
			
		||||
 | 
			
		||||
  .wave-container {
 | 
			
		||||
    padding 2rem
 | 
			
		||||
    .wave-animation {
 | 
			
		||||
      display: flex;
 | 
			
		||||
      justify-content: center;
 | 
			
		||||
      align-items: center;
 | 
			
		||||
      gap: 10px;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  .wave-ellipse {
 | 
			
		||||
    width: 40px;
 | 
			
		||||
    height: 40px;
 | 
			
		||||
    background-color: white;
 | 
			
		||||
    border-radius: 20px;
 | 
			
		||||
    animation: wave 0.8s infinite ease-in-out;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .wave-ellipse:nth-child(odd) {
 | 
			
		||||
    height: 60px;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .wave-ellipse:nth-child(even) {
 | 
			
		||||
    height: 80px;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @keyframes wave {
 | 
			
		||||
    0%, 100% {
 | 
			
		||||
      transform: scaleY(0.8);
 | 
			
		||||
    }
 | 
			
		||||
    50% {
 | 
			
		||||
      transform: scaleY(1.2);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .wave-ellipse:nth-child(2) {
 | 
			
		||||
    animation-delay: 0.1s;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .wave-ellipse:nth-child(3) {
 | 
			
		||||
    animation-delay: 0.2s;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .wave-ellipse:nth-child(4) {
 | 
			
		||||
    animation-delay: 0.3s;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .wave-ellipse:nth-child(5) {
 | 
			
		||||
    animation-delay: 0.4s;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /* 其余样式保持不变 */
 | 
			
		||||
  .voice-indicators {
 | 
			
		||||
    display: flex;
 | 
			
		||||
    justify-content: space-between;
 | 
			
		||||
    width: 100%;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .voice-indicator {
 | 
			
		||||
    display: flex;
 | 
			
		||||
    align-items: flex-end;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .bar {
 | 
			
		||||
    width: 10px;
 | 
			
		||||
    height: 20px;
 | 
			
		||||
    background-color: #3498db;
 | 
			
		||||
    margin: 0 2px;
 | 
			
		||||
    transition: height 0.2s ease;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .voice-indicator.left .bar:nth-child(1) {
 | 
			
		||||
    height: 15px;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .voice-indicator.left .bar:nth-child(2) {
 | 
			
		||||
    height: 25px;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .voice-indicator.left .bar:nth-child(3) {
 | 
			
		||||
    height: 20px;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .voice-indicator.right .bar:nth-child(1) {
 | 
			
		||||
    height: 20px;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .voice-indicator.right .bar:nth-child(2) {
 | 
			
		||||
    height: 10px;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .voice-indicator.right .bar:nth-child(3) {
 | 
			
		||||
    height: 30px;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .call-controls {
 | 
			
		||||
    display: flex;
 | 
			
		||||
    justify-content: center;
 | 
			
		||||
    gap: 2rem;
 | 
			
		||||
    padding 2rem
 | 
			
		||||
 | 
			
		||||
    .call-button {
 | 
			
		||||
      width: 60px;
 | 
			
		||||
      height: 60px;
 | 
			
		||||
      border-radius: 50%;
 | 
			
		||||
      border: none;
 | 
			
		||||
      display: flex;
 | 
			
		||||
      justify-content: center;
 | 
			
		||||
      align-items: center;
 | 
			
		||||
      font-size: 24px;
 | 
			
		||||
      color: white;
 | 
			
		||||
      cursor: pointer;
 | 
			
		||||
 | 
			
		||||
      .iconfont {
 | 
			
		||||
        font-size 24px
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    .hangup {
 | 
			
		||||
      background-color: #e74c3c;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    .answer {
 | 
			
		||||
      background-color: #2ecc71;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    .icon {
 | 
			
		||||
      font-size: 28px;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
canvas {
 | 
			
		||||
  background-color: transparent;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
</style>
 | 
			
		||||
@@ -1,184 +0,0 @@
 | 
			
		||||
<template>
 | 
			
		||||
  <div class="audio-chat-page">
 | 
			
		||||
    <el-button style="margin: 20px" type="primary" size="large" @click="connect">开始语音对话</el-button>
 | 
			
		||||
 | 
			
		||||
    <el-dialog v-model="showDialog" title="语音通话" :fullscreen="true">
 | 
			
		||||
      <el-container>
 | 
			
		||||
        <calling v-if="!connected" :height="dialogHeight+'px'" />
 | 
			
		||||
        <conversation v-else :height="dialogHeight+'px'" @hang-up="hangUp" />
 | 
			
		||||
      </el-container>
 | 
			
		||||
    </el-dialog>
 | 
			
		||||
  </div>
 | 
			
		||||
</template>
 | 
			
		||||
 | 
			
		||||
<script setup>
 | 
			
		||||
import {ref} from 'vue';
 | 
			
		||||
import { RealtimeClient } from '@openai/realtime-api-beta';
 | 
			
		||||
import Calling from "@/components/Calling.vue";
 | 
			
		||||
import Conversation from "@/components/RealtimeConversation.vue";
 | 
			
		||||
import {playPCM16} from "@/utils/wav_player";
 | 
			
		||||
import {showMessageError} from "@/utils/dialog";
 | 
			
		||||
 | 
			
		||||
const showDialog = ref(false);
 | 
			
		||||
const connected = ref(false);
 | 
			
		||||
const dialogHeight = ref(window.innerHeight - 75);
 | 
			
		||||
 | 
			
		||||
const recognition = ref(null)
 | 
			
		||||
if (!('webkitSpeechRecognition' in window)) {
 | 
			
		||||
  alert("你的浏览器不支持语音识别,请使用最新版本的 Chrome 浏览器。");
 | 
			
		||||
} else {
 | 
			
		||||
  recognition.value = new webkitSpeechRecognition();
 | 
			
		||||
  recognition.value.lang = 'zh-CN'; // 设置语言为简体中文
 | 
			
		||||
  recognition.value.continuous = false; // 设置为单句识别
 | 
			
		||||
  recognition.value.interimResults = false; // 不需要中间结果
 | 
			
		||||
 | 
			
		||||
  recognition.value.onresult = function(event) {
 | 
			
		||||
    const transcript = event.results[0][0].transcript;
 | 
			
		||||
    try {
 | 
			
		||||
      client.cancelResponse(chatId.value)
 | 
			
		||||
      speaker.value.stop()
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
      console.warn(e)
 | 
			
		||||
    }
 | 
			
		||||
    console.log(`你说的是: ${transcript}`)
 | 
			
		||||
    console.log(client.isConnected())
 | 
			
		||||
   if (client.isConnected()){
 | 
			
		||||
     client.sendUserMessageContent([{ type: 'input_text', text: transcript }]);
 | 
			
		||||
   }
 | 
			
		||||
    //recognition.value.start()
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  recognition.value.onerror = function(event) {
 | 
			
		||||
    showMessageError("识别失败:", event.error)
 | 
			
		||||
  };
 | 
			
		||||
  recognition.value.onend = function() {
 | 
			
		||||
    console.log('语音识别结束,重新开始');
 | 
			
		||||
    recognition.value.start(); // 在结束时重新开始
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  //recognition.value.start()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const client = new RealtimeClient({
 | 
			
		||||
  url: "wss://api.geekai.pro/v1/realtime",
 | 
			
		||||
  apiKey: "sk-Gc5cEzDzGQLIqxWA9d62089350F3454bB359C4A3Fa21B3E4",
 | 
			
		||||
  dangerouslyAllowAPIKeyInBrowser: true,
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// Can set parameters ahead of connecting, either separately or all at once
 | 
			
		||||
client.updateSession({ instructions: 'You are a great, upbeat friend.' });
 | 
			
		||||
client.updateSession({ voice: 'nova' });
 | 
			
		||||
client.updateSession({
 | 
			
		||||
  turn_detection: 'disabled', // or 'server_vad'
 | 
			
		||||
  input_audio_transcription: { model: 'whisper-1' },
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
const chatId = ref("")
 | 
			
		||||
const audioChunks = ref([])
 | 
			
		||||
// Set up event handling
 | 
			
		||||
client.on('conversation.updated', ({ item, delta }) => {
 | 
			
		||||
  chatId.value = item.id
 | 
			
		||||
  //console.info('conversation.updated', item, delta)
 | 
			
		||||
  switch (item.type) {
 | 
			
		||||
    case 'message':
 | 
			
		||||
      // system, user, or assistant message (item.role)
 | 
			
		||||
      localStorage.setItem("chat_data", JSON.stringify(Array.from(item.formatted.audio)))
 | 
			
		||||
      console.log(item)
 | 
			
		||||
      break;
 | 
			
		||||
    case 'function_call':
 | 
			
		||||
      // always a function call from the model
 | 
			
		||||
      break;
 | 
			
		||||
    case 'function_call_output':
 | 
			
		||||
      // always a response from the user / application
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
  if (delta) {
 | 
			
		||||
    // console.info(delta.audio)
 | 
			
		||||
    if (delta.audio && delta.audio.length > 1) {
 | 
			
		||||
      audioChunks.value.push(delta.audio)
 | 
			
		||||
    }
 | 
			
		||||
    if (audioChunks.value.length === 1) {
 | 
			
		||||
      playAudio(0)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //localStorage.setItem("chat_data", JSON.stringify(Array.from(delta.audio)))
 | 
			
		||||
    // Only one of the following will be populated for any given event
 | 
			
		||||
    // delta.audio = Int16Array, audio added
 | 
			
		||||
    // delta.transcript = string, transcript added
 | 
			
		||||
    // delta.arguments = string, function arguments added
 | 
			
		||||
  }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
const speaker = ref(null)
 | 
			
		||||
const playAudio = (index) => {
 | 
			
		||||
  if (index === 0 && speaker.value) {
 | 
			
		||||
    speaker.value.stop()
 | 
			
		||||
  }
 | 
			
		||||
  const data = audioChunks.value[index]
 | 
			
		||||
  console.log(data)
 | 
			
		||||
  if (index === audioChunks.value.length-1) {
 | 
			
		||||
    audioChunks.value = []
 | 
			
		||||
  }
 | 
			
		||||
  speaker.value = playPCM16(data, 24000);
 | 
			
		||||
  if (speaker.value !== null) {
 | 
			
		||||
    speaker.value.onended = () => {
 | 
			
		||||
      playAudio(index + 1)
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
client.on('conversation.interrupted', async () => {
 | 
			
		||||
  console.log('聊天中断')
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
client.on('conversation.item.appended', ({ item }) => {
 | 
			
		||||
  if (item.role === 'assistant') {
 | 
			
		||||
    // playPCM16(item.formatted.audio, 24000);
 | 
			
		||||
    // console.log(item)
 | 
			
		||||
  }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
const connect = () => {
 | 
			
		||||
  showDialog.value = true
 | 
			
		||||
  client.connect().then(res => {
 | 
			
		||||
    if (res) {
 | 
			
		||||
      console.log("连接成功!")
 | 
			
		||||
      connected.value = true
 | 
			
		||||
      // const data = JSON.parse(localStorage.getItem("chat_data"))
 | 
			
		||||
      // playPCM16(data, 24000)
 | 
			
		||||
      client.sendUserMessageContent([{ type: 'input_text', text: `你好,我是老阳。` }]);
 | 
			
		||||
    }
 | 
			
		||||
  }).catch(e => {
 | 
			
		||||
    console.log(e)
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const hangUp = () => {
 | 
			
		||||
 try {
 | 
			
		||||
   client.cancelResponse(chatId.value)
 | 
			
		||||
   speaker.value.stop()
 | 
			
		||||
 } catch (e) {
 | 
			
		||||
   console.warn(e)
 | 
			
		||||
  }
 | 
			
		||||
  showDialog.value = false
 | 
			
		||||
  connected.value = false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<style scoped lang="stylus">
 | 
			
		||||
.audio-chat-page {
 | 
			
		||||
  display flex
 | 
			
		||||
  flex-flow column
 | 
			
		||||
  justify-content center
 | 
			
		||||
  align-items center
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
canvas {
 | 
			
		||||
  background-color: transparent;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
</style>
 | 
			
		||||
@@ -15,8 +15,8 @@
 | 
			
		||||
            </span>
 | 
			
		||||
          </template>
 | 
			
		||||
        </el-table-column>
 | 
			
		||||
        <el-table-column prop="price" label="产品价格"/>
 | 
			
		||||
        <el-table-column prop="discount" label="优惠金额"/>
 | 
			
		||||
        <el-table-column prop="price" label="商品原价"/>
 | 
			
		||||
        <el-table-column prop="discount" label="优惠价"/>
 | 
			
		||||
        <el-table-column prop="days" label="有效期(天)">
 | 
			
		||||
          <template #default="scope">
 | 
			
		||||
            <el-tag v-if="scope.row.days === 0">长期有效</el-tag>
 | 
			
		||||
@@ -56,15 +56,15 @@
 | 
			
		||||
        :close-on-click-modal="false"
 | 
			
		||||
    >
 | 
			
		||||
      <el-form :model="item" label-width="120px" ref="formRef" :rules="rules">
 | 
			
		||||
        <el-form-item label="产品名称:" prop="name">
 | 
			
		||||
        <el-form-item label="商品名称:" prop="name">
 | 
			
		||||
          <el-input v-model="item.name" autocomplete="off"/>
 | 
			
		||||
        </el-form-item>
 | 
			
		||||
 | 
			
		||||
        <el-form-item label="产品价格:" prop="price">
 | 
			
		||||
        <el-form-item label="商品原价:" prop="price">
 | 
			
		||||
          <el-input v-model="item.price" autocomplete="off"/>
 | 
			
		||||
        </el-form-item>
 | 
			
		||||
 | 
			
		||||
        <el-form-item label="优惠金额:" prop="discount">
 | 
			
		||||
        <el-form-item label="优惠价:" prop="discount">
 | 
			
		||||
          <el-input v-model="item.discount" autocomplete="off"/>
 | 
			
		||||
        </el-form-item>
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -75,7 +75,7 @@
 | 
			
		||||
          <van-cell-group>
 | 
			
		||||
            <van-cell title="商品价格">
 | 
			
		||||
              <span class="price">
 | 
			
		||||
                ¥{{ (item.price - item.discount).toFixed(2) }}
 | 
			
		||||
                ¥{{ item.discount }}
 | 
			
		||||
              </span>
 | 
			
		||||
              (
 | 
			
		||||
              <del>¥{{ item.price }}</del>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user