From f64155f6ad27c50451bb7d4a15f553bc72ead7de Mon Sep 17 00:00:00 2001
From: WXL <wl_5969728@163.com>
Date: 星期五, 12 九月 2025 14:35:26 +0800
Subject: [PATCH] 年龄计算相关

---
 src/views/project/donatebaseinfo/index.vue |  141 ++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 122 insertions(+), 19 deletions(-)

diff --git a/src/views/project/donatebaseinfo/index.vue b/src/views/project/donatebaseinfo/index.vue
index c16b502..01e076a 100644
--- a/src/views/project/donatebaseinfo/index.vue
+++ b/src/views/project/donatebaseinfo/index.vue
@@ -217,7 +217,19 @@
           />
         </template>
       </el-table-column>
-      <el-table-column label="骞撮緞" align="center" prop="age" width="100" />
+      <el-table-column label="骞撮緞" align="center" prop="age" width="100">
+        <template slot-scope="scope">
+           {{
+            `${
+              scope.row.age && scope.row.age !== 0 ? `${scope.row.age}${scope.row.ageunit?scope.row.ageunit:''}` : ""
+            } ${
+              scope.row.age2 && scope.row.age2 !== 0
+                ? `${scope.row.age2}${scope.row.ageunit2}`
+                : ""
+            }`.trim()
+          }}
+        </template>
+      </el-table-column>
       <el-table-column
         label="鍖荤枟鏈烘瀯"
         align="center"
@@ -457,8 +469,15 @@
               </el-form-item>
             </el-col>
             <el-col :span="6">
-              <el-form-item label="骞撮緞" prop="age">
-                <el-input v-model="form.age" placeholder="璇疯緭鍏ュ勾榫�" />
+              <el-form-item label="瀛﹀巻" prop="education">
+                <el-select v-model="form.education" placeholder="璇烽�夋嫨瀛﹀巻">
+                  <el-option
+                    v-for="dict in dict.type.sys_education"
+                    :key="dict.value"
+                    :label="dict.label"
+                    :value="dict.value"
+                  ></el-option>
+                </el-select>
               </el-form-item>
             </el-col>
           </el-row>
@@ -471,10 +490,19 @@
                   v-model="form.birthday"
                   type="date"
                   style="width: 174px"
-                  value-format="yyyy-MM-dd HH:mm:ss"
+                  @change="calculateAge"
+                  value-format="yyyy-MM-dd"
                   placeholder="閫夋嫨鍑虹敓鏃ユ湡"
                 >
                 </el-date-picker>
+              </el-form-item>
+            </el-col>
+            <el-col :span="6">
+              <!-- <el-form-item label="骞撮緞" prop="age">
+                <el-input v-model="form.age" placeholder="璇疯緭鍏ュ勾榫�" />
+              </el-form-item> -->
+              <el-form-item label="骞撮緞" prop="age">
+                <el-input v-model="form.andAge" disabled> </el-input>
               </el-form-item>
             </el-col>
             <el-col :span="6">
@@ -482,18 +510,6 @@
                 <el-select v-model="form.occupation" placeholder="璇烽�夋嫨鑱屼笟">
                   <el-option
                     v-for="dict in dict.type.sys_occupation"
-                    :key="dict.value"
-                    :label="dict.label"
-                    :value="dict.value"
-                  ></el-option>
-                </el-select>
-              </el-form-item>
-            </el-col>
-            <el-col :span="6">
-              <el-form-item label="瀛﹀巻" prop="education">
-                <el-select v-model="form.education" placeholder="璇烽�夋嫨瀛﹀巻">
-                  <el-option
-                    v-for="dict in dict.type.sys_education"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value"
@@ -1041,7 +1057,11 @@
         sex: null,
         idcardtype: null,
         idcardno: null,
-        age: null,
+        age: null, // 骞撮緞锛堝勾锛�
+        andAge: "",
+        ageunit: "骞�", // 鍗曚綅锛岄�氬父鍥哄畾涓衡�滃勾鈥�
+        age2: null, // 骞撮緞锛堟湀鎴栧ぉ锛�
+        ageunit2: "鏈�", // 鍗曚綅锛屾牴鎹绠楀喅瀹氭槸鈥滄湀鈥濊繕鏄�滃ぉ鈥�
         ageunit: null,
         birthday: null,
         phone: null,
@@ -1144,7 +1164,7 @@
           { required: true, message: "璇锋纭緭鍏ヨ瘉浠跺彿鐮�", trigger: "blur" }
         ],
         sex: [{ required: true, message: "鎬у埆涓嶈兘涓虹┖", trigger: "blur" }],
