AcidNeutralizationModel.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <template>
  2. <el-card class="box-card">
  3. <template #header>
  4. <div class="card-header">
  5. <span>{{ $t('Calculation.neutralizationTitle') }}</span>
  6. </div>
  7. </template>
  8. <el-form
  9. :model="form"
  10. ref="predictForm"
  11. label-width="240px"
  12. label-position="left"
  13. >
  14. <el-form-item
  15. :label="$t('Calculation.initialPH')"
  16. prop="init_pH"
  17. :error="errorMessages.init_pH"
  18. required
  19. >
  20. <el-input
  21. v-model="form.init_pH"
  22. size="large"
  23. :placeholder="$t('Calculation.neutralization.initialPHPlaceholder')"
  24. ref="inputRefs.init_pH"
  25. @input="handleInput('init_pH', $event, 3, 6)"
  26. >
  27. </el-input>
  28. </el-form-item>
  29. <el-form-item
  30. :label="$t('Calculation.targetPH')"
  31. prop="target_pH"
  32. :error="errorMessages.target_pH"
  33. required
  34. >
  35. <el-input
  36. v-model="form.target_pH"
  37. size="large"
  38. :placeholder="$t('Calculation.neutralization.targetPHPlaceholder')"
  39. ref="inputRefs.target_pH"
  40. @input="handleInput('target_pH', $event, 5, 7)"
  41. >
  42. </el-input>
  43. </el-form-item>
  44. <el-form-item
  45. :label="$t('Calculation.exchangeableHydrogen')"
  46. prop="H"
  47. :error="errorMessages.H"
  48. required
  49. >
  50. <el-input
  51. v-model="form.H"
  52. size="large"
  53. :placeholder="$t('Calculation.neutralization.exchangeableHydrogenPlaceholder')"
  54. ref="inputRefs.H"
  55. @input="handleInput('H', $event, 0, 4)"
  56. >
  57. </el-input>
  58. </el-form-item>
  59. <el-form-item
  60. :label="$t('Calculation.exchangeableAluminum')"
  61. prop="Al"
  62. :error="errorMessages.Al"
  63. required
  64. >
  65. <el-input
  66. v-model="form.Al"
  67. size="large"
  68. :placeholder="$t('Calculation.neutralization.exchangeableAluminumPlaceholder')"
  69. ref="inputRefs.Al"
  70. @input="handleInput('Al', $event, 0, 8)"
  71. >
  72. </el-input>
  73. </el-form-item>
  74. <el-form-item
  75. :label="$t('Calculation.soilOrganicMatter')"
  76. prop="OM"
  77. :error="errorMessages.OM"
  78. required
  79. >
  80. <el-input
  81. v-model="form.OM"
  82. size="large"
  83. :placeholder="$t('Calculation.neutralization.soilOrganicMatterPlaceholder')"
  84. ref="inputRefs.OM"
  85. @input="handleInput('OM', $event, 0, 35)"
  86. >
  87. </el-input>
  88. </el-form-item>
  89. <el-form-item
  90. :label="$t('Calculation.nitrate')"
  91. prop="NO3"
  92. :error="errorMessages.NO3"
  93. required
  94. >
  95. <el-input
  96. v-model="form.NO3"
  97. size="large"
  98. :placeholder="$t('Calculation.neutralization.nitratePlaceholder')"
  99. ref="inputRefs.NO3"
  100. @input="handleInput('NO3', $event, 10, 70)"
  101. >
  102. </el-input>
  103. </el-form-item>
  104. <el-form-item
  105. :label="$t('Calculation.ammoniumSalt')"
  106. prop="NH4"
  107. :error="errorMessages.NH4"
  108. required
  109. >
  110. <el-input
  111. v-model="form.NH4"
  112. size="large"
  113. :placeholder="$t('Calculation.neutralization.ammoniumSaltPlaceholder')"
  114. ref="inputRefs.NH4"
  115. @input="handleInput('NH4', $event, 0, 20)"
  116. >
  117. </el-input>
  118. </el-form-item>
  119. <el-form-item
  120. :label="$t('Calculation.cationExchangeCapacity')"
  121. prop="CEC"
  122. :error="errorMessages.CEC"
  123. required
  124. >
  125. <el-input
  126. v-model="form.CEC"
  127. size="large"
  128. :placeholder="$t('Calculation.neutralization.cationExchangeCapacityPlaceholder')"
  129. ref="inputRefs.CEC"
  130. @input="handleInput('CEC', $event, 0, 15)"
  131. >
  132. </el-input>
  133. </el-form-item>
  134. <el-button type="primary" @click="onSubmit" class="onSubmit"
  135. >{{ $t('Calculation.calculateButton') }}</el-button
  136. >
  137. <el-dialog
  138. v-model="dialogVisible"
  139. @close="onDialogClose"
  140. :close-on-click-modal="false"
  141. width="500px"
  142. align-center
  143. class="dialog-class"
  144. :title="$t('Calculation.resultTitle')"
  145. >
  146. <span class="dialog-class"
  147. >{{ $t('Calculation.resultUnit') }} {{ result }}{{ $t('Calculation.quicklime') }}</span
  148. ><br />
  149. <template #footer>
  150. <el-button @click="dialogVisible = false">{{ $t('Calculation.closeButton') }}</el-button>
  151. </template>
  152. </el-dialog>
  153. </el-form>
  154. </el-card>
  155. </template>
  156. <script setup lang="ts">
  157. import { reactive, ref, nextTick, onMounted } from "vue";
  158. import { ElMessage } from 'element-plus';
  159. import axios from 'axios';
  160. import { api5000 } from "../../../utils/request";
  161. import { useI18n } from 'vue-i18n';
  162. const { t } = useI18n();
  163. // 根据文档修改表单接口
  164. const form = reactive<{
  165. init_pH: number | null,
  166. target_pH: number | null,
  167. H: number | null,
  168. Al: number | null,
  169. OM: number | null,
  170. NO3: number | null,
  171. NH4: number | null,
  172. CEC: number | null,
  173. }>({
  174. init_pH: null,
  175. target_pH: null,
  176. H: null,
  177. Al: null,
  178. OM: null,
  179. NO3: null,
  180. NH4: null,
  181. CEC: null,
  182. });
  183. const result = ref<number | null>(null);
  184. const dialogVisible = ref(false);
  185. const predictForm = ref<any>(null);
  186. const errorMessages = reactive({
  187. init_pH: '',
  188. target_pH: '',
  189. H: '',
  190. Al: '',
  191. OM: '',
  192. NO3: '',
  193. NH4: '',
  194. CEC: '',
  195. });
  196. // 当前选中模型 - 根据文档修改为 33
  197. const selectedModelId = ref<number>(33);
  198. const selectedModelName = ref<string>("降酸预测模型");
  199. // 使用 ref 来存储输入框元素
  200. const inputRefs = reactive<{
  201. [key in keyof typeof form]: any;
  202. }>({
  203. init_pH: ref<HTMLInputElement | null>(null),
  204. target_pH: ref<HTMLInputElement | null>(null),
  205. H: ref<HTMLInputElement | null>(null),
  206. Al: ref<HTMLInputElement | null>(null),
  207. OM: ref<HTMLInputElement | null>(null),
  208. NO3: ref<HTMLInputElement | null>(null),
  209. NH4: ref<HTMLInputElement | null>(null),
  210. CEC: ref<HTMLInputElement | null>(null),
  211. });
  212. // 限制输入为数字并校验范围
  213. const handleInput = (field: keyof typeof form, event: Event, min: number, max: number) => {
  214. try {
  215. const inputElement = inputRefs[field].value;
  216. if (!inputElement) {
  217. console.error('未找到输入框元素');
  218. return;
  219. }
  220. // 过滤非数字和小数点字符
  221. const rawValue = (event.target as HTMLInputElement).value;
  222. const filteredValue = rawValue.replace(/[^0-9.]/g, '');
  223. inputElement.value = filteredValue;
  224. if (filteredValue === '') {
  225. form[field] = null;
  226. errorMessages[field] = '';
  227. return;
  228. }
  229. const numValue = parseFloat(filteredValue);
  230. if (isNaN(numValue) || numValue < min || numValue > max) {
  231. form[field] = null;
  232. errorMessages[field] = t('Calculation.validationRange', { min, max });
  233. } else {
  234. form[field] = numValue;
  235. errorMessages[field] = '';
  236. }
  237. } catch (error) {
  238. console.error(`处理输入时出错:`, error);
  239. errorMessages[field] = t('Calculation.validationError');
  240. }
  241. };
  242. const validateInput = (value: string | number, min: number, max: number): boolean => {
  243. const numValue = parseFloat(value as string);
  244. if (isNaN(numValue)) {
  245. return false;
  246. }
  247. return numValue >= min && numValue <= max;
  248. };
  249. // 获取当前保存的模型
  250. const fetchSelectedModel = async () => {
  251. try {
  252. const resp = await api5000.post('/model-selection?model_type=acid_reduction');
  253. if (resp.data && resp.data.data && resp.data.data.model_id) {
  254. selectedModelId.value = resp.data.data.model_id;
  255. selectedModelName.value = resp.data.data.model_name;
  256. } else {
  257. selectedModelId.value = 33;
  258. selectedModelName.value = "降酸预测模型";
  259. console.warn("未获取到选中模型,使用默认 model_id=33");
  260. }
  261. } catch (error) {
  262. selectedModelId.value = 33;
  263. selectedModelName.value = "降酸预测模型";
  264. console.warn("获取当前模型失败,使用默认 model_id=33:", error);
  265. }
  266. };
  267. // 页面挂载时调用
  268. onMounted(() => {
  269. fetchSelectedModel();
  270. });
  271. // 计算方法 - 根据文档修改参数
  272. const onSubmit = async () => {
  273. const inputConfigs = [
  274. { field: 'init_pH' as keyof typeof form, min: 3, max: 6 },
  275. { field: 'target_pH' as keyof typeof form, min: 5, max: 7 },
  276. { field: 'H' as keyof typeof form, min: 0, max: 4 },
  277. { field: 'Al' as keyof typeof form, min: 0, max: 8 },
  278. { field: 'OM' as keyof typeof form, min: 0, max: 35 },
  279. { field: 'NO3' as keyof typeof form, min: 10, max: 70 },
  280. { field: 'NH4' as keyof typeof form, min: 0, max: 20 },
  281. { field: 'CEC' as keyof typeof form, min: 0, max: 15 },
  282. ];
  283. let isValid = true;
  284. inputConfigs.forEach((config) => {
  285. const { field, min, max } = config;
  286. const value = form[field];
  287. if (value === null) {
  288. isValid = false;
  289. errorMessages[field] = t('Calculation.validationRange', { min, max });
  290. return;
  291. }
  292. if (!validateInput(value, min, max)) {
  293. isValid = false;
  294. errorMessages[field] = t('Calculation.validationRange', { min, max });
  295. } else {
  296. errorMessages[field] = '';
  297. }
  298. });
  299. if (!isValid) {
  300. ElMessage.error(t('Calculation.validationError'));
  301. return;
  302. }
  303. console.log('开始计算...');
  304. // 根据文档修改参数结构
  305. const data = {
  306. model_id: selectedModelId.value,
  307. parameters: {
  308. init_pH: form.init_pH,
  309. target_pH: form.target_pH,
  310. H: form.H,
  311. Al: form.Al,
  312. OM: form.OM,
  313. NO3: form.NO3,
  314. NH4: form.NH4,
  315. CEC: form.CEC,
  316. }
  317. };
  318. // console.log('提交的数据:', data);
  319. try {
  320. const response = await api5000.post('/predict', data, {
  321. headers: {
  322. "Content-Type": "application/json",
  323. },
  324. });
  325. console.log('预测结果:', response.data);
  326. if (response.data && typeof response.data.result === 'number') {
  327. result.value = parseFloat((response.data.result / 10).toFixed(2));
  328. } else {
  329. console.error('未获取到有效的预测结果');
  330. ElMessage.error(t('Calculation.invalidResult'));
  331. }
  332. dialogVisible.value = true;
  333. } catch (error: unknown) {
  334. console.error('请求失败:', error);
  335. if (axios.isAxiosError(error)) {
  336. if (error.response) {
  337. ElMessage.error(`${t('Calculation.requestFailed')}${error.response.status}`);
  338. } else if (error.request) {
  339. ElMessage.error(t('Calculation.noResponse'));
  340. } else {
  341. ElMessage.error(`${t('Calculation.requestError')}${(error as Error).message}`);
  342. }
  343. } else {
  344. ElMessage.error(`${t('Calculation.requestError')}${(error as Error).message}`);
  345. }
  346. }
  347. };
  348. // 监听弹窗关闭事件,关闭后初始化值
  349. const onDialogClose = () => {
  350. dialogVisible.value = false;
  351. Object.keys(form).forEach((key) => {
  352. const typedKey = key as keyof typeof form;
  353. form[typedKey] = null;
  354. });
  355. Object.keys(errorMessages).forEach((key) => {
  356. const typedKey = key as keyof typeof errorMessages;
  357. errorMessages[typedKey] = '';
  358. });
  359. nextTick(() => {
  360. if (predictForm.value) {
  361. predictForm.value.resetFields();
  362. }
  363. });
  364. };
  365. </script>
  366. <style scoped>
  367. .box-card {
  368. max-width: 850px;
  369. margin: 0 auto;
  370. padding: 20px;
  371. background-color: #f0f5ff;
  372. border-radius: 10px;
  373. box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  374. }
  375. .card-header {
  376. font-size: 25px;
  377. text-align: center;
  378. color: #333;
  379. margin-bottom: 0px;
  380. }
  381. .el-form-item {
  382. margin-bottom: 20px;
  383. }
  384. :deep(.el-form-item__label) {
  385. font-size: 18px;
  386. color: #666;
  387. }
  388. .model-info {
  389. text-align: center;
  390. margin-bottom: 20px;
  391. font-size: 16px;
  392. color: #555;
  393. }
  394. .el-input {
  395. width: 80%;
  396. }
  397. .onSubmit {
  398. display: block;
  399. margin: 0 auto;
  400. background-color: #007bff;
  401. color: white;
  402. padding: 10px 20px;
  403. border-radius: 5px;
  404. font-size: 16px;
  405. transition: background-color 0.3s ease;
  406. }
  407. .onSubmit:hover {
  408. background-color: #0056b3;
  409. }
  410. .dialog-class {
  411. display: flex;
  412. justify-content: center;
  413. align-items: center;
  414. height: 100%;
  415. font-size: 22px;
  416. }
  417. @media (max-width: 576px) {
  418. .el-form {
  419. --el-form-label-width: 100px;
  420. }
  421. }
  422. </style>