support plus account balance which account currency does not equal to default currency

This commit is contained in:
MaysWind
2020-11-18 02:03:46 +08:00
parent 06166c100e
commit 1ed03f3710
2 changed files with 89 additions and 12 deletions
+33
View File
@@ -1,3 +1,5 @@
import utils from './utils.js'
const exchangeRatesLocalStorageKey = 'lab_exchange_rates';
function getExchangeRates() {
@@ -14,8 +16,39 @@ function clearExchangeRates() {
localStorage.removeItem(exchangeRatesLocalStorageKey);
}
function getExchangeRate(fromCurrency, toCurrency) {
const exchangeRates = getExchangeRates().exchangeRates;
const exchangeRateMap = {};
for (let i = 0; i < exchangeRates.length; i++) {
const exchangeRate = exchangeRates[i];
exchangeRateMap[exchangeRate.currency] = exchangeRate;
}
const fromCurrencyExchangeRate = exchangeRateMap[fromCurrency];
const toCurrencyExchangeRate = exchangeRateMap[toCurrency];
if (!fromCurrencyExchangeRate || !toCurrencyExchangeRate) {
return null;
}
return parseFloat(toCurrencyExchangeRate.rate) / parseFloat(fromCurrencyExchangeRate.rate);
}
function getOtherCurrencyAmount(amount, fromCurrency, toCurrency) {
const exchangeRate = getExchangeRate(fromCurrency, toCurrency);
if (!utils.isNumber(exchangeRate)) {
return null;
}
return amount * exchangeRate;
}
export default {
getExchangeRates,
setExchangeRates,
clearExchangeRates,
getExchangeRate,
getOtherCurrencyAmount,
};