|
|
@@ -0,0 +1,105 @@
|
|
|
+import { defineSetupStore } from '@/stores/public/template'
|
|
|
+import type { SimCardStatus, SimCardPlatform } from '@/service/simCardManage/interface'
|
|
|
+
|
|
|
+const STORES_KEY = 'SimCardManageStateStore'
|
|
|
+const CURRENT_VERSION = '0.0.0'
|
|
|
+
|
|
|
+/** 状态过期时间:1 小时(毫秒) */
|
|
|
+const EXPIRE_MS = 60 * 60 * 1000
|
|
|
+
|
|
|
+/** SIM 卡管理界面需要保存的界面状态 */
|
|
|
+export interface SimCardManageState {
|
|
|
+ searchKeyword: string // GICCID 搜索关键词
|
|
|
+ selectStatus: SimCardStatus[] // 状态筛选
|
|
|
+ selectPlatform: SimCardPlatform[] // 平台筛选
|
|
|
+ page: number // 当前页
|
|
|
+ pageSize: number // 每页条数
|
|
|
+}
|
|
|
+
|
|
|
+function createDefaultState(): SimCardManageState {
|
|
|
+ return {
|
|
|
+ searchKeyword: '',
|
|
|
+ selectStatus: [],
|
|
|
+ selectPlatform: [],
|
|
|
+ page: 1,
|
|
|
+ pageSize: 10,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export const useSimCardManageStateStore = defineSetupStore(
|
|
|
+ STORES_KEY,
|
|
|
+ CURRENT_VERSION,
|
|
|
+ () => {
|
|
|
+ // 界面状态
|
|
|
+ const state = ref<SimCardManageState>(createDefaultState())
|
|
|
+ // 状态保存时间戳(毫秒)。0 表示尚未保存
|
|
|
+ const savedAt = ref<number>(0)
|
|
|
+
|
|
|
+ function $reset() {
|
|
|
+ state.value = createDefaultState()
|
|
|
+ savedAt.value = 0
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 判断已保存状态是否已过期(超过 1 小时或从未保存) */
|
|
|
+ function isExpired(): boolean {
|
|
|
+ if (savedAt.value === 0) return true
|
|
|
+ return Date.now() - savedAt.value > EXPIRE_MS
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 保存界面状态,并刷新保存时间戳 */
|
|
|
+ function save(next: SimCardManageState) {
|
|
|
+ state.value = {
|
|
|
+ searchKeyword: next.searchKeyword,
|
|
|
+ selectStatus: [...next.selectStatus],
|
|
|
+ selectPlatform: [...next.selectPlatform],
|
|
|
+ page: next.page,
|
|
|
+ pageSize: next.pageSize,
|
|
|
+ }
|
|
|
+ savedAt.value = Date.now()
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取界面状态(惰性过期):
|
|
|
+ * - 未过期:返回已保存状态的副本
|
|
|
+ * - 已过期:清空并返回默认状态
|
|
|
+ */
|
|
|
+ function restore(): SimCardManageState {
|
|
|
+ if (isExpired()) {
|
|
|
+ $reset()
|
|
|
+ return createDefaultState()
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ searchKeyword: state.value.searchKeyword,
|
|
|
+ selectStatus: [...state.value.selectStatus],
|
|
|
+ selectPlatform: [...state.value.selectPlatform],
|
|
|
+ page: state.value.page,
|
|
|
+ pageSize: state.value.pageSize,
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ // normal state
|
|
|
+ state,
|
|
|
+ savedAt,
|
|
|
+
|
|
|
+ // reactive state - ref()
|
|
|
+
|
|
|
+ // reactive getters - computed()
|
|
|
+
|
|
|
+ // normal getters - function()
|
|
|
+ isExpired,
|
|
|
+ restore,
|
|
|
+
|
|
|
+ // actions - function()
|
|
|
+ save,
|
|
|
+ $reset,
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ persist: {
|
|
|
+ key: STORES_KEY, // 存储 key
|
|
|
+ storage: localStorage, // 与其他 views state 保持一致
|
|
|
+ debug: true,
|
|
|
+ },
|
|
|
+ },
|
|
|
+)
|