基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面

这篇具有很好参考价值的文章主要介绍了基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

使用vue3+pinia2开发仿制chatgpt界面聊天实例Vue3-Chatgpt

基于Vue3.x+Pinia2+VueRouter+Vue3-Markdown等技术构建仿ChatGPT网页端聊天程序。支持经典+分栏界面布局、light/dark模式、全屏+半屏显示、Markdown语法解析、侧边栏隐藏等功能。

基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面

技术框架

  • 编辑工具:Cursor
  • 框架技术:Vue3+Vite4.x+Pinia2
  • 组件库:VEPlus (基于vue3桌面端组件库)
  • 国际化多语言:vue-i18n^9.2.2
  • 代码高亮:highlight.js^11.7.0
  • 本地存储:pinia-plugin-persistedstate^3.1.0
  • markdown解析:vue3-markdown-it

基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面

项目目录结构

基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面

vite.config.js配置

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import { parseEnv } from './src/utils/env'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
	const viteEnv = loadEnv(mode, process.cwd())
	const env = parseEnv(viteEnv)

	return {
		plugins: [vue()],

		// base: '/',
		// mode: 'development', // development|production

		/*构建选项*/
		build: {
			// minify: 'esbuild', // 打包方式 esbuild(打包快)|terser
			// chunkSizeWarningLimit: 2000, // 打包大小警告
			// rollupOptions: {
			// 	output: {
			// 		chunkFileNames: 'assets/js/[name]-[hash].js',
			// 		entryFileNames: 'assets/js/[name]-[hash].js',
			// 		assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
			// 	}
			// }
		},
		esbuild: {
			// 打包去除 console.log 和 debugger
			drop: env.VITE_DROP_CONSOLE ? ['console', 'debugger'] : []
		},

		/*开发服务器选项*/
		server: {
			// 端口
			port: env.VITE_PORT,
			// 是否浏览器自动打开
			open: env.VITE_OPEN,
			// 开启https
			https: env.VITE_HTTPS,
			// 代理配置
			proxy: {
				// ...
			}
		},

		resolve: {
			// 设置别名
			alias: {
				'@': resolve(__dirname, 'src'),
				'@assets': resolve(__dirname, 'src/assets'),
				'@components': resolve(__dirname, 'src/components'),
				'@views': resolve(__dirname, 'src/views'),
				// 解决vue-i18n警告提示:You are running the esm-bundler build of vue-i18n.
				'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
			}
		}
	}
})

main.js主入口

import { createApp } from 'vue'
import App from './App.vue'

// 引入Router和Store
import Router from './router'
import Store from './store'

// 引入插件配置
import Plugins from './plugins'

const app = createApp(App)

app
.use(Router)
.use(Store)
.use(Plugins)
.mount('#app')

vue3.x组件库

项目中使用的组件库是基于vue3自定义UI组件库Ve-plus。一个支持40+组件的轻量级组件库。
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面

基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
安装组件

yarn add ve-plus
npm i ve-plus --save

https://blog.csdn.net/yanxinyun1990/article/details/129312570

整体布局

项目支持2种布局模式,整体分为顶栏+侧边栏+主体内容三大模块构成。
基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面

基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面

<div class="ve__layout-body flex1 flexbox">
	<!-- //中间栏 -->
	<div class="ve__layout-menus flexbox" :class="{'hidden': store.config.collapse}">
		<aside class="ve__layout-aside flexbox flex-col">
			<ChatNew />
			<Scrollbar class="flex1" autohide size="4" gap="1">
				<ChatList />
			</Scrollbar>
			<ExtraLink />
			<Collapse />
		</aside>
	</div>

	<!-- //右边栏 -->
	<div class="ve__layout-main flex1 flexbox flex-col">
		<!-- 主内容区 -->
		<Main />
	</div>
</div>
<template>
	<div class="vegpt__editor">
		<div class="vegpt__editor-inner">
			<Flex :gap="0">
				<Popover placement="top" trigger="click" width="150">
					<Button class="btn" type="link" icon="ve-icon-yuyin1" v-tooltip="{content: '发送语音', theme: 'light', arrow: false}"></Button>
					<template #content>
						<div class="flexbox flex-alignc flex-col" style="padding: 15px 0;">
							<Icon name="ve-icon-yuyin" size="40" color="#0fa27e" />
							<p class="fs-12 mb-15 c-999">网络不给力</p>
							<Button size="small"><i style="background:#f00;border-radius:50%;box-shadow:0 1px 2px #999;margin-right:5px;height:8px;width:8px;"></i>开始讲话</Button>
						</div>
					</template>
				</Popover>
				<Button class="btn" type="link" v-tooltip="{content: '发送图片', theme: 'light', arrow: false}">
					<Icon name="ve-icon-photo" size="16" cursor />
					<input ref="uploadImgRef" type="file" title="" accept="image/*" @change="handleUploadImage" />
				</Button>
				<Input
					class="flex1"
					ref="editorRef"
					v-model="editorText"
					type="textarea"
					:autosize="{maxRows: 4}"
					clearable
					placeholder="Prompt..."
					@keydown="handleKeydown"
					@clear="handleClear"
					style="margin: 0 5px;"
				/>
				<Button class="btn" type="link" icon="ve-icon-submit" @click="handleSubmit"></Button>
			</Flex>
		</div>
	</div>
