dialog.vue 5.63 KB
<template>
  <el-dialog
    class="entryDialog classCUploadDialog"
    :title="title"
    :visible.sync="outerVisible"
    :show-close="false"
    width="40%"
    :close-on-click-modal="false"
    v-loading="loadings.dialogLoading"
    @close="close(false)"
  >
    <div class="modelItem">
      <el-form
        class="fedexFormStyle"
        ref="formData"
        :model="formData"
        :rules="type == 'roleChange' ? {} : rules"
        label-position="top"
        :disabled="type == 'roleChange'"
        size="small"
      >
        <el-row :gutter="5">
          <!-- 用户ID -->
          <el-col :span="12">
            <Formitem label="用户ID" prop="userId" type="edit">
              <el-input
                type="text"
                class="inputShadow"
                id="inputInner-edit-userId"
                resize="none"
                v-model="formData.userId"
                clearable
                placeholder=""
              ></el-input>
            </Formitem>
          </el-col>
          <!-- key -->
          <el-col :span="12">
            <Formitem label="KEY" prop="key" type="edit">
              <el-input
                type="text"
                class="inputShadow"
                id="inputInner-edit-key"
                resize="none"
                v-model="formData.key"
                clearable
                placeholder=""
              ></el-input>
            </Formitem>
          </el-col>
          <!-- 说明 -->
          <el-col :span="24">
            <Formitem label="说明" prop="description" type="edit">
              <el-input
                type="text"
                class="inputShadow"
                id="inputInner-edit-description"
                resize="none"
                v-model="formData.description"
                clearable
                placeholder=""
              ></el-input>
            </Formitem>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="dialogOption entrySave">
      <el-button
        class="entrySaveButton"
        :disabled="loadings.submitLoading"
        @click="submit"
        >确 定</el-button
      >
      <el-button
        class="entrySaveButton"
        :disabled="loadings.uploadLoading"
        @click="close(false)"
        >关 闭</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { generateKey, updateKeyInfo } from "@/api/api-key.js";
export default {
  props: {
    outerVisible: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: "新增密码机",
    },
    type: {
      type: String,
      default: "",
    },
    active: {
      type: Object,
      default: null,
    },
  },
  data() {
    return {
      formData: {
        description: "",
        userId: "",
        key: "",
        id: undefined,
      },
      rules: {
        key: [
          {
            required: false,
            trigger: "blur",
            message: "用户ID不能为空!",
          },
        ],
        userId: [
          {
            required: true,
            trigger: "blur",
            message: "KEY不能为空!",
          },
        ],
      },
      loadings: {
        dialogLoading: false,
        uploadLoading: false,
        submitLoading: false,
      },
      loadingText: "",
    };
  },
  watch: {
    outerVisible(val) {
      if (val) {
        this.loadings.dialogLoading = false;
        if (this.type == "update") {
          this.formData = JSON.parse(JSON.stringify(this.active));
        }
      }
    },
  },
  methods: {
    //数据保存
    submit() {
      this.loadings.dialogLoading = true;
      let obj = {};
      if (this.type == "add") {
        this.$refs.formData.validate((valid) => {
          if (valid) {
            obj = this.formData;
            this.addApi(obj);
          } else {
            this.loadings.dialogLoading = false;
          }
        });
      } else if (this.type == "update") {
        this.$refs.formData.validate((valid) => {
          if (valid) {
            obj.description = this.formData.description;
            obj.userId = this.formData.userId;
            obj.key = this.formData.key;
            obj.id = this.formData.id;
            this.updateapi(obj);
          } else {
            this.loadings.dialogLoading = false;
          }
        });
      }
    },
    //新增
    addApi(val) {
      const { description = "", userId = "", key = "" } = val;
      const params = new URLSearchParams();
      params.append("description", description);
      params.append("userId", userId);
      params.append("key", key);

      generateKey(params)
        .then((res) => {
          if (res.code === "200") {
            this.msgSuccess(res.message);
            this.close(true);
          } else {
            this.alertWarningMsg(res.message);
            this.loadings.dialogLoading = false;
          }
        })
        .catch((err) => {
          console.log(err);
          this.loadings.dialogLoading = false;
        });
    },
    //修改
    updateapi(val) {
      updateKeyInfo(val)
        .then((res) => {
          if (res.code === "200") {
            this.msgSuccess(res.message);
            this.close(true);
          } else {
            this.alertWarningMsg(res.message);
            this.loadings.dialogLoading = false;
          }
        })
        .catch((err) => {
          console.log(err);
          this.loadings.dialogLoading = false;
        });
    },
    close(flag) {
      this.formData = {
        description: "",
        userId: "",
        key: "",
      };
      this.$emit("close", flag);
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/styles/variables.scss";
</style>