|
|
@@ -29,45 +29,56 @@ const decrypt = (encryptedData: string, key: string) => {
|
|
|
return decrypt.toString(Utf8).toString()
|
|
|
}
|
|
|
|
|
|
-export function useAuth(autoLogin = false) {
|
|
|
- const { loginStore } = useStore()
|
|
|
- const autologinKey = localStorage.getItem('thj_autologin_key') // 自动登录密钥
|
|
|
- const autologinData = localStorage.getItem('thj_autologin_data') // 自动登录数据
|
|
|
- const loading = ref(false)
|
|
|
-
|
|
|
- const user = ref<Proto.LoginReq>({
|
|
|
- LoginID: '',
|
|
|
- LoginPWD: '',
|
|
|
- GUID: '',
|
|
|
- LoginType: 0,
|
|
|
- ClientType: 4,
|
|
|
- Version: '2.0.0.0',
|
|
|
- DeviceID: ''
|
|
|
- })
|
|
|
-
|
|
|
- if (autologinKey && autologinData) {
|
|
|
- const decryptedData = decrypt(autologinData, autologinKey)
|
|
|
- if (decryptedData) {
|
|
|
- const loginReq: Proto.LoginReq = JSON.parse(decryptedData)
|
|
|
- // 自动登录
|
|
|
- if (autoLogin) {
|
|
|
- user.value = loginReq
|
|
|
- } else {
|
|
|
- user.value.LoginID = loginReq.LoginID
|
|
|
+function useAutoLogin() {
|
|
|
+ // 获取自动登录数据
|
|
|
+ const getAutoLoginData = (autoLogin = false) => {
|
|
|
+ const autologinKey = localStorage.getItem('thj_autologin_key') // 自动登录密钥
|
|
|
+ const autologinData = localStorage.getItem('thj_autologin_data') // 自动登录数据
|
|
|
+ const loginData: Proto.LoginReq = {
|
|
|
+ LoginID: '',
|
|
|
+ LoginPWD: '',
|
|
|
+ GUID: '',
|
|
|
+ LoginType: 0,
|
|
|
+ ClientType: 4,
|
|
|
+ Version: '2.0.0.0',
|
|
|
+ DeviceID: ''
|
|
|
+ }
|
|
|
+ if (autologinKey && autologinData) {
|
|
|
+ const decryptedData = decrypt(autologinData, autologinKey)
|
|
|
+ if (decryptedData) {
|
|
|
+ const loginReq: Proto.LoginReq = JSON.parse(decryptedData)
|
|
|
+ // 自动登录
|
|
|
+ if (autoLogin) {
|
|
|
+ return loginReq
|
|
|
+ } else {
|
|
|
+ loginData.LoginID = loginReq.LoginID
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ return loginData
|
|
|
}
|
|
|
|
|
|
- // 设置自动登录数据
|
|
|
- const setAutoLoginData = (loginKey: string | null, password = '') => {
|
|
|
- if (loginKey) {
|
|
|
- user.value.LoginPWD = password
|
|
|
- const encryptedData = encrypt(JSON.stringify(user.value), loginKey)
|
|
|
- localStorage.setItem('thj_autologin_key', loginKey)
|
|
|
- localStorage.setItem('thj_autologin_data', encryptedData)
|
|
|
- }
|
|
|
+ // 保存自动登录数据
|
|
|
+ const setAutoLoginData = (data?: Proto.LoginReq) => {
|
|
|
+ const loginKey = v4() // 随机密钥
|
|
|
+ const loginData = data ?? getAutoLoginData(false)
|
|
|
+ const encryptedData = encrypt(JSON.stringify(loginData), loginKey)
|
|
|
+ localStorage.setItem('thj_autologin_key', loginKey)
|
|
|
+ localStorage.setItem('thj_autologin_data', encryptedData)
|
|
|
}
|
|
|
|
|
|
+ return {
|
|
|
+ getAutoLoginData,
|
|
|
+ setAutoLoginData
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export function useAuth(autoLogin = false) {
|
|
|
+ const { loginStore } = useStore()
|
|
|
+ const { getAutoLoginData, setAutoLoginData } = useAutoLogin()
|
|
|
+ const loading = ref(false)
|
|
|
+ const user = ref<Proto.LoginReq>(getAutoLoginData(autoLogin))
|
|
|
+
|
|
|
// 用户登录
|
|
|
const login = () => {
|
|
|
const { LoginID, LoginPWD } = user.value
|
|
|
@@ -81,15 +92,14 @@ export function useAuth(autoLogin = false) {
|
|
|
|
|
|
loginStore.userLogin(user.value).then(() => {
|
|
|
initBaseData().then(() => {
|
|
|
- const loginKey = v4() // 每次成功登录后更新密钥
|
|
|
- setAutoLoginData(loginKey, LoginPWD)
|
|
|
+ setAutoLoginData(user.value)
|
|
|
checkTokenLoop()
|
|
|
resolve()
|
|
|
}).catch((err) => {
|
|
|
catchError(err)
|
|
|
})
|
|
|
}).catch((err) => {
|
|
|
- setAutoLoginData(autologinKey)
|
|
|
+ setAutoLoginData()
|
|
|
catchError(err)
|
|
|
})
|
|
|
})
|
|
|
@@ -100,7 +110,9 @@ export function useAuth(autoLogin = false) {
|
|
|
// 用户登出
|
|
|
const logout = (callback?: () => void) => {
|
|
|
const { loginStore, accountStore } = useStore()
|
|
|
- setAutoLoginData(autologinKey)
|
|
|
+ const { setAutoLoginData } = useAutoLogin()
|
|
|
+
|
|
|
+ setAutoLoginData()
|
|
|
socket.closeAll()
|
|
|
timerTask.clearAll()
|
|
|
loginStore.reset()
|