goodsInfo.vue
7.71 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<!--重量体积计算-->
<template>
<view class="boxBg">
<view class="goodsCon">
<view class="item">
<text>预估重量</text>
<view class="bg goodInfo">
<view
class="symbol"
:class="isLessThan ? 'active' : ''"
@click="handleMinus"
>-</view
>
<view class="num">
<input
class="uni-input"
type="number"
maxlength="6"
v-model="weight"
@blur="handleSymbol"
/>
<text>kg</text>
</view>
<view
class="symbol"
:class="isExceed ? 'active' : ''"
@click="handleAdd"
>+</view
>
</view>
</view>
<view class="item">
<text>总体积</text>
<view class="bg goodInfo">
<!-- 暂时去除 :class="isLessThanVolume ? 'active' : ''" -->
<view class="symbol" @click="handleVolumeMinus">-</view>
<view class="num">
<input
class="uni-input"
type="number"
maxlength="6"
v-model="volume"
@blur="handleVolume"
/>
<text>m³</text>
</view>
<!-- 暂时去除 :class="isExceedVolume ? 'active' : ''" -->
<view class="symbol" @click="handleVolumeAdd">+</view>
</view>
</view>
<view class="item calculate">
<view class="bg">
<input
class="uni-input"
type="number"
maxlength="3"
v-model="measureLong"
placeholder="长"
@input="handleCalculate"
/>
<text :class="measureLong ? 'active' : ''">cm</text>
</view>
<text>*</text>
<view class="bg">
<input
class="uni-input"
type="number"
maxlength="3"
v-model="measureWidth"
placeholder="宽"
@input="handleCalculate"
/>
<text :class="measureWidth ? 'active' : ''">cm</text>
</view>
<text>*</text>
<view class="bg">
<input
class="uni-input"
type="number"
maxlength="3"
v-model="measureHigh"
placeholder="高"
@input="handleCalculate"
/>
<text :class="measureHigh ? 'active' : ''">cm</text>
</view>
</view>
</view>
</view>
</template>
<script setup>
import {nextTick, ref, watch} from "vue";
// ------定义变量------
let weight = ref(1); //重量
let volume = ref(0); //体积
let measureLong = ref(null); //长
let measureWidth = ref(null); //宽
let measureHigh = ref(null); //高
let isLessThan = ref(true); //判断重量是否小于0.1
let isExceed = ref(false); //判断重量是否大于9999
let isLessThanVolume = ref(true); //判断体积是否小于0.0001m³
let isExceedVolume = ref(false); //判断体积是否大于99m³
// 暴漏给父组件
defineExpose({
weight,
volume,
measureLong,
measureWidth,
measureHigh,
});
// ------生命周期------
// 监听重量数值,小数点后保留一位
watch(weight, (newValue, oldValue) => {
const val = Number(newValue);
nextTick(() => {
// 数值小于0.1并且大于0 数值默认为1
if (val < 0.1 && val > 0) {
weight.value = 1;
}
// 处理小数点,小数点保留1位
if (val > 0.1) {
weight.value = parseInt(val * 10) / 10;
}
// 数值小于等于1 左侧按钮置灰
if (val <= 1) {
isLessThan.value = true; //左侧减号置灰
} else {
isLessThan.value = false; //左侧减号去除置灰
if (val >= 9999) {
weight.value = 9999;
isExceed.value = true; //右侧加号置灰
} else {
isExceed.value = false; //右侧加号去除置灰
}
}
});
});
// 监听长,取整
watch(measureLong, (newValue, oldValue) => {
const val = Number(newValue);
nextTick(() => {
measureLong.value = Math.floor(val);
if (newValue <= 0) {
measureLong.value = null;
}
});
});
// 监听宽,取整
watch(measureWidth, (newValue, oldValue) => {
const val = Number(newValue);
nextTick(() => {
measureWidth.value = Math.floor(val);
if (newValue <= 0) {
measureWidth.value = null;
}
});
});
// 监听高,取整
watch(measureHigh, (newValue, oldValue) => {
const val = Number(newValue);
nextTick(() => {
measureHigh.value = Math.floor(val);
if (newValue <= 0) {
measureHigh.value = null;
}
});
});
// ------定义方法------
//触发重量输入如果输入0,自动判断为1kg,最小可输入值为0.1kg,最大值为9999kg
const handleSymbol = (e) => {
const value = e.detail.value;
if (value < 0.1) {
weight.value = 1;
isLessThan.value = true; //左侧减号置灰
} else {
if (value > 0.1 && value <= 1) {
isLessThan.value = true;
} else {
isLessThan.value = false; //左侧减号去除置灰
}
if (value >= 9999) {
isExceed.value = true; //右侧加号置灰
weight.value = 9999;
} else {
isExceed.value = false; //右侧加号去除置灰
}
}
};
// 减重量
const handleMinus = () => {
// 重量减去1
if (weight.value > 1) {
weight.value--;
isExceed.value = false; //右侧加号去除置灰
weight.value = weight.value.toFixed(1);
}
if (weight.value <= 0) {
weight.value = 1;
isLessThan.value = true; //左侧减号置灰
}
};
// 加重量
const handleAdd = () => {
// 重量加1
if (weight.value < 9999) {
++weight.value;
isLessThan.value = false; //左侧减号去除置灰
}
if (weight.value === 9999) {
isExceed.value = true; //右侧加号置灰
}
};
// 体积
const handleVolume = (e) => {
const value = Number(e.detail.value);
if (value < 0.0001) {
volume.value = 0;
} else {
if (value > 99) {
volume.value = 99;
return uni.showToast({
title: "体积最大可不能超过99m³",
duration: 1000,
icon: "none",
});
}
}
};
// 减体积
const handleVolumeMinus = () => {
// 体积减去1
if (volume.value > 1) {
volume.value--;
volume.value = parseInt(volume.value * 10000) / 10000;
}
// 体积
if (volume.value <= 0 || volume.value === 1) {
volume.value = 0;
}
};
// 加体积
const handleVolumeAdd = () => {
// 体积加1
if (volume.value < 99) {
++volume.value;
isLessThanVolume.value = false; //左侧减号去除置灰
}
if (volume.value === 99) {
isExceedVolume.value = true; //右侧加号置灰
}
};
// 计算立方米
const handleCalculate = () => {
const long = measureLong.value; //长
const wide = measureWidth.value; //宽
const height = measureHigh.value; //高
// 长宽高都大于1才可以计算
if (long >= 1 && wide >= 1 && height >= 1) {
nextTick(() => {
// 计算立方米:长/100*宽/100*高/100
let val = (long / 100) * (wide / 100) * (height / 100);
// 立方米必须大于0.0001
if (val < 0.0001) {
volume.value = 0;
} else if (val > 99) {
// 小数点后保留四位小数
isExceedVolume.value = true; //右侧加号置灰
volume.value = 99;
return uni.showToast({
title: "体积最大可不能超过99m³",
duration: 1000,
icon: "none",
});
} else {
volume.value = parseInt(val * 10000) / 10000;
// 如果体积大于1左侧减号按钮去除置灰
if (val > 1) {
isLessThanVolume.value = false; //左侧减号去除置灰
} else {
isLessThanVolume.value = true; //左侧减号置灰
}
isExceedVolume.value = false; //右侧加号去除置灰
}
});
}
};
</script>