初始化

This commit is contained in:
xiaoyi
2024-01-27 19:53:17 +08:00
commit 07dbe71c31
840 changed files with 119152 additions and 0 deletions

10
chat/src/store/index.ts Normal file
View File

@@ -0,0 +1,10 @@
import type { App } from 'vue'
import { createPinia } from 'pinia'
export const store = createPinia()
export function setupStore(app: App) {
app.use(store)
}
export * from './modules'

View File

@@ -0,0 +1,46 @@
import { ss } from '@/utils/storage'
function detectEnvironment() {
if (typeof process !== 'undefined' && process?.type === 'renderer')
return 'electron'
else if (typeof wx !== 'undefined')
return 'wechat'
else if (typeof window !== 'undefined' && window.matchMedia('(display-mode: standalone)').matches)
return 'webApp'
else if (/(Android|webOS|iPhone|iPad|iPod|BlackBerry|Windows Phone)/i.test(navigator.userAgent))
return 'mobile'
else
return 'webBrowser'
}
const LOCAL_NAME = 'appSetting'
export type Theme = 'light' | 'dark' | 'auto'
export type Language = 'zh-CN' | 'zh-TW' | 'en-US'
export type Env = 'electron' | 'wecaht' | 'web' | 'mobile'
export interface AppState {
siderCollapsed: boolean
theme: Theme
language: Language
env: Env
}
export function defaultSetting(): AppState {
return { siderCollapsed: false, theme: 'auto', language: 'zh-CN', env: detectEnvironment() }
}
export function getLocalSetting(): AppState {
const localSetting: AppState | undefined = ss.get(LOCAL_NAME)
return { ...defaultSetting(), ...localSetting }
}
export function setLocalSetting(setting: AppState): void {
ss.set(LOCAL_NAME, setting)
}

View File

@@ -0,0 +1,58 @@
import { defineStore } from 'pinia'
import type { AppState, Language, Theme } from './helper'
import { getLocalSetting, setLocalSetting } from './helper'
import { store } from '@/store'
export const useAppStore = defineStore('app-store', {
state: (): AppState => getLocalSetting(),
actions: {
setSiderCollapsed(collapsed: boolean) {
this.siderCollapsed = collapsed
this.recordState()
},
setTheme(theme: Theme) {
localStorage.theme = theme
this.theme = theme
window.theme = theme
this.recordState()
},
setLanguage(language: Language) {
if (this.language !== language) {
this.language = language
this.recordState()
}
},
recordState() {
setLocalSetting(this.$state)
},
setEnv() {
const isWeChat = /micromessenger/i.test(navigator.userAgent)
const isElectron = navigator.userAgent.includes('Electron')
const isMobile = /(iPhone|iPad|iPod|Android|webOS|BlackBerry|Windows Phone)/i.test(navigator.userAgent)
const isWeb = !isWeChat && !isElectron
if (isWeChat)
this.env = 'wechat'
else if (isElectron)
this.env = 'electron'
else if (isMobile)
this.env = 'mobile'
else if (isWeb)
this.env = 'web'
},
},
})
export function useAppStoreWithOut() {
return useAppStore(store)
}

View File

@@ -0,0 +1,20 @@
export interface MineApp {
userId: number
catId: number
appId: number
public: boolean
status: number
demoData: string
order: number
appDes: string
preset: string
appRole: string
coverImg: string
appName: string
loading?: boolean
}
export interface AppStoreState {
catId: number
mineApps: MineApp[]
}

View File

@@ -0,0 +1,26 @@
import { defineStore } from 'pinia'
import type { AppStoreState } from './helper'
import { fetchQueryMineAppsAPI } from '@/api/appStore'
import { store } from '@/store'
export const useAppCatStore = defineStore('app-cat-store', {
state: (): AppStoreState => ({
catId: 0,
mineApps: [],
}),
actions: {
setCatId(catId: number) {
this.catId = catId
},
async queryMineApps() {
const res = await fetchQueryMineAppsAPI()
this.mineApps = res?.data?.rows || []
},
},
})
export function useAppCatStoreWithOut() {
return useAppStore(store)
}

