settlementSign.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { defineStore } from 'pinia'
  2. import { uniStorage } from '@/utils/uniStorage'
  3. export interface SettlementSignContext {
  4. signTraceId: string
  5. subjectLocation: string
  6. createdAt: number
  7. }
  8. interface SettlementSignState {
  9. context?: SettlementSignContext
  10. }
  11. const SIGN_CONTEXT_EXPIRE_TIME = 30 * 60 * 1000
  12. const createSignTraceId = (subjectLocation: string): string => {
  13. return `${Date.now()}_${subjectLocation}`
  14. }
  15. export const useSettlementSignStore = defineStore('settlementSign', {
  16. state: (): SettlementSignState => ({
  17. context: undefined,
  18. }),
  19. getters: {
  20. validContext(state): SettlementSignContext | undefined {
  21. if (!state.context?.subjectLocation) return undefined
  22. const isExpired = Date.now() - state.context.createdAt > SIGN_CONTEXT_EXPIRE_TIME
  23. if (isExpired) return undefined
  24. return state.context
  25. },
  26. pendingSubjectLocation(): string {
  27. return this.validContext?.subjectLocation || ''
  28. },
  29. },
  30. actions: {
  31. createContext(subjectLocation: string): SettlementSignContext {
  32. const context: SettlementSignContext = {
  33. signTraceId: createSignTraceId(subjectLocation),
  34. subjectLocation,
  35. createdAt: Date.now(),
  36. }
  37. this.context = context
  38. return context
  39. },
  40. clearContext(): void {
  41. this.context = undefined
  42. },
  43. },
  44. persist: {
  45. key: 'settlement-sign-store',
  46. storage: uniStorage,
  47. pick: ['context'],
  48. },
  49. })