增加小程序,修改游戏前端

This commit is contained in:
2026-02-04 17:29:51 +08:00
parent de17737ca1
commit 8c677908d7
835 changed files with 283328 additions and 2165 deletions

View File

@@ -0,0 +1,4 @@
{
"usingComponents": {},
"navigationBarTitleText": "单位换算"
}

View File

@@ -0,0 +1,171 @@
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)
});
},
formatResult(val: number): string {
if (Math.abs(val) < 0.000001 || Math.abs(val) > 10000000) {
return val.toExponential(4);
}
return parseFloat(val.toPrecision(6)).toString();
}
});

View File

@@ -0,0 +1,31 @@
<view class="container">
<view class="section">
<picker bindchange="bindCategoryChange" value="{{categoryIndex}}" range="{{categories}}">
<view class="picker">
当前分类:{{categories[categoryIndex]}}
</view>
</picker>
</view>
<view class="converter-box">
<view class="input-group">
<input type="digit" placeholder="输入数值" bindinput="bindInput" value="{{inputValue}}" />
<picker bindchange="bindFromUnitChange" value="{{fromIndex}}" range="{{currentUnits}}" range-key="name">
<view class="unit-picker">
{{currentUnits[fromIndex].name}}
</view>
</picker>
</view>
<view class="separator">=</view>
<view class="input-group">
<view class="result-display">{{outputValue}}</view>
<picker bindchange="bindToUnitChange" value="{{toIndex}}" range="{{currentUnits}}" range-key="name">
<view class="unit-picker">
{{currentUnits[toIndex].name}}
</view>
</picker>
</view>
</view>
</view>

View File

@@ -0,0 +1,56 @@
.container {
padding: 20px;
}
.section {
background: #f0f0f0;
border-radius: 8px;
margin-bottom: 20px;
padding: 10px;
text-align: center;
}
.picker {
font-size: 18px;
color: #333;
}
.converter-box {
display: flex;
flex-direction: column;
gap: 20px;
}
.input-group {
display: flex;
align-items: center;
border: 1px solid #ddd;
border-radius: 8px;
padding: 10px;
background: #fff;
}
.input-group input, .result-display {
flex: 1;
font-size: 24px;
height: 40px;
line-height: 40px;
}
.result-display {
min-height: 40px;
}
.unit-picker {
margin-left: 10px;
padding: 5px 10px;
background: #eee;
border-radius: 4px;
font-size: 16px;
}
.separator {
text-align: center;
font-size: 30px;
color: #999;
}