-        age: [{ required: true, message: "璇疯緭鍏ュ勾榫�", trigger: "blur" }],
+        // age: [{ required: true, message: "璇疯緭鍏ュ勾榫�", trigger: "blur" }],
         treatmenthospitalno: [
           { required: true, message: "璇烽�夋嫨鍖荤枟鏈烘瀯", trigger: "blur" }
         ],
@@ -1287,7 +1307,83 @@
         this.currentuser = response.data;
       });
     },
+    calculateAge(birthday) {
+      if (!birthday) {
+        // 娓呯┖骞撮緞瀛楁
+        this.form.age = null;
+        this.form.age2 = null;
+        // this.form.age3 = null;
+        return;
+      }
 
+      const birthDate = new Date(birthday);
+      const today = new Date();
+
+      // 璁$畻鎬绘湀浠藉樊寮�
+      let yearDiff = today.getFullYear() - birthDate.getFullYear();
+      let monthDiff = today.getMonth() - birthDate.getMonth();
+      let dayDiff = today.getDate() - birthDate.getDate();
+
+      // 濡傛灉褰撳墠鏃ユ湡鐨勫ぉ鏁板皬浜庡嚭鐢熸棩鏈熺殑澶╂暟锛屽垯鍊熸湀
+      if (dayDiff < 0) {
+        monthDiff--;
+        // 鑾峰彇涓婁釜鏈堢殑鏈�鍚庝竴澶�
+        const lastDayOfMonth = new Date(
+          today.getFullYear(),
+          today.getMonth(),
+          0
+        ).getDate();
+        dayDiff += lastDayOfMonth;
+      }
+
+      // 濡傛灉鏈堜唤宸负璐燂紝鍒欏�熷勾
+      if (monthDiff < 0) {
+        yearDiff--;
+        monthDiff += 12;
+      }
+
+      // 璧嬪�煎勾閮ㄥ垎
+      this.form.age = yearDiff;
+      this.form.ageunit = "宀�";
+
+      // 璧嬪�兼湀閮ㄥ垎锛堟垨鑰呮偍涔熷彲浠ラ�夋嫨鏄剧ず鍓╀綑鐨勫ぉ鏁帮級
+      this.form.age2 = monthDiff;
+      this.form.ageunit2 = "鏈�";
+
+      // 濡傛灉鎮ㄨ繕鎯虫樉绀哄ぉ鏁�
+      // this.form.age3 = dayDiff;
+      // this.form.ageunit3 = '澶�';
+
+      // 濡傛灉骞撮緞灏忎簬1宀侊紝鍙兘鎮ㄥ笇鏈涚敤鏈堟垨澶╂潵琛ㄧず涓诲勾榫�
+      // 杩欓噷鏄竴涓畝鍗曠殑閫昏緫锛屾偍鍙互鏍规嵁闇�姹傝皟鏁�
+      if (yearDiff === 0) {
+        if (monthDiff === 0) {
+          // 灏忎簬1涓湀锛岀敤澶╄〃绀�
+          this.form.age = dayDiff;
+          this.form.ageunit = "澶�";
+          this.form.age2 = null; // 娓呯┖绗簩瀛楁
+          // this.form.age3 = null;
+        } else {
+          // 灏忎簬1骞达紝鐢ㄦ湀琛ㄧず
+          this.form.age = monthDiff;
+          this.form.ageunit = "鏈�";
+          this.form.age2 = dayDiff; // 绗簩瀛楁鏄剧ず澶�
+          this.form.ageunit2 = "澶�";
+        }
+      }
+
+      this.form.andAge = `${
+        this.form.age && this.form.age !== 0
+          ? `${this.form.age}${this.form.ageunit}`
+          : ""
+      } ${
+        this.form.age2 && this.form.age2 !== 0
+          ? `${this.form.age2}${this.form.ageunit2}`
+          : ""
+      }`.trim();
+      this.$forceUpdate(); // 寮哄埗鏇存柊
+      console.log(this.form.andAge);
+    },
     LoadReportList() {
       listDonatebaseinfo().then(res => {
         let list = res.rows;
@@ -1450,7 +1546,8 @@
           // 璧嬪�肩粰琛ㄦ牸
           this.form.sex = sex;
           this.form.birthday = birthday;
-          this.form.age = age;
+          this.calculateAge(birthday);
+          // this.form.age = age;
         } else {
         }
       } catch {}
@@ -1927,4 +2024,10 @@
   margin: auto !important;
   padding-bottom: 0px !important;
 }
+::v-deep .el-input.is-disabled .el-input__inner {
+  background-color: #fff;
+  border-color: #dfe4ed;
+  color: #000;
+  cursor: not-allowed;
+}
 </style>

--
Gitblit v1.9.3