49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
import { postAction, putAction } from '@/api/manage'
|
|
export default {
|
|
data() {
|
|
return {
|
|
visible: false,
|
|
isAdd: false,
|
|
model: {}
|
|
}
|
|
},
|
|
methods: {
|
|
async submit() {
|
|
if (!this.$refs.form) {
|
|
throw new Error('form表单引用为空')
|
|
}
|
|
if (!(this.url && this.url.add && this.url.edit)) {
|
|
throw new Error('链接配置为空')
|
|
}
|
|
await this.$refs.form.validate()
|
|
if (this.beforeSubmit && typeof this.beforeSubmit === 'function') {
|
|
this.beforeSubmit()
|
|
}
|
|
let method = putAction
|
|
let url = this.url.edit
|
|
let successMsg = 'Edit Success'
|
|
let failMsg = 'Edit Fail'
|
|
if (this.isAdd) {
|
|
method = postAction
|
|
url = this.url.add
|
|
successMsg = 'Add Success'
|
|
failMsg = 'Add Fail'
|
|
}
|
|
const { success, message } = await method(url, this.model)
|
|
if (success) {
|
|
this.$message.success(successMsg)
|
|
this.loadData()
|
|
} else {
|
|
this.$message.error(failMsg)
|
|
throw new Error(message)
|
|
}
|
|
}
|
|
},
|
|
watch: {
|
|
visible(val) {
|
|
if (val && this.$refs.form) {
|
|
this.$refs.form.resetFields()
|
|
}
|
|
}
|
|
}
|
|
} |