lock.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { defineStore } from 'pinia'
  2. import { useUserStore } from './user'
  3. import type { LockInfo } from '@/types/store'
  4. import { LOCK_INFO_KEY } from '@/enums/cacheEnum'
  5. import { Persistent } from '@/utils/cache/persistent'
  6. interface LockState {
  7. lockInfo: Nullable<LockInfo>
  8. }
  9. export const useLockStore = defineStore('app-lock', {
  10. state: (): LockState => ({
  11. lockInfo: Persistent.getLocal(LOCK_INFO_KEY),
  12. }),
  13. getters: {
  14. getLockInfo(state): Nullable<LockInfo> {
  15. return state.lockInfo
  16. },
  17. },
  18. actions: {
  19. setLockInfo(info: LockInfo) {
  20. this.lockInfo = Object.assign({}, this.lockInfo, info)
  21. Persistent.setLocal(LOCK_INFO_KEY, this.lockInfo, true)
  22. },
  23. resetLockInfo() {
  24. Persistent.removeLocal(LOCK_INFO_KEY, true)
  25. this.lockInfo = null
  26. },
  27. // Unlock
  28. async unLock(password?: string) {
  29. const userStore = useUserStore()
  30. if (this.lockInfo?.pwd === password) {
  31. this.resetLockInfo()
  32. return true
  33. }
  34. const tryLogin = async () => {
  35. // TODO 滑块验证码
  36. try {
  37. const username = userStore.getUserInfo?.user.nickname
  38. const res = await userStore.login({
  39. username,
  40. password: password!,
  41. goHome: false,
  42. mode: 'none',
  43. captchaVerification: '',
  44. })
  45. if (res)
  46. this.resetLockInfo()
  47. return res
  48. }
  49. catch (error) {
  50. return false
  51. }
  52. }
  53. return await tryLogin()
  54. },
  55. },
  56. })