微信小程序接入deepseek

ops/2025/3/10 18:14:43/

先上效果

在这里插入图片描述

话不多说,直接上代码(本人用的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/ops/164720.html

相关文章

全域网络安全防御 健全网络安全防护体系

网络安全基本概念 网络安全&#xff08;Cyber Security&#xff09;是指网络系统的硬件、软件及其系统中的数据受到保护&#xff0c;不因偶然的或者恶意的原因而遭受到破坏、更改、泄露&#xff0c;系统连续可靠正常地运行&#xff0c;网络服务不中断&#xff0c;使网络处于稳…

k-Shape:高效准确的聚类方法

引言 时间数据在许多学科中的扩散和无处不在&#xff0c;已经对时间序列的分析和挖掘产生了极大的兴趣。聚类是最流行的数据挖掘方法之一&#xff0c;不仅因为它的探索性&#xff0c;而且作为其他技术的预处理步骤或子程序。常用的有-means聚类算法。本文介绍了一种新的时间序…

MySQL 5.7.40 主从同步配置教程

MySQL 主从同步能有效提升数据冗余备份与负载均衡。下面我将以 MySQL 5.7.40 版本为例&#xff0c;详细讲解如何进行主从同步配置。 MySQL 5.7.40 主从同步配置教程 一、环境准备 假设我们有两台服务器&#xff0c;一台作为主服务器&#xff08;Master&#xff09;&#xff…

let、const和var的区别

文章目录 一、概述二、var变量的问题与限制三、let、const和var的区别3.1、作用域‌3.2、变量提升&#xff08;Hoisting&#xff09;‌3.3、重复声明3.4、可变性‌3.5、 全局对象属性‌3.6、初始值设置‌‌ 四、总结五、实践使用 一、概述 在JavaScript中&#xff0c;let、con…

C++程序设计语言笔记——基本功能:表达式

0 优先使用标准库&#xff0c;然后是其他库&#xff0c;最后才是“手工打造的代码”。 在软件开发中&#xff0c;遵循“优先使用标准库 → 第三方库 → 手动实现”的原则能有效提升代码质量、开发效率和可维护性。以下是具体实践指南&#xff1a; 1. 优先使用标准库 为什么&a…

IDE集成开发环境MyEclipse中安装SVN

打开Myeclipse的help菜单----install from site 点击add弹出对话框 在输入框中输入对应内容 http://subclipse.tigris.org/update_1.10.x 点击OK之后&#xff0c;会刷新出两个选项&#xff0c;需要选中的 点击next&#xff0c;出现许可的时候选中同意&#xff0c;一直结束等…

Array and string offset access syntax with curly braces is deprecated

警告信息 “Array and string offset access syntax with curly braces is deprecated” 是 PHP 中的一个弃用警告&#xff08;Deprecation Notice&#xff09;&#xff0c;表明在 PHP 中使用花括号 {} 来访问数组或字符串的偏移量已经被标记为过时。 背景 在 PHP 的早期版本…

Unity UGUI下实现精确点击的一种方式

比如有这样一个情况&#xff0c;UI的显示区域是个圆形&#xff0c;在点击的时候也需要精确点击到这个圆形显示区域&#xff0c;但是UI元素的RectTransform是个矩形 1. 使用脚本修改 2. 原理探究 此脚本继承了Image组件&#xff0c;但是获取了自身的Collider2D&#xff0c;目…