</template>
import { ref, watch } from 'vue'
import { guid } from '@/utils'
import { chatStore } from '@/store/modules/chat'

const props = defineProps({
	value: { type: [String, Number] }
})
const emit = defineEmits(['clear'])

const chatState = chatStore()

const uploadImgRef = ref()
const editorRef = ref()
const editorText = ref(props.value)

// ...

// 发送会话
const handleSubmit = () => {
	editorRef.value.focus()
	if(!editorText.value) return

	let data = {
		type: 'text',
		role: 'User',
		key: guid(),
		content: editorText.value
	}
	chatState.addSession(data)
	// 清空
	editorText.value = ''
}
const handleKeydown = (e) => {
	// ctrl+enter
	if(e.ctrlKey && e.keyCode == 13) {
		handleSubmit()
	}
}
const handleClear = () => {
	emit('clear')
}
// 选择图片
const handleUploadImage = () => {
	let file = uploadImgRef.value.files[0]
	if(!file) return
	let size = Math.floor(file.size / 1024)
	console.log(size)
	if(size > 2*1024) {
		Message.danger('图片大小不能超过2M')
		uploadImgRef.value.value = ''
		return false
	}
	let reader = new FileReader()
	reader.readAsDataURL(file)
	reader.onload = function() {
		let img = this.result

		let data = {
			type: 'image',
			role: 'User',
			key: guid(),
			content: img
		}
		chatState.addSession(data)
	}
}

基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面

/**
 * 聊天状态管理
 * @author YXY  Q:282310962
 */

import { defineStore } from 'pinia'
import { guid, isEmpty } from '@/utils'

export const chatStore = defineStore('chat', {
	state: () => ({
		// 聊天会话记录
		sessionId: '',
		session: []
	}),
	getters: {},
	actions: {
		// 创建新会话
		createSession(ssid) {
			this.sessionId = ssid
			this.session.push({
				sessionId: ssid,
				title: '',
				data: []
			})
		},

		// 新增会话
		addSession(message) {
			// 判断当前会话uuid是否存在,不存在创建新会话
			if(!this.sessionId) {
				const ssid = guid()
				this.createSession(ssid)
			}
			this.session.map(item => {
				if(item.sessionId == this.sessionId) {
					if(!item.title) {
						item.title = message.content
					}
					item.data.push(message)
				}
			})
			// ...
		},

		// 获取会话
		getSession() {
			return this.session.find(item => item.sessionId == this.sessionId)
		},

		// 移除会话
		removeSession(ssid) {
			const index = this.session.findIndex(item => item?.sessionId === ssid)
			if(index > -1) {
				this.session.splice(index, 1)
			}
			this.sessionId = ''
		},
		// 删除某一条会话
		deleteSession(ssid) {
			// ...
		},

		// 清空会话
		clearSession() {
			this.session = []
			this.sessionId = ''
		}
	},
	// 本地持久化存储(默认存储localStorage)
	persist: true
	/* persist: {
		// key: 'chatStore', // 不设置则是默认app
		storage: localStorage,
		paths: ['aa', 'bb'] // 设置缓存键
	} */
})

好了,基于vue3+vite4+pinia2开发模仿chatgpt聊天就分享到这里。

Tauri-Vue3聊天实例|Tauri跨端聊天
uniapp-ttlive短视频聊天|uniapp+uview仿抖音实例

基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面文章来源地址https://www.toymoban.com/news/detail-438630.html

