244 lines
6.5 KiB
JavaScript
244 lines
6.5 KiB
JavaScript
import { defineStore } from 'pinia'
|
||
|
||
export const useUserStore = defineStore('user', {
|
||
state: () => ({
|
||
token: uni.getStorageSync('token') || '',
|
||
userInfo: uni.getStorageSync('userInfo') || null,
|
||
isLogin: false,
|
||
currentRole: uni.getStorageSync('currentRole') || 'user',
|
||
// ✅ 新增:所有身份列表
|
||
allRoles: uni.getStorageSync('allRoles') || []
|
||
}),
|
||
|
||
getters: {
|
||
hasLogin: (state) => !!state.token,
|
||
nickname: (state) => state.userInfo?.nickname || '未登录',
|
||
avatar: (state) => state.userInfo?.avatar || '/static/images/default-avatar.png',
|
||
isUser: (state) => state.currentRole === 'user' || state.currentRole === 'parent',
|
||
isTeacher: (state) => state.currentRole === 'teacher',
|
||
isManager: (state) => state.currentRole === 'manager',
|
||
isDistributor: (state) => state.currentRole === 'distributor',
|
||
isServiceProvider: (state) => state.currentRole === 'serviceProvider',
|
||
roleName: (state) => {
|
||
const roleMap = {
|
||
user: '家长',
|
||
parent: '家长',
|
||
teacher: '陪伴员',
|
||
manager: '管理师',
|
||
distributor: '分销员',
|
||
serviceProvider: '服务商'
|
||
}
|
||
return roleMap[state.currentRole] || '未知'
|
||
},
|
||
// ✅ 新增:是否有多个身份
|
||
hasMultipleRoles: (state) => state.allRoles.length > 1,
|
||
// ✅ 新增:可切换的身份列表
|
||
availableRoles: (state) => {
|
||
return state.allRoles.map(role => ({
|
||
value: role,
|
||
label: getRoleName(role),
|
||
icon: getRoleIcon(role)
|
||
}))
|
||
}
|
||
},
|
||
|
||
actions: {
|
||
setToken(token) {
|
||
this.token = token
|
||
this.isLogin = true
|
||
uni.setStorageSync('token', token)
|
||
},
|
||
|
||
setUserInfo(userInfo) {
|
||
this.userInfo = userInfo
|
||
uni.setStorageSync('userInfo', userInfo)
|
||
},
|
||
|
||
setRole(role) {
|
||
this.currentRole = role
|
||
uni.setStorageSync('currentRole', role)
|
||
},
|
||
|
||
logout() {
|
||
this.token = ''
|
||
this.userInfo = null
|
||
this.isLogin = false
|
||
this.currentRole = 'user'
|
||
this.allRoles = []
|
||
uni.removeStorageSync('token')
|
||
uni.removeStorageSync('userInfo')
|
||
uni.removeStorageSync('currentRole')
|
||
uni.removeStorageSync('allRoles')
|
||
},
|
||
|
||
switchRole(role) {
|
||
this.setRole(role)
|
||
// 使用 switchTab 切换到首页,然后再切换回"我的"页面
|
||
uni.switchTab({
|
||
url: '/pages/index/index',
|
||
success: () => {
|
||
// 延迟切换回"我的"页面,确保状态已更新
|
||
setTimeout(() => {
|
||
uni.switchTab({ url: '/pages/user/index' })
|
||
}, 100)
|
||
}
|
||
})
|
||
},
|
||
|
||
checkLogin() {
|
||
// 防止页面逻辑只依赖 pinia 状态而未同步 storage,导致"刚登录仍判未登录"
|
||
if (!this.token) {
|
||
this.token = uni.getStorageSync('token') || ''
|
||
}
|
||
if (!this.userInfo) {
|
||
this.userInfo = uni.getStorageSync('userInfo') || null
|
||
}
|
||
if (!this.hasLogin) {
|
||
uni.showModal({
|
||
title: '提示',
|
||
content: '请先登录',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
uni.navigateTo({ url: '/pages/login/index' })
|
||
}
|
||
}
|
||
})
|
||
return false
|
||
}
|
||
return true
|
||
},
|
||
|
||
// ✅ 新增:加载所有身份
|
||
async loadAllRoles() {
|
||
try {
|
||
const res = await uni.request({
|
||
url: '/api/user/roles/list',
|
||
method: 'GET',
|
||
header: { Authorization: this.token }
|
||
})
|
||
|
||
if (res.data.code === 200) {
|
||
this.allRoles = res.data.data.allRoles || []
|
||
uni.setStorageSync('allRoles', this.allRoles)
|
||
|
||
// 同步主身份
|
||
if (res.data.data.primaryRole) {
|
||
this.currentRole = res.data.data.primaryRole
|
||
uni.setStorageSync('currentRole', res.data.data.primaryRole)
|
||
}
|
||
}
|
||
} catch (error) {
|
||
console.error('加载身份列表失败:', error)
|
||
}
|
||
},
|
||
|
||
// ✅ 新增:切换身份(调用后端API)
|
||
async switchRoleWithApi(roleType) {
|
||
try {
|
||
uni.showLoading({ title: '切换中...' })
|
||
|
||
const res = await uni.request({
|
||
url: '/api/user/roles/switch',
|
||
method: 'POST',
|
||
data: { roleType },
|
||
header: { Authorization: this.token }
|
||
})
|
||
|
||
uni.hideLoading()
|
||
|
||
if (res.data.code === 200) {
|
||
this.currentRole = roleType
|
||
uni.setStorageSync('currentRole', roleType)
|
||
|
||
// 刷新页面
|
||
uni.reLaunch({ url: '/pages/index/index' })
|
||
|
||
uni.showToast({
|
||
title: '切换成功',
|
||
icon: 'success'
|
||
})
|
||
} else {
|
||
uni.showToast({
|
||
title: res.data.message || '切换失败',
|
||
icon: 'none'
|
||
})
|
||
}
|
||
} catch (error) {
|
||
uni.hideLoading()
|
||
console.error('切换身份失败:', error)
|
||
uni.showToast({
|
||
title: '切换失败',
|
||
icon: 'none'
|
||
})
|
||
}
|
||
},
|
||
|
||
// ✅ 新增:申请新身份
|
||
async applyNewRole(roleType) {
|
||
try {
|
||
uni.showLoading({ title: '申请中...' })
|
||
|
||
const res = await uni.request({
|
||
url: '/api/user/roles/apply',
|
||
method: 'POST',
|
||
data: { roleType },
|
||
header: { Authorization: this.token }
|
||
})
|
||
|
||
uni.hideLoading()
|
||
|
||
if (res.data.code === 200) {
|
||
// 重新加载身份列表
|
||
await this.loadAllRoles()
|
||
|
||
uni.showToast({
|
||
title: '申请成功',
|
||
icon: 'success'
|
||
})
|
||
|
||
return true
|
||
} else {
|
||
uni.showToast({
|
||
title: res.data.message || '申请失败',
|
||
icon: 'none'
|
||
})
|
||
return false
|
||
}
|
||
} catch (error) {
|
||
uni.hideLoading()
|
||
console.error('申请身份失败:', error)
|
||
uni.showToast({
|
||
title: '申请失败',
|
||
icon: 'none'
|
||
})
|
||
return false
|
||
}
|
||
}
|
||
}
|
||
})
|
||
|
||
// 辅助函数
|
||
function getRoleName(role) {
|
||
const roleMap = {
|
||
user: '家长',
|
||
parent: '家长',
|
||
teacher: '陪伴员',
|
||
manager: '管理师',
|
||
distributor: '分销员',
|
||
serviceProvider: '服务商'
|
||
}
|
||
return roleMap[role] || '未知'
|
||
}
|
||
|
||
function getRoleIcon(role) {
|
||
const iconMap = {
|
||
user: '👨👩👧',
|
||
parent: '👨👩👧',
|
||
teacher: '👨🏫',
|
||
manager: '👔',
|
||
distributor: '💼',
|
||
serviceProvider: '🏢'
|
||
}
|
||
return iconMap[role] || '❓'
|
||
}
|