View File

@@ -0,0 +1,110 @@
import { ss } from '@/utils/storage'
const LOCAL_NAME = 'SECRET_TOKEN'
export function getToken() {
return ss.get(LOCAL_NAME)
}
export function setToken(token: string) {
return ss.set(LOCAL_NAME, token)
}
export function removeToken() {
return ss.remove(LOCAL_NAME)
}
export interface UserBalance {
isMember: boolean
model3Count: number
model4Count: number
drawMjCount: number
memberModel3Count: number
memberModel4Count: number
memberDrawMjCount: number
useModel3Count: number
useModel4Count: number
useModel3Token: number
useModel4Token: number
useDrawMjToken: number
sumModel3Count: number
sumModel4Count: number
sumDrawMjCount: number
expirationTime: Date
}
export interface GlobalConfig {
siteName: string
qqNumber: string
vxNumber: string
baiduCode: string
buyCramiAddress: string
noticeInfo: string
inviteSendStatus: string
registerSendStatus: string
registerSendModel3Count: string
registerSendModel4Count: string
registerSendDrawMjCount: string
inviteGiveSendModel3Count: string
inviteGiveSendModel4Count: string
inviteGiveSendDrawMjCount: string
invitedGuestSendModel3Count: string
invitedGuestSendModel4Count: string
invitedGuestSendDrawMjCount: string
clientHomePath: string
clientLogoPath: string
clientFavoIconPath: string
isUseWxLogin: boolean
robotAvatar: string
siteRobotName: string
mindDefaultData: string
payEpayStatus: string
payHupiStatus: string
payWechatStatus: string
payEpayChannel: string
payMpayChannel: string
payEpayApiPayUrl: string
payMpayStatus: string
isAutoOpenNotice: string
isShowAppCatIcon: string
salesBaseRatio: string
salesSeniorRatio: string
salesAllowDrawMoney: string
companyName: string
filingNumber: string
emailRegisterStatus: string
emailLoginStatus: string
phoneLoginStatus: string
phoneRegisterStatus: string
wechatRegisterStatus: string
wechatSilentLoginStatus: string
signInStatus: string
signInModel3Count: string
signInModel4Count: string
signInMjDrawToken: string
appMenuHeaderTips: string
appMenuHeaderBgUrl: string
mjHideNotBlock: string
mjUseBaiduFy: string
mjHideWorkIn: string
isVerifyEmail: string
}
export interface AuthState {
token: string | undefined
loginDialog: boolean
globalConfigLoading: boolean
loadInit: boolean
userInfo: {
username: string
email: string
role: string
id: number
avatar?: string
sign?: string
inviteCode: string
isBindWx: boolean
consecutiveDays: number
}
userBalance: UserBalance
globalConfig: GlobalConfig
}

View File

