|
| 1 | +<template> |
| 2 | + <div class="addconfig" v-if="isShow"> |
| 3 | + <h1 v-if="formItem.id">Edit {{this.$route.params.type.toUpperCase()}} Credential</h1> |
| 4 | + <h1 v-else>Add {{this.$route.params.type.toUpperCase()}} Credential</h1> |
| 5 | + <hr> |
| 6 | + <div style="margin-top:10px;border: 1px solid #C0C0C0;padding:20px"> |
| 7 | + <Form :model="formItem" :label-width="300" ref="formItem" :rules="getRules"> |
| 8 | + <FormItem label="Name" prop="name"> |
| 9 | + <Input v-model.trim="formItem.name" placeholder="Name"></Input> |
| 10 | + </FormItem> |
| 11 | + <FormItem :label="getLabel" prop="number"> |
| 12 | + <Input v-model.trim="formItem.number" placeholder="Number"></Input> |
| 13 | + </FormItem> |
| 14 | + <FormItem label="User" prop="user"> |
| 15 | + <Input v-model.trim="formItem.user" placeholder="User"></Input> |
| 16 | + </FormItem> |
| 17 | + <FormItem label="Password" prop="password"> |
| 18 | + <Input v-model.trim="formItem.password" :type="passwordtype" placeholder="Password"></Input> |
| 19 | + <Checkbox v-model="showPassword" @on-change="handlePassword">Show Password</Checkbox> |
| 20 | + </FormItem> |
| 21 | + <FormItem> |
| 22 | + <Button v-if="formItem.id" type="primary" @click="handleEdit('formItem')">Update</Button> |
| 23 | + <Button v-else type="primary" :loading="loading" @click="handleSubmit('formItem')">Submit</Button> |
| 24 | + <Button type="ghost" style="margin-left: 8px" @click="handleCancel">Cancel</Button> |
| 25 | + </FormItem> |
| 26 | + </Form> |
| 27 | + </div> |
| 28 | + </div> |
| 29 | +</template> |
| 30 | + |
| 31 | +<script> |
| 32 | +import asconfigurationModel from '@/api/asconfiguration' |
| 33 | +import _ from 'lodash' |
| 34 | +
|
| 35 | +export default { |
| 36 | + name: 'addconfig', |
| 37 | + data () { |
| 38 | + const validateName = async(rule, value, callback) => { |
| 39 | + let res = await asconfigurationModel.get({ |
| 40 | + userID: this.$store.state.user._id, |
| 41 | + name: value |
| 42 | + }).then(resp => { |
| 43 | + console.log('resp.data.data', resp.data.data) |
| 44 | + if (resp.data.data.length > 0) { |
| 45 | + return {status: true, valid: false} |
| 46 | + } else { |
| 47 | + return {status: true, valid: true} |
| 48 | + } |
| 49 | + }).catch(err => { |
| 50 | + console.log('Error', err) |
| 51 | + return {status: false, valid: false} |
| 52 | + }) |
| 53 | + if (res.status) { |
| 54 | + if (!res.valid) { |
| 55 | + callback(new Error('! Name Already Exist, Please Enter another Name.')) |
| 56 | + } else { |
| 57 | + callback() |
| 58 | + } |
| 59 | + } else { |
| 60 | + callback(new Error('! Network Error')) |
| 61 | + } |
| 62 | + } |
| 63 | + const editvalidateName = async(rule, value, callback) => { |
| 64 | + if (this.validName === value) { |
| 65 | + callback() |
| 66 | + } else { |
| 67 | + let res = await asconfigurationModel.get({ |
| 68 | + userID: this.$store.state.user._id, |
| 69 | + name: value |
| 70 | + }).then(resp => { |
| 71 | + console.log('resp.data.data', resp.data.data) |
| 72 | + if (resp.data.data.length > 0) { |
| 73 | + return {status: true, valid: false} |
| 74 | + } else { |
| 75 | + return {status: true, valid: true} |
| 76 | + } |
| 77 | + }).catch(err => { |
| 78 | + console.log('Error', err) |
| 79 | + return {status: false, valid: false} |
| 80 | + }) |
| 81 | + if (res.status) { |
| 82 | + if (!res.valid) { |
| 83 | + callback(new Error('! Name Already Exist, Please Enter another Name.')) |
| 84 | + } else { |
| 85 | + callback() |
| 86 | + } |
| 87 | + } else { |
| 88 | + callback(new Error('! Network Error')) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + return { |
| 93 | + isShow: false, |
| 94 | + showPassword: false, |
| 95 | + loading: false, |
| 96 | + passwordtype: 'password', |
| 97 | + formItem: { |
| 98 | + name: '', |
| 99 | + number: '', |
| 100 | + user: '', |
| 101 | + password: '', |
| 102 | + userID: this.$store.state.user._id, |
| 103 | + type: this.$route.params.type |
| 104 | + }, |
| 105 | + rulesformItem: { |
| 106 | + name: [ |
| 107 | + { required: true, message: '! Please Enter the Name', trigger: 'blur' }, |
| 108 | + { validator: validateName, trigger: 'blur' } |
| 109 | + ], |
| 110 | + number: [ |
| 111 | + { required: true, message: '! Please Enter the Number', trigger: 'blur' } |
| 112 | + ], |
| 113 | + user: [ |
| 114 | + { required: true, message: '! Please Enter the User', trigger: 'blur' } |
| 115 | + ], |
| 116 | + password: [ |
| 117 | + { required: true, message: '! Please Enter the Password', trigger: 'blur' } |
| 118 | + ] |
| 119 | + }, |
| 120 | + updateRulesformItem: { |
| 121 | + name: [ |
| 122 | + { required: true, message: '! Please Enter the Name', trigger: 'blur' }, |
| 123 | + { validator: editvalidateName, trigger: 'blur' } |
| 124 | + ], |
| 125 | + number: [ |
| 126 | + { required: true, message: '! Please Enter the Number', trigger: 'blur' } |
| 127 | + ], |
| 128 | + user: [ |
| 129 | + { required: true, message: '! Please Enter the User', trigger: 'blur' } |
| 130 | + ], |
| 131 | + password: [ |
| 132 | + { required: true, message: '! Please Enter the Password', trigger: 'blur' } |
| 133 | + ] |
| 134 | + }, |
| 135 | + validName: '' |
| 136 | + } |
| 137 | + }, |
| 138 | + computed: { |
| 139 | + getLabel () { |
| 140 | + return this.$route.params.type.toUpperCase() + ' Number' |
| 141 | + }, |
| 142 | + getRules () { |
| 143 | + if (this.formItem.id !== undefined) { |
| 144 | + return this.updateRulesformItem |
| 145 | + } else { |
| 146 | + return this.rulesformItem |
| 147 | + } |
| 148 | + } |
| 149 | + }, |
| 150 | + methods: { |
| 151 | + handlePassword (name) { |
| 152 | + if (name) { |
| 153 | + this.passwordtype = 'text' |
| 154 | + } else { |
| 155 | + this.passwordtype = 'password' |
| 156 | + } |
| 157 | + }, |
| 158 | + async handleSubmit (name) { |
| 159 | + this.$refs[name].validate(async (valid) => { |
| 160 | + if (valid) { |
| 161 | + this.loading = true |
| 162 | + let isExist = await (this.checkExist(this.formItem)) |
| 163 | + if (isExist) { |
| 164 | + await asconfigurationModel.post(this.formItem).then(res => { |
| 165 | + this.$Notice.success({title: 'Success', desc: 'Successfully saved.', duration: 3}) |
| 166 | + this.$router.push('/settings') |
| 167 | + }).catch(err => { |
| 168 | + console.log('Error', err) |
| 169 | + this.loading = false |
| 170 | + this.$Notice.error({title: 'Error', desc: 'Not Saved.', duration: 3}) |
| 171 | + }) |
| 172 | + } |
| 173 | + } |
| 174 | + }) |
| 175 | + }, |
| 176 | + async checkExist (formItem, value) { |
| 177 | + let res = await (asconfigurationModel.get({ |
| 178 | + number: formItem.number, |
| 179 | + user: formItem.user, |
| 180 | + password: formItem.password, |
| 181 | + userID: formItem.userID |
| 182 | + }).then(async resp => { |
| 183 | + if (value !== undefined) { |
| 184 | + let finx = _.findIndex(resp.data.data, {id: formItem.id}) |
| 185 | + if (finx !== -1) { |
| 186 | + resp.data.data.splice(finx, 1) |
| 187 | + } |
| 188 | + } |
| 189 | + if (resp.data.data.length > 0) { |
| 190 | + this.$Notice.warning({'title': 'Already exist with <b><i>' + resp.data.data[0].name + '</i></b>'}) |
| 191 | + this.loading = false |
| 192 | + return false |
| 193 | + } else { |
| 194 | + return true |
| 195 | + } |
| 196 | + }).catch(e => { |
| 197 | + return false |
| 198 | + })) |
| 199 | + return res |
| 200 | + }, |
| 201 | + handleCancel () { |
| 202 | + this.$router.push('/settings') |
| 203 | + }, |
| 204 | + handleEdit (name) { |
| 205 | + this.$refs[name].validate(async (valid) => { |
| 206 | + if (valid) { |
| 207 | + this.loading = true |
| 208 | + let isExist = await (this.checkExist(this.formItem, 'edit')) |
| 209 | + if (isExist) { |
| 210 | + await asconfigurationModel.put(this.formItem.id, this.formItem).then(res => { |
| 211 | + this.$Notice.success({title: 'Success', desc: 'Successfully saved.', duration: 3}) |
| 212 | + this.$router.push('/settings') |
| 213 | + }).catch(err => { |
| 214 | + console.log('Error', err) |
| 215 | + this.loading = false |
| 216 | + this.$Notice.error({title: 'Error', desc: 'Not Saved.', duration: 3}) |
| 217 | + }) |
| 218 | + } |
| 219 | + } |
| 220 | + }) |
| 221 | + } |
| 222 | + }, |
| 223 | + mounted () { |
| 224 | + // this.$router.push('/uploader-joblist') |
| 225 | + console.log('this.$route.params.type', this.$route.params.type, this.$route.params.id) |
| 226 | + if (this.$route.params.type === 'asi' || this.$route.params.type === 'sage') { |
| 227 | + if (this.$route.params.id !== undefined) { |
| 228 | + this.$Spin.show() |
| 229 | + asconfigurationModel.getThis(this.$route.params.id).then(res => { |
| 230 | + console.log('edit:', res) |
| 231 | + this.formItem = res.data |
| 232 | + this.validName = res.data.name |
| 233 | + this.$Spin.hide() |
| 234 | + }).catch(err => { |
| 235 | + this.$Spin.hide() |
| 236 | + console.log('Error:: ', err) |
| 237 | + }) |
| 238 | + } |
| 239 | + this.isShow = true |
| 240 | + } |
| 241 | + } |
| 242 | +} |
| 243 | +</script> |
| 244 | + |
| 245 | +<style scoped> |
| 246 | + .addconfig { |
| 247 | + padding: 40px; |
| 248 | + } |
| 249 | +</style> |
0 commit comments