181 lines
4.7 KiB
TypeScript
181 lines
4.7 KiB
TypeScript
|
|
Page({
|
|
data: {
|
|
categories: ['长度', '面积', '体积', '重量', '温度'],
|
|
categoryIndex: 0,
|
|
|
|
units: {
|
|
'长度': [
|
|
{ name: '米', factor: 1 },
|
|
{ name: '千米', factor: 1000 },
|
|
{ name: '分米', factor: 0.1 },
|
|
{ name: '厘米', factor: 0.01 },
|
|
{ name: '毫米', factor: 0.001 },
|
|
{ name: '微米', factor: 0.000001 },
|
|
{ name: '纳米', factor: 0.000000001 },
|
|
{ name: '英里', factor: 1609.344 },
|
|
{ name: '海里', factor: 1852 },
|
|
{ name: '码', factor: 0.9144 },
|
|
{ name: '英尺', factor: 0.3048 },
|
|
{ name: '英寸', factor: 0.0254 }
|
|
],
|
|
'面积': [
|
|
{ name: '平方米', factor: 1 },
|
|
{ name: '平方千米', factor: 1000000 },
|
|
{ name: '公顷', factor: 10000 },
|
|
{ name: '公亩', factor: 100 },
|
|
{ name: '平方英里', factor: 2589988.11 },
|
|
{ name: '英亩', factor: 4046.8564 },
|
|
{ name: '平方码', factor: 0.836127 },
|
|
{ name: '平方英尺', factor: 0.092903 },
|
|
{ name: '平方英寸', factor: 0.000645 }
|
|
],
|
|
'体积': [
|
|
{ name: '立方米', factor: 1 },
|
|
{ name: '立方分米', factor: 0.001 },
|
|
{ name: '立方厘米', factor: 0.000001 },
|
|
{ name: '升', factor: 0.001 },
|
|
{ name: '分升', factor: 0.0001 },
|
|
{ name: '毫升', factor: 0.000001 },
|
|
{ name: '立方英尺', factor: 0.028317 },
|
|
{ name: '立方英寸', factor: 0.000016 },
|
|
{ name: '立方码', factor: 0.764555 }
|
|
],
|
|
'重量': [
|
|
{ name: '千克', factor: 1 },
|
|
{ name: '克', factor: 0.001 },
|
|
{ name: '毫克', factor: 0.000001 },
|
|
{ name: '吨', factor: 1000 },
|
|
{ name: '磅', factor: 0.453592 },
|
|
{ name: '盎司', factor: 0.02835 },
|
|
{ name: '克拉', factor: 0.0002 }
|
|
],
|
|
'温度': [
|
|
{ name: '摄氏度', type: 'C' },
|
|
{ name: '华氏度', type: 'F' },
|
|
{ name: '开氏度', type: 'K' }
|
|
]
|
|
},
|
|
|
|
currentUnits: [] as any[],
|
|
fromIndex: 0,
|
|
toIndex: 1,
|
|
inputValue: '',
|
|
outputValue: ''
|
|
},
|
|
|
|
onLoad() {
|
|
this.updateCurrentUnits();
|
|
},
|
|
|
|
updateCurrentUnits() {
|
|
const category = this.data.categories[this.data.categoryIndex];
|
|
// @ts-ignore
|
|
const units = this.data.units[category];
|
|
this.setData({
|
|
currentUnits: units,
|
|
fromIndex: 0,
|
|
toIndex: 1,
|
|
inputValue: '',
|
|
outputValue: ''
|
|
});
|
|
},
|
|
|
|
bindCategoryChange(e: any) {
|
|
this.setData({
|
|
categoryIndex: e.detail.value
|
|
});
|
|
this.updateCurrentUnits();
|
|
},
|
|
|
|
bindFromUnitChange(e: any) {
|
|
this.setData({
|
|
fromIndex: e.detail.value
|
|
});
|
|
this.calculate();
|
|
},
|
|
|
|
bindToUnitChange(e: any) {
|
|
this.setData({
|
|
toIndex: e.detail.value
|
|
});
|
|
this.calculate();
|
|
},
|
|
|
|
bindInput(e: any) {
|
|
this.setData({
|
|
inputValue: e.detail.value
|
|
});
|
|
this.calculate();
|
|
},
|
|
|
|
calculate() {
|
|
const val = parseFloat(this.data.inputValue);
|
|
if (isNaN(val)) {
|
|
this.setData({ outputValue: '' });
|
|
return;
|
|
}
|
|
|
|
const category = this.data.categories[this.data.categoryIndex];
|
|
if (category === '温度') {
|
|
this.calculateTemperature(val);
|
|
} else {
|
|
this.calculateStandard(val);
|
|
}
|
|
},
|
|
|
|
calculateStandard(val: number) {
|
|
const fromUnit = this.data.currentUnits[this.data.fromIndex];
|
|
const toUnit = this.data.currentUnits[this.data.toIndex];
|
|
|
|
// Convert to base unit then to target unit
|
|
const baseVal = val * fromUnit.factor;
|
|
const result = baseVal / toUnit.factor;
|
|
|
|
this.setData({
|
|
outputValue: this.formatResult(result)
|
|
});
|
|
},
|
|
|
|
calculateTemperature(val: number) {
|
|
const fromUnit = this.data.currentUnits[this.data.fromIndex];
|
|
const toUnit = this.data.currentUnits[this.data.toIndex];
|
|
|
|
let celsius = val;
|
|
// Convert to Celsius first
|
|
if (fromUnit.type === 'F') {
|
|
celsius = (val - 32) * 5 / 9;
|
|
} else if (fromUnit.type === 'K') {
|
|
celsius = val - 273.15;
|
|
}
|
|
|
|
// Convert from Celsius to target
|
|
let result = celsius;
|
|
if (toUnit.type === 'F') {
|
|
result = celsius * 9 / 5 + 32;
|
|
} else if (toUnit.type === 'K') {
|
|
result = celsius + 273.15;
|
|
}
|
|
|
|
this.setData({
|
|
outputValue: this.formatResult(result)
|
|
});
|
|
},
|
|
|
|
swapUnits() {
|
|
const { fromIndex, toIndex } = this.data;
|
|
this.setData({
|
|
fromIndex: toIndex,
|
|
toIndex: fromIndex
|
|
});
|
|
this.calculate();
|
|
},
|
|
|
|
formatResult(val: number): string {
|
|
if (Math.abs(val) < 0.000001 || Math.abs(val) > 10000000) {
|
|
return val.toExponential(4);
|
|
}
|
|
return parseFloat(val.toPrecision(6)).toString();
|
|
}
|
|
});
|