zhitong.yu
8 天以前 378d781e6f35f89652aa36e079a8b7fc44cea77e
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
<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="drawerProps.title + t('cabinet.cabinet')">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item :label="t('cabinet.cabinetNumber')" prop="cabinetnum">
        <el-input v-model="drawerProps.row!.cabinetnum" :placeholder="t('cabinet.inputNumber')" clearable></el-input>
      </el-form-item>
      <el-form-item :label="t('cabinet.cabinetName')" prop="cabinetname">
        <el-input v-model="drawerProps.row!.cabinetname" :placeholder="t('cabinet.inputName')" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">{{ t("Config.cancel") }}</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">{{ t("Config.sure") }}</el-button>
    </template>
  </el-drawer>
</template>
 
<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { useI18n } from "vue-i18n";
import { ElMessage, FormInstance } from "element-plus";
import { User } from "@/api/interface";
 
const { t } = useI18n();
 
const rules = reactive({
  cabinetname: [{ required: true, message: t("cabinet.validation.nameRequired") }],
  cabinetnum: [{ required: true, message: t("cabinet.validation.numberRequired") }]
});
 
interface DrawerProps {
  title: string;
  isView: boolean;
  initParam: Partial<User.ResUserList>;
  row: Partial<User.ResUserList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}
 
const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  initParam: {},
  title: "",
  row: {}
});
 
// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};
 
// 提交数据(新增/编辑)
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      if (!drawerProps.value.initParam.departmentId == "") {
        drawerProps.value.row.companyid = drawerProps.value.initParam.departmentId;
      }
      const res = await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${res.msg}` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};
 
defineExpose({
  acceptParams
});
</script>