@@ -0,0 +1,101 @@
import { defineStore } from 'pinia'
import { useChatStore } from '../chat'
import type { AuthState, GlobalConfig, UserBalance } from './helper'
import { getToken, removeToken, setToken } from './helper'
import { store } from '@/store'
import { fetchGetInfo } from '@/api'
import { fetchQueryConfigAPI } from '@/api/config'
import { fetchGetBalanceQueryAPI } from '@/api/balance'
import type { ResData } from '@/api/types'
export const useAuthStore = defineStore('auth-store', {
state: (): AuthState => ({
token: getToken(),
loginDialog: false,
globalConfigLoading: true,
userInfo: {},
userBalance: {},
globalConfig: {} as GlobalConfig,
loadInit: false,
}),
getters: {
isLogin: (state: AuthState) => !!state.token,
},
actions: {
async getUserInfo(): Promise<T> {
try {
if (!this.loadInit)
await this.getglobalConfig()
const res = await fetchGetInfo()
if (!res)
return Promise.resolve(res)
const { data } = res
const { userInfo, userBalance } = data
this.userInfo = { ...userInfo }
this.userBalance = { ...userBalance }
return Promise.resolve(data)
}
catch (error) {
return Promise.reject(error)
}
},
updateUserBanance(userBalance: UserBalance) {
this.userBalance = userBalance
},
async getUserBalance() {
const res: ResData = await fetchGetBalanceQueryAPI()
const { success, data } = res
if (success)
this.userBalance = data
},
async getglobalConfig(domain = '') {
const res = await fetchQueryConfigAPI({ domain })
this.globalConfig = res.data
this.globalConfigLoading = false
this.loadInit = true
},
setToken(token: string) {
this.token = token
setToken(token)
},
removeToken() {
this.token = undefined
removeToken()
},
setLoginDialog(bool: boolean) {
this.loginDialog = bool
},
logOut() {
this.token = undefined
removeToken()
this.userInfo = {}
this.userBalance = {}
window.$message.success('登出账户成功!')
const chatStore = useChatStore()
chatStore.clearChat()
},
updatePasswordSuccess() {
this.token = undefined
removeToken()
this.userInfo = {}
this.userBalance = {}
this.loginDialog = true
},
},
})
export function useAuthStoreWithout() {
return useAuthStore(store)
}

View File

@@ -0,0 +1,41 @@
import { ss } from '@/utils/storage'
const LOCAL_NAME = 'chatStorage'
export function defaultState(): Chat.ChatState {
return {
active: 0,
usingContext: true,
usingNetwork: false,
groupList: [],
chatList: [],
groupKeyWord: '',
baseConfig: null
}
}
export function getLocalState(): Chat.ChatState {
const localState = ss.get(LOCAL_NAME)
return { ...defaultState(), ...localState }
}
export function setLocalState({ active }: Chat.ChatState) {
ss.set(LOCAL_NAME, { ...ss.get(LOCAL_NAME), active })
}
export function formatChatPre(data: any): any{
return data.map( (item: any) => {
const { name, childList, id } = item
return {
label: name,
value: id,
children: childList.map( (t: any) => {
return {
label: t.title,
value: t.prompt
}
})
}
})
}

View File

