gaoluyang
2026-09-20 e449a5408265e4bd1f6c66f5be28a42efac444ee
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
<template>
  <view class="account-detail">
    <PageHeader :title="pageTitle"
                @back="goBack" />
    <view class="form-container">
      <up-form ref="formRef"
               :model="form"
               :rules="rules"
               label-width="110"
               input-align="right"
               error-message-align="right">
        <u-cell-group title="车辆信息"
                      class="form-section">
          <up-form-item label="车牌号码"
                        prop="plateNumber"
                        required>
            <up-input v-model="form.plateNumber"
                      placeholder="请输入车牌号码"
                      clearable />
          </up-form-item>
          <up-form-item label="产权所有"
                        prop="propertyOwnership">
            <up-input v-model="form.propertyOwnership"
                      placeholder="如:自有、租赁等"
                      clearable />
          </up-form-item>
          <up-form-item label="所属部门"
                        prop="deptId"
                        required>
            <FormPicker v-model="form.deptId"
                        type="select"
                        title="选择所属部门"
                        :options="deptOptions"
                        placeholder="请选择所属部门"
                        @change="onDeptChange" />
          </up-form-item>
          <up-form-item label="责任人"
                        prop="userId">
            <FormPicker v-model="form.userId"
                        type="select"
                        title="选择责任人"
                        :options="userOptions"
                        placeholder="请选择责任人"
                        @change="onUserChange" />
          </up-form-item>
          <up-form-item label="状态"
                        prop="vehicleStatus"
                        required>
            <FormPicker v-model="form.vehicleStatus"
                        type="select"
                        title="选择状态"
                        :options="VEHICLE_STATUS_OPTIONS"
                        placeholder="请选择状态" />
          </up-form-item>
          <up-form-item label="年检到期"
                        prop="inspectionExpireDate">
            <FormPicker v-model="form.inspectionExpireDate"
                        type="date"
                        placeholder="请选择年检到期日期" />
          </up-form-item>
          <up-form-item label="保险到期"
                        prop="insuranceExpireDate">
            <FormPicker v-model="form.insuranceExpireDate"
                        type="date"
                        placeholder="请选择保险到期日期" />
          </up-form-item>
          <up-form-item label="备注"
                        prop="remark">
            <up-textarea v-model="form.remark"
                         placeholder="请输入备注"
                         auto-height />
          </up-form-item>
        </u-cell-group>
      </up-form>
    </view>
 
    <FooterButtons :loading="loading"
                   confirmText="保存"
                   @cancel="goBack"
                   @confirm="handleSubmit" />
  </view>
</template>
 
<script setup>
  import { computed, onMounted, ref } from "vue";
  import { onLoad } from "@dcloudio/uni-app";
  import FooterButtons from "@/components/FooterButtons.vue";
  import PageHeader from "@/components/PageHeader.vue";
  import FormPicker from "@/components/FormPicker.vue";
  import { getDept } from "@/api/collaborativeApproval/approvalProcess";
  import { userListNoPageByTenantId } from "@/api/system/user";
  import { loadDetailData } from "@/utils/detailCache";
  import {
    addVehicleInfo,
    listVehicleInfo,
    updateVehicleInfo,
  } from "@/api/inventoryManagement/vehicle";
 
  const VEHICLE_STATUS_OPTIONS = [
    { name: "在用", value: "NORMAL" },
    { name: "闲置", value: "IDLE" },
    { name: "维修", value: "REPAIR" },
  ];
 
  const formRef = ref();
  const loading = ref(false);
  const vehicleId = ref("");
  const deptOptions = ref([]);
  const userOptions = ref([]);
 
  const form = ref({
    id: undefined,
    plateNumber: "",
    propertyOwnership: "",
    deptId: "",
    deptName: "",
    userId: "",
    userName: "",
    vehicleStatus: "NORMAL",
    inspectionExpireDate: "",
    insuranceExpireDate: "",
    remark: "",
  });
 
  const rules = {
    plateNumber: [
      { required: true, message: "请输入车牌号码", trigger: "blur" },
    ],
    deptId: [{ required: true, message: "请选择所属部门", trigger: "change" }],
    vehicleStatus: [{ required: true, message: "请选择状态", trigger: "change" }],
  };
 
  const pageTitle = computed(() => (vehicleId.value ? "编辑车辆" : "新增车辆"));
 
  const goBack = () => uni.navigateBack();
 
  const onDeptChange = (value, item) => {
    form.value.deptName = item?.name || "";
  };
 
  const onUserChange = (value, item) => {
    form.value.userName = item?.name || "";
  };
 
  const loadOptions = async () => {
    const [depts, users] = await Promise.all([
      getDept().catch(() => ({})),
      userListNoPageByTenantId().catch(() => ({})),
    ]);
    deptOptions.value = (depts?.data || []).map(item => ({
      name: item.deptName,
      value: item.deptId,
    }));
    const rows = users?.data || [];
    userOptions.value = (Array.isArray(rows) ? rows : []).map(item => ({
      name: item.nickName || item.userName || `用户${item.userId}`,
      value: item.userId,
    }));
  };
 
  // 车辆模块没有单条详情接口,从列表里按 id 捞,包装成 loadDetailData 期望的响应形状
  const fetchVehicle = async id => {
    const res = await listVehicleInfo({ current: -1, size: -1 });
    const rows = res?.data?.records || res?.records || res?.data || [];
    const found = (Array.isArray(rows) ? rows : []).find(
      item => String(item.id) === String(id)
    );
    return { data: found };
  };
 
  const loadDetail = async () => {
    if (!vehicleId.value) return;
    let data = null;
    try {
      data = await loadDetailData({
        cacheKey: "vehicleInfoDetail",
        id: vehicleId.value,
        fetcher: fetchVehicle,
      });
    } catch (error) {
      data = null;
    }
    if (!data) {
      uni.showToast({ title: "获取车辆详情失败", icon: "none" });
      return;
    }
    form.value = { ...form.value, ...data, id: data.id };
  };
 
  const handleSubmit = async () => {
    const valid = await formRef.value.validate().catch(() => false);
    if (!valid) return;
 
    loading.value = true;
    const action = vehicleId.value ? updateVehicleInfo : addVehicleInfo;
    action({ ...form.value, id: vehicleId.value || undefined })
      .then(() => {
        uni.showToast({ title: "保存成功", icon: "success" });
        setTimeout(() => uni.navigateBack(), 300);
      })
      .catch(() => {
        uni.showToast({ title: "保存失败", icon: "none" });
      })
      .finally(() => {
        loading.value = false;
      });
  };
 
  onLoad(options => {
    if (options?.id) {
      vehicleId.value = options.id;
      form.value.id = options.id;
    }
  });
 
  onMounted(async () => {
    await loadOptions();
    if (vehicleId.value) await loadDetail();
  });
</script>
 
<style scoped lang="scss">
  @import "@/static/scss/form-common.scss";
 
  .account-detail {
    min-height: 100vh;
    background: #f8f9fa;
    padding-bottom: 100px;
  }
 
  .form-container {
    padding: 12px 12px 0;
  }
 
  .form-section {
    margin-bottom: 12px;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 10px rgba(15, 35, 95, 0.05);
  }
 
  :deep(.u-cell-group__title) {
    padding: 14px 18px 10px !important;
    font-size: 15px !important;
    font-weight: 600 !important;
    color: #22324d !important;
    background: #f8fbff !important;
  }
</style>