微信小程序接入deepseek

devtools/2025/3/17 17:23:01/

先上效果

在这里插入图片描述

话不多说,直接上代码(本人用的hbuilder X+uniapp)

<template><view class="container"><!-- 聊天内容区域 --><scroll-view class="chat-list" scroll-y :scroll-top="scrollTop":scroll-with-animation="true"><view v-for="(item, index) in chatList" :key="index" class="chat-item":class="item.role === 'user' ? 'user' : 'assistant'"><view class="content-box"><text class="content">{{ item.content }}</text><text class="time">{{ item.time }}</text></view><view v-if="item.loading" class="loading"><view class="dot"></view><view class="dot"></view><view class="dot"></view></view></view></scroll-view><!-- 输入区域 --><view class="input-area"><input class="input" v-model="inputMessage" placeholder="请输入问题..."@confirm="sendMessage":disabled="isSending"placeholder-class="placeholder"/><button class="send-btn" @tap="sendMessage":disabled="isSending || !inputMessage">{{ isSending ? '发送中...' : '发送' }}</button></view></view>
</template><script>
const API_URL = 'https://api.deepseek.com/v1/chat/completions' // 替换为实际API地址
const API_KEY = 'sk-' // 替换为你的API密钥export default {data() {return {chatList: [],inputMessage: '',isSending: false,scrollTop: 0}},methods: {async sendMessage() {if (!this.inputMessage.trim() || this.isSending) returnconst userMessage = {role: 'user',content: this.inputMessage,time: this.getCurrentTime(),loading: false}// 添加用户消息console.log(userMessage)this.chatList.push(userMessage)this.inputMessage = ''// 添加AI的loading状态const assistantMessage = {role: 'assistant',content: '',time: '',loading: true}this.chatList.push(assistantMessage)this.isSending = truethis.scrollToBottom()try {const res = await uni.request({url: API_URL,method: 'POST',header: {'Content-Type': 'application/json','Authorization': `Bearer ${API_KEY}`},data: {model: 'deepseek-chat', // 模型messages: this.chatList.filter(item => !item.loading).map(item => ({role: item.role,content: item.content}))}})// 更新AI消息const lastIndex = this.chatList.length - 1this.chatList[lastIndex] = {role: 'assistant',content: res.data.choices[0].message.content,time: this.getCurrentTime(),loading: false}this.$forceUpdate()} catch (error) {console.error('API请求失败:', error)uni.showToast({title: '请求失败,请重试',icon: 'none'})this.chatList.pop() // 移除loading状态的消息} finally {this.isSending = falsethis.scrollToBottom()}},getCurrentTime() {const date = new Date()return `${date.getHours()}:${date.getMinutes().toString().padStart(2, '0')}`},scrollToBottom() {this.$nextTick(() => {this.scrollTop = Math.random() * 1000000 // 通过随机数强制滚动到底部})}}
}
</script><style scoped>
/* 新增修改的样式 */
.container {padding-bottom: 120rpx; /* 给输入框留出空间 */
}.chat-list {padding: 20rpx 20rpx 160rpx; /* 底部留白防止遮挡 */
}.chat-item {margin: 30rpx 0;
}.content-box {max-width: 75%;padding: 20rpx 28rpx;border-radius: 16rpx;margin: 0 20rpx;position: relative;
}.user .content-box {background: #1989fa;color: white;margin-left: auto; /* 右对齐关键属性 */
}.assistant .content-box {background: #f8f8f8;color: #333;margin-right: auto; /* 左对齐关键属性 */
}.time {font-size: 24rpx;color: #999;position: absolute;bottom: -40rpx;white-space: nowrap;
}/* 新版加载动画 */
.loading {display: flex;align-items: center;padding: 20rpx;
}.dot {width: 12rpx;height: 12rpx;background: #ddd;border-radius: 50%;margin: 0 4rpx;animation: bounce 1.4s infinite ease-in-out;
}.dot:nth-child(2) {animation-delay: 0.2s;
}.dot:nth-child(3) {animation-delay: 0.4s;
}@keyframes bounce {0%, 80%, 100% { transform: translateY(0);}40% {transform: translateY(-10rpx);}
}/* 输入区域样式优化 */
.input-area {position: fixed;bottom: 0;left: 0;right: 0;display: flex;align-items: center;padding: 20rpx;background: #fff;border-top: 1rpx solid #eee;box-shadow: 0 -4rpx 20rpx rgba(0,0,0,0.05);
}.input {flex: 1;height: 80rpx;padding: 0 28rpx;background: #e9e9e9;border-radius: 40rpx;font-size: 28rpx;color: #333;border: none;
}.placeholder {color: #999;
}.send-btn {width: 140rpx;height: 80rpx;line-height: 80rpx;margin-left: 20rpx;background: #1989fa;color: white;border-radius: 40rpx;font-size: 28rpx;border: none;
}.send-btn[disabled] {opacity: 0.6;background: blue;color: white;
}
</style>

