员工端h5版本代码提交
This commit is contained in:
109
src/stores/auth.ts
Normal file
109
src/stores/auth.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import { computed, ref } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import {
|
||||
bindUserInfoAndGetUserMemberInfoByPhone,
|
||||
loginByMobile,
|
||||
type OrganizationMemberInfo
|
||||
} from '@/api/auth'
|
||||
import type { TokenResponse } from '@/types/api'
|
||||
import { env } from '@/utils/env'
|
||||
|
||||
export interface StaffUser {
|
||||
id?: string
|
||||
username?: string
|
||||
phone?: string
|
||||
tenantId?: string | number
|
||||
}
|
||||
|
||||
const TOKEN_KEY = 'hotel_h5_access_token'
|
||||
const REFRESH_TOKEN_KEY = 'hotel_h5_refresh_token'
|
||||
const USER_KEY = 'hotel_h5_user'
|
||||
const MEMBER_KEY = 'hotel_h5_member'
|
||||
|
||||
const readUser = (): StaffUser | null => {
|
||||
const raw = localStorage.getItem(USER_KEY)
|
||||
if (!raw) return null
|
||||
try {
|
||||
return JSON.parse(raw) as StaffUser
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
const readMember = (): OrganizationMemberInfo | null => {
|
||||
const raw = localStorage.getItem(MEMBER_KEY)
|
||||
if (!raw) return null
|
||||
try {
|
||||
return JSON.parse(raw) as OrganizationMemberInfo
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const token = ref(localStorage.getItem(TOKEN_KEY) || '')
|
||||
const refreshToken = ref(localStorage.getItem(REFRESH_TOKEN_KEY) || '')
|
||||
const user = ref<StaffUser | null>(readUser())
|
||||
const memberInfo = ref<OrganizationMemberInfo | null>(readMember())
|
||||
|
||||
const isLoggedIn = computed(() => Boolean(token.value))
|
||||
const hasOrganization = computed(() => Boolean(memberInfo.value?.organizationId))
|
||||
|
||||
const persistToken = (payload: TokenResponse, phone: string) => {
|
||||
token.value = payload.access_token
|
||||
refreshToken.value = payload.refresh_token || ''
|
||||
user.value = {
|
||||
id: payload.user_id,
|
||||
username: payload.username || phone,
|
||||
phone,
|
||||
tenantId: payload.tenant_id
|
||||
}
|
||||
|
||||
localStorage.setItem(TOKEN_KEY, token.value)
|
||||
if (refreshToken.value) {
|
||||
localStorage.setItem(REFRESH_TOKEN_KEY, refreshToken.value)
|
||||
}
|
||||
localStorage.setItem(USER_KEY, JSON.stringify(user.value))
|
||||
}
|
||||
|
||||
const login = async (phone: string, code: string) => {
|
||||
const payload = await loginByMobile(phone, code)
|
||||
persistToken(payload, phone)
|
||||
try {
|
||||
const member = await bindUserInfoAndGetUserMemberInfoByPhone(phone)
|
||||
if (!member?.organizationId || !member?.memberId) {
|
||||
throw new Error('未绑定组织,请联系管理员')
|
||||
}
|
||||
memberInfo.value = member
|
||||
localStorage.setItem(MEMBER_KEY, JSON.stringify(member))
|
||||
} catch (error) {
|
||||
logout()
|
||||
throw new Error('未绑定组织,请联系管理员')
|
||||
}
|
||||
}
|
||||
|
||||
const logout = () => {
|
||||
token.value = ''
|
||||
refreshToken.value = ''
|
||||
user.value = null
|
||||
memberInfo.value = null
|
||||
localStorage.removeItem(TOKEN_KEY)
|
||||
localStorage.removeItem(REFRESH_TOKEN_KEY)
|
||||
localStorage.removeItem(USER_KEY)
|
||||
localStorage.removeItem(MEMBER_KEY)
|
||||
}
|
||||
|
||||
return {
|
||||
token,
|
||||
refreshToken,
|
||||
user,
|
||||
memberInfo,
|
||||
hasOrganization,
|
||||
isLoggedIn,
|
||||
isMockMode: env.useMock,
|
||||
clientId: env.clientId,
|
||||
clientConfigId: env.clientConfigId,
|
||||
login,
|
||||
logout
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user