@@ -0,0 +1,215 @@
import { defineStore } from 'pinia'
import { formatChatPre, getLocalState, setLocalState } from './helper'
import { fetchCreateGroupAPI, fetchDelAllGroupAPI, fetchDelGroupAPI, fetchQueryGroupAPI, fetchUpdateGroupAPI } from '@/api/group'
import { fetchDelChatLogAPI, fetchDelChatLogByGroupIdAPI, fetchQueryChatLogListAPI } from '@/api/chatLog'
import { fetchModelBaseConfigAPI } from '@/api/models'
import { fetchGetChatPreList } from '@/api/index'
export const useChatStore = defineStore('chat-store', {
state: (): Chat.ChatState => getLocalState(),
getters: {
/* 当前选用模型的配置 */
activeConfig: (state) => {
const uuid = state.active
if (!uuid)
return {}
const config = state.groupList.find(item => item.uuid === uuid)?.config
return config ? JSON.parse(config) : state.baseConfig
},
activeGroupAppId: (state) => {
const uuid = state.active
if (!uuid)
return null
return state.groupList.find(item => item.uuid === uuid)?.appId
},
/* 当前选用模型的扣费类型 */
activeModelKeyDeductType(state) {
return this.activeConfig?.modelInfo?.deductType
},
/* 当前选用模型的模型类型 */
activeModelKeyType(state) {
return this.activeConfig?.modelInfo?.keyType
},
/* 当前选用模型的调用价格 */
activeModelKeyPrice(state) {
return this.activeConfig?.modelInfo?.deduct
},
},
actions: {
/* 对话组过滤 */
setGroupKeyWord(keyWord: string) {
this.groupKeyWord = keyWord
},
/* 计算拿到当前选择的对话组信息 */
getChatByGroupInfo() {
if (this.active)
return this.groupList.find(item => item.uuid === this.active) || {}
},
/* */
getConfigFromUuid(uuid: any) {
return this.groupList.find(item => item.uuid === uuid)?.config
},
/* 新增新的对话组 */
async addNewChatGroup(appId = 0) {
const res: any = await fetchCreateGroupAPI({ appId })
const { id: uuid } = res.data
await this.setActiveGroup(uuid)
this.recordState()
},
/* 查询基础模型配置 兼容老的chatgroup */
async getBaseModelConfig() {
const res = await fetchModelBaseConfigAPI()
this.baseConfig = res?.data
},
/* 查询我的对话组 */
async queryMyGroup() {
const res: any = await fetchQueryGroupAPI()
this.groupList = [...res.data.map((item: any) => {
const { id: uuid, title, isSticky, createdAt, updatedAt, appId, config, appLogo } = item
return { uuid, title, isEdit: false, appId, config, isSticky, appLogo, createdAt, updatedAt: new Date(updatedAt).getTime() }
})]
const isHasActive = this.groupList.some(item => Number(item.uuid) === Number(this.active))
if (!this.active || !isHasActive)
this.groupList.length && this.setActiveGroup(this.groupList[0].uuid)
},
/* 修改对话组信息 */
async updateGroupInfo(params: { groupId: number; title?: string; isSticky?: boolean }) {
await fetchUpdateGroupAPI(params)
},
/* 变更对话组 */
async setActiveGroup(uuid: number) {
this.active = uuid
if (this.active)
await this.queryActiveChatLogList()
else
this.chatList = []
this.groupList.forEach(item => (item.isEdit = false))
this.recordState()
},
/* 删除对话组 */
async deleteGroup(params: Chat.History) {
const curIndex = this.groupList.findIndex(item => item.uuid === params.uuid)
const { uuid: groupId } = params
await fetchDelGroupAPI({ groupId })
await this.queryMyGroup()
if (this.groupList.length === 0)
await this.setActiveGroup(0)
if (curIndex > 0 && curIndex < this.groupList.length)
await this.setActiveGroup(this.groupList[curIndex].uuid)
if (curIndex === 0 && this.groupList.length > 0)
await this.setActiveGroup(this.groupList[0].uuid)
if (curIndex > this.groupList.length || (curIndex === 0 && this.groupList.length === 0))
await this.setActiveGroup(0)
if (curIndex > 0 && curIndex === this.groupList.length)
await this.setActiveGroup(this.groupList[curIndex - 1].uuid)
this.recordState()
},
/* 删除全部非置顶对话组 */
async delAllGroup() {
if (!this.active || !this.groupList.length)
return
await fetchDelAllGroupAPI()
await this.queryMyGroup()
if (this.groupList.length === 0)
await this.setActiveGroup(0)
else
await this.setActiveGroup(this.groupList[0].uuid)
},
/* 查询当前对话组的聊天记录 */
async queryActiveChatLogList() {
if (!this.active || Number(this.active) === 0)
return
const res: any = await fetchQueryChatLogListAPI({ groupId: this.active })
this.chatList = res.data
},
/* 添加一条虚拟的对话记录 */
addGroupChat(data) {
this.chatList = [...this.chatList, data]
},
/* 动态修改对话记录 */
updateGroupChat(index: number, data: Chat.Chat) {
this.chatList[index] = { ...this.chatList[index], ...data }
},
/* 修改其中部分内容 */
updateGroupChatSome(index: number, data: Partial<Chat.Chat>) {
this.chatList[index] = { ...this.chatList[index], ...data }
},
/* 删除一条对话记录 */
async deleteChatById(chatId: number | undefined) {
console.log(chatId)
if (!chatId)
return
await fetchDelChatLogAPI({ id: chatId })
await this.queryActiveChatLogList()
},
/* 查询快问预设 */
async queryChatPre() {
const res: any = await fetchGetChatPreList()
if (!res.data)
return
this.chatPreList = formatChatPre(res.data)
},
/* 设置使用上下文 */
setUsingContext(context: boolean) {
this.usingContext = context
this.recordState()
},
/* 设置使用联网 */
setUsingNetwork(context: boolean) {
this.usingNetwork = context
this.recordState()
},
/* 删除当前对话组的全部内容 */
async clearChatByGroupId() {
if (!this.active)
return
await fetchDelChatLogByGroupIdAPI({ groupId: this.active })
await this.queryActiveChatLogList()
},
recordState() {
setLocalState(this.$state)
},
clearChat() {
this.chatList = []
this.groupList = []
this.active = 0
this.recordState()
},
},
})