注:
1、修改你的密钥
2、需要在微信公众平台里面配置服务器域名(在开发管理里面)

https://api.deepseek.com

3、如果接口中有返回数据,但页面中没有数据展示,同时还提示API请求失败,打开接口返回的地址,与下面这段代码一一对应,看路径有没有问题

content: res.data.choices[0].message.content

在这里插入图片描述


http://www.ppmy.cn/devtools/167882.html

相关文章

LabVIEW VI Scripting随机数波形图自动生成

通过LabVIEW VI Scripting 技术&#xff0c;实现从零开始编程化创建并运行一个随机数波形监测VI。核心功能包括自动化生成VI框架、添加控件与函数、配置数据流逻辑及界面布局优化&#xff0c;适用于批量生成测试工具、教学模板开发或复杂系统的模块化构建。通过脚本化操作&…

文件解析漏洞靶场---解析详解

⽂件解析漏洞是由于中间件错误的将任意格式的⽂件解析成⽹⻚可执⾏⽂件&#xff0c;配合⽂件上传漏洞进⾏ GetShell的漏洞&#xff01; 一、IIS解析漏洞 3个 1. IIS6.X 2个 环境需求&#xff1a;windows2003iis6 安装windows2003&#xff1b; 安装iis&#xff1a;控…

k8s系统学习路径

学习 Kubernetes&#xff08;K8s&#xff09;需要循序渐进&#xff0c;结合理论知识和实践操作。以下是学习 Kubernetes 的推荐步骤&#xff1a; 1. 先决条件 • 掌握容器基础&#xff1a;先学习 Docker&#xff0c;理解容器化概念&#xff08;镜像、容器、仓库&#xff09;、…

【蓝桥杯每日一题】3.16

&#x1f3dd;️专栏&#xff1a; 【蓝桥杯备篇】 &#x1f305;主页&#xff1a; f狐o狸x 目录 3.9 高精度算法 一、高精度加法 题目链接&#xff1a; 题目描述&#xff1a; 解题思路&#xff1a; 解题代码&#xff1a; 二、高精度减法 题目链接&#xff1a; 题目描述&…

【DeepSeek应用】DeepSeek模型本地化部署方案及Python实现

DeepSeek实在是太火了,虽然经过扩容和调整,但反应依旧不稳定,甚至小圆圈转半天最后却提示“服务器繁忙,请稍后再试。” 故此,本文通过讲解在本地部署 DeepSeek并配合python代码实现,让你零成本搭建自己的AI助理,无惧任务提交失败的压力。 一、环境准备 1. 安装依赖库 …

LiveData 与 ViewModel 协同工作:从原理到面试实战

在 Android 开发的技术长河中&#xff0c;Jetpack 架构组件犹如璀璨星辰&#xff0c;其中 LiveData 与 ViewModel 更是在数据管理和 UI 交互领域发挥着举足轻重的作用。 它们的协同工作模式不仅极大地提升了代码的可维护性和稳定性&#xff0c;还成为了众多 Android 开发者面试…

MyBatis 如何创建 SqlSession 对象的?

MyBatis 创建 SqlSession 对象的过程主要由 SqlSessionFactory 接口及其实现类来完成。以下是详细步骤&#xff1a; 1. SqlSessionFactory 接口: SqlSessionFactory 是 MyBatis 的核心接口之一&#xff0c;它负责创建 SqlSession 对象。 你可以将 SqlSessionFactory 视为 Sql…

【机器学习-基础知识】统计和贝叶斯推断

1. 概率论基本概念回顾 1. 概率分布 定义: 概率分布(Probability Distribution)指的是随机变量所有可能取值及其对应概率的集合。它描述了一个随机变量可能取的所有值以及每个值被取到的概率。 对于离散型随机变量,使用概率质量函数来描述。对于连续型随机变量,使用概率…