currency-settings.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { STORAGE } from '@/core/constants/storage-keys'
  2. export const DEFAULT_CURRENCY = 'HKD'
  3. export const CURRENCY_OPTIONS = Object.freeze([
  4. { code: 'HKD', labelKey: 'user.currencyHKD' },
  5. { code: 'CNY', labelKey: 'user.currencyCNY' },
  6. { code: 'USD', labelKey: 'user.currencyUSD' },
  7. { code: 'MOP', labelKey: 'user.currencyMOP' },
  8. { code: 'TWD', labelKey: 'user.currencyTWD' }
  9. ])
  10. export function isSupportedCurrency(code) {
  11. const next = String(code || '').trim().toUpperCase()
  12. return CURRENCY_OPTIONS.some((row) => row.code === next)
  13. }
  14. export function getStoredCurrency() {
  15. try {
  16. const raw = uni.getStorageSync(STORAGE.currency)
  17. const code = String(raw || '').trim().toUpperCase()
  18. if (isSupportedCurrency(code)) return code
  19. } catch (_) {}
  20. return DEFAULT_CURRENCY
  21. }
  22. export function setStoredCurrency(code) {
  23. const next = String(code || '').trim().toUpperCase()
  24. if (!isSupportedCurrency(next)) return false
  25. uni.setStorageSync(STORAGE.currency, next)
  26. return true
  27. }
  28. export function currencyLabelForCode(code, t) {
  29. const row = CURRENCY_OPTIONS.find((item) => item.code === String(code || '').toUpperCase())
  30. if (!row) return String(code || DEFAULT_CURRENCY)
  31. return t(row.labelKey)
  32. }