View File

@@ -0,0 +1,55 @@
import { ss } from '@/utils/storage'
const LOCAL_NAME = 'userStorage'
export interface UserInfo {
avatar: string
name: string
}
export interface OrderInfo {
pkgInfo: {
id: number
des: string
name: string
price: string
model3Count: number
model4Count: number
drawMjCount: number
coverImg: string
days: number
}
}
export interface GlobalState {
payDialog: boolean
goodsDialog: boolean
fingerprint: number
noticeDialog: boolean
bindWxDialog: boolean
signInDialog: boolean
modelDialog: boolean
isChatIn: boolean
orderInfo: OrderInfo
model: number
iframeUrl: string
clipboardText: string
}
export function defaultSetting(): UserState {
return {
userInfo: {
avatar: 'https://public-1300678944.cos.ap-shanghai.myqcloud.com/blog/1681310872890image.png',
name: '未登录',
},
}
}
export function getLocalState(): UserState {
const localSetting: UserState | undefined = ss.get(LOCAL_NAME)
return { ...defaultSetting(), ...localSetting }
}
export function setLocalState(setting: UserState): void {
ss.set(LOCAL_NAME, setting)
}

View File

@@ -0,0 +1,84 @@
import { defineStore } from 'pinia'
import type { GlobalState, OrderInfo } from './helper'
import { store } from '@/store'
import { ss } from '@/utils/storage'
export const useGlobalStore = defineStore('global-store', {
state: (): GlobalState => ({
payDialog: false,
goodsDialog: false,
noticeDialog: false,
bindWxDialog: false,
signInDialog: false,
modelDialog: false,
isChatIn: false,
fingerprint: 0,
model: ss.get('model') || 3,
orderInfo: {} as OrderInfo,
iframeUrl: '',
clipboardText: ''
}),
actions: {
updateClipboardText(text: string){
this.clipboardText = text
},
updateFingerprint(str: number ) {
let id = str
/* 超过mysql最大值进行截取 */
if(id > 2147483647){
id = Number(id.toString().slice(-9))
id = Number(String(Number(id)))
}
ss.set('fingerprint',id)
this.fingerprint = id
},
updateIframeUrl(iframeUrl: string) {
this.iframeUrl = iframeUrl
},
updatePayDialog(payDialog: boolean) {
this.payDialog = payDialog
},
//
updateModelDialog(modelDialog: boolean) {
this.modelDialog = modelDialog
},
updateIsChatIn(isChatIn: boolean) {
this.isChatIn = isChatIn
},
updateGoodsDialog(goodsDialog: boolean) {
this.goodsDialog = goodsDialog
},
updateBindwxDialog(bindWxDialog: boolean) {
this.bindWxDialog = bindWxDialog
},
updateSignInDialog(signInDialog: boolean) {
this.signInDialog = signInDialog
},
updateNoticeDialog(noticeDialog: boolean) {
this.noticeDialog = noticeDialog
},
updateOrderInfo(orderInfo: OrderInfo | {}) {
this.orderInfo = orderInfo
},
updateModel(model: number) {
ss.set('model', model)
this.model = model
},
},
})
export function useGlobalStoreWithOut() {
return useGlobalStore(store)
}

