add.vue
6.14 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<template>
<div class="category-ctnr">
<div class="main-wrap">
<div class="global-title">新增二级分类</div>
<glsx-combi-form :isInline="false" class="glsx-combi-form" ref="baseInfoRef" labelWidth="154px" defaultWidth="432px" :formObj.sync="formObj" :formData="formData" :formRules="myRule">
<div slot="mainCategorySlot">
{{routeInfo.parentCategoryName}}
</div>
<div slot="secondCategorySlot" class="category-slot">
<div class="category-item" v-for="(item,index) in categoryList" :key="index">
<el-input placeholder="请输入" v-model="item.name" class="input" maxlength="20" show-word-limit></el-input>
<i v-if="categoryList.length>1" class="el-icon-delete del-icon" @click="delSecondCategory(index)"></i>
</div>
<div v-if="!routeInfo.id" class="add-btn" @click="addCategory()">
<i class="el-icon-circle-plus-outline"></i>
新增二级分类
</div>
</div>
</glsx-combi-form>
</div>
<GLFooter>
<el-button @click="handleCancel">取消</el-button>
<el-button type="primary" @click="handleSave">确定</el-button>
</GLFooter>
</div>
</template>
<script>
import glsxCombiForm from '@/components/glsx-combi-form'
export default {
name: 'add',
components: {
glsxCombiForm
},
data() {
return {
formObj:{
brandType: 1,
videoCoverArr: []
},
myRule:{
mainCategory: [{ required: true,validator: (rule, value, callback) => {
if(!this.routeInfo.parentCategoryId) {
callback(new Error(`缺失一级分类ID`))
}else if(!this.routeInfo.categoryType){
callback(new Error(`缺失频道ID`))
}else{
callback()
}
}, trigger: 'change' }],
secondCategory: [{ required: true, validator: (rule, value, callback) => {
const _target = this.categoryList.find(item => !item.name)
if(_target) {
callback(new Error(`请输入二级分类`))
}else{
callback()
}
}, trigger: 'change' }],
},
formData:[
{
label: '一级分类',
name: 'mainCategory',
addHtml: 'mainCategorySlot'
},
{
label: '二级分类名称',
name: 'secondCategory',
addHtml: 'secondCategorySlot'
}
],
categoryList: []
}
},
created(){
const urlParam = this.$route.params
this.routeInfo = JSON.parse(decodeURIComponent(urlParam.id))
const { id, name } = this.routeInfo
this.categoryList = [{id: id || '', name: name || ''}]
},
methods:{
addCategory(){
const _target = this.categoryList.find(item => !item.name)
if(_target) return this.$message.error('请输入二级分类')
this.categoryList.push({id: '', name: ''})
},
delSecondCategory(index){
this.categoryList.splice(index, 1)
},
handleSave(){
this.$refs.baseInfoRef.$refs.formRef.validate(valid => {
if(valid){
if(this.routeInfo.id){
this.editCategory()
}else{
this.batchAddCategory()
}
}
})
},
batchAddCategory(){
const { categoryType, parentCategoryId } = this.routeInfo
const params = {
categoryType,
parentCategoryId,
categoryNameList: this.categoryList.map(item => item.name)
}
this.$apis.category.batchAddCategoryApi(params).then(res => {
if(res) {
this.$message.success('操作成功')
this.handleCancel()
}
})
},
editCategory(){
const { categoryType, parentCategoryId, id } = this.routeInfo
const params = {
id,
categoryType,
parentCategoryId,
categoryLevel: 2,
categoryName: this.categoryList[0]?.name
}
this.$apis.category.addCategoryApi(params).then(res => {
if(res){
this.$message.success('编辑成功')
this.handleCancel()
}
})
},
handleCancel(){
this.$router.back()
},
}
}
</script>
<style scoped lang="scss">
.category-ctnr{
min-height: 500px;
height: calc(100vh - 140px);
.main-wrap{
height: 100%;
background-color: #fff;
margin-bottom: 16px;
.glsx-combi-form{
padding: 20px;
background-color: #fff;
margin-bottom: 16px;
.category-slot{
.category-item{
margin-top: 10px;
&:first-child{
margin-top: 0;
}
.el-input{
width: 432px;
}
.del-icon{
font-size: 16px;
color: #999999;
margin-left: 8px;
vertical-align: middle;
}
}
.add-btn{
display: inline-block;
color: #5850FF;
cursor: pointer;
i{
font-size: 20px;
vertical-align: middle;
}
}
}
}
}
}
</style>