|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +<template> |
| 19 | + <div class="form-layout" v-ctrl-enter="handleSubmit"> |
| 20 | + <a-spin :spinning="loading"> |
| 21 | + <a-form |
| 22 | + :ref="formRef" |
| 23 | + :model="form" |
| 24 | + :rules="rules" |
| 25 | + layout="vertical" |
| 26 | + @finish="handleSubmit"> |
| 27 | + <a-form-item name="username" ref="username"> |
| 28 | + <template #label> |
| 29 | + <tooltip-label :title="$t('label.username')" :tooltip="apiParams.username.description"/> |
| 30 | + </template> |
| 31 | + <a-input |
| 32 | + v-model:value="form.username" |
| 33 | + :placeholder="$t('label.username')"/> |
| 34 | + </a-form-item> |
| 35 | + <a-form-item name="password" ref="password"> |
| 36 | + <template #label> |
| 37 | + <tooltip-label :title="$t('label.new.password')" :tooltip="apiParams.password.description"/> |
| 38 | + </template> |
| 39 | + <a-input-password |
| 40 | + v-model:value="form.password" |
| 41 | + :placeholder="$t('label.new.password')"/> |
| 42 | + </a-form-item> |
| 43 | + <a-form-item name="confirmpassword" ref="confirmpassword"> |
| 44 | + <template #label> |
| 45 | + <tooltip-label :title="$t('label.confirmpassword')" :tooltip="apiParams.password.description"/> |
| 46 | + </template> |
| 47 | + <a-input-password |
| 48 | + v-model:value="form.confirmpassword" |
| 49 | + :placeholder="$t('label.confirmpassword.description')"/> |
| 50 | + </a-form-item> |
| 51 | + |
| 52 | + <div :span="24" class="action-button"> |
| 53 | + <a-button @click="closeAction">{{ $t('label.cancel') }}</a-button> |
| 54 | + <a-button :loading="loading" ref="submit" type="primary" @click="handleSubmit">{{ $t('label.ok') }}</a-button> |
| 55 | + </div> |
| 56 | + </a-form> |
| 57 | + </a-spin> |
| 58 | + </div> |
| 59 | +</template> |
| 60 | + |
| 61 | +<script> |
| 62 | +import { ref, reactive, toRaw } from 'vue' |
| 63 | +import { api } from '@/api' |
| 64 | +import TooltipLabel from '@/components/widgets/TooltipLabel' |
| 65 | +
|
| 66 | +export default { |
| 67 | + name: 'ChangeHostPassword', |
| 68 | + components: { |
| 69 | + TooltipLabel |
| 70 | + }, |
| 71 | + props: { |
| 72 | + resource: { |
| 73 | + type: Object, |
| 74 | + required: true |
| 75 | + } |
| 76 | + }, |
| 77 | + data () { |
| 78 | + return { |
| 79 | + loading: false |
| 80 | + } |
| 81 | + }, |
| 82 | + beforeCreate () { |
| 83 | + this.apiParams = this.$getApiParams('updateHostPassword') |
| 84 | + }, |
| 85 | + created () { |
| 86 | + this.initForm() |
| 87 | + }, |
| 88 | + methods: { |
| 89 | + initForm () { |
| 90 | + this.formRef = ref() |
| 91 | + this.form = reactive({}) |
| 92 | + this.rules = reactive({ |
| 93 | + username: [{ required: true, message: this.$t('message.error.host.username') }], |
| 94 | + password: [{ required: true, message: this.$t('message.error.new.password') }], |
| 95 | + confirmpassword: [ |
| 96 | + { required: true, message: this.$t('message.error.confirm.password') }, |
| 97 | + { validator: this.validateTwoPassword } |
| 98 | + ] |
| 99 | + }) |
| 100 | + }, |
| 101 | + async validateTwoPassword (rule, value) { |
| 102 | + const messageConfirm = this.$t('message.validate.equalto') |
| 103 | + const passwordVal = this.form.password |
| 104 | + if (passwordVal && passwordVal !== value) { |
| 105 | + return Promise.reject(messageConfirm) |
| 106 | + } |
| 107 | + }, |
| 108 | + handleSubmit (e) { |
| 109 | + e.preventDefault() |
| 110 | + if (this.loading) return |
| 111 | + this.formRef.value.validate().then(() => { |
| 112 | + const values = toRaw(this.form) |
| 113 | + this.loading = true |
| 114 | + const params = { |
| 115 | + username: values.username, |
| 116 | + hostId: this.resource.id, |
| 117 | + password: values.password |
| 118 | + } |
| 119 | + api('updateHostPassword', {}, 'POST', params).then(json => { |
| 120 | + this.$notification.success({ |
| 121 | + message: this.$t('label.action.change.password'), |
| 122 | + description: `${this.$t('message.success.change.host.password', { name: this.resource.name })}` |
| 123 | + }) |
| 124 | + this.$emit('refresh-data') |
| 125 | + this.closeAction() |
| 126 | + }).catch(error => { |
| 127 | + this.$notifyError(error) |
| 128 | + }).finally(() => { |
| 129 | + this.loading = false |
| 130 | + }) |
| 131 | + }).catch(error => { |
| 132 | + this.formRef.value.scrollToField(error.errorFields[0].name) |
| 133 | + }) |
| 134 | + }, |
| 135 | + closeAction () { |
| 136 | + this.$emit('close-action') |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | +</script> |
| 141 | + |
| 142 | +<style scoped lang="less"> |
| 143 | +.form-layout { |
| 144 | + width: 80vw; |
| 145 | +
|
| 146 | + @media (min-width: 600px) { |
| 147 | + width: 450px; |
| 148 | + } |
| 149 | +} |
| 150 | +</style> |
0 commit comments