feat: 汇率换算功能完整实现
- 首页双向换算:30种主流货币,实时计算,一键互换 - 汇率总览页:一对多查看所有货币换算结果 - 数据源 open.er-api.com,4小时本地缓存 + 失败兜底 - UI 美化:渐变背景、分色货币块、紫色互换按钮 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,106 @@
|
||||
const { CURRENCIES, getPickerData } = require('../../utils/currencies');
|
||||
const { getRates, convert, formatAmount, formatRate } = require('../../utils/rate');
|
||||
|
||||
const pickerData = getPickerData();
|
||||
const DEFAULT_FROM = 0; // CNY
|
||||
const DEFAULT_TO = 1; // USD
|
||||
|
||||
Page({
|
||||
data: {},
|
||||
data: {
|
||||
pickerData,
|
||||
fromIndex: DEFAULT_FROM,
|
||||
toIndex: DEFAULT_TO,
|
||||
amount: '100',
|
||||
result: '',
|
||||
unitRate: '',
|
||||
reverseRate: '',
|
||||
updatedAt: '',
|
||||
loading: true,
|
||||
error: '',
|
||||
rates: null,
|
||||
baseCurrency: 'CNY',
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
console.log('Index page loaded');
|
||||
this.loadRates();
|
||||
},
|
||||
|
||||
onPullDownRefresh() {
|
||||
this.loadRates().then(() => {
|
||||
wx.stopPullDownRefresh();
|
||||
});
|
||||
},
|
||||
|
||||
async loadRates() {
|
||||
this.setData({ loading: true, error: '' });
|
||||
try {
|
||||
const { rates, updatedAt } = await getRates('CNY');
|
||||
this.setData({
|
||||
rates,
|
||||
baseCurrency: 'CNY',
|
||||
updatedAt,
|
||||
loading: false,
|
||||
});
|
||||
this.calculate();
|
||||
} catch (e) {
|
||||
this.setData({
|
||||
loading: false,
|
||||
error: '汇率获取失败,请检查网络后下拉刷新',
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
calculate() {
|
||||
const { rates, fromIndex, toIndex, amount, baseCurrency } = this.data;
|
||||
if (!rates) return;
|
||||
|
||||
const num = parseFloat(amount);
|
||||
if (isNaN(num) || num <= 0) {
|
||||
this.setData({ result: '', unitRate: '', reverseRate: '' });
|
||||
return;
|
||||
}
|
||||
|
||||
const fromCode = CURRENCIES[fromIndex].code;
|
||||
const toCode = CURRENCIES[toIndex].code;
|
||||
const converted = convert(num, fromCode, toCode, rates);
|
||||
const unitForward = convert(1, fromCode, toCode, rates);
|
||||
const unitReverse = convert(1, toCode, fromCode, rates);
|
||||
|
||||
this.setData({
|
||||
result: formatAmount(converted, toCode),
|
||||
unitRate: `1 ${fromCode} = ${formatRate(unitForward)} ${toCode}`,
|
||||
reverseRate: `1 ${toCode} = ${formatRate(unitReverse)} ${fromCode}`,
|
||||
});
|
||||
},
|
||||
|
||||
handleAmountInput(e) {
|
||||
const value = e.detail.value;
|
||||
this.setData({ amount: value });
|
||||
this.calculate();
|
||||
},
|
||||
|
||||
handleFromChange(e) {
|
||||
this.setData({ fromIndex: Number(e.detail.value) });
|
||||
this.calculate();
|
||||
},
|
||||
|
||||
handleToChange(e) {
|
||||
this.setData({ toIndex: Number(e.detail.value) });
|
||||
this.calculate();
|
||||
},
|
||||
|
||||
handleSwap() {
|
||||
const { fromIndex, toIndex } = this.data;
|
||||
this.setData({
|
||||
fromIndex: toIndex,
|
||||
toIndex: fromIndex,
|
||||
});
|
||||
this.calculate();
|
||||
},
|
||||
|
||||
goOverview() {
|
||||
wx.navigateTo({
|
||||
url: '/pages/overview/overview',
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
{
|
||||
"navigationBarTitleText": "汇率换算",
|
||||
"enablePullDownRefresh": true,
|
||||
"usingComponents": {}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,76 @@
|
||||
<view class="container">
|
||||
<view class="page-title">汇率换算</view>
|
||||
<view class="page-subtitle">实时汇率,主流货币换算</view>
|
||||
<!-- 顶部标题区 -->
|
||||
<view class="hero">
|
||||
<view class="hero-title">💱 汇率换算</view>
|
||||
<view class="hero-desc" wx:if="{{updatedAt}}">{{updatedAt}} 更新</view>
|
||||
<view class="hero-desc" wx:if="{{loading}}">正在获取最新汇率...</view>
|
||||
</view>
|
||||
|
||||
<view class="card">
|
||||
<view class="placeholder-text">功能开发中</view>
|
||||
<!-- 换算主卡片 -->
|
||||
<view class="card converter-card">
|
||||
<!-- 源币种 -->
|
||||
<view class="currency-block from-block">
|
||||
<picker range="{{pickerData}}" value="{{fromIndex}}" bindchange="handleFromChange">
|
||||
<view class="currency-tag">
|
||||
<text class="currency-tag-text">{{pickerData[fromIndex]}}</text>
|
||||
<text class="tag-arrow">▼</text>
|
||||
</view>
|
||||
</picker>
|
||||
<input
|
||||
class="amount-input"
|
||||
type="digit"
|
||||
value="{{amount}}"
|
||||
bindinput="handleAmountInput"
|
||||
placeholder="输入金额"
|
||||
placeholder-class="amount-placeholder"
|
||||
/>
|
||||
</view>
|
||||
|
||||
<!-- 互换按钮 -->
|
||||
<view class="swap-wrapper">
|
||||
<view class="swap-line"></view>
|
||||
<view class="swap-btn" bindtap="handleSwap">
|
||||
<text class="swap-icon">⇅</text>
|
||||
</view>
|
||||
<view class="swap-line"></view>
|
||||
</view>
|
||||
|
||||
<!-- 目标币种 -->
|
||||
<view class="currency-block to-block">
|
||||
<picker range="{{pickerData}}" value="{{toIndex}}" bindchange="handleToChange">
|
||||
<view class="currency-tag">
|
||||
<text class="currency-tag-text">{{pickerData[toIndex]}}</text>
|
||||
<text class="tag-arrow">▼</text>
|
||||
</view>
|
||||
</picker>
|
||||
<view class="result-display">{{result || '—'}}</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 单位汇率卡片 -->
|
||||
<view class="card rate-card" wx:if="{{unitRate}}">
|
||||
<view class="rate-row">
|
||||
<text class="rate-dot from-dot"></text>
|
||||
<text class="rate-text">{{unitRate}}</text>
|
||||
</view>
|
||||
<view class="rate-row">
|
||||
<text class="rate-dot to-dot"></text>
|
||||
<text class="rate-text">{{reverseRate}}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 错误提示 -->
|
||||
<view class="card error-card" wx:if="{{error}}">
|
||||
<text class="error-icon">⚠</text>
|
||||
<text class="error-text">{{error}}</text>
|
||||
</view>
|
||||
|
||||
<!-- 汇率总览入口 -->
|
||||
<view class="card overview-entry" bindtap="goOverview">
|
||||
<view class="overview-left">
|
||||
<text class="overview-icon">📊</text>
|
||||
<text class="overview-label">查看汇率总览</text>
|
||||
</view>
|
||||
<text class="overview-arrow">›</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
@@ -1,6 +1,192 @@
|
||||
.placeholder-text {
|
||||
color: #999999;
|
||||
font-size: 28rpx;
|
||||
text-align: center;
|
||||
padding: 48rpx 0;
|
||||
/* ===== 顶部标题 ===== */
|
||||
.hero {
|
||||
padding: 16rpx 8rpx 24rpx;
|
||||
}
|
||||
|
||||
.hero-title {
|
||||
font-size: 48rpx;
|
||||
font-weight: 800;
|
||||
color: #1E293B;
|
||||
letter-spacing: 2rpx;
|
||||
}
|
||||
|
||||
.hero-desc {
|
||||
font-size: 24rpx;
|
||||
color: #94A3B8;
|
||||
margin-top: 8rpx;
|
||||
}
|
||||
|
||||
/* ===== 换算主卡片 ===== */
|
||||
.converter-card {
|
||||
padding: 40rpx 36rpx;
|
||||
}
|
||||
|
||||
.currency-block {
|
||||
border-radius: 20rpx;
|
||||
padding: 28rpx;
|
||||
}
|
||||
|
||||
.from-block {
|
||||
background: #F8FAFC;
|
||||
border: 2rpx solid #E2E8F0;
|
||||
}
|
||||
|
||||
.to-block {
|
||||
background: linear-gradient(135deg, #EEF2FF 0%, #E0E7FF 100%);
|
||||
border: 2rpx solid #C7D2FE;
|
||||
}
|
||||
|
||||
.currency-tag {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
background: #FFFFFF;
|
||||
border-radius: 12rpx;
|
||||
padding: 10rpx 20rpx;
|
||||
box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
|
||||
}
|
||||
|
||||
.currency-tag-text {
|
||||
font-size: 26rpx;
|
||||
font-weight: 500;
|
||||
color: #334155;
|
||||
}
|
||||
|
||||
.tag-arrow {
|
||||
font-size: 18rpx;
|
||||
color: #94A3B8;
|
||||
margin-left: 10rpx;
|
||||
}
|
||||
|
||||
.amount-input {
|
||||
width: 100%;
|
||||
margin-top: 20rpx;
|
||||
font-size: 56rpx;
|
||||
font-weight: 800;
|
||||
color: #1E293B;
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.amount-placeholder {
|
||||
color: #CBD5E1;
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
.result-display {
|
||||
margin-top: 20rpx;
|
||||
font-size: 56rpx;
|
||||
font-weight: 800;
|
||||
color: #4F46E5;
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
/* ===== 互换按钮 ===== */
|
||||
.swap-wrapper {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 12rpx 0;
|
||||
}
|
||||
|
||||
.swap-line {
|
||||
flex: 1;
|
||||
height: 2rpx;
|
||||
background: #E2E8F0;
|
||||
}
|
||||
|
||||
.swap-btn {
|
||||
width: 80rpx;
|
||||
height: 80rpx;
|
||||
border-radius: 50%;
|
||||
background: #4F46E5;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
margin: 0 24rpx;
|
||||
box-shadow: 0 6rpx 20rpx rgba(79, 70, 229, 0.3);
|
||||
}
|
||||
|
||||
.swap-icon {
|
||||
font-size: 36rpx;
|
||||
color: #FFFFFF;
|
||||
}
|
||||
|
||||
/* ===== 单位汇率 ===== */
|
||||
.rate-card {
|
||||
padding: 28rpx 36rpx;
|
||||
}
|
||||
|
||||
.rate-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 8rpx 0;
|
||||
}
|
||||
|
||||
.rate-dot {
|
||||
width: 14rpx;
|
||||
height: 14rpx;
|
||||
border-radius: 50%;
|
||||
margin-right: 16rpx;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.from-dot {
|
||||
background: #64748B;
|
||||
}
|
||||
|
||||
.to-dot {
|
||||
background: #4F46E5;
|
||||
}
|
||||
|
||||
.rate-text {
|
||||
font-size: 26rpx;
|
||||
color: #64748B;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
/* ===== 错误提示 ===== */
|
||||
.error-card {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 28rpx 36rpx;
|
||||
background: #FEF2F2;
|
||||
}
|
||||
|
||||
.error-icon {
|
||||
font-size: 32rpx;
|
||||
margin-right: 16rpx;
|
||||
}
|
||||
|
||||
.error-text {
|
||||
font-size: 26rpx;
|
||||
color: #DC2626;
|
||||
}
|
||||
|
||||
/* ===== 汇率总览入口 ===== */
|
||||
.overview-entry {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 36rpx;
|
||||
}
|
||||
|
||||
.overview-left {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.overview-icon {
|
||||
font-size: 36rpx;
|
||||
margin-right: 16rpx;
|
||||
}
|
||||
|
||||
.overview-label {
|
||||
font-size: 30rpx;
|
||||
font-weight: 500;
|
||||
color: #334155;
|
||||
}
|
||||
|
||||
.overview-arrow {
|
||||
font-size: 40rpx;
|
||||
color: #CBD5E1;
|
||||
font-weight: 300;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,84 @@
|
||||
const { CURRENCIES, getPickerData } = require('../../utils/currencies');
|
||||
const { getRates, convert, formatAmount, formatRate } = require('../../utils/rate');
|
||||
|
||||
const pickerData = getPickerData();
|
||||
|
||||
Page({
|
||||
data: {},
|
||||
data: {
|
||||
pickerData,
|
||||
baseIndex: 0, // CNY
|
||||
amount: '100',
|
||||
list: [],
|
||||
updatedAt: '',
|
||||
loading: true,
|
||||
error: '',
|
||||
rates: null,
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
console.log('Overview page loaded');
|
||||
this.loadRates();
|
||||
},
|
||||
|
||||
onPullDownRefresh() {
|
||||
this.loadRates().then(() => {
|
||||
wx.stopPullDownRefresh();
|
||||
});
|
||||
},
|
||||
|
||||
async loadRates() {
|
||||
this.setData({ loading: true, error: '' });
|
||||
try {
|
||||
const baseCode = CURRENCIES[this.data.baseIndex].code;
|
||||
const { rates, updatedAt } = await getRates(baseCode);
|
||||
this.setData({
|
||||
rates,
|
||||
updatedAt,
|
||||
loading: false,
|
||||
});
|
||||
this.buildList();
|
||||
} catch (e) {
|
||||
this.setData({
|
||||
loading: false,
|
||||
error: '汇率获取失败,请检查网络后下拉刷新',
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
buildList() {
|
||||
const { rates, baseIndex, amount } = this.data;
|
||||
if (!rates) return;
|
||||
|
||||
const num = parseFloat(amount);
|
||||
if (isNaN(num) || num <= 0) {
|
||||
this.setData({ list: [] });
|
||||
return;
|
||||
}
|
||||
|
||||
const baseCode = CURRENCIES[baseIndex].code;
|
||||
const list = CURRENCIES
|
||||
.filter((c) => c.code !== baseCode)
|
||||
.map((c) => {
|
||||
const converted = convert(num, baseCode, c.code, rates);
|
||||
const unitRate = convert(1, baseCode, c.code, rates);
|
||||
return {
|
||||
flag: c.flag,
|
||||
code: c.code,
|
||||
name: c.name,
|
||||
converted: formatAmount(converted, c.code),
|
||||
unitRate: formatRate(unitRate),
|
||||
};
|
||||
});
|
||||
|
||||
this.setData({ list });
|
||||
},
|
||||
|
||||
handleBaseChange(e) {
|
||||
this.setData({ baseIndex: Number(e.detail.value) });
|
||||
this.loadRates();
|
||||
},
|
||||
|
||||
handleAmountInput(e) {
|
||||
this.setData({ amount: e.detail.value });
|
||||
this.buildList();
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"navigationBarTitleText": "汇率总览",
|
||||
"enablePullDownRefresh": true,
|
||||
"usingComponents": {}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,64 @@
|
||||
<view class="container">
|
||||
<view class="page-title">汇率总览</view>
|
||||
<view class="page-subtitle">一个基准货币对所有主流货币</view>
|
||||
<!-- 顶部控制区 -->
|
||||
<view class="card header-card">
|
||||
<view class="header-top">
|
||||
<text class="header-title">📊 汇率总览</text>
|
||||
</view>
|
||||
|
||||
<view class="card">
|
||||
<view class="placeholder-text">功能开发中</view>
|
||||
<view class="control-row">
|
||||
<text class="control-label">基准</text>
|
||||
<picker range="{{pickerData}}" value="{{baseIndex}}" bindchange="handleBaseChange">
|
||||
<view class="base-tag">
|
||||
<text class="base-tag-text">{{pickerData[baseIndex]}}</text>
|
||||
<text class="tag-arrow">▼</text>
|
||||
</view>
|
||||
</picker>
|
||||
</view>
|
||||
|
||||
<view class="control-row">
|
||||
<text class="control-label">金额</text>
|
||||
<input
|
||||
class="amount-input"
|
||||
type="digit"
|
||||
value="{{amount}}"
|
||||
bindinput="handleAmountInput"
|
||||
placeholder="输入金额"
|
||||
placeholder-class="amount-placeholder"
|
||||
/>
|
||||
</view>
|
||||
|
||||
<view class="updated-at" wx:if="{{updatedAt}}">{{updatedAt}} 更新</view>
|
||||
</view>
|
||||
|
||||
<!-- 错误提示 -->
|
||||
<view class="card error-card" wx:if="{{error}}">
|
||||
<text class="error-icon">⚠</text>
|
||||
<text class="error-text">{{error}}</text>
|
||||
</view>
|
||||
|
||||
<!-- 加载中 -->
|
||||
<view class="loading-card" wx:if="{{loading}}">
|
||||
<text>正在获取汇率数据...</text>
|
||||
</view>
|
||||
|
||||
<!-- 汇率列表 -->
|
||||
<view class="card list-card" wx:if="{{list.length}}">
|
||||
<view
|
||||
class="rate-item"
|
||||
wx:for="{{list}}"
|
||||
wx:key="code"
|
||||
>
|
||||
<view class="rate-left">
|
||||
<text class="rate-flag">{{item.flag}}</text>
|
||||
<view class="rate-info">
|
||||
<text class="rate-code">{{item.code}}</text>
|
||||
<text class="rate-name">{{item.name}}</text>
|
||||
</view>
|
||||
</view>
|
||||
<view class="rate-right">
|
||||
<text class="rate-value">{{item.converted}}</text>
|
||||
<text class="rate-unit">1 = {{item.unitRate}}</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
@@ -1,6 +1,157 @@
|
||||
.placeholder-text {
|
||||
color: #999999;
|
||||
font-size: 28rpx;
|
||||
text-align: center;
|
||||
padding: 48rpx 0;
|
||||
/* ===== 顶部控制区 ===== */
|
||||
.header-card {
|
||||
padding: 36rpx;
|
||||
}
|
||||
|
||||
.header-top {
|
||||
margin-bottom: 28rpx;
|
||||
}
|
||||
|
||||
.header-title {
|
||||
font-size: 40rpx;
|
||||
font-weight: 800;
|
||||
color: #1E293B;
|
||||
}
|
||||
|
||||
.control-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 20rpx 0;
|
||||
border-top: 2rpx solid #F1F5F9;
|
||||
}
|
||||
|
||||
.control-label {
|
||||
font-size: 28rpx;
|
||||
color: #64748B;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.base-tag {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
background: #F8FAFC;
|
||||
border: 2rpx solid #E2E8F0;
|
||||
border-radius: 12rpx;
|
||||
padding: 10rpx 20rpx;
|
||||
}
|
||||
|
||||
.base-tag-text {
|
||||
font-size: 26rpx;
|
||||
font-weight: 500;
|
||||
color: #334155;
|
||||
}
|
||||
|
||||
.tag-arrow {
|
||||
font-size: 18rpx;
|
||||
color: #94A3B8;
|
||||
margin-left: 10rpx;
|
||||
}
|
||||
|
||||
.amount-input {
|
||||
flex: 1;
|
||||
text-align: right;
|
||||
font-size: 40rpx;
|
||||
font-weight: 800;
|
||||
color: #1E293B;
|
||||
margin-left: 24rpx;
|
||||
}
|
||||
|
||||
.amount-placeholder {
|
||||
color: #CBD5E1;
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
.updated-at {
|
||||
font-size: 22rpx;
|
||||
color: #94A3B8;
|
||||
margin-top: 16rpx;
|
||||
}
|
||||
|
||||
/* ===== 错误 & 加载 ===== */
|
||||
.error-card {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 28rpx 36rpx;
|
||||
background: #FEF2F2;
|
||||
}
|
||||
|
||||
.error-icon {
|
||||
font-size: 32rpx;
|
||||
margin-right: 16rpx;
|
||||
}
|
||||
|
||||
.error-text {
|
||||
font-size: 26rpx;
|
||||
color: #DC2626;
|
||||
}
|
||||
|
||||
.loading-card {
|
||||
padding: 40rpx;
|
||||
text-align: center;
|
||||
color: #94A3B8;
|
||||
font-size: 26rpx;
|
||||
}
|
||||
|
||||
/* ===== 汇率列表 ===== */
|
||||
.list-card {
|
||||
padding: 8rpx 0;
|
||||
}
|
||||
|
||||
.rate-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 28rpx 36rpx;
|
||||
border-bottom: 2rpx solid #F8FAFC;
|
||||
transition: background 0.15s;
|
||||
}
|
||||
|
||||
.rate-item:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.rate-left {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.rate-flag {
|
||||
font-size: 44rpx;
|
||||
margin-right: 20rpx;
|
||||
}
|
||||
|
||||
.rate-info {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.rate-code {
|
||||
font-size: 30rpx;
|
||||
font-weight: 700;
|
||||
color: #1E293B;
|
||||
}
|
||||
|
||||
.rate-name {
|
||||
font-size: 22rpx;
|
||||
color: #94A3B8;
|
||||
margin-top: 4rpx;
|
||||
}
|
||||
|
||||
.rate-right {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-end;
|
||||
}
|
||||
|
||||
.rate-value {
|
||||
font-size: 32rpx;
|
||||
font-weight: 800;
|
||||
color: #1E293B;
|
||||
}
|
||||
|
||||
.rate-unit {
|
||||
font-size: 22rpx;
|
||||
color: #94A3B8;
|
||||
margin-top: 4rpx;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user