Mr.Shi
2023-08-11 5a7fb4358eb902ce139096f20ef3c0625adcd7f4
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<template>
  <el-dialog
    :width="width"
    :title="title"
    :visible.sync="visible"
    append-to-body
    custom-class="eva-dialog two-fa-window"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false"
  >
    <slot name="beforeForm"><p class="two-fa-window__message">{{message}}</p></slot>
    <el-form ref="form" :model="form" :rules="rules" inline>
      <el-form-item label="登录密码" prop="password" label-width="85px" required>
        <PasswordInput v-model="form.password"/>
      </el-form-item>
    </el-form>
    <slot name="afterForm"></slot>
    <div slot="footer" class="two-fa-window__footer">
      <div class="remember-pwd">
        <el-checkbox v-model="rememberPwd"/><span>记住密码</span>
      </div>
      <div class="opera">
        <el-button @click="cancel">{{cancelText}}</el-button>
        <el-button type="primary" @click="confirm" :loading="isWorking">{{confirmText}}</el-button>
      </div>
    </div>
  </el-dialog>
</template>
 
<script>
import PasswordInput from './PasswordInput'
export default {
  name: 'TwoFAWindow',
  components: { PasswordInput },
  props: {
    // 标题
    title: {
      type: String,
      default: '安全验证'
    },
    // 消息内容
    message: {
      type: String,
      required: false
    },
    // 确认按钮文案
    confirmText: {
      type: String,
      default: '确定'
    },
    // 取消按钮文案
    cancelText: {
      type: String,
      default: '取消'
    },
    // 宽度
    width: {
      type: String,
      default: '400px'
    }
  },
  data () {
    return {
      visible: false,
      isWorking: false,
      // 记住密码
      rememberPwd: false,
      // 请求对象
      request: null,
      // 请求方式
      method: null,
      // 请求参数
      requestArguments: null,
      // 表单
      form: {
        password: ''
      },
      // 表单验证规则
      rules: {
        password: { required: true, message: '请输入登录密码' }
      }
    }
  },
  methods: {
    /**
     * 打开窗口(通常情况下由2FA机制自动调用)
     *
     * @param axiosInstance axios实例
     * @param method 请求方式
     * @param requestArguments 请求参数
     */
    open (axiosInstance, method, requestArguments) {
      this.request = axiosInstance
      this.method = method
      this.requestArguments = requestArguments
      this.visible = true
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
    },
    /**
     * 确定
     */
    confirm () {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return
        }
        // 写入密码
        this.$cache.twoFA.setPassword(this.form.password, this.rememberPwd)
        // 接口2fa调用
        if (this.request != null) {
          this.isWorking = true
          this.request[this.method].apply(this.request, this.requestArguments)
            .then(data => {
              this.$emit('then', data)
              // 销毁窗口
              this.close()
            })
            .catch(e => {
              this.$emit('catch', e)
            })
            .finally(() => {
              this.isWorking = false
            })
          return
        }
        // 手动调用
        this.$emit('confirm', this.form)
      })
    },
    /**
     * 取消
     */
    cancel () {
      // 销毁窗口
      this.close()
      // 接口2fa调用
      if (this.request != null) {
        this.$emit('cancel', new Error('#ignore#: cancel 2fa'))
        return
      }
      // 手动调用
      this.$emit('cancel')
    },
    /**
     * 关闭窗口
     */
    close () {
      this.visible = false
      setTimeout(() => {
        document.body.removeChild(this.$el)
      }, 300)
    }
  }
}
</script>
 
<style scoped lang="scss">
.two-fa-window {
  // 提示消息
  .two-fa-window__message {
    margin: 0 0 10px 0;
  }
  // 底部操作
  .two-fa-window__footer {
    display: flex;
    .remember-pwd {
      width: 100px;
      flex-shrink: 0;
      text-align: left;
      font-size: 13px;
      display: flex;
      align-items: center;
      .el-checkbox {
        margin-right: 10px;
      }
      & > * {
        vertical-align: middle;
      }
    }
    .opera {
      flex-grow: 1;
    }
  }
}
</style>