初始化
This commit is contained in:
468
src/store/index.js
Normal file
468
src/store/index.js
Normal file
@@ -0,0 +1,468 @@
|
||||
import { reactive } from 'vue'
|
||||
import api from '../api'
|
||||
|
||||
// 用户状态
|
||||
export const userState = reactive({
|
||||
isLoggedIn: false,
|
||||
token: '',
|
||||
refreshToken: '',
|
||||
userInfo: {
|
||||
userId: '',
|
||||
username: '',
|
||||
email: '',
|
||||
avatar: ''
|
||||
},
|
||||
subscription: {
|
||||
plan: 'FREE',
|
||||
remainingMails: 0,
|
||||
maxAttachmentSize: 0,
|
||||
features: {
|
||||
advancedTriggers: false,
|
||||
customCapsules: false,
|
||||
aiAssistant: false
|
||||
},
|
||||
expireDate: null
|
||||
}
|
||||
})
|
||||
|
||||
// 邮件状态
|
||||
export const mailState = reactive({
|
||||
inboxList: [],
|
||||
sentList: [],
|
||||
draftList: [],
|
||||
currentMail: null,
|
||||
loading: false,
|
||||
pagination: {
|
||||
page: 1,
|
||||
size: 10,
|
||||
total: 0
|
||||
}
|
||||
})
|
||||
|
||||
// 胶囊状态
|
||||
export const capsuleState = reactive({
|
||||
capsules: [],
|
||||
scene: 'SPACE',
|
||||
background: '',
|
||||
loading: false
|
||||
})
|
||||
|
||||
// 时间线状态
|
||||
export const timelineState = reactive({
|
||||
timeline: [],
|
||||
loading: false,
|
||||
filter: {
|
||||
startDate: null,
|
||||
endDate: null,
|
||||
type: 'ALL'
|
||||
}
|
||||
})
|
||||
|
||||
// 统计数据状态
|
||||
export const statisticsState = reactive({
|
||||
totalSent: 0,
|
||||
totalReceived: 0,
|
||||
timeTravelDuration: 0,
|
||||
mostFrequentRecipient: '',
|
||||
mostCommonYear: new Date().getFullYear(),
|
||||
keywordCloud: [],
|
||||
monthlyStats: [],
|
||||
loading: false
|
||||
})
|
||||
|
||||
// 用户相关操作
|
||||
export const userActions = {
|
||||
// 登录
|
||||
async login(credentials) {
|
||||
const res = await api.auth.login(credentials)
|
||||
const { token, refreshToken, ...userInfo } = res.data
|
||||
|
||||
// 保存到本地存储
|
||||
localStorage.setItem('token', token)
|
||||
localStorage.setItem('refreshToken', refreshToken)
|
||||
localStorage.setItem('userInfo', JSON.stringify(userInfo))
|
||||
|
||||
// 更新状态
|
||||
userState.isLoggedIn = true
|
||||
userState.token = token
|
||||
userState.refreshToken = refreshToken
|
||||
userState.userInfo = userInfo
|
||||
|
||||
// 获取用户订阅信息
|
||||
await this.getSubscription()
|
||||
|
||||
return res
|
||||
},
|
||||
|
||||
// 注册
|
||||
async register(userData) {
|
||||
const res = await api.auth.register(userData)
|
||||
const { token, refreshToken, ...userInfo } = res.data
|
||||
|
||||
// 保存到本地存储
|
||||
localStorage.setItem('token', token)
|
||||
localStorage.setItem('refreshToken', refreshToken)
|
||||
localStorage.setItem('userInfo', JSON.stringify(userInfo))
|
||||
|
||||
// 更新状态
|
||||
userState.isLoggedIn = true
|
||||
userState.token = token
|
||||
userState.refreshToken = refreshToken
|
||||
userState.userInfo = userInfo
|
||||
|
||||
// 获取用户订阅信息
|
||||
await this.getSubscription()
|
||||
|
||||
return res
|
||||
},
|
||||
|
||||
// 退出登录
|
||||
async logout() {
|
||||
await api.auth.logout().catch(error => {
|
||||
console.error('退出登录请求失败:', error)
|
||||
})
|
||||
|
||||
// 清除本地存储
|
||||
localStorage.removeItem('token')
|
||||
localStorage.removeItem('refreshToken')
|
||||
localStorage.removeItem('userInfo')
|
||||
|
||||
// 重置状态
|
||||
userState.isLoggedIn = false
|
||||
userState.token = ''
|
||||
userState.refreshToken = ''
|
||||
userState.userInfo = {
|
||||
userId: '',
|
||||
username: '',
|
||||
email: '',
|
||||
avatar: ''
|
||||
}
|
||||
},
|
||||
|
||||
// 刷新token
|
||||
async refreshToken() {
|
||||
try {
|
||||
const refreshToken = userState.refreshToken || localStorage.getItem('refreshToken')
|
||||
if (!refreshToken) {
|
||||
throw new Error('没有刷新令牌')
|
||||
}
|
||||
|
||||
const res = await api.auth.refreshToken(refreshToken)
|
||||
const { token: newToken, refreshToken: newRefreshToken } = res.data
|
||||
|
||||
// 更新本地存储
|
||||
localStorage.setItem('token', newToken)
|
||||
localStorage.setItem('refreshToken', newRefreshToken)
|
||||
|
||||
// 更新状态
|
||||
userState.token = newToken
|
||||
userState.refreshToken = newRefreshToken
|
||||
|
||||
return res
|
||||
} catch (error) {
|
||||
// 刷新失败,退出登录
|
||||
await this.logout()
|
||||
throw error
|
||||
}
|
||||
},
|
||||
|
||||
// 获取用户订阅信息
|
||||
async getSubscription() {
|
||||
try {
|
||||
const res = await api.user.getSubscription()
|
||||
userState.subscription = res.data
|
||||
return res
|
||||
} catch (error) {
|
||||
console.error('获取订阅信息失败:', error)
|
||||
throw error
|
||||
}
|
||||
},
|
||||
|
||||
// 获取用户信息
|
||||
async fetchUserInfo() {
|
||||
if (!userState.token) return;
|
||||
|
||||
const response = await api.user.getUserInfo();
|
||||
if (response.data.code === 200) {
|
||||
userState.userInfo = response.data.data;
|
||||
localStorage.setItem('userInfo', JSON.stringify(response.data.data));
|
||||
}
|
||||
},
|
||||
|
||||
// 初始化用户状态(从本地存储恢复)
|
||||
initUserState() {
|
||||
const token = localStorage.getItem('token')
|
||||
const refreshToken = localStorage.getItem('refreshToken')
|
||||
const userInfoStr = localStorage.getItem('userInfo')
|
||||
|
||||
if (token && userInfoStr) {
|
||||
try {
|
||||
const userInfo = JSON.parse(userInfoStr)
|
||||
userState.isLoggedIn = true
|
||||
userState.token = token
|
||||
userState.refreshToken = refreshToken || ''
|
||||
userState.userInfo = userInfo
|
||||
|
||||
// 获取订阅信息
|
||||
this.getSubscription()
|
||||
} catch (error) {
|
||||
console.error('解析用户信息失败:', error)
|
||||
// 清除无效数据
|
||||
localStorage.removeItem('token')
|
||||
localStorage.removeItem('refreshToken')
|
||||
localStorage.removeItem('userInfo')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 邮件相关操作
|
||||
export const mailActions = {
|
||||
// 获取邮件列表
|
||||
async getMails(type = 'INBOX', page = 1, size = 10, status = '') {
|
||||
try {
|
||||
mailState.loading = true
|
||||
const params = { type, page, size }
|
||||
if (status) params.status = status
|
||||
|
||||
const res = await api.mail.getMails(params)
|
||||
|
||||
// 根据类型更新不同的列表
|
||||
if (type === 'INBOX') {
|
||||
mailState.inboxList = res.data.list
|
||||
} else if (type === 'SENT') {
|
||||
mailState.sentList = res.data.list
|
||||
} else if (type === 'DRAFT') {
|
||||
mailState.draftList = res.data.list
|
||||
}
|
||||
|
||||
// 更新分页信息
|
||||
mailState.pagination = {
|
||||
page: res.data.page,
|
||||
size: res.data.size,
|
||||
total: res.data.total
|
||||
}
|
||||
|
||||
return res
|
||||
} catch (error) {
|
||||
console.error('获取邮件列表失败:', error)
|
||||
throw error
|
||||
} finally {
|
||||
mailState.loading = false
|
||||
}
|
||||
},
|
||||
|
||||
// 获取邮件详情
|
||||
async getMailDetail(mailId) {
|
||||
try {
|
||||
mailState.loading = true
|
||||
const res = await api.mail.getMailDetail(mailId)
|
||||
mailState.currentMail = res.data
|
||||
return res
|
||||
} catch (error) {
|
||||
console.error('获取邮件详情失败:', error)
|
||||
throw error
|
||||
} finally {
|
||||
mailState.loading = false
|
||||
}
|
||||
},
|
||||
|
||||
// 创建邮件
|
||||
async createMail(mailData) {
|
||||
try {
|
||||
const res = await api.mail.createMail(mailData)
|
||||
|
||||
// 如果是草稿,添加到草稿列表
|
||||
if (mailData.status === 'DRAFT') {
|
||||
await this.getMails('DRAFT')
|
||||
}
|
||||
|
||||
return res
|
||||
} catch (error) {
|
||||
console.error('创建邮件失败:', error)
|
||||
throw error
|
||||
}
|
||||
},
|
||||
|
||||
// 更新邮件
|
||||
async updateMail(mailId, mailData) {
|
||||
try {
|
||||
const res = await api.mail.updateMail(mailId, mailData)
|
||||
|
||||
// 更新当前邮件
|
||||
if (mailState.currentMail && mailState.currentMail.mailId === mailId) {
|
||||
await this.getMailDetail(mailId)
|
||||
}
|
||||
|
||||
return res
|
||||
} catch (error) {
|
||||
console.error('更新邮件失败:', error)
|
||||
throw error
|
||||
}
|
||||
},
|
||||
|
||||
// 删除邮件
|
||||
async deleteMail(mailId) {
|
||||
try {
|
||||
const res = await api.mail.deleteMail(mailId)
|
||||
|
||||
// 从各个列表中移除该邮件
|
||||
mailState.inboxList = mailState.inboxList.filter(mail => mail.mailId !== mailId)
|
||||
mailState.sentList = mailState.sentList.filter(mail => mail.mailId !== mailId)
|
||||
mailState.draftList = mailState.draftList.filter(mail => mail.mailId !== mailId)
|
||||
|
||||
// 如果是当前邮件,清空
|
||||
if (mailState.currentMail && mailState.currentMail.mailId === mailId) {
|
||||
mailState.currentMail = null
|
||||
}
|
||||
|
||||
return res
|
||||
} catch (error) {
|
||||
console.error('删除邮件失败:', error)
|
||||
throw error
|
||||
}
|
||||
},
|
||||
|
||||
// 撤销邮件
|
||||
async revokeMail(mailId) {
|
||||
try {
|
||||
const res = await api.mail.revokeMail(mailId)
|
||||
|
||||
// 更新列表中的邮件状态
|
||||
const updateMailStatus = (mailList) => {
|
||||
const mail = mailList.find(m => m.mailId === mailId)
|
||||
if (mail) {
|
||||
mail.status = 'REVOKED'
|
||||
}
|
||||
}
|
||||
|
||||
updateMailStatus(mailState.inboxList)
|
||||
updateMailStatus(mailState.sentList)
|
||||
|
||||
// 更新当前邮件
|
||||
if (mailState.currentMail && mailState.currentMail.mailId === mailId) {
|
||||
mailState.currentMail.status = 'REVOKED'
|
||||
}
|
||||
|
||||
return res
|
||||
} catch (error) {
|
||||
console.error('撤销邮件失败:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 胶囊相关操作
|
||||
export const capsuleActions = {
|
||||
// 获取胶囊列表
|
||||
async getCapsules() {
|
||||
try {
|
||||
capsuleState.loading = true
|
||||
const res = await api.capsule.getCapsules()
|
||||
capsuleState.capsules = res.data.capsules
|
||||
capsuleState.scene = res.data.scene
|
||||
capsuleState.background = res.data.background
|
||||
return res
|
||||
} catch (error) {
|
||||
console.error('获取胶囊列表失败:', error)
|
||||
throw error
|
||||
} finally {
|
||||
capsuleState.loading = false
|
||||
}
|
||||
},
|
||||
|
||||
// 更新胶囊样式
|
||||
async updateCapsuleStyle(capsuleId, style) {
|
||||
try {
|
||||
const res = await api.capsule.updateCapsuleStyle(capsuleId, style)
|
||||
|
||||
// 更新本地胶囊样式
|
||||
const capsule = capsuleState.capsules.find(c => c.capsuleId === capsuleId)
|
||||
if (capsule) {
|
||||
capsule.style = style
|
||||
}
|
||||
|
||||
return res
|
||||
} catch (error) {
|
||||
console.error('更新胶囊样式失败:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 时间线相关操作
|
||||
export const timelineActions = {
|
||||
// 获取时间线
|
||||
async getTimeline(filter = {}) {
|
||||
try {
|
||||
timelineState.loading = true
|
||||
timelineState.filter = { ...timelineState.filter, ...filter }
|
||||
|
||||
const res = await api.user.getTimeline(timelineState.filter)
|
||||
timelineState.timeline = res.data.timeline
|
||||
return res
|
||||
} catch (error) {
|
||||
console.error('获取时间线失败:', error)
|
||||
throw error
|
||||
} finally {
|
||||
timelineState.loading = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 统计数据相关操作
|
||||
export const statisticsActions = {
|
||||
// 获取统计数据
|
||||
async getStatistics() {
|
||||
try {
|
||||
statisticsState.loading = true
|
||||
const res = await api.user.getStatistics()
|
||||
|
||||
// 更新统计数据
|
||||
Object.assign(statisticsState, res.data)
|
||||
|
||||
return res
|
||||
} catch (error) {
|
||||
console.error('获取统计数据失败:', error)
|
||||
throw error
|
||||
} finally {
|
||||
statisticsState.loading = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AI助手相关操作
|
||||
export const aiActions = {
|
||||
// 写作辅助
|
||||
async writingAssistant(data) {
|
||||
try {
|
||||
const res = await api.ai.writingAssistant(data)
|
||||
return res
|
||||
} catch (error) {
|
||||
console.error('写作辅助失败:', error)
|
||||
throw error
|
||||
}
|
||||
},
|
||||
|
||||
// 情感分析
|
||||
async sentimentAnalysis(content) {
|
||||
try {
|
||||
const res = await api.ai.sentimentAnalysis(content)
|
||||
return res
|
||||
} catch (error) {
|
||||
console.error('情感分析失败:', error)
|
||||
throw error
|
||||
}
|
||||
},
|
||||
|
||||
// 未来预测
|
||||
async futurePrediction(data) {
|
||||
try {
|
||||
const res = await api.ai.futurePrediction(data)
|
||||
return res
|
||||
} catch (error) {
|
||||
console.error('未来预测失败:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user