form.vue
3.3 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
<!--
* @FileDescription: form
* @Author: kahu
* @Date: 2022/8/29
* @LastEditors: kahu
* @LastEditTime: 2022/8/29
-->
<template>
<div>
<el-dialog
:title="title"
:visible.sync="dialogVisible"
width="50%"
:before-close="handleClose"
>
<el-form
ref="form"
:model="form"
:rules="formRules"
label-width="80px"
>
<el-form-item label="名称" prop="brandName">
<el-input v-model="form.brandName" placeholder="请输入品牌名称" />
</el-form-item>
<el-form-item label="logo" prop="brandLogo">
<ImageUpload filePath="bueniss" :value="form.brandLogo" inputtype="brandLogo" :limit="1"
@changimg="e=>changimg(e,'brandLogo')"></ImageUpload>
</el-form-item>
</el-form>
<span
slot="footer"
class="dialog-footer"
>
<el-button @click="handleClose">取 消</el-button>
<el-button
type="primary"
:loading="loading"
@click="handleSubmit"
>确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
import ImageUpload from '@/components/ImageUpload';
import { addBrand, updateBrand } from '@/api/renovation';
const Form = function () {
this.id = null
this.brandLogo = null
this.brandName = null
}
export default {
name: 'Form',
components: { ImageUpload },
model: {
prop: 'show',
event: 'change'
},
props: {
show: {
type: Boolean,
default: () => true
},
item: {
// eslint-disable-next-line vue/require-prop-type-constructor
type: Object | null,
default: () => ({})
}
},
data () {
return {
loading: false,
title: '新增品牌',
form: {},
formRules: {
brandName: [
{ required: true, message: '请输入品牌名称', trigger: 'blur' },
{ max: 20, message: '品牌名称应小于20个字符', trigger: 'blur' }
],
brandLogo: [
{ required: true, message: '请上传品牌logo', trigger: 'blur' }
]
}
}
},
computed: {
dialogVisible: {
get () {
return this.show
},
set (val) {
this.$emit('change', val)
}
}
},
watch: {
'item': {
deep: true,
handler () {
if (this.item) {
this.form = JSON.parse(JSON.stringify(this.item))
if (this.form.id !== null || this.form.id !== undefined) {
this.title = '修改品牌'
} else {
this.title = '新增品牌'
}
} else {
this.form = new Form()
}
}
}
},
methods: {
changimg(e, type) {
this.form[type] = e
},
handleSubmit () {
this.$refs.form.validate(async val => {
this.loading = true
if (!val) return this.$message.warning('请完善表单')
if (this.form.id != null) {
await updateBrand(this.form)
} else {
await addBrand(this.form)
}
this.$message.success('操作成功')
this.loading = false
this.handleClose()
})
},
handleClose () {
this.$emit('confirm', this.form)
this.$refs.form.resetFields()
this.dialogVisible = false
}
}
}
</script>
<style
lang="scss"
scoped
>
</style>