云
6 小时以前 fb4db4ae9b6a7f113ba049f6789e6916c0dcc276
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { ref } from 'vue';
import { clearMobileScroll } from './scroll';
export type TerminalType = 'pc' | 'mobile';
const KEY = 'mom-terminal';
function initial(): TerminalType {
  const stored = sessionStorage.getItem(KEY);
  return stored === 'pc' || stored === 'mobile' ? stored : (window.innerWidth < 768 ? 'mobile' : 'pc');
}
export const terminalType = ref<TerminalType>(initial());
export function selectTerminal(value: TerminalType) {
  terminalType.value = value;
  sessionStorage.setItem(KEY, value);
}
export function resetTerminal() {
  clearMobileScroll();
  sessionStorage.removeItem(KEY);
  for (const key of Object.keys(sessionStorage)) {
    if (key.startsWith('mom-mobile-')) sessionStorage.removeItem(key);
  }
  terminalType.value = window.innerWidth < 768 ? 'mobile' : 'pc';
}
export function terminalHome() { return terminalType.value === 'mobile' ? '/mobile/home' : undefined; }
export function safeDestination(path: unknown, fallback: string) {
  if (typeof path !== 'string') return fallback;
  let decoded: string;
  try { decoded = decodeURIComponent(path); } catch { return fallback; }
  if (!decoded.startsWith('/') || decoded.startsWith('//') || decoded.includes('\\') || decoded.startsWith('/auth')) return fallback;
  return decoded.startsWith('/mobile/') === (terminalType.value === 'mobile') ? decoded : fallback;
}