在微信开发者工具中创建小程序项目,并开通云开发。
在 project.config.json
中配置云函数目录:
json
复制
{
"cloudfunctionRoot": "cloud/functions/"
}
初始化云开发环境:
javascript
复制
wx.cloud.init({
env: 'your-env-id', // 替换为你的云环境 ID
traceUser: true
});
复制
cloud/
├── functions/
│ ├── asr/ # 语音识别云函数
│ │ ├── index.js
│ │ ├── config.json
│ │ └── package.json
│ ├── deepseek/ # DeepSeek 处理云函数
│ │ ├── index.js
│ │ ├── config.json
│ │ └── package.json
asr/index.js
)javascript
复制
const tencentcloud = require('tencentcloud-sdk-nodejs');
const AsrClient = tencentcloud.asr.v20190614.Client;
const fs = require('fs');
const path = require('path');
exports.main = async (event, context) => {
const { fileID } = event;
// 下载录音文件
const fileContent = await wx.cloud.downloadFile({
fileID: fileID
});
const audioPath = fileContent.tempFilePath;
const audioData = fs.readFileSync(audioPath, { encoding: 'base64' });
// 调用腾讯云语音识别
const client = new AsrClient({
credential: {
secretId: process.env.TENCENT_SECRET_ID, // 从环境变量获取
secretKey: process.env.TENCENT_SECRET_KEY
},
region: 'ap-guangzhou',
profile: {
httpProfile: {
endpoint: 'asr.tencentcloudapi.com'
}
}
});
const params = {
EngineModelType: '16k_zh',
VoiceFormat: 'wav',
Data: audioData
};
try {
const response = await client.SentenceRecognition(params);
return {
success: true,
data: response.Result
};
} catch (error) {
return {
success: false,
message: error.message
};
}
};
deepseek/index.js
)javascript
复制
const axios = require('axios');
exports.main = async (event, context) => {
const { text } = event;
try {
const response = await axios.post(
'https://api.deepseek.com/v1/chat/completions',
{
model: 'deepseek-chat',
messages: [
{ role: 'system', content: '你是一个文本处理助手。' },
{ role: 'user', content: text }
]
},
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.DEEPSEEK_API_KEY}` // 从环境变量获取
}
}
);
return {
success: true,
data: response.data.choices[0].message.content
};
} catch (error) {
return {
success: false,
message: error.message
};
}
};
在云函数目录下运行:
bash
复制
npm install tencentcloud-sdk-nodejs axios
在云开发控制台中,为云函数配置环境变量:
TENCENT_SECRET_ID
: 腾讯云 SecretIdTENCENT_SECRET_KEY
: 腾讯云 SecretKeyDEEPSEEK_API_KEY
: DeepSeek API 密钥复制
miniprogram/
├── pages/
│ ├── index/
│ │ ├── index.js # 页面逻辑
│ │ ├── index.wxml # 页面结构
│ │ └── index.wxss # 页面样式
├── app.js # 小程序逻辑
├── app.json # 小程序配置
└── app.wxss # 全局样式
index.js
)javascript
复制
Page({
data: {
isRecording: false, // 是否正在录音
recordTime: 0, // 录音时长
resultText: '', // 识别结果
editedText: '', // 编辑后的文本
isLoading: false // 加载状态
},
// 开始录音
startRecord() {
this.setData({ isRecording: true, recordTime: 0 });
this.recorderManager = wx.getRecorderManager();
this.recorderManager.start({
format: 'wav',
sampleRate: 16000,
numberOfChannels: 1,
encodeBitRate: 48000
});
this.timer = setInterval(() => {
this.setData({ recordTime: this.data.recordTime + 1 });
}, 1000);
this.recorderManager.onStop(async (res) => {
clearInterval(this.timer);
this.setData({ isRecording: false });
await this.uploadAudio(res.tempFilePath); // 上传录音文件
});
},
// 停止录音
stopRecord() {
if (this.recorderManager) {
this.recorderManager.stop();
}
},
// 上传录音文件
async uploadAudio(filePath) {
this.setData({ isLoading: true });
try {
// 上传到云存储
const uploadRes = await wx.cloud.uploadFile({
cloudPath: `audios/${Date.now()}.wav`,
filePath: filePath
});
// 调用语音识别云函数
const asrRes = await wx.cloud.callFunction({
name: 'asr',
data: { fileID: uploadRes.fileID }
});
if (asrRes.result.success) {
this.setData({ resultText: asrRes.result.data, editedText: asrRes.result.data });
} else {
wx.showToast({ title: '识别失败', icon: 'none' });
}
} catch (error) {
wx.showToast({ title: '上传失败', icon: 'none' });
} finally {
this.setData({ isLoading: false });
}
},
// 编辑文本
handleEditText(e) {
this.setData({ editedText: e.detail.value });
},
// 调用 DeepSeek 处理文本
async processText() {
const { editedText } = this.data;
if (!editedText) {
wx.showToast({ title: '请输入文本', icon: 'none' });
return;
}
this.setData({ isLoading: true });
try {
const deepseekRes = await wx.cloud.callFunction({
name: 'deepseek',
data: { text: editedText }
});
if (deepseekRes.result.success) {
this.setData({ resultText: deepseekRes.result.data });
} else {
wx.showToast({ title: '处理失败', icon: 'none' });
}
} catch (error) {
wx.showToast({ title: '网络错误', icon: 'none' });
} finally {
this.setData({ isLoading: false });
}
}
});
index.wxml
)xml
复制
录音中:{{recordTime}}s
识别结果:
处理结果:
处理中...
运行 HTML
index.wxss
)css
复制
.container {
padding: 20px;
background-color: #F5F5F5;
min-height: 100vh;
}
.record-button {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 20px;
}
.start-button {
background-color: #07C160;
color: white;
width: 80%;
}
.stop-button {
background-color: #F5222D;
color: white;
width: 80%;
}
.record-time {
margin-top: 10px;
font-size: 14px;
color: #666666;
}
.result-box {
width: 100%;
margin-top: 20px;
}
.result-title {
font-size: 16px;
font-weight: bold;
color: #333333;
margin-bottom: 10px;
}
.result-text {
width: 100%;
height: 100px;
background-color: #FFFFFF;
border: 1px solid #CCCCCC;
border-radius: 5px;
padding: 10px;
font-size: 14px;
color: #333333;
}
.process-button {
margin-top: 20px;
background-color: #1890FF;
color: white;
width: 80%;
}
.loading {
margin-top: 20px;
font-size: 14px;
color: #666666;
text-align: center;
}