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
<!-- 新增密钥 -->
<template>
  <div>
    <el-drawer size="40%" :with-header="false" :visible.sync="visible" :before-close="handleAddClose">
      <el-form ref="form" :model="form" label-width="100px" :rules="rules" style="width: 90%;margin: 50px auto;">
        <el-form-item :label="$t('security.keyId')" v-if="type == 'detail'">
          <el-input v-model="form.securityId" :placeholder="$t('common.placeholder')"></el-input>
        </el-form-item>
        <el-form-item :label="$t('security.keyType')">
          <el-radio-group v-model="form.type">
            <el-radio label="RSA">RSA</el-radio>
            <el-radio label="AES">AES</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('security.keyEncoding')" prop="key">
          <el-input v-model="form.key" :placeholder="$t('common.placeholder')"></el-input>
        </el-form-item>
        <el-form-item :label="$t('security.keyValue')" prop="value">
          <el-input type="textarea" clearable :rows="5" v-model="form.value"
            :placeholder="$t('common.placeholder')"></el-input>
        </el-form-item>
        <el-form-item :label="$t('security.validTime')">
          <el-date-picker :clearable="false" v-model="dateTimes" value-format="timestamp"
            :default-time="['00:00:00', '23:59:59']" type="datetimerange" :range-separator="$t('common.to')"
            :start-placeholder="$t('common.startTime')" :end-placeholder="$t('common.endTime')">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div class="dialog-footer">
        <el-button type="warning" @click="handleAddClose(false)">{{ $t('common.cancel') }}</el-button>
        <el-button v-if="type == 'add'" type="primary" @click="updata()">{{ $t('common.confirm') }}</el-button>
      </div>
    </el-drawer>
  </div>
</template>
 
<script>
import { generateRandomString, resetObjectValues } from '@/utils/index.js'
export default {
  data () {
    return {
      form: {
        securityId: "",
        type: "",
        key: "",
        value: ""
      },
      rules: {
        key: [{ required: true, message: this.$t('common.placeholder'), trigger: ["blur"] },],
        value: [{ required: true, message: this.$t('common.placeholder'), trigger: ["blur"] },],
      },
      dateTimes: [],
      visible: false,
      type: 'info'
    }
  },
  created () {
    this.initialForm = JSON.parse(JSON.stringify(this.form));
  },
  mounted () {
 
  },
  methods: {
    open (type, row) {
      if (type == 'add') {
        this.form.type = 'RSA'
        console.log('add')
      } else {
        this.form.securityId = row.securityId
        this.form.type = row.type
        this.form.key = row.key
        this.form.value = row.value
        this.dateTimes = [row.startTime * 1000, row.endTime * 1000]
      }
      this.type = type
      this.visible = true
    },
 
    handleAddClose (showConfirm = true) {
      const closeAction = () => {
        if (this.$refs.form) {
          this.$refs.form.clearValidate();
        }
        this.visible = false;
        this.form = { ...this.initialForm };
        this.dateTimes = []
      };
      if (showConfirm) {
        this.$confirm(this.$t('common.closeTips'))
          .then(closeAction)
          .catch(() => { });
      } else {
        closeAction();
      }
    },
 
    updata () {
      let that = this
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          if (!that.dateTimes.length) {
            that.$message.warning(this.$t('permission.choose_time_range'));
            return false
          }
          let data = {
            securityId: generateRandomString(),
            type: that.form.type,
            key: that.form.key,
            value: that.form.value,
            startTime: new Date(that.dateTimes[0]).getTime() / 1000,
            endTime: new Date(that.dateTimes[1]).getTime() / 1000
          }
          const res = await this.$http.post(
            "/insertSecurity",
            { data: [data] }
          );
          if (res.code == 200) {
            this.$message.success(this.$t('common.addSuccess'))
            this.visible = false
            resetObjectValues(this.form)
            this.$emit("operation-success")
          } else {
            if (res.data && res.data[0]) {
              this.$message.error(res.data[0].errmsg)
            }
          }
        }
      })
    }
  }
}
</script>