zhangwencui
2026-07-02 5f42f3f378ab4b22ac17e4ab2ee849f1c5ae5144
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
250
251
252
253
254
255
256
257
258
259
260
261
262
<template>
  <view class="account-detail">
    <PageHeader :title="pageTitle"
                @back="goBack" />
    <up-form :model="form"
             label-width="110">
      <up-form-item label="设施名称">
        <up-input :model-value="facilityName"
                  disabled
                  placeholder="自动带出" />
      </up-form-item>
      <up-form-item label="设施编号">
        <up-input :model-value="facilityCode"
                  disabled
                  placeholder="自动带出" />
      </up-form-item>
      <up-form-item label="巡检编号"
                    required>
        <up-input v-model="form.inspectionCode"
                  :disabled="isView"
                  placeholder="请输入"
                  clearable />
      </up-form-item>
      <up-form-item label="巡检类型">
        <up-input :model-value="form.inspectionType"
                  placeholder="请选择"
                  readonly
                  :disabled="isView"
                  @click="openTypeSheet" />
        <template #right>
          <up-icon name="arrow-right"
                   @click="openTypeSheet"></up-icon>
        </template>
      </up-form-item>
      <up-form-item label="计划巡检时间">
        <up-input :model-value="form.planTime"
                  placeholder="请选择"
                  readonly
                  :disabled="isView"
                  @click="openPlanPicker" />
        <template #right>
          <up-icon name="arrow-right"
                   @click="openPlanPicker"></up-icon>
        </template>
      </up-form-item>
      <template v-if="mode === 'do' || mode === 'view'">
        <up-form-item label="实际巡检时间">
          <up-input :model-value="form.actualTime"
                    disabled
                    placeholder="自动填充" />
        </up-form-item>
        <up-form-item label="检查结果"
                      :required="mode === 'do'">
          <up-input :model-value="form.checkResult"
                    placeholder="请选择"
                    readonly
                    :disabled="isView"
                    @click="openResultSheet" />
          <template #right>
            <up-icon name="arrow-right"
                     @click="openResultSheet"></up-icon>
          </template>
        </up-form-item>
        <up-form-item label="检查说明">
          <up-textarea v-model="form.checkDesc"
                       :disabled="isView"
                       placeholder="请输入"
                       auto-height />
        </up-form-item>
      </template>
    </up-form>
    <FooterButtons :loading="loading"
                   :confirmText="isView ? '返回' : '保存'"
                   @cancel="goBack"
                   @confirm="handleSubmit" />
    <up-action-sheet :show="showTypeSheet"
                     title="选择巡检类型"
                     :actions="typeActions"
                     @select="onSelectType"
                     @close="showTypeSheet = false" />
    <up-action-sheet :show="showResultSheet"
                     title="选择检查结果"
                     :actions="resultActions"
                     @select="onSelectResult"
                     @close="showResultSheet = false" />
    <up-datetime-picker :show="showPlanPicker"
                        mode="datetime"
                        :value="planValue"
                        @confirm="onPlanConfirm"
                        @cancel="showPlanPicker = false" />
  </view>
</template>
 
<script setup>
  import { computed, ref } from "vue";
  import { onLoad } from "@dcloudio/uni-app";
  import dayjs from "dayjs";
  import PageHeader from "@/components/PageHeader.vue";
  import FooterButtons from "@/components/FooterButtons.vue";
  import {
    addFacilityInspection,
    updateFacilityInspection,
  } from "@/api/safeProduction/safetyFacility";
 
  const loading = ref(false);
  const mode = ref("add");
 
  const facilityName = ref("");
  const facilityCode = ref("");
 
  const form = ref({
    id: null,
    facilityId: null,
    inspectionCode: "",
    inspectionType: "定期巡检",
    planTime: "",
    checkResult: "",
    checkDesc: "",
    actualTime: "",
    status: "",
  });
 
  const showTypeSheet = ref(false);
  const showResultSheet = ref(false);
  const showPlanPicker = ref(false);
 
  const typeActions = [
    { name: "定期巡检", value: "定期巡检" },
    { name: "临时巡检", value: "临时巡检" },
  ];
  const resultActions = [
    { name: "正常", value: "正常" },
    { name: "异常", value: "异常" },
  ];
 
  const isView = computed(() => mode.value === "view");
 
  const pageTitle = computed(() => {
    if (mode.value === "add") return "发起巡检";
    if (mode.value === "do") return "巡检";
    return "查看巡检";
  });
 
  const planValue = computed(() => {
    if (!form.value.planTime) return Date.now();
    const ts = dayjs(form.value.planTime).valueOf();
    return Number.isFinite(ts) ? ts : Date.now();
  });
 
  const goBack = () => {
    uni.navigateBack();
  };
 
  const openTypeSheet = () => {
    if (isView.value) return;
    showTypeSheet.value = true;
  };
 
  const openPlanPicker = () => {
    if (isView.value) return;
    showPlanPicker.value = true;
  };
 
  const openResultSheet = () => {
    if (isView.value) return;
    showResultSheet.value = true;
  };
 
  const onSelectType = action => {
    if (isView.value) return;
    form.value.inspectionType = action.value;
    showTypeSheet.value = false;
  };
 
  const onSelectResult = action => {
    if (isView.value) return;
    form.value.checkResult = action.value;
    showResultSheet.value = false;
  };
 
  const onPlanConfirm = e => {
    if (isView.value) return;
    form.value.planTime = dayjs(e?.value).format("YYYY-MM-DD HH:mm:ss");
    showPlanPicker.value = false;
  };
 
  const handleSubmit = () => {
    if (isView.value) {
      goBack();
      return;
    }
 
    const inspectionCode = String(form.value.inspectionCode || "").trim();
    if (!inspectionCode) {
      uni.showToast({ title: "请输入巡检编号", icon: "none" });
      return;
    }
    if (!form.value.facilityId) {
      uni.showToast({ title: "缺少设施信息", icon: "none" });
      return;
    }
    if (!form.value.planTime) {
      uni.showToast({ title: "请选择计划巡检时间", icon: "none" });
      return;
    }
    if (mode.value === "do") {
      const checkResult = String(form.value.checkResult || "").trim();
      if (!checkResult) {
        uni.showToast({ title: "请选择检查结果", icon: "none" });
        return;
      }
      form.value.status = "已巡检";
      form.value.actualTime = dayjs(Date.now()).format("YYYY-MM-DD HH:mm:ss");
    }
 
    const payload = {
      ...form.value,
      inspectionCode,
      checkDesc: String(form.value.checkDesc || "").trim(),
    };
 
    loading.value = true;
    const api = payload.id ? updateFacilityInspection : addFacilityInspection;
    api(payload)
      .then(() => {
        uni.showToast({ title: "操作成功", icon: "success" });
        uni.$emit("safetyFacility:refresh");
        goBack();
      })
      .catch(() => {
        uni.showToast({ title: "操作失败", icon: "none" });
      })
      .finally(() => {
        loading.value = false;
      });
  };
 
  onLoad(options => {
    if (options?.data) {
      try {
        const obj = JSON.parse(decodeURIComponent(options.data));
        mode.value = obj.mode || "add";
        if (obj.facility) {
          const f = obj.facility;
          form.value.facilityId = f.id;
          facilityName.value = f.facilityName || "";
          facilityCode.value = f.facilityCode || "";
        }
        if (obj.row) {
          const r = obj.row;
          form.value = { ...form.value, ...(r || {}) };
          facilityName.value = r.facilityName || facilityName.value;
          facilityCode.value = r.facilityCode || facilityCode.value;
        }
      } catch (e) {}
    }
  });
</script>
 
<style scoped lang="scss">
  @import "@/static/scss/form-common.scss";
</style>