zhangwencui
2026-06-30 263b4a60670671cef5445b102e670c40ba34a162
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
<template>
  <view class="account-detail">
    <PageHeader :title="pageTitle" @back="goBack" />
    <up-form ref="formRef" :model="form" label-width="110">
      <up-form-item label="设备编号" required>
        <up-input v-model="form.equipmentNo" placeholder="请输入" clearable />
      </up-form-item>
      <up-form-item label="设备名称" required>
        <up-input v-model="form.equipmentName" placeholder="请输入" clearable />
      </up-form-item>
      <up-form-item label="设备类别">
        <up-input v-model="form.equipmentType" placeholder="请输入" clearable />
      </up-form-item>
      <up-form-item label="规格型号">
        <up-input v-model="form.modelSpec" placeholder="请输入" clearable />
      </up-form-item>
      <up-form-item label="安装位置">
        <up-input v-model="form.installLocation" placeholder="请输入" clearable />
      </up-form-item>
      <up-form-item label="使用单位">
        <up-input v-model="form.useUnit" placeholder="请输入" clearable />
      </up-form-item>
      <up-form-item label="负责人">
        <up-input v-model="managerName" placeholder="请选择" readonly @click="showManagerSheet = true" />
        <template #right>
          <up-icon name="arrow-right" @click="showManagerSheet = true"></up-icon>
        </template>
      </up-form-item>
    </up-form>
 
    <FooterButtons :loading="loading" confirmText="保存" @cancel="goBack" @confirm="handleSubmit" />
 
    <up-action-sheet
      :show="showManagerSheet"
      title="选择负责人"
      :actions="managerActions"
      @select="onSelectManager"
      @close="showManagerSheet = false"
    />
  </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 { userListNoPageByTenantId } from "@/api/system/user";
  import {
    specialEquipmentLedgerAdd,
    specialEquipmentLedgerUpdate,
  } from "@/api/safeProduction/specialEquipmentManagement";
 
  const formRef = ref();
  const loading = ref(false);
  const showManagerSheet = ref(false);
  const managerActions = ref([]);
  const managerName = ref("");
 
  const form = ref({
    id: "",
    equipmentNo: "",
    equipmentName: "",
    equipmentType: "",
    modelSpec: "",
    installLocation: "",
    useUnit: "",
    managerId: "",
    managerName: "",
  });
 
  const pageTitle = computed(() => (form.value.id ? "编辑台账" : "新增台账"));
 
  const goBack = () => {
    uni.navigateBack();
  };
 
  const loadUsers = () => {
    userListNoPageByTenantId()
      .then(res => {
        const rows = Array.isArray(res?.data) ? res.data : [];
        managerActions.value = rows.map(u => ({
          name: u.nickName || u.userName || "",
          value: u.userId,
        }));
      })
      .catch(() => {
        managerActions.value = [];
      });
  };
 
  const onSelectManager = action => {
    form.value.managerId = action.value;
    form.value.managerName = action.name;
    managerName.value = action.name;
    showManagerSheet.value = false;
  };
 
  const handleSubmit = async () => {
    const equipmentNo = String(form.value.equipmentNo || "").trim();
    if (!equipmentNo) {
      uni.showToast({ title: "请输入设备编号", icon: "none" });
      return;
    }
    const equipmentName = String(form.value.equipmentName || "").trim();
    if (!equipmentName) {
      uni.showToast({ title: "请输入设备名称", icon: "none" });
      return;
    }
 
    loading.value = true;
    const api = form.value.id ? specialEquipmentLedgerUpdate : specialEquipmentLedgerAdd;
    api({ ...form.value, equipmentNo, equipmentName })
      .then(() => {
        uni.showToast({ title: "保存成功", icon: "success" });
        uni.$emit("specialEquipmentManagement:refresh");
        goBack();
      })
      .catch(() => {
        uni.showToast({ title: "保存失败", icon: "none" });
      })
      .finally(() => {
        loading.value = false;
      });
  };
 
  onLoad(options => {
    if (!options?.data) return;
    try {
      const row = JSON.parse(decodeURIComponent(options.data));
      form.value = { ...form.value, ...(row || {}) };
      managerName.value = form.value.managerName || "";
    } catch {
      uni.showToast({ title: "数据加载失败", icon: "none" });
    }
  });
 
  onMounted(() => {
    loadUsers();
  });
</script>
 
<style scoped lang="scss">
  @import "@/static/scss/form-common.scss";
</style>