伴随着openAI发布了IOS版ChatGPT应用,最近也使用vue3+vant开发了一个mobile版chatgpt模板。
mobileGPT-Vue3 基于vue3移动端仿制ChatGPT智能聊天模板
mobilegpt for vue3 基于
vite4+vue3+pinia2+vue-router+vant
等技术构建手机端chatgpt聊天实例。支持light+dark两种主题,搭载vue3组件库Vant。
mobilegpt-vue3 基于vite4.x构建,采用vue3 setup语法编码开发项目。
技术栈
- 开发工具:Vscode
- 框架技术:vite4 + vue3 + vue-router + pinia2
- 组件库:Vant^4.3.1 + ve-plus^0.3.2
- 代码高亮:highlight.js^11.7.0
- markdown解析:vue3-markdown-it
- 数据存储:pinia-plugin-persistedstate^3.1.0
- 样式处理:sass^1.62.1
项目结构目录
App.vue模板
<script setup>
import { Loaded } from '@/plugins/loaded'
// 初始化
Loaded()
</script>
<template>
<div class="vgpt__container flexbox flex-col">
<router-view />
</div>
</template>
<style scoped></style>
main.js配置
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')
引入插件
/**
* 公共插件配置
* @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
Pinia状态管理
vue3推荐使用pinia进行状态管理,其更加轻量,更易于扩展。
/**
* pinia状态管理
* @author YXY
*/
import { createPinia } from 'pinia'
// 引入pinia本地持久化存储
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)
export default pinia
modules/chat.js
/**
* 会话状态管理
* @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
})
pinia语法和vuex有些大同小异,不过使用起来更加方便。
mobile-gpt会话框
<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>
会话框支持多行文本,自定义后缀插槽等功能。
<script setup>
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 showPopover = ref(false)
const uploadImgRef = ref()
const editorRef = ref()
const editorText = ref(props.value)
watch(() => props.value, () => {
editorText.value = props.value
})
// 发送会话
const handleSubmit = () => {
// editorRef.value.focus()
if(!editorText.value) return
let data = {
type: 'text',
role: 'ME',
key: guid(),
content: editorText.value
}
chatState.addSession(data)
// 清空
editorText.value = ''
}
// 选择图片
const handleUploadImage = () => {
let file = uploadImgRef.value.files[0]
if(!file) return
let size = Math.floor(file.size / 1024)
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: 'ME',
key: guid(),
content: img
}
chatState.addSession(data)
}
}
// ...
</script>
Oka,以上就是vue3+vant开发移动版chatgpt模板的一些分享,希望能喜欢~~