Calculator.vue
5.77 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
<template>
<h1 class="m-auto my-8 text-4xl text-center">能量密度计算器</h1>
<!-- w-[80%] m-auto sm:w-[600px] -->
<div class="border-gray-200 w-[90%] m-auto sm:w-[800px] relative pb-12">
<div v-for="(groupItem, index) in result">
<h2 class="my-4 text-2xl font-bold text-center">
{{ groupNames[index] }}
</h2>
<div v-for="item in groupItem">
<div class="p-2 mb-4 bg-gray-100 rounded-lg shadow-sm">
<h3 class="pl-2 font-bold my-3 text-[16px]">
{{ item.label }}
</h3>
<a-form>
<div class="flex flex-wrap">
<template v-for="(obj, index) in item.obj">
<div class="flex items-center w-full md:w-1/2" v-if="isShow ? isShow : formRef[item.key][obj.key].show">
<span class="inline-block w-[110px] mr-6 text-right text-gray-700 py-2">{{ obj.label }}</span>
<a-input-number v-if="showAdvanced
? obj.type === 'advancedInput' ||
obj.type === 'ordinaryInput'
: obj.type === 'ordinaryInput'
" v-model:value="formRef[item.key][obj.key].value" :min="obj.min" class="w-[60%]"
:precision="formRef[item.key][obj.key].decimal > 0 ? formRef[item.key][obj.key].decimal : 0">
<template #addonAfter>{{ obj.unity }}</template>
</a-input-number>
<template v-else>
{{ obj.value + ' ' + (!!obj.unity ? obj.unity : '') }}
</template>
</div>
</template>
</div>
</a-form>
</div>
</div>
</div>
<div class="fixed left-0 flex justify-end w-full bottom-4">
<div class="w-[90%] sm:w-[800px] m-auto text-right">
<!-- <a-button class="bg-[#1677ff] w-[115px] mr-2" type="primary" @click="showAdvanced = !showAdvanced">
{{ showAdvanced ? '默认输入' : '高级输入' }}
</a-button> -->
<a-button type="primary" @click="handleSave" class="bg-[#1677ff] w-[85px]">
计算
</a-button>
</div>
</div>
</div>
</template>
<script setup>
// @ts-ignore
import data from './data.js';
import { reactive, watchEffect, toRaw, ref } from 'vue';
import { toArray } from 'lodash-es';
import { Form, message } from 'ant-design-vue';
import _ from 'lodash';
import { onMounted } from 'vue';
import axios from 'axios';
import { useRoute } from 'vue-router';
const useForm = Form.useForm;
// 获取query
const isShow = useRoute()?.query?.show || false
let formRef = reactive({});
const result = ref([]);
const groupNames = ref([]);
const showAdvanced = ref(false);
const format = (data) => {
const newData = _.mapValues(data, (item) => {
if (_.isObject(item)) {
return _.mapValues(item, (value, key) => {
if (_.isObject(value)) {
return _.mapValues(value, (v, k) => {
if (k === 'value') {
if (value.component === 'number') {
// if (key === 'volume') {
// return parseFloat(Number(v).toFixed(4));
// }
console.log('%c [ value.decimal ]-84', 'font-size:13px; background:pink; color:#bf2c9f;', value.decimal, v)
return parseFloat(Number(v).toFixed(value.decimal));
} else {
return v;
}
}
return v;
});
}
return value;
});
}
return item;
});
return newData;
};
onMounted(async () => {
// axios post 请求
const res = await axios.post(
'/cal/battery/cellOutputResults',
{
form: '',
},
{
'Content-Type': 'application/json',
}
);
res.data.data = format(res.data.data);
Object.keys(res.data.data).forEach((key) => {
res.data.data[key].key = key;
});
Object.keys(res.data.data).forEach((key) => {
formRef[key] = res.data.data[key];
});
});
const handleSave = async () => {
const extractValues = (obj) => _.mapValues(obj, 'value');
const transformedData = _.mapValues(formRef, extractValues);
const res = await axios.post(
'/cal/battery/cellOutputResults',
{
...transformedData,
from: 'calBtn',
},
{
'Content-Type': 'application/json',
}
);
if (res.data.result === 0) {
res.data.data = format(res.data.data);
Object.keys(res.data.data).forEach((key) => {
res.data.data[key].key = key;
});
Object.keys(formRef).forEach((key) => {
formRef[key] = res.data.data[key];
});
message.success('计算成功');
// 滚动到顶部
window.scrollTo(0, 0);
} else {
message.error(res.data.data?.[0]?.message);
}
};
watchEffect(() => {
const groupedResults = _.groupBy(formRef, 'group');
let transformed = _.mapValues(groupedResults, (groupItem) => {
let groupValue = _.mapValues(groupItem, (item) => {
let { label, key, group, ...newItem } = item;
const obj = {
label: item.label,
key,
};
newItem = _.mapValues(newItem, (value, key) => {
return { ...value, key: key };
});
const newItemArr = _.values(newItem);
const sortNewItemArr = _.orderBy(newItemArr, ['order']);
const orderKey = Object.keys(newItem)[0];
return {
order: newItem[orderKey].order,
label,
key,
group,
obj: sortNewItemArr,
};
});
const sortGroupValue = _.values(groupValue);
// 按照group排序
const sortedArray = _.orderBy(groupValue, ['order']);
return sortedArray;
});
const newTransformed = _.values(transformed);
const sortNewTransformed = _.orderBy(newTransformed, (item) => {
return item.length;
});
result.value = sortNewTransformed;
groupNames.value = ['', '极片尺寸', '电芯设计'];
});
</script>