Skip to content

Commit

Permalink
Merge pull request #205 from Aqr-K/dev-login
Browse files Browse the repository at this point in the history
  • Loading branch information
jxxghp authored Oct 22, 2024
2 parents 539cf9a + 9949a16 commit 6b9c74d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
45 changes: 25 additions & 20 deletions src/components/cards/UserCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ const props = defineProps({
},
})
// 当前用户名称
const currentLoginUser = store.state.auth.userName
// 当前用户的ID
const currentLoginUserId = computed(() => store.state.auth.userID)
// 当前用户是否是管理员
const currentUserIsSuperuser = computed(() => store.state.auth.superUser)
// 定义触发的自定义事件
const emit = defineEmits(['remove', 'save'])
Expand Down Expand Up @@ -57,7 +60,7 @@ async function fetchSubscriptions() {
// 删除用户
async function removeUser() {
if (props.user.name == currentLoginUser) {
if (props.user.id === currentLoginUserId.value) {
$toast.error('不能删除当前登录用户!')
return
}
Expand All @@ -84,19 +87,6 @@ function editUser() {
userEditDialog.value = true
}
// 计算是否有用户编辑权限
const canEditUser = computed(() => {
if (store.state.auth.superUser && props.user.name !== currentLoginUser) return true
return false
})
// 计算是否有用户管理权限
const canManageUser = computed(() => {
if (props.user.name == currentLoginUser) return false
if (store.state.auth.superUser) return true
return false
})
// 用户重新完成时
function onUserUpdate() {
userEditDialog.value = false
Expand Down Expand Up @@ -139,8 +129,9 @@ onMounted(() => {
</div>
</VCardText>
<VCardText class="pb-6">
<h5 class="text-h6">详情</h5>
<VDivider class="my-2" />
<VDivider class="my-2">
<h5 class="text-h6">详情</h5>
</VDivider>
<VList lines="one">
<VListItem>
<VListItemTitle class="text-sm">
Expand Down Expand Up @@ -170,8 +161,22 @@ onMounted(() => {
</VList>
</VCardText>
<VCardText class="flex flex-row justify-center">
<VBtn v-if="canEditUser" color="primary" class="me-4" @click="editUser">编辑</VBtn>
<VBtn v-if="canManageUser" color="error" variant="outlined" @click="removeUser"> 删除 </VBtn>
<VBtn
v-if="currentUserIsSuperuser"
color="primary"
class="me-4"
@click="editUser"
>
编辑
</VBtn>
<VBtn
v-if="currentUserIsSuperuser && props.user.id != currentLoginUserId"
color="error"
variant="outlined"
@click="removeUser"
>
删除
</VBtn>
</VCardText>
</VCard>
<!-- 用户编辑弹窗 -->
Expand Down
4 changes: 3 additions & 1 deletion src/pages/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,16 @@ function login() {
// 获取token
const token = response.access_token
const superUser = response.super_user
const userID = response.user_id
const userName = response.user_name
const avatar = response.avatar
const level = response.level
const remember = form.value.remember
const permissions = response.permissions
// 更新token和remember状态到Vuex Store
store.dispatch('auth/login', { token, remember, superUser, userName, avatar, level, permissions })
store.dispatch('auth/login', {
token, remember, superUser, userID, userName, avatar, level, permissions })
// 登录后处理
afterLogin(superUser)
Expand Down
9 changes: 8 additions & 1 deletion src/store/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface AuthState {
token: string | null
remember: boolean
superUser: boolean
userID: number
userName: string
avatar: string
originalPath: string | null
Expand All @@ -24,6 +25,7 @@ const authModule: Module<AuthState, RootState> = {
token: null, // 用户令牌
remember: false, // 记住我
superUser: false, // 超级管理员
userID: 999, // 用户ID
userName: '', // 用户名
avatar: '', // 头像
originalPath: null, // 原始路径
Expand All @@ -43,6 +45,9 @@ const authModule: Module<AuthState, RootState> = {
setSuperUser(state, superUser: boolean) {
state.superUser = superUser
},
setUserID(state, userID: number) {
state.userID = userID
},
setUserName(state, userName: string) {
state.userName = userName
},
Expand All @@ -60,10 +65,11 @@ const authModule: Module<AuthState, RootState> = {
},
},
actions: {
login({ commit }, { token, remember, superUser, userName, avatar, level, permissions }) {
login({ commit }, { token, remember, superUser, userID, userName, avatar, level, permissions }) {
commit('setToken', token)
commit('setRemember', remember)
commit('setSuperUser', superUser)
commit('setUserID', userID)
commit('setUserName', userName)
commit('setAvatar', avatar)
commit('setLevel', level)
Expand All @@ -78,6 +84,7 @@ const authModule: Module<AuthState, RootState> = {
getToken: state => state.token,
getRemember: state => state.remember,
getSuperUser: state => state.superUser,
getUserID: state => state.userID,
getUserName: state => state.userName,
getAvatar: state => state.avatar,
getOriginalPath: state => state.originalPath,
Expand Down

0 comments on commit 6b9c74d

Please sign in to comment.