以下是在 后端ThinkPHP 6 + 前端UniApp环境下接入DeepSeek到微信生态的完整解决方案,包含详细步骤和代码示例:
一、准备工作
1. 微信生态配置
小程序
- 设置服务器域名:登录微信公众平台 -> 开发管理 -> 服务器域名
request合法域名:https://yourdomain.com
socket合法域名:wss://yourdomain.com
公众号
- 服务器配置: URL:
https://yourdomain.com/wechat
Token:自定义令牌 EncodingAESKey:随机生成
2. ThinkPHP 6项目初始化
composer create-project topthink/think tp6_deepseek
cd tp6_deepseek
composer require topthink/think-multi-app # 多应用支持
二、后端开发(ThinkPHP 6)
1. 目录结构
app
├── deepseek
│ ├── controller
│ │ └── Index.php # DeepSeek接口控制器
│ └── service
│ └── DeepseekService.php # 服务层
└── wechat
└── controller
└── Index.php # 公众号消息处理
2. 核心代码
(1) DeepSeek服务类 //
app/deepseek/service/DeepseekService.php
'deepseek-chat',
'messages' => [['role' => 'user', 'content' => $prompt]],
'max_tokens' => 1000
];
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $this->apiUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Bearer ' . $this->apiKey
],
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($data)
]);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
}
(2) 控制器实现 //
app/deepseek/controller/Index.php
chat($input['prompt'] ?? '');
return json([
'code' => isset($result['choices']) ? 200 : 500,
'data' => $result['choices'][0]['message']['content'] ?? '服务繁忙'
]);
}
}
(3) 公众号消息处理 //
app/wechat/controller/Index.php
request->get('echostr')) {
return $this->request->get('echostr');
}
// 处理消息
$xml = $this->request->getContent();
$data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$service = new DeepseekService();
$response = $service->chat((string)$data->Content);
return $this->response->xml([
'ToUserName' => $data->FromUserName,
'FromUserName' => $data->ToUserName,
'CreateTime' => time(),
'MsgType' => 'text',
'Content' => $response['choices'][0]['message']['content'] ?? '请稍后再试'
]);
}
}
3. 路由配置
// config/route.php
use think\facade\Route;
// DeepSeek接口
Route::post('deepseek/chat', 'deepseek.Index/chat');
// 公众号消息入口
Route::any('wechat', 'wechat.Index/index');
三、前端开发(UniApp)
1. 页面实现
{{ msg.content }}
<script>
export default {
data() {
return {
inputMsg: '',
messages: []
}
},
methods: {
async send() {
if (!this.inputMsg.trim()) return;
this.messages.push({ role: 'user', content: this.inputMsg });
const msg = this.inputMsg;
this.inputMsg = '';
try {
const { code, data } = await uni.request({
url: 'https://yourdomain.com/deepseek/chat',
method: 'POST',
header: { 'Content-Type': 'application/json' },
data: { prompt: msg }
});
if (code === 200) {
this.messages.push({ role: 'assistant', content: data });
} else {
uni.showToast({ title: '服务异常', icon: 'none' });
}
} catch (e) {
uni.showToast({ title: '网络错误', icon: 'none'})
}
}
}
四、部署与调试建议
1. Nginx配置建议
server {
listen 80;
server_name yourdomain.com;
location / {
root /path/to/tp6/public;
index index.php index.html;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
}
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
2. 常见调试技巧
# 实时查看DeepSeek请求日志
tail -f /var/log/nginx/deepseek_access.log | grep "POST /deepseek/chat"
# ThinkPHP调试模式
# 修改.env文件
APP_DEBUG = true
# 微信接口调试工具
curl -X POST "https://yourdomain.com/wechat" \
-d '<xml>...</xml>' \
-H "Content-Type: text/xml"
3. 性能优化建议
// 在DeepseekService中使用连接池
$pool = new Swoole\Coroutine\Channel(10);
for ($i = 0; $i < 10 i pool->push(new Swoole\Coroutine\Http\Client('api.deepseek.com', 443, true));
}
// 请求时复用连接
$client = $pool->pop();
$client->post('/v1/chat/completions', json_encode($data));
$response = $client->body;
$pool->push($client);
以上为完整实现方案,可根据实际业务需求调整参数和扩展功能模块。
本文暂时没有评论,来添加一个吧(●'◡'●)