40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
|
|
interface CurrencyOption {
|
|
code: string
|
|
symbol: string
|
|
name: string
|
|
}
|
|
|
|
interface CurrencyState {
|
|
currentCurrency: CurrencyOption
|
|
}
|
|
|
|
export const availableCurrencies: CurrencyOption[] = [
|
|
{ code: 'USD', symbol: '$', name: '美元' },
|
|
{ code: 'CNY', symbol: '¥', name: '人民币' },
|
|
{ code: 'EUR', symbol: '€', name: '欧元' },
|
|
{ code: 'GBP', symbol: '£', name: '英镑' },
|
|
]
|
|
|
|
export const useCurrencyStore = defineStore('currency', {
|
|
state: (): CurrencyState => ({
|
|
currentCurrency: availableCurrencies[0]
|
|
}),
|
|
|
|
actions: {
|
|
setCurrency(currencyCode: string) {
|
|
const currency = availableCurrencies.find(c => c.code === currencyCode)
|
|
if (currency) {
|
|
this.currentCurrency = currency
|
|
}
|
|
}
|
|
},
|
|
|
|
getters: {
|
|
symbol: (state: CurrencyState): string => state.currentCurrency.symbol,
|
|
code: (state: CurrencyState): string => state.currentCurrency.code,
|
|
name: (state: CurrencyState): string => state.currentCurrency.name
|
|
}
|
|
})
|