-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
69 lines (57 loc) · 2.76 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//const exchangeRateApiKey = 'yuokey';
//const exchangeRateApiUrl = `https://v6.exchangerate-api.com/v6/${exchangeRateApiKey}/latest/CNY`;
const exchangeRateApiUrl = `https://api.exchangerate-api.com/v4/latest/CNY`;
let exchangeRates = {};
async function fetchExchangeRates() {
try {
const response = await fetch(exchangeRateApiUrl);
const data = await response.json();
//const cnyRates = data.conversion_rates;
const cnyRates = data.rates;
exchangeRates = {
"USD": 1 / cnyRates["USD"],
"EUR": 1 / cnyRates["EUR"],
"GBP": 1 / cnyRates["GBP"],
"CAD": 1 / cnyRates["CAD"],
"JPY": 1 / cnyRates["JPY"],
"KRW": 1 / cnyRates["KRW"],
"HKD": 1 / cnyRates["HKD"],
"TWD": 1 / cnyRates["TWD"]
};
updateExchangeRate();
} catch (error) {
console.error('Failed to fetch exchange rates:', error);
}
}
// 获取当前日期
var currentDate = new Date().toISOString().split('T')[0];
// 设置input的默认值为当前日期
document.getElementById('purchaseDate').value = currentDate;
function updateExchangeRate() {
const currency = document.getElementById('currency').value;
const exchangeRate = exchangeRates[currency];
document.getElementById('exchangeRate').value = exchangeRate ? exchangeRate.toFixed(4) : '';
}
function updateExchangeRate() {
const currency = document.getElementById('currency').value;
const exchangeRate = exchangeRates[currency];
document.getElementById('exchangeRate').value = exchangeRate ? exchangeRate.toFixed(4) : '';
}
function calculate() {
const purchaseAmount = parseFloat(document.getElementById('purchaseAmount').value);
const renewalAmount = parseFloat(document.getElementById('renewalAmount').value);
const exchangeRate = parseFloat(document.getElementById('exchangeRate').value);
const paymentPeriod = parseInt(document.getElementById('paymentPeriod').value);
const purchaseDate = new Date(document.getElementById('purchaseDate').value);
const expiryDate = new Date(document.getElementById('expiryDate').value);
const currentDate = new Date();
const remainingMonths = Math.max(0, (expiryDate.getFullYear() - currentDate.getFullYear()) * 12 + (expiryDate.getMonth() - currentDate.getMonth()));
const remainingValue = (renewalAmount * exchangeRate / paymentPeriod) * remainingMonths;
const overchargeAmount = purchaseAmount - remainingValue;
document.getElementById('remainingValue').textContent = remainingValue.toFixed(2);
document.getElementById('overchargeAmount').textContent = overchargeAmount.toFixed(2);
}
document.addEventListener('DOMContentLoaded', () => {
fetchExchangeRates();
updateExchangeRate();
});