feat: 新增 pinia 状态管理
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
|
||||
export const useAccountStore = defineStore('account', () => {
|
||||
// 存储所有账号信息
|
||||
const accounts = ref([])
|
||||
|
||||
// 平台类型映射
|
||||
const platformTypes = {
|
||||
1: '小红书',
|
||||
2: '视频号',
|
||||
3: '抖音',
|
||||
4: '快手'
|
||||
}
|
||||
|
||||
// 设置账号列表
|
||||
const setAccounts = (accountsData) => {
|
||||
// 转换后端返回的数据格式为前端使用的格式
|
||||
accounts.value = accountsData.map(item => {
|
||||
return {
|
||||
id: item[0],
|
||||
type: item[1],
|
||||
filePath: item[2],
|
||||
name: item[3],
|
||||
status: item[4] === 1 ? '正常' : '异常',
|
||||
platform: platformTypes[item[1]] || '未知',
|
||||
avatar: '/vite.svg' // 默认使用vite.svg作为头像
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 添加账号
|
||||
const addAccount = (account) => {
|
||||
accounts.value.push(account)
|
||||
}
|
||||
|
||||
// 更新账号
|
||||
const updateAccount = (id, updatedAccount) => {
|
||||
const index = accounts.value.findIndex(acc => acc.id === id)
|
||||
if (index !== -1) {
|
||||
accounts.value[index] = { ...accounts.value[index], ...updatedAccount }
|
||||
}
|
||||
}
|
||||
|
||||
// 删除账号
|
||||
const deleteAccount = (id) => {
|
||||
accounts.value = accounts.value.filter(acc => acc.id !== id)
|
||||
}
|
||||
|
||||
// 根据平台获取账号
|
||||
const getAccountsByPlatform = (platform) => {
|
||||
return accounts.value.filter(acc => acc.platform === platform)
|
||||
}
|
||||
|
||||
return {
|
||||
accounts,
|
||||
setAccounts,
|
||||
addAccount,
|
||||
updateAccount,
|
||||
deleteAccount,
|
||||
getAccountsByPlatform
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,69 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
|
||||
export const useAppStore = defineStore('app', () => {
|
||||
// 是否是第一次进入账号管理页面
|
||||
const isFirstTimeAccountManagement = ref(true)
|
||||
|
||||
// 是否是第一次进入素材管理页面
|
||||
const isFirstTimeMaterialManagement = ref(true)
|
||||
|
||||
// 账号管理页面刷新状态
|
||||
const isAccountRefreshing = ref(false)
|
||||
|
||||
// 素材列表数据
|
||||
const materials = ref([])
|
||||
|
||||
// 设置账号管理页面已访问
|
||||
const setAccountManagementVisited = () => {
|
||||
isFirstTimeAccountManagement.value = false
|
||||
}
|
||||
|
||||
// 设置素材管理页面已访问
|
||||
const setMaterialManagementVisited = () => {
|
||||
isFirstTimeMaterialManagement.value = false
|
||||
}
|
||||
|
||||
// 重置所有访问状态(用于重新登录或刷新应用时)
|
||||
const resetVisitStatus = () => {
|
||||
isFirstTimeAccountManagement.value = true
|
||||
isFirstTimeMaterialManagement.value = true
|
||||
}
|
||||
|
||||
// 更新素材列表
|
||||
const setMaterials = (materialList) => {
|
||||
materials.value = materialList
|
||||
}
|
||||
|
||||
// 添加新素材
|
||||
const addMaterial = (material) => {
|
||||
materials.value.push(material)
|
||||
}
|
||||
|
||||
// 删除素材
|
||||
const removeMaterial = (materialId) => {
|
||||
const index = materials.value.findIndex(m => m.id === materialId)
|
||||
if (index > -1) {
|
||||
materials.value.splice(index, 1)
|
||||
}
|
||||
}
|
||||
|
||||
// 设置账号管理页面刷新状态
|
||||
const setAccountRefreshing = (status) => {
|
||||
isAccountRefreshing.value = status
|
||||
}
|
||||
|
||||
return {
|
||||
isFirstTimeAccountManagement,
|
||||
isFirstTimeMaterialManagement,
|
||||
isAccountRefreshing,
|
||||
materials,
|
||||
setAccountManagementVisited,
|
||||
setMaterialManagementVisited,
|
||||
resetVisitStatus,
|
||||
setMaterials,
|
||||
addMaterial,
|
||||
removeMaterial,
|
||||
setAccountRefreshing
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,9 @@
|
||||
import { createPinia } from 'pinia'
|
||||
import { useUserStore } from './user'
|
||||
import { useAccountStore } from './account'
|
||||
import { useAppStore } from './app'
|
||||
|
||||
const pinia = createPinia()
|
||||
|
||||
export default pinia
|
||||
export { useUserStore, useAccountStore, useAppStore }
|
||||
@@ -0,0 +1,31 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
|
||||
export const useUserStore = defineStore('user', () => {
|
||||
const userInfo = ref({
|
||||
name: '',
|
||||
email: ''
|
||||
})
|
||||
|
||||
const isLoggedIn = ref(false)
|
||||
|
||||
const setUserInfo = (info) => {
|
||||
userInfo.value = info
|
||||
isLoggedIn.value = true
|
||||
}
|
||||
|
||||
const logout = () => {
|
||||
userInfo.value = {
|
||||
name: '',
|
||||
email: ''
|
||||
}
|
||||
isLoggedIn.value = false
|
||||
}
|
||||
|
||||
return {
|
||||
userInfo,
|
||||
isLoggedIn,
|
||||
setUserInfo,
|
||||
logout
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user