form.vue
3.23 KB
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
<!--
* @FileDescription: 添加渠道表单
* @Author: kahu
* @Date: 2022/8/25
* @LastEditors: kahu
* @LastEditTime: 2022/8/25
-->
<template>
<div class="form_content">
<el-dialog
:close-on-click-modal="false"
:title="dialogOption.title"
:visible.sync="diaShow"
width="40%"
:before-close="handleClose"
>
<el-form
ref="form"
:model="form"
:rules="formRules"
label-width="120px"
>
<el-form-item
label="渠道名称:"
prop="channelName"
>
<el-input
v-model="form.channelName"
clearable
size="mini"
type="text"
placeholder="请输入渠道名称"
/>
</el-form-item>
</el-form>
<span
slot="footer"
class="dialog-footer"
>
<el-button @click="handleClose" size="mini" style="background-color: #fff;color: #000;">取 消</el-button>
<el-button
:loading="dialogOption.loading"
@click="handleConfirm"
size="mini" style="background-color: #3F9B6A;color: #fff;"
>确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
import { add, edit } from '@/api/channel';
export default {
name: 'Form',
model: {
prop: 'show',
event: 'change'
},
props: {
show: {
type: Boolean,
default: () => false
},
item: {
// eslint-disable-next-line vue/require-prop-type-constructor
type: Object | null,
default: () => ({
id: null,
channelName: null
})
}
},
data () {
return {
dialogOption: {
loading: false,
title: '新增渠道'
},
form: {
id: null,
channelName: null
},
formRules: {
channelName: [
{ required: true, message: '请输入渠道名称', trigger: 'blur' },
{ min: 1, max: 20, message: '渠道名称长度在1-20个字符', trigger: 'blur' }
]
}
}
},
computed: {
diaShow: {
get () {
return this.show
},
set (value) {
this.$emit('change', value)
}
}
},
watch: {
'item': {
deep: true,
handler () {
if (this.item?.id) {
this.dialogOption.title = '修改渠道'
this.form = this.item
} else {
this.dialogOption.title = '添加渠道'
this.handleResetForm()
}
}
}
},
methods: {
handleConfirm () {
this.$refs.form.validate(async val => {
if (!val) return this.$message.error('请完善表单')
this.dialogOption.loading = true
if (this.form.id) {
await edit(this.form)
} else {
await add(this.form)
}
this.dialogOption.loading = false
this.$message.success('添加成功')
this.handleResetForm()
this.$emit('confirm', this.form)
this.diaShow = false
})
},
handleClose () {
this.handleResetForm()
this.diaShow = false
this.$emit('close', this.form)
},
handleResetForm () {
this.$refs.form?.resetFields()
}
}
}
</script>
<style
lang="scss"
scoped
>
.form_content {
}
</style>