|
|
@@ -1,14 +1,42 @@
|
|
|
-import { ref, reactive } from 'vue'
|
|
|
+import { ref } from 'vue'
|
|
|
import { v4 } from 'uuid'
|
|
|
-import { useLoginStore } from '@/stores'
|
|
|
-import { initBaseData } from '@/business/common'
|
|
|
-import eventBus from '@/services/bus'
|
|
|
+import { timerTask } from '@/utils/timer'
|
|
|
+import { useStore } from '@/stores'
|
|
|
+import { initBaseData, checkTokenLoop } from '@/business/common'
|
|
|
+import socket from '@/services/socket'
|
|
|
+import cryptojs from 'crypto-js'
|
|
|
|
|
|
-export function useAuth() {
|
|
|
- const { userLogin } = useLoginStore()
|
|
|
+// AES加密处理
|
|
|
+const encrypt = (data: string, key: string) => {
|
|
|
+ const { enc: { Utf8 }, AES } = cryptojs
|
|
|
+ // 统一将传入的字符串转成UTF8编码
|
|
|
+ const dataHex = Utf8.parse(data) // 需要加密的数据
|
|
|
+ const keyHex = Utf8.parse(key) // 秘钥
|
|
|
+ const ivHex = Utf8.parse(key) // 偏移量
|
|
|
+ const encrypted = AES.encrypt(dataHex, keyHex, { iv: ivHex, })
|
|
|
+ return encrypted.ciphertext.toString() // 返回加密后的值
|
|
|
+}
|
|
|
+
|
|
|
+// AES解密处理
|
|
|
+const decrypt = (encryptedData: string, key: string) => {
|
|
|
+ const { enc: { Utf8, Hex, Base64 }, AES } = cryptojs
|
|
|
+ // 统一将传入的字符串转成UTF8编码
|
|
|
+ const encryptedHexStr = Hex.parse(encryptedData)
|
|
|
+ const srcs = Base64.stringify(encryptedHexStr)
|
|
|
+ const keyHex = Utf8.parse(key) // 秘钥
|
|
|
+ const ivHex = Utf8.parse(key) // 偏移量
|
|
|
+ const decrypt = AES.decrypt(srcs, keyHex, { iv: ivHex })
|
|
|
+ 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 = reactive<Proto.LoginReq>({
|
|
|
- LoginID: localStorage.getItem('thj_app_loginid') ?? '',
|
|
|
+
|
|
|
+ const user = ref<Proto.LoginReq>({
|
|
|
+ LoginID: '',
|
|
|
LoginPWD: '',
|
|
|
GUID: '',
|
|
|
LoginType: 0,
|
|
|
@@ -17,24 +45,67 @@ export function useAuth() {
|
|
|
DeviceID: ''
|
|
|
})
|
|
|
|
|
|
- // 用户登录
|
|
|
- const login = async () => {
|
|
|
- loading.value = true
|
|
|
- const catchError = (err: string) => {
|
|
|
- loading.value = false
|
|
|
- return Promise.reject(err)
|
|
|
+ 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
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置自动登录数据
|
|
|
+ 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)
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- await userLogin({ ...user, GUID: v4() }).then(() => {
|
|
|
- localStorage.setItem('thj_app_loginid', user.LoginID)
|
|
|
- }).catch((err) => catchError(err))
|
|
|
+ // 用户登录
|
|
|
+ const login = () => {
|
|
|
+ const { LoginID, LoginPWD } = user.value
|
|
|
+ if (LoginID && LoginPWD) {
|
|
|
+ return new Promise<void>((resolve, reject) => {
|
|
|
+ loading.value = true
|
|
|
+ const catchError = (err: string) => {
|
|
|
+ loading.value = false
|
|
|
+ reject(err)
|
|
|
+ }
|
|
|
|
|
|
- await initBaseData().catch((err) => catchError(err))
|
|
|
+ loginStore.userLogin(user.value).then(() => {
|
|
|
+ initBaseData().then(() => {
|
|
|
+ const loginKey = v4() // 每次成功登录后更新密钥
|
|
|
+ setAutoLoginData(loginKey, LoginPWD)
|
|
|
+ checkTokenLoop()
|
|
|
+ resolve()
|
|
|
+ }).catch((err) => {
|
|
|
+ catchError(err)
|
|
|
+ })
|
|
|
+ }).catch((err) => {
|
|
|
+ setAutoLoginData(autologinKey)
|
|
|
+ catchError(err)
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return Promise.reject('登录失败')
|
|
|
}
|
|
|
|
|
|
// 用户登出
|
|
|
- const logout = () => {
|
|
|
- eventBus.$emit('LogoutNotify')
|
|
|
+ const logout = (callback?: () => void) => {
|
|
|
+ const { loginStore, accountStore } = useStore()
|
|
|
+ setAutoLoginData(autologinKey)
|
|
|
+ socket.closeAll()
|
|
|
+ timerTask.clearAll()
|
|
|
+ loginStore.reset()
|
|
|
+ accountStore.reset()
|
|
|
+ callback && callback()
|
|
|
}
|
|
|
|
|
|
return {
|