到了这里,关于基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Vite4+Typescript+Vue3+Pinia 从零搭建(3) - vite配置

    项目代码同步至码云 weiz-vue3-template 关于vite的详细配置可查看 vite官方文档,本文简单介绍vite的常用配置。 项目初建后, vite.config.ts 的默认内容如下: 比如,修改 App.vue : 根目录下新建 .env 、 .env.development 、 .env.production 三个文件。 .env 文件内新增内容: .env.development 文件内

    2024年02月05日
    浏览(65)
  • Vite4+Typescript+Vue3+Pinia 从零搭建(2) - ts配置

    项目代码同步至码云 weiz-vue3-template 关于tsconfig的配置字段可查看其他文档,如 typeScript tsconfig配置详解 文件修改如下: 修改文件如下: 新建文件夹 types ,用来存放类型定义。比如新建 index.d.ts : 后续也可以新增其他文件,比如 global.d.ts 存放全局定义, router.d.ts 存放路由定

    2024年02月05日
    浏览(50)
  • Vite4+Typescript+Vue3+Pinia 从零搭建(7) - request封装

    项目代码同步至码云 weiz-vue3-template 基于 axios 封装请求,支持多域名请求地址 utils 目录下新建 request 文件夹,并新建 index.ts 、 request.ts 和 status.ts 文件。 此时,eslint会报 switch 前面的空格错误,需要修改 .eslintrc.cjs 里的 indent ,修改后,错误消失。 src 目录下新建 api 文件夹,

    2024年02月04日
    浏览(44)
  • Vite4+Typescript+Vue3+Pinia 从零搭建(5) - 路由router

    项目代码同步至码云 weiz-vue3-template Vue Router 是 Vue.js 的官方路由。它与 Vue.js 核心深度集成,让用 Vue.js 构建单页应用变得轻而易举。 在 src/view 下新建 home.vue 和 login.vue ,内容如下: login.vue 里修改下对应name即可 index.ts 作为路由入口, static.ts 作为静态路由, modules 内还可以

    2024年02月05日
    浏览(46)
  • Vite4+Typescript+Vue3+Pinia 从零搭建(6) - 状态管理pina

    项目代码同步至码云 weiz-vue3-template pina 是 vue3 官方推荐的状态管理库,由 Vue 核心团队维护,旨在替代 vuex。pina 的更多介绍,可从 pina官网 查看 更简洁直接的 API,提供组合式风格的 API 支持模块热更新和服务端渲染 对TS支持更为友好 src目录下新建store文件夹,并新建index.t

    2024年02月05日
    浏览(58)
  • Vue3 Vite4 ElementPlus TS模板(含Vue-Router4+Pinia4)

    手动安装配置Vue3 ElementPlus模板比较繁琐,网上寻找一些模板不太符合自己预期,因此花点精力搭建一个符合自己需求的架子 采用最新的组件,版本如下: vite 4.3.9 vite-plugin-mock 2.9.8 vue 3.3.4 pinia 2.1.3 vue-router 4.2.2 element-plus 2.3.6 满足自己以下功能: Vite工具热启动速度快,修改后

    2024年02月08日
    浏览(39)
  • Electron-ChatGPT桌面端ChatGPT实例|electron25+vue3聊天AI模板EXE

    基于 electron25+vite4+vue3 仿制chatgpt客户端聊天模板 ElectronChatGPT 。 electron-chatgpt 使用最新桌面端技术 Electron25.x 结合 Vite4.x 全家桶技术开发跨端模仿ChatGPT智能聊天程序模板。支持 经典+分栏两种布局、暗黑+明亮主题模式,集成electron封装多窗口及通讯 功能。 编码工具:vscode 框架

    2024年02月08日
    浏览(27)
  • Vue3.2 + TypeScript + Pinia + Vite4 + Element-Plus + 微前端(qiankun) 后台管理系统模板(已开源---显示项目页面截图)

    Wocwin-Admin,是基于 Vue3.2、TypeScript、Vite、Pinia、Element-Plus、Qiankun(微前端) 开源的一套后台管理模板;同时集成了微前端 qiankun也可以当做一个子应用。项目中组件页面使用了Element-plus 二次封装 t-ui-plus 组件,目前已新增fastmock接口。 Link:https://wocwin.github.io/wocwin-admin/ 账号:

    2024年02月08日
    浏览(48)
  • 基于Electron24+Vite4+Vue3搭建桌面端应用

    一说到创建桌面应用,就不得不提及Electron和Tauri框架。这次给大家主要分享的是基于electron最新版本整合vite4.x构建vue3桌面端应用程序。 之前也有使用vite2+vue3+electronc创建桌面端项目,不过  vue-cli-plugin-electron-builder  脚手架插件构建的项目electron版本只有13.x。如今electron版本

    2024年02月06日
    浏览(51)
  • 基于uni-app+vue3跨端「h5+小程序+App」仿制chatGPT模板实例

    uni-chatgpt 一款uniapp+vite4+uview-plus多端ChatGPT模板实例。 全新首发的一款多端仿制chatgpt智能对话实战项目,基于 uniApp+Vue3+Pinia+uViewUI+MarkdownIt 等技术开发搭建项目。支持编译到 h5+小程序+APP端 ,支持markdown语法解析及代码高亮。 全屏沉浸式顶部导航条+底部tabbar 支持解析h5+小程序

    2024年02月12日
    浏览(53)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包