stores.ts 913 Bytes
import type { UsAppBoundLocationDto } from '../types/usAppBound'
import { getBoundLocations } from './authSession'

export type StoreInfo = UsAppBoundLocationDto

export { getBoundLocations } from './authSession'

export function getCurrentStoreId(): string {
  return uni.getStorageSync('storeId') || ''
}

export function switchStore(id: string, storeName: string, locationCode?: string) {
  uni.setStorageSync('storeId', id)
  uni.setStorageSync('storeName', storeName)
  if (locationCode != null && locationCode !== '') {
    uni.setStorageSync('storeLocationCode', locationCode)
  }
}

/** 顶部药丸展示用,如 LOC-1 */
export function getCurrentLocationCode(): string {
  const saved = uni.getStorageSync('storeLocationCode')
  if (saved) return saved
  const id = getCurrentStoreId()
  if (!id) return ''
  const row = getBoundLocations().find((l) => l.id === id)
  return row?.locationCode || ''
}