vite4-mobileGPT 基于vue3+vant-ui+pinia2
移动端chatgpt聊天实例。
mobile-gpt for vue3.x 使用最新vue3 setup语法编码开发,支持亮色/暗黑模式。
import { createApp } from 'vue' import './style.scss' import App from './App.vue' import Router from './router' import Store from './store' import Plugins from './plugins' const app = createApp(App) app.use(Router) app.use(Store) app.use(Plugins) app.mount('#app')
/** * 状态管理 Pinia * @author YXY */ import { createPinia } from 'pinia' // 引入pinia本地持久化存储 import piniaPluginPersistedstate from 'pinia-plugin-persistedstate' const pinia = createPinia() pinia.use(piniaPluginPersistedstate) export default pinia
/** * 公共插件配置 * @author YXY */ import '@assets/js/fontSize' // 引入Vant4.x组件库 import Vant from 'vant' import 'vant/lib/index.css' // 引入ve-plus组件库 import VEPlus from 've-plus' import 've-plus/dist/ve-plus.css' // 引入vue3弹框组件 import V3Popup from '@/components/v3popup' import Navbar from '@/components/Navbar.vue' const Plugins = (app) => { app.use(Vant) app.use(VEPlus) app.use(V3Popup) // 注册组件 app.component('Navbar', Navbar) } export default Plugins
项目中聊天会话框使用Input组件实现输入功能,支持多行文本、自定义后缀插槽功能。
<template> <div class="vgpt__editor"> <div class="vgpt__editor-inner flexbox"> <Input class="flex1" ref="editorRef" v-model="editorText" type="textarea" :autosize="{maxRows: 6}" clearable placeholder="Prompt..." @keydown="handleKeydown" @clear="handleClear" style="margin: 0 5px;" > <template #suffix> <Button class="btn" type="link" @click.stop> <Icon name="ve-icon-image" size="16" cursor /> <input ref="uploadImgRef" type="file" title="" accept="image/*" @change="handleUploadImage" /> </Button> <van-popover v-model:show="showPopover" placement="top"> <template #reference> <Button class="btn" type="link" icon="ve-icon-audio"></Button> </template> <div class="flexbox flex-alignc flex-col" style="padding: 15px 0; min-width: 120px;"> <Icon name="ve-icon-yuyin" size="40" color="#0fa27e" /> <p class="fs-12 mb-15 c-999">网络不给力</p> <van-button size="mini"><i></i>开始讲话</van-button> </div> </van-popover> </template> </Input> <Button type="primary" icon="ve-icon-arrowup" circle :disabled="!editorText" @click="handleSubmit"></Button> </div> </div> </template>
/** * 聊天会话状态管理 * @author YXY * @contact Q:282310962 */ import { defineStore } from 'pinia' import { guid, isEmpty } from '@/utils' export const chatStore = defineStore('chat', { state: () => ({ sessionId: '', session: [] }), 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) { this.session.map(item => { if(item.sessionId == this.sessionId) { if(item.data && !isEmpty(item.data)) { item.data.map((it, key) => { if(it.key == ssid) { item.data.splice(key, 1) } }) } } }) }, // 清空会话 clearSession() { this.session = [] this.sessionId = '' } }, // 本地持久化存储(默认存储localStorage) persist: true })
OK,基于vite4+vant开发移动端仿chatgpt聊天实例就分享到这里,希望对大家有所帮助~~