Calculation.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <template>
  2. <el-card class="box-card">
  3. <template #header>
  4. <div class="card-header">
  5. <span>反酸模型</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="土壤有机质(g/kg)"
  16. prop="OM"
  17. :error="errorMessages.OM"
  18. required
  19. >
  20. <el-input
  21. v-model="form.OM"
  22. size="large"
  23. placeholder="请输入土壤有机质0~30(g/kg)"
  24. @input="handleInput('OM', $event, 0, 30)"
  25. >
  26. </el-input>
  27. </el-form-item>
  28. <el-form-item
  29. label="土壤粘粒(g/kg)"
  30. prop="CL"
  31. :error="errorMessages.CL"
  32. required
  33. >
  34. <el-input
  35. v-model="form.CL"
  36. size="large"
  37. placeholder="请输入土壤粘粒50~400(g/kg)"
  38. @input="handleInput('CL', $event, 50, 400)"
  39. >
  40. </el-input>
  41. </el-form-item>
  42. <!-- 修改:将pH改为CEC -->
  43. <el-form-item
  44. label="阳离子交换量(cmol/kg)"
  45. prop="CEC"
  46. :error="errorMessages.CEC"
  47. required
  48. >
  49. <el-input
  50. v-model="form.CEC"
  51. size="large"
  52. placeholder="请输入阳离子交换量0~15(cmol/kg)"
  53. @input="handleInput('CEC', $event, 0, 15)"
  54. >
  55. </el-input>
  56. </el-form-item>
  57. <el-form-item
  58. label="交换性氢(cmol/kg)"
  59. prop="H_plus"
  60. :error="errorMessages.H_plus"
  61. required
  62. >
  63. <el-input
  64. v-model="form.H_plus"
  65. size="large"
  66. placeholder="请输入交换性氢0~1(cmol/kg)"
  67. @input="handleInput('H_plus', $event, 0, 1)"
  68. >
  69. </el-input>
  70. </el-form-item>
  71. <el-form-item
  72. label="水解氮(g/kg)"
  73. prop="N"
  74. :error="errorMessages.N"
  75. required
  76. >
  77. <el-input
  78. v-model="form.N"
  79. size="large"
  80. placeholder="请输入水解氮0~0.2(g/kg)"
  81. @input="handleInput('N', $event, 0, 0.2)"
  82. >
  83. </el-input>
  84. </el-form-item>
  85. <el-form-item
  86. label="交换性铝(cmol/kg)"
  87. prop="Al3_plus"
  88. :error="errorMessages.Al3_plus"
  89. required
  90. >
  91. <el-input
  92. v-model="form.Al3_plus"
  93. size="large"
  94. placeholder="请输入交换性铝0~6(cmol/kg)"
  95. @input="handleInput('Al3_plus', $event, 0, 6)"
  96. >
  97. </el-input>
  98. </el-form-item>
  99. <el-button type="primary" @click="onSubmit" class="onSubmit"
  100. >计算</el-button
  101. >
  102. <el-dialog
  103. v-model="dialogVisible"
  104. @close="onDialogClose"
  105. :close-on-click-modal="false"
  106. width="500px"
  107. align-center
  108. title="计算结果"
  109. >
  110. <span class="dialog-class">pH值: {{ result }}</span>
  111. <template #footer>
  112. <el-button @click="dialogVisible = false">关闭</el-button>
  113. </template>
  114. </el-dialog>
  115. </el-form>
  116. </el-card>
  117. </template>
  118. <script setup lang="ts">
  119. import { reactive, ref, nextTick } from "vue";
  120. import { ElMessage } from "element-plus";
  121. import { api5000 } from "../../../utils/request"; // 使用api5000
  122. // 定义表单数据接口
  123. interface Form {
  124. OM: number | null;
  125. CL: number | null;
  126. CEC: number | null; // 修改:将pH改为CEC
  127. H_plus: number | null;
  128. N: number | null;
  129. Al3_plus: number | null;
  130. }
  131. const form = reactive<Form>({
  132. OM: null,
  133. CL: null,
  134. CEC: null, // 修改:将pH改为CEC
  135. H_plus: null,
  136. N: null,
  137. Al3_plus: null,
  138. });
  139. const result = ref<number | null>(null);
  140. const dialogVisible = ref(false);
  141. const predictForm = ref<any>(null);
  142. const errorMessages = reactive<Record<string, string>>({
  143. OM: "",
  144. CL: "",
  145. CEC: "", // 修改:将pH改为CEC
  146. H_plus: "",
  147. N: "",
  148. Al3_plus: "",
  149. });
  150. // 限制输入为数字并校验范围
  151. const handleInput = (
  152. field: keyof Form,
  153. event: Event,
  154. min: number,
  155. max: number
  156. ) => {
  157. const target = event.target as HTMLInputElement;
  158. let value = target.value.replace(/[^0-9.]/g, "");
  159. form[field] = value ? parseFloat(value) : null;
  160. if (!value) {
  161. errorMessages[field] = "";
  162. return;
  163. }
  164. const numValue = parseFloat(value);
  165. if (isNaN(numValue) || numValue < min || numValue > max) {
  166. errorMessages[field] = `输入值应在 ${min} 到 ${max} 之间且为有效数字`;
  167. } else {
  168. errorMessages[field] = "";
  169. }
  170. };
  171. const validateInput = (value: string, min: number, max: number): boolean => {
  172. const numValue = parseFloat(value);
  173. return !isNaN(numValue) && numValue >= min && numValue <= max;
  174. };
  175. // 计算方法
  176. const onSubmit = async () => {
  177. const inputConfigs = [
  178. { field: "OM" as keyof Form, min: 0, max: 30 },
  179. { field: "CL" as keyof Form, min: 50, max: 400 },
  180. { field: "CEC" as keyof Form, min: 0, max: 15 }, // 修改:将pH改为CEC
  181. { field: "H_plus" as keyof Form, min: 0, max: 1 },
  182. { field: "N" as keyof Form, min: 0, max: 0.2 },
  183. { field: "Al3_plus" as keyof Form, min: 0, max: 6 },
  184. ];
  185. let isValid = true;
  186. for (const config of inputConfigs) {
  187. const { field, min, max } = config;
  188. const value = form[field];
  189. if (value === null || !validateInput(value.toString(), min, max)) {
  190. isValid = false;
  191. errorMessages[field] = `输入值应在 ${min} 到 ${max} 之间且为有效数字`;
  192. } else {
  193. errorMessages[field] = "";
  194. }
  195. }
  196. if (!isValid) {
  197. ElMessage.error("输入值不符合要求,请检查输入");
  198. return;
  199. }
  200. console.log("开始计算...");
  201. const data = {
  202. model_id: 24,
  203. parameters: {
  204. OM: form.OM,
  205. CL: form.CL,
  206. CEC: form.CEC, // 修改:将pH改为CEC
  207. H_plus: form.H_plus,
  208. N: form.N,
  209. Al3_plus: form.Al3_plus,
  210. },
  211. };
  212. try {
  213. const response = await api5000.post('/predict', data, {
  214. headers: {
  215. "Content-Type": "application/json",
  216. },
  217. });
  218. if (response.data?.result?.length > 0) {
  219. result.value = parseFloat(response.data.result[0].toFixed(2));
  220. dialogVisible.value = true;
  221. } else {
  222. throw new Error("未获取到有效的预测结果");
  223. }
  224. dialogVisible.value = true;
  225. } catch (error: any) {
  226. console.error("请求失败详情:", error);
  227. let errorMessage = "请求失败";
  228. if (error.code === "ECONNABORTED") {
  229. errorMessage = "请求超时,请稍后再试";
  230. } else if (error.response) {
  231. // 服务器有响应但状态码不在2xx范围
  232. errorMessage = `请求失败,状态码: ${error.response.status}`;
  233. if (error.response.data?.detail) {
  234. errorMessage += ` - ${error.response.data.detail}`;
  235. }
  236. } else if (error.request) {
  237. // 请求已发出但没有收到响应
  238. errorMessage = "无法连接到服务器,请检查网络连接";
  239. if (error.message.includes("Network Error")) {
  240. errorMessage = "网络错误,请检查后端服务是否运行";
  241. }
  242. }
  243. ElMessage.error(errorMessage);
  244. }
  245. };
  246. // 监听弹窗关闭事件,关闭后初始化值
  247. const onDialogClose = () => {
  248. dialogVisible.value = false;
  249. // 重置表单
  250. Object.keys(form).forEach((key) => {
  251. if (key in form) {
  252. const typedKey = key as keyof Form;
  253. form[typedKey] = null;
  254. }
  255. });
  256. // 重置错误消息
  257. Object.keys(errorMessages).forEach((key) => {
  258. if (key in errorMessages) {
  259. const typedKey = key as keyof typeof errorMessages;
  260. errorMessages[typedKey] = "";
  261. }
  262. });
  263. nextTick(() => {
  264. if (predictForm.value) {
  265. (predictForm.value as any).resetFields();
  266. }
  267. });
  268. };
  269. </script>
  270. <style scoped>
  271. .box-card {
  272. max-width: 850px;
  273. margin: 0 auto;
  274. padding: 20px;
  275. background-color: #f0f5ff;
  276. border-radius: 10px;
  277. box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  278. }
  279. .card-header {
  280. text-align: center;
  281. color: #333;
  282. margin-bottom: 30px;
  283. font-size: 25px;
  284. }
  285. .el-form-item {
  286. margin-bottom: 20px;
  287. }
  288. :deep(.el-form-item__label) {
  289. font-size: 18px;
  290. color: #666;
  291. }
  292. .el-input {
  293. width: 80%;
  294. }
  295. .onSubmit {
  296. display: block;
  297. margin: 0 auto;
  298. background-color: #007bff;
  299. color: white;
  300. padding: 10px 20px;
  301. border-radius: 5px;
  302. font-size: 16px;
  303. transition: background-color 0.3s ease;
  304. }
  305. .onSubmit:hover {
  306. background-color: #0056b3;
  307. }
  308. .dialog-class {
  309. display: flex;
  310. justify-content: center;
  311. align-items: center;
  312. height: 100%; /* 确保容器占满对话框内容区域 */
  313. font-size: 20px;
  314. }
  315. @media (max-width: 576px) {
  316. .el-form {
  317. --el-form-label-width: 100px;
  318. }
  319. }
  320. /* 自定义 tooltip 样式 */
  321. :deep(.el-tooltip__popper.is-light) {
  322. background-color: white;
  323. border-color: #dcdfe6;
  324. color: #606266;
  325. }
  326. </style>