Browse Source

添加切换模式的方式

cc12458 1 month ago
parent
commit
ac1710c752

+ 2 - 3
src/composables/start/start.directive.ts

@@ -31,13 +31,12 @@ function resolveMode(modifiers: StartBinding['modifiers']): 'duration' | 'distan
 function readValue(binding: StartBinding): StartDirectiveValue | null {
   const v = binding.value;
   if (v == null || typeof v !== 'object') return null;
-  if (typeof v.threshold !== 'number' || Number.isNaN(v.threshold)) return null;
+  if (Number.isNaN(v.threshold)) return null;
   return v;
 }
 
 function mount(el: HTMLElement, binding: StartBinding) {
-  let down: { timeStamp: number; clientX: number; clientY: number; pointerId: number } | null =
-    null;
+  let down: { timeStamp: number; clientX: number; clientY: number; pointerId: number } | null = null;
 
   const onPointerDown = (e: PointerEvent) => {
     down = {

+ 13 - 4
src/composables/start/useStart.ts

@@ -2,7 +2,7 @@ import type { StartDirectiveValue } from '@/composables/start/start.directive';
 
 import { showLoadingToast } from 'vant';
 import { Notify } from '@/platform';
-import { registerVisitorMethod } from '@/request/api';
+import { registerVisitorMethod, toggleApplicationMethod } from '@/request/api';
 import { useFlowStore, useVisitor } from '@/stores';
 
 const showLoading = () => {
@@ -12,16 +12,25 @@ const showLoading = () => {
     duration: 0,
   });
   return () => instance.close();
-}
+};
 
 export function useStart(ui?: boolean) {
+  const router = useRouter();
   const Visitor = useVisitor();
   const Flow = useFlowStore();
   const { flow } = storeToRefs(Flow);
 
   const extra: StartDirectiveValue = {
     threshold: 1000 * 5,
-    fn() {},
+    async fn() {
+      await toggleApplicationMethod().send(true);
+      await showConfirmDialog({ title: '应用需要重新加载', showCancelButton: false });
+      try {
+        await router.replace('/');
+      } finally {
+        location.href = location.origin;
+      }
+    },
   };
 
   const register = async () => {
@@ -34,7 +43,7 @@ export function useStart(ui?: boolean) {
       Notify.error(error.message);
     } finally {
       loading.value = false;
-      close?.()
+      close?.();
     }
   };
 

+ 2 - 1
src/loader/launch.loader.ts

@@ -1,5 +1,5 @@
 import router from '@/router';
-import pinia, { useFlowStore } from '@/stores';
+import pinia, { useFlowStore, usePlatformStore } from '@/stores';
 
 const components = import.meta.glob('@/pages/**/*.vue');
 
@@ -23,6 +23,7 @@ export default function launchLoader(container = '#app'): DEV.Loader {
     }
 
     if (config.image.page && config.image.page !== currentRoute) await router.replace(config.image.page);
+    usePlatformStore().$init(config.warrant)
     useFlowStore().$init(config.flowConfig)
 
     app.mount(container);

+ 1 - 1
src/request/alova.ts

@@ -12,7 +12,7 @@ export default createAlova({
   requestAdapter: import.meta.env.DEV ? adapterMock() : adapterFetch(),
   async beforeRequest(method) {
     const pinia = getPinia();
-    method.config.headers.warrant ??= usePlatformStore(pinia).serialNumber;
+    method.config.headers.warrant ??= usePlatformStore(pinia).serialNumber ?? '';
     if ( !method.config.meta?.ignoreToken ) method.config.headers.Authorization ??= useAccountStore(pinia).token;
   },
   responded: {

+ 3 - 1
src/request/api/account.api.ts

@@ -3,7 +3,7 @@ import { cacheFor } from '@/request/api/index';
 import { fromRegisterFields } from '@/request/model';
 import { getRoutePath } from '@/router';
 import { useVisitor } from '@/stores';
-import { getClientURL } from '@/tools';
+import { getClientURL, randomUUID } from '@/tools';
 
 import HTTP from '../alova';
 
@@ -43,6 +43,7 @@ export function registerAccountMethod(params: Partial<RegisterModel>) {
   return HTTP.Post<string, string>(`/fdhb-tablet/patientInfoManage/savePatientInfo`, params, {
     name: 'register',
     transform(data) {
+      data ??= randomUUID();
       const Visitor = useVisitor();
       Visitor.patientId = data;
       return data;
@@ -55,6 +56,7 @@ export function searchAccountMethod(params: Partial<RegisterModel>) {
     hitSource: 'register',
     params,
     transform(data: Record<string, any>, headers) {
+      data ??= {};
       if (data) data.address = [];
       const address = ['province', 'city', 'area'];
       for (const key of address) {

+ 19 - 0
src/request/api/index.ts

@@ -1,5 +1,7 @@
+import { invalidateCache } from 'alova';
 import HTTP from '@/request/alova';
 import { application } from '@/request/model';
+import { getPinia, usePlatformStore } from '@/stores';
 
 export * from './account.api';
 export * from './questionnaire.api';
@@ -13,4 +15,21 @@ export function getApplicationMethod() {
   });
 }
 
+export function toggleApplicationMethod() {
+  const pinia = getPinia();
+  const warrant = usePlatformStore(pinia).serialNumber ?? '';
+  return HTTP.Post(
+    `/fdhb-tablet/warrantManage/updateWarrant`,
+    { warrant },
+    {
+      cacheFor: null,
+      async transform() {
+        await invalidateCache();
+        const store = (pinia as any)._s as Map<string, { $reset?: () => void }>;
+        for (const [key, value] of store) if (key !== 'platform') value.$reset?.();
+      },
+    }
+  );
+}
+
 export const cacheFor = 24 * 60 * 60 * 1000;

+ 1 - 0
src/request/model/index.ts

@@ -30,6 +30,7 @@ export function application(data: Record<string, any>) {
   image.debug ??= image.el.includes('debug');
 
   return {
+    warrant: data.warrant as string ?? void 0,
     copyright: [data?.partner, data?.technicalSupporter].filter(Boolean).join('<br>'),
     registerFields: Array.isArray(data?.tabletFileFields) ? data.tabletFileFields : [],
     flowConfig: Array.isArray(data?.tabletProcessModules) ? data?.tabletProcessModules : [],

+ 10 - 1
src/stores/account.store.ts

@@ -4,7 +4,6 @@ import { useWatcher } from 'alova/client';
 import { getMenus } from '@/request/api/manage';
 import { filterMenuTreeByIds } from '@/request/model/manage.model';
 
-
 export const useAccountStore = defineStore('account', () => {
   const token = ref<string>();
   const user = shallowRef<UserModel>();
@@ -25,6 +24,7 @@ export const useAccountStore = defineStore('account', () => {
   const $reset = () => {
     token.value = '';
     user.value = void 0;
+    menus.value = [];
   };
 
   const login = (value: string, data: UserModel) => {
@@ -34,3 +34,12 @@ export const useAccountStore = defineStore('account', () => {
 
   return { token, user, loading, menus, $reset, login, logout: $reset };
 });
+
+export function getToken() {
+  try {
+    const value = localStorage.getItem(`${import.meta.env.SIX_APP_NAME ?? '@six/unknown'}:account`);
+    return JSON.parse(value).token;
+  } catch {
+    return void 0;
+  }
+}

+ 7 - 8
src/stores/platform.store.ts

@@ -1,10 +1,9 @@
 import { getSerialNumberSync } from '@/platform';
 
-
-export const usePlatformStore = defineStore(
-  'platform',
-  () => {
-    const serialNumber = ref(getSerialNumberSync());
-    return { serialNumber };
-  },
-);
+export const usePlatformStore = defineStore('platform', () => {
+  const serialNumber = ref(getSerialNumberSync());
+  const $init = (sn?: string) => {
+    serialNumber.value ??= sn;
+  };
+  return { serialNumber, $init };
+});