View File

@@ -0,0 +1,8 @@
export * from './app'
export * from './chat'
// export * from './user'
export * from './prompt'
export * from './settings'
export * from './auth'
export * from './global'
export * from './appStore'

View File

@@ -0,0 +1,19 @@
import { ss } from '@/utils/storage'
const LOCAL_NAME = 'promptStore'
export type PromptList = []
export interface PromptStore {
promptList: PromptList
}
export function getLocalPromptList(): PromptStore {
const storage = ss.get(LOCAL_NAME)
const promptStore: PromptStore | undefined = storage
return promptStore ?? { promptList: [] }
}
export function setLocalPromptList(promptStore: PromptStore): void {
ss.set(LOCAL_NAME, promptStore)
}

View File

@@ -0,0 +1,17 @@
import { defineStore } from 'pinia'
import type { PromptStore } from './helper'
import { getLocalPromptList, setLocalPromptList } from './helper'
export const usePromptStore = defineStore('prompt-store', {
state: (): PromptStore => getLocalPromptList(),
actions: {
updatePromptList(promptList: []) {
this.$patch({ promptList })
setLocalPromptList({ promptList })
},
getPromptList() {
return this.$state
},
},
})

View File

@@ -0,0 +1,26 @@
import { ss } from '@/utils/storage'
const LOCAL_NAME = 'settingsStorage'
export interface SettingsState {
systemMessage: string
}
export function defaultSetting(): SettingsState {
return {
systemMessage: '',
}
}
export function getLocalState(): SettingsState {
const localSetting: SettingsState | undefined = ss.get(LOCAL_NAME)
return { ...defaultSetting(), ...localSetting }
}
export function setLocalState(setting: SettingsState): void {
ss.set(LOCAL_NAME, setting)
}
export function removeLocalState() {
ss.remove(LOCAL_NAME)
}

View File

@@ -0,0 +1,22 @@
import { defineStore } from 'pinia'
import type { SettingsState } from './helper'
import { defaultSetting, getLocalState, removeLocalState, setLocalState } from './helper'
export const useSettingStore = defineStore('setting-store', {
state: (): SettingsState => getLocalState(),
actions: {
updateSetting(settings: Partial<SettingsState>) {
this.$state = { ...this.$state, ...settings }
this.recordState()
},
resetSetting() {
this.$state = defaultSetting()
removeLocalState()
},
recordState() {
setLocalState(this.$state)
},
},
})

View File

@@ -0,0 +1,30 @@
import { ss } from '@/utils/storage'
const LOCAL_NAME = 'userStorage'
export interface UserInfo {
avatar: string
name: string
}
export interface UserState {
userInfo: UserInfo
}
export function defaultSetting(): UserState {
return {
userInfo: {
avatar: 'https://public-1300678944.cos.ap-shanghai.myqcloud.com/blog/1681310872890image.png',
name: 'Nine Ai',
},
}
}
export function getLocalState(): UserState {
const localSetting: UserState | undefined = ss.get(LOCAL_NAME)
return { ...defaultSetting(), ...localSetting }
}
export function setLocalState(setting: UserState): void {
ss.set(LOCAL_NAME, setting)
}

View File

@@ -0,0 +1,18 @@
import { defineStore } from 'pinia'
import type { UserInfo, UserState } from './helper'
import { getLocalState, setLocalState } from './helper'
export const useUserStore = defineStore('user-store', {
state: (): UserState => getLocalState(),
actions: {
updateUserInfo(userInfo: Partial<UserInfo>) {
this.userInfo = { ...this.userInfo, ...userInfo }
this.recordState()
},
recordState() {
setLocalState(this.$state)
},
},
})