feat: 汇率换算功能完整实现
- 首页双向换算:30种主流货币,实时计算,一键互换 - 汇率总览页:一对多查看所有货币换算结果 - 数据源 open.er-api.com,4小时本地缓存 + 失败兜底 - UI 美化:渐变背景、分色货币块、紫色互换按钮 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
65
miniprogram/utils/currencies.js
Normal file
65
miniprogram/utils/currencies.js
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* 主流货币配置(约 30 种)
|
||||
* flag: emoji 国旗
|
||||
* code: ISO 4217 货币代码
|
||||
* name: 中文名
|
||||
*/
|
||||
const CURRENCIES = [
|
||||
{ flag: '🇨🇳', code: 'CNY', name: '人民币' },
|
||||
{ flag: '🇺🇸', code: 'USD', name: '美元' },
|
||||
{ flag: '🇪🇺', code: 'EUR', name: '欧元' },
|
||||
{ flag: '🇬🇧', code: 'GBP', name: '英镑' },
|
||||
{ flag: '🇯🇵', code: 'JPY', name: '日元' },
|
||||
{ flag: '🇰🇷', code: 'KRW', name: '韩元' },
|
||||
{ flag: '🇭🇰', code: 'HKD', name: '港币' },
|
||||
{ flag: '🇹🇼', code: 'TWD', name: '新台币' },
|
||||
{ flag: '🇲🇴', code: 'MOP', name: '澳门元' },
|
||||
{ flag: '🇸🇬', code: 'SGD', name: '新加坡元' },
|
||||
{ flag: '🇦🇺', code: 'AUD', name: '澳元' },
|
||||
{ flag: '🇨🇦', code: 'CAD', name: '加元' },
|
||||
{ flag: '🇳🇿', code: 'NZD', name: '新西兰元' },
|
||||
{ flag: '🇹🇭', code: 'THB', name: '泰铢' },
|
||||
{ flag: '🇲🇾', code: 'MYR', name: '马来西亚林吉特' },
|
||||
{ flag: '🇵🇭', code: 'PHP', name: '菲律宾比索' },
|
||||
{ flag: '🇮🇩', code: 'IDR', name: '印尼盾' },
|
||||
{ flag: '🇻🇳', code: 'VND', name: '越南盾' },
|
||||
{ flag: '🇮🇳', code: 'INR', name: '印度卢比' },
|
||||
{ flag: '🇷🇺', code: 'RUB', name: '俄罗斯卢布' },
|
||||
{ flag: '🇧🇷', code: 'BRL', name: '巴西雷亚尔' },
|
||||
{ flag: '🇲🇽', code: 'MXN', name: '墨西哥比索' },
|
||||
{ flag: '🇿🇦', code: 'ZAR', name: '南非兰特' },
|
||||
{ flag: '🇹🇷', code: 'TRY', name: '土耳其里拉' },
|
||||
{ flag: '🇸🇦', code: 'SAR', name: '沙特里亚尔' },
|
||||
{ flag: '🇦🇪', code: 'AED', name: '阿联酋迪拉姆' },
|
||||
{ flag: '🇨🇭', code: 'CHF', name: '瑞士法郎' },
|
||||
{ flag: '🇸🇪', code: 'SEK', name: '瑞典克朗' },
|
||||
{ flag: '🇩🇰', code: 'DKK', name: '丹麦克朗' },
|
||||
{ flag: '🇳🇴', code: 'NOK', name: '挪威克朗' },
|
||||
];
|
||||
|
||||
const CURRENCY_MAP = {};
|
||||
CURRENCIES.forEach((c) => {
|
||||
CURRENCY_MAP[c.code] = c;
|
||||
});
|
||||
|
||||
function getCurrency(code) {
|
||||
return CURRENCY_MAP[code] || null;
|
||||
}
|
||||
|
||||
function getCurrencyLabel(code) {
|
||||
const c = CURRENCY_MAP[code];
|
||||
if (!c) return code;
|
||||
return `${c.flag} ${c.code} ${c.name}`;
|
||||
}
|
||||
|
||||
function getPickerData() {
|
||||
return CURRENCIES.map((c) => `${c.flag} ${c.code} ${c.name}`);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
CURRENCIES,
|
||||
CURRENCY_MAP,
|
||||
getCurrency,
|
||||
getCurrencyLabel,
|
||||
getPickerData,
|
||||
};
|
||||
130
miniprogram/utils/rate.js
Normal file
130
miniprogram/utils/rate.js
Normal file
@@ -0,0 +1,130 @@
|
||||
/**
|
||||
* 汇率 API 调用 + 本地缓存
|
||||
* 数据源:https://open.er-api.com/v6/latest/{base}
|
||||
* 免费,无需 key,每日更新,1500 次/月
|
||||
*/
|
||||
|
||||
const CACHE_KEY = 'exchange_rates';
|
||||
const CACHE_TTL = 4 * 60 * 60 * 1000; // 4 小时
|
||||
|
||||
function getCache() {
|
||||
try {
|
||||
const data = wx.getStorageSync(CACHE_KEY);
|
||||
if (!data) return null;
|
||||
return data;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function setCache(data) {
|
||||
try {
|
||||
wx.setStorageSync(CACHE_KEY, data);
|
||||
} catch (e) {
|
||||
console.warn('cache write failed', e);
|
||||
}
|
||||
}
|
||||
|
||||
function isCacheValid(cache, base) {
|
||||
if (!cache || !cache.rates || !cache.timestamp) return false;
|
||||
if (cache.base !== base) return false;
|
||||
return Date.now() - cache.timestamp < CACHE_TTL;
|
||||
}
|
||||
|
||||
function fetchRates(base) {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: `https://open.er-api.com/v6/latest/${base}`,
|
||||
method: 'GET',
|
||||
success(res) {
|
||||
if (res.statusCode === 200 && res.data && res.data.result === 'success') {
|
||||
resolve(res.data.rates);
|
||||
} else {
|
||||
reject(new Error('API 返回异常'));
|
||||
}
|
||||
},
|
||||
fail(err) {
|
||||
reject(err);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function getRates(base) {
|
||||
const cache = getCache();
|
||||
|
||||
if (isCacheValid(cache, base)) {
|
||||
return {
|
||||
rates: cache.rates,
|
||||
updatedAt: cache.updatedAt,
|
||||
fromCache: true,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const rates = await fetchRates(base);
|
||||
const now = new Date();
|
||||
const cacheData = {
|
||||
base,
|
||||
rates,
|
||||
timestamp: now.getTime(),
|
||||
updatedAt: formatTime(now),
|
||||
};
|
||||
setCache(cacheData);
|
||||
return {
|
||||
rates,
|
||||
updatedAt: cacheData.updatedAt,
|
||||
fromCache: false,
|
||||
};
|
||||
} catch (e) {
|
||||
// API 失败时用过期缓存兜底
|
||||
if (cache && cache.rates) {
|
||||
return {
|
||||
rates: cache.rates,
|
||||
updatedAt: cache.updatedAt + '(缓存)',
|
||||
fromCache: true,
|
||||
};
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
function convert(amount, fromCode, toCode, rates) {
|
||||
if (!rates || !rates[fromCode] || !rates[toCode]) return 0;
|
||||
if (fromCode === toCode) return amount;
|
||||
return (amount / rates[fromCode]) * rates[toCode];
|
||||
}
|
||||
|
||||
function formatAmount(value, code) {
|
||||
// 日元、韩元、越南盾、印尼盾等无小数货币
|
||||
const noDecimal = ['JPY', 'KRW', 'VND', 'IDR'];
|
||||
if (noDecimal.includes(code)) {
|
||||
return Math.round(value).toLocaleString('en-US');
|
||||
}
|
||||
return Number(value.toFixed(2)).toLocaleString('en-US', {
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
});
|
||||
}
|
||||
|
||||
function formatRate(value) {
|
||||
if (value >= 100) return value.toFixed(2);
|
||||
if (value >= 1) return value.toFixed(4);
|
||||
return value.toFixed(6);
|
||||
}
|
||||
|
||||
function formatTime(date) {
|
||||
const y = date.getFullYear();
|
||||
const m = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const d = String(date.getDate()).padStart(2, '0');
|
||||
const h = String(date.getHours()).padStart(2, '0');
|
||||
const min = String(date.getMinutes()).padStart(2, '0');
|
||||
return `${y}-${m}-${d} ${h}:${min}`;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getRates,
|
||||
convert,
|
||||
formatAmount,
|
||||
formatRate,
|
||||
};
|
||||
Reference in New Issue
Block a user