step1.vue
6.27 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<template>
<div>
<Row>
<div class="create-group">
<span class="create-group-indicator"></span>
<span class="create-group-title">类目选择</span>
<span class="create-group-sub-title">(指定商品的类目)</span>
</div>
</Row>
<Form ref="product" :model="product" :label-width="70" :rules="ruleValidate">
<Row>
<Col span="8">
<Form-item label="品牌" prop="brandId">
<Select v-model="product.brandId" placeholder="请选择" :label-in-value="true" @on-change="selectBrand">
<Option v-for="brand in brands" :value="brand.brandId" :key="brand">{{ brand.brandName }}</Option>
</Select>
</Form-item>
</Col>
</Row>
<Row>
<Col span="8" v-if="show[1]">
<Form-item label="类目" prop="maxSortId">
<Select v-model="product.maxSortId" placeholder="一级类目" @on-change="selectSort1" :label-in-value="true">
<Option v-for="sort in sorts[1]" :value="sort.sortId" :key="sort">{{ sort.sortName }}</Option>
</Select>
</Form-item>
</Col>
<Col span="8" v-if="show[2]">
<Form-item prop="middleSortId">
<Select v-model="product.middleSortId" placeholder="二级类目" @on-change="selectSort2" :label-in-value="true">
<Option v-for="sort in sorts[2]" :value="sort.sortId" :key="sort">{{ sort.sortName }}</Option>
</Select>
</Form-item>
</Col>
<Col span="8" v-if="show[3]">
<Form-item prop="smallSortId">
<Select v-model="product.smallSortId" placeholder="三级类目" @on-change="selectSort3" :label-in-value="true">
<Option v-for="sort in sorts[3]" :value="sort.sortId" :key="sort">{{ sort.sortName }}</Option>
</Select>
</Form-item>
</Col>
</Row>
</Form>
<Row>
<div class="product-create__step1-next">
<Button size="large" type="primary" @click="nextStep">下一步</Button>
</div>
</Row>
</div>
</template>
<script>
import api from '../api';
export default {
props: ['step', 'product'],
mounted: function() {
this.getBrand();
},
data() {
const validateEmpty = (rule, value, callback) => {
if (!value) {
return callback(new Error());
} else {
return callback()
}
};
return {
brands: [],
sorts: {
1: [],
2: [],
3: []
},
pickedSortId: {
1: '',
2: '',
3: ''
},
pickedLabel: {
1: '',
2: '',
3: ''
},
show: {
1: true,
2: false,
3: false
},
level: 1,
sortId: '',
formValidate: {
brandId: '',
maxSortId: '',
middleSortId: '',
smallSortId: ''
},
ruleValidate: {
brandId: [
{ required: true, message: '品牌不能为空', trigger: 'change', validator: validateEmpty}
],
maxSortId: [
{ required: true, message: '一级类目不能为空', trigger: 'change', validator: validateEmpty}
],
middleSortId: [
{ required: true, message: '二级类目不能为空', trigger: 'change', validator: validateEmpty}
],
smallSortId: [
{ required: true, message: '三级类目能为空', trigger: 'change', validator: validateEmpty}
]
}
};
},
methods: {
nextStep: function() {
this.$refs['product'].validate((valid) => {
if (valid) {
this.goNext()
}
});
},
goNext: function() {
this.step.value = 1;
this.$router.push({name: 'product.create.step2'});
},
selectSort1: function(value) {
this.level = 2;
this.sortId = this.product.maxSortId;
this.pickedLabel[1] = value.label;
this.sorts[2] = [];
this.sorts[3] = [];
this.getSort();
},
selectSort2: function(value) {
this.level = 3;
this.sortId = this.product.middleSortId;
this.pickedLabel[2] = value.label;
this.getSort();
},
selectSort3: function(value) {
this.sortId = this.product.smallSortId;
this.pickedLabel[3] = value.label;
},
selectBrand: function(value) {
this.product.brandName = value.label;
},
getBrand: function() {
return api.getBrand().then((result) => {
if (result.code === 200) {
this.brands = result.data;
}
});
},
getSort: function() {
return api.getSort(
this.product.brandId,
this.level,
this.sortId
).then((result) => {
if (result.code === 200 && result.data && result.data.length > 0) {
this.sorts[this.level] = result.data;
this.show[this.level] = true;
}
});
}
},
watch: {
'product.brandId': function() {
this.level = 1;
this.sortId = '';
this.getSort();
},
'pickedLabel.3': function() {
this.product.sortName = `${this.pickedLabel[1]}/${this.pickedLabel[2]}/${this.pickedLabel[3]}`;
}
}
};
</script>
<style lang="scss" scoped>
$prefix: product-create;
.#{product-create}__step1-next {
text-align: center;
display: block;
margin: 20px 0;
}
</style>