zhangwencui
7 小时以前 34af9a2cd41fb88985d05d57e9fa425bc98990cc
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
<template>
  <view class="account-detail">
    <PageHeader :title="pageTitle" @back="goBack" />
    <up-form :model="form" label-width="110">
      <up-form-item label="检查点" required>
        <up-input v-model="form.checkPoint" placeholder="请输入" clearable />
      </up-form-item>
      <up-form-item label="检查结果" required>
        <up-input :model-value="form.checkResult" placeholder="请选择" readonly @click="showResultSheet = true" />
        <template #right>
          <up-icon name="arrow-right" @click="showResultSheet = true"></up-icon>
        </template>
      </up-form-item>
      <up-form-item label="检查内容" required>
        <up-textarea v-model="form.checkContent" placeholder="请输入" auto-height />
      </up-form-item>
      <up-form-item label="检查说明">
        <up-textarea v-model="form.checkDesc" placeholder="请输入" auto-height />
      </up-form-item>
      <up-form-item label="检查时间">
        <up-input :model-value="form.checkTime" placeholder="请选择" readonly @click="showTimePicker = true" />
        <template #right>
          <up-icon name="arrow-right" @click="showTimePicker = true"></up-icon>
        </template>
      </up-form-item>
    </up-form>
 
    <FooterButtons :loading="loading" confirmText="保存" @cancel="goBack" @confirm="handleSubmit" />
 
    <up-action-sheet
      :show="showResultSheet"
      title="选择检查结果"
      :actions="resultActions"
      @select="onSelectResult"
      @close="showResultSheet = false"
    />
 
    <up-datetime-picker
      :show="showTimePicker"
      mode="datetime"
      :value="checkTimeValue"
      @confirm="onTimeConfirm"
      @cancel="showTimePicker = 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 { addInspectionRecord, updateInspectionRecord } from "@/api/safeProduction/lineInspection";
 
  const loading = ref(false);
  const inspectionId = ref(null);
 
  const form = ref({
    id: null,
    inspectionId: null,
    checkPoint: "",
    checkContent: "",
    checkResult: "正常",
    checkDesc: "",
    checkTime: "",
  });
 
  const showResultSheet = ref(false);
  const showTimePicker = ref(false);
 
  const resultActions = [
    { name: "正常", value: "正常" },
    { name: "异常", value: "异常" },
  ];
 
  const pageTitle = computed(() => (form.value.id ? "编辑巡检记录" : "新增巡检记录"));
 
  const checkTimeValue = computed(() => {
    if (!form.value.checkTime) return Date.now();
    const ts = dayjs(form.value.checkTime).valueOf();
    return Number.isFinite(ts) ? ts : Date.now();
  });
 
  const goBack = () => {
    uni.navigateBack();
  };
 
  const onSelectResult = action => {
    form.value.checkResult = action.value;
    showResultSheet.value = false;
  };
 
  const onTimeConfirm = e => {
    const ts = e?.value;
    form.value.checkTime = dayjs(ts).format("YYYY-MM-DD HH:mm:ss");
    showTimePicker.value = false;
  };
 
  const handleSubmit = () => {
    const checkPoint = String(form.value.checkPoint || "").trim();
    if (!checkPoint) {
      uni.showToast({ title: "请输入检查点", icon: "none" });
      return;
    }
    const checkContent = String(form.value.checkContent || "").trim();
    if (!checkContent) {
      uni.showToast({ title: "请输入检查内容", icon: "none" });
      return;
    }
    const checkResult = String(form.value.checkResult || "").trim();
    if (!checkResult) {
      uni.showToast({ title: "请选择检查结果", icon: "none" });
      return;
    }
 
    const payload = {
      ...form.value,
      inspectionId: inspectionId.value,
      checkPoint,
      checkContent,
      checkResult,
      checkDesc: String(form.value.checkDesc || "").trim(),
      checkTime: form.value.checkTime || "",
    };
 
    loading.value = true;
    const api = payload.id ? updateInspectionRecord : addInspectionRecord;
    api(payload)
      .then(() => {
        uni.showToast({ title: "保存成功", icon: "success" });
        uni.$emit("lineInspection:recordsRefresh");
        goBack();
      })
      .catch(() => {
        uni.showToast({ title: "保存失败", icon: "none" });
      })
      .finally(() => {
        loading.value = false;
      });
  };
 
  onLoad(options => {
    if (options?.data) {
      try {
        const obj = JSON.parse(decodeURIComponent(options.data));
        inspectionId.value = obj.inspectionId;
        if (obj?.row) {
          form.value = { ...form.value, ...(obj.row || {}) };
        }
      } catch (e) {}
    }
    if (!form.value.checkTime) form.value.checkTime = dayjs(Date.now()).format("YYYY-MM-DD HH:mm:ss");
  });
</script>
 
<style scoped lang="scss">
  @import "@/static/scss/form-common.scss";
</style>