index.vue
23.8 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
<!-- -->
<template>
<div style="padding: 0 10px;background-color:#f7f7f7;">
<div class="tab_show">
<!-- 搜索 -->
<div class="formSearch">
<el-form :inline="true" :model="formInline" class="demo-form-inline">
<el-form-item label="商品名称">
<el-input v-model="formInline.search" placeholder="请输入商品名称" style="width:168px"/>
</el-form-item>
<el-form-item label="上架状态">
<el-select v-model="formInline.shelveState" placeholder="请选择上架状态" style="width:168px">
<el-option label="全部" :value="null" />
<el-option label="上架" value="1" />
<el-option label="下架" value="0" />
</el-select>
</el-form-item>
<el-form-item label="官方分类">
<el-cascader
v-model="formInline.classifyId"
:options="categoryList"
clearable
:props="{
checkStrictly: true,
expandTrigger: 'hover',
label:'categoryName',
value:'id',
children:'childs'
}"
/>
</el-form-item>
</el-form>
<div style="width:20%">
<el-button @click="search" style="background-color: #3F9B6A;color: #fff;">查询</el-button>
<el-button @click="clear" class="buttonHover"
style="color: #606266;border: 1px solid #dddfe5;background-color: #fff;">重置</el-button>
</div>
</div>
<div style="margin-bottom:20px;">
<el-button @click="add" style="background-color: #3F9B6A;color: #fff;">新增商品</el-button>
<!-- <el-button @click="sends" style="background-color: #3F9B6A;color: #fff;">批量导入</el-button> -->
<el-button @click="productDataExport" style="background-color: #3F9B6A;color: #fff;">导出商品</el-button>
</div>
<!-- 表格 -->
<div class="tableBox">
<el-table
ref="multipleTable"
:data="tableData"
tooltip-effect="dark"
@selection-change="handleSelectionChange"
:header-cell-style="{fontSize: '14px',color:'#0009',fontWeight: 'normal',backgroundColor:'#F2F3F5'}"
>
<!-- <el-table-column
type="selection"
v-if="teble_sel"
min-width="4%">
</el-table-column> -->
<el-table-column prop="productId" label="商品id" show-overflow-tooltip />
<el-table-column label="商品主图" width="150" align="center">
<template slot-scope="scope">
<img height="80" width="80" :src="$baseURL+msgList(scope.row.productImage)" alt srcset>
</template>
</el-table-column>
<el-table-column prop="productName" label="商品名称" width="220" />
<el-table-column prop="section" label="售价区间" show-overflow-tooltip />
<el-table-column prop="memberSection" label="会员价" show-overflow-tooltip />
<el-table-column prop="ifCredit" label="积分兑换" show-overflow-tooltip>
<template slot-scope="scope">
<span v-if="scope.row.ifCredit == 1">允许</span>
<span v-if="scope.row.ifCredit == 0">不允许</span>
</template>
</el-table-column>
<el-table-column prop="stockNumber" label="库存" show-overflow-tooltip />
<el-table-column prop="volume" label="销量" show-overflow-tooltip />
<el-table-column prop="volume" label="上架状态" show-overflow-tooltip>
<template slot-scope="scope">
<span v-if="scope.row.shelveState==0">未上架</span>
<span v-if="scope.row.shelveState==1">已上架</span>
<span v-if="scope.row.shelveState==2">待审核</span>
<span v-if="scope.row.shelveState==3">审核失败</span>
</template>
</el-table-column>
<el-table-column prop="reject" label="驳回原因" show-overflow-tooltip />
<el-table-column :formatter="row=>getBrandName(row)" label="品牌" show-overflow-tooltip />
<el-table-column prop="createTime" label="创建时间" width="180" />
<el-table-column label="操作" width="300">
<template slot-scope="scope">
<div class="btnList">
<div @click="edit(scope.row)" class="tableBtn greens" >编辑</div>
<div @click="down(scope.row)" v-if="scope.row.shelveState==0" class="tableBtn greens" >上架</div>
<div @click="down(scope.row)" v-if="scope.row.shelveState==1" class="tableBtn greens" >下架</div>
<div @click="setVipPrice(scope.row)" v-if="scope.row.shelveState==1" class="tableBtn greens" >设置会员价</div>
<div @click="del(scope.row)" v-if="scope.row.shelveState!=1" class="tableBtn greens" >删除</div>
<!-- <el-button type="text" @click="edit(scope.row)">编辑</el-button>
<el-button v-if="scope.row.shelveState==0" type="text" @click="down(scope.row)">上架</el-button>
<el-button v-if="scope.row.shelveState==1" type="text" @click="down(scope.row)">下架</el-button>
<el-button v-if="scope.row.shelveState==1" type="text" @click="setVipPrice(scope.row)">设置会员价</el-button>
<el-button v-if="scope.row.shelveState!=1" type="text" @click="del(scope.row)">删除</el-button> -->
</div>
</template>
</el-table-column>
</el-table>
<div class="fenye">
<el-pagination
:current-page="formInline.page"
:page-sizes="[10, 20, 50, 100]"
:page-size="10"
background
layout="prev, pager, next,total"
:total="total"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</div>
</div>
</div>
<!-- 批量导入 -->
<el-dialog
title="批量导入商品"
:visible.sync="batchAdd"
:close-on-click-modal="false"
center
width="400px"
>
<div class="uploadDialog">
<el-upload
drag
:limit="1"
:auto-upload="false"
accept=".xlsx"
:headers="headers"
:action="UploadUrls"
:before-upload="beforeUploadFile"
:on-change="fileChange"
:on-remove="batchRemove"
:on-exceed="exceedFile"
:on-success="handleSuccess"
:on-error="handleError"
:file-list="batchFileList"
>
<i class="el-icon-upload" />
<div class="el-upload__text">
<em>点击上传</em>
</div>
<div slot="tip" class="el-upload__tip">只能上传xlsx文件,且不超过10M</div>
<div slot="tip" class="el-upload__tip">
上传前不知道excel模板的,请点击
<span class="clickMe" @click="poDownload">点我下载模板</span> 去下载
</div>
</el-upload>
<br>
<el-button size="small" type="primary" @click="uploadFile">立即上传</el-button>
</div>
</el-dialog>
<!-- 设置会员价弹窗 -->
<el-dialog
title="设置会员价"
:visible.sync="vipPriceVisible"
width="50%"
class="vipDialog"
>
<el-form ref="form" label-width="80px">
<el-form-item label="优惠方式">
<el-radio-group v-model="mode" @change="modeChange">
<el-radio :label="1">折扣</el-radio>
<el-radio :label="2">指定价格</el-radio>
</el-radio-group>
</el-form-item>
<div class="priceTable">
<table>
<tr>
<th>规格</th>
<th>售价</th>
<th v-for="(item,index) in gradeList" :key="index">
{{ item }}
</th>
</tr>
<tr v-for="(item,index) in productData" :key="index">
<td>
{{ item.value }}
</td>
<td>
{{ item.price }}
</td>
<td v-for="(itemJ,indexJ) in item.memberPrices" :key="indexJ" class="td-input">
<el-input v-model="itemJ.price" oninput="value=value.replace(/([^\d|\.])/g, '')" /> {{ mode==1?'折':'元' }}
</td>
</tr>
</table>
</div>
<el-button type="success" class="clearBtn" @click="clearProductMember">清除所有旧会员价</el-button>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="vipPriceVisible = false">取 消</el-button>
<el-button type="primary" @click="vipPriceSubmit">确 定</el-button>
</span>
</el-dialog>
<!-- 新增/修改商品弹窗 -->
<el-dialog
:title="!commId ? '新增商品' : '编辑商品'"
:visible.sync="commidyVisible"
custom-class="bian_css" width="65%"
center
top="10vh"
:close-on-click-modal="false"
@before-close="closeModal"
@close="closeModal"
>
<CommAdd
ref="child"
:show-tinymce="showTinymce"
:product-id="commId"
@cancel="cancelForm"
/>
</el-dialog>
</div>
</template>
<script>
import CommAdd from '@/views/commodity/commoditySystem/addCommodity.vue'
import { getBtnList, getToken } from '@/utils/auth'
import { uploadUrl } from '@/utils/request'
import {
getClassifyGetAll,
getClassifyDelete,
getClassifyStart,
downloadTemplate,
importProduct,
getClassify,
getProductMembers,
setProductMember,
clearProductMember,
productExport,
getBrandList
} from '@/api/commodity'
export default {
components: {
CommAdd
},
data() {
// 这里存放数据
return {
showTinymce: false,
btnList: '',
activeName: 'first',
formInline: {
search: '', // 搜索字段
shelveState: '', // 上架状态 1-上架 0-不上架 null-全部
stock: '', // 库存状态 1-有库存 0-无库存 null-全部
classifyId: '', // 最下级分类id
page: 1, // 当前页
pageSize: 10
},
batchAdd: false,
batchFileList: [],
total: 1,
tableData: [],
categoryList: [],
UploadUrl: uploadUrl,
headers: {
'Authorization-business': getToken()
},
vipPriceVisible: false,
productId: 0,
mode: 1,
productData: [],
gradeList: [],
commidyVisible: false,
commId: 0,
brandList: [],
teble_sel:false,
canst:'' //组合商品ID
}
},
computed: {
getBrandName() {
return (row) => {
const find = this.brandList.find(item => item.id === row.brandId);
if (find) return find.brandName
return '-'
}
}
},
mounted() {
this.getBrandList()
this.getAll(this.formInline)
this.queryAllCategory()
this.btnList = getBtnList()
},
// 方法集合
methods: {
msgList(list){
if(list && list!=''){
let strin = list.split(',')
return strin[0]
}
},
async getBrandList() {
const { data } = await getBrandList()
this.brandList = data
},
cancelForm() {
this.commidyVisible = false
this.formInline = {
search: '',
shelveState: '',
stock: '',
classifyId: '',
page: 1,
pageSize: 10
}
this.getAll(this.formInline)
this.closeModal()
},
closeModal() {
this.showTinymce = false
this.$refs.child.last()
this.$refs.child.reset()
},
btnClick(id) {
if (id.permissionName === '批量导入') {
this.sends()
} else if (id.permissionName === '新增商品') {
this.add()
}
},
handleSizeChange(val) {
this.formInline.pageSize = val
this.getAll(this.formInline)
},
handleCurrentChange(val) {
this.formInline.page = val
this.getAll(this.formInline)
},
// 查询
search() {
this.total = 1
this.formInline.page = 1
this.getAll(this.formInline)
},
// 重置
clear() {
this.formInline = {
search: '', // 搜索字段
shelveState: '', // 上架状态 1-上架 0-不上架 null-全部
stock: '', // 库存状态 1-有库存 0-无库存 null-全部
classifyId: '', // 最下级分类id
page: 1, // 当前页
pageSize: 10
}
},
// 新增商品
add() {
this.showTinymce = true
if (this.commId) {
this.$refs.child.reset()
}
this.commidyVisible = true
this.commId = 0
// this.$router.push({ name: 'addCommodity', params: { id: '1' }})
},
// 导出商品
async productDataExport() {
this.$message({
message: '数据导出中,请勿重复操作!',
type: 'success'
})
const res = await productExport(this.formInline)
if (!res) { return }
const blob = new Blob([res], { type: 'application/vnd.ms-excel' })
const fileName = '商品数据明细表.xls'
if ('download' in document.createElement('a')) {
// 非IE下载
const elink = document.createElement('a')
elink.download = fileName
elink.href = URL.createObjectURL(blob)
elink.style.display = 'none'
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
} else {
// IE10+下载
navigator.msSaveBlob(blob, fileName)
}
},
// 批量导入
sends() {
this.batchAdd = true
this.batchFileList = []
},
// 编辑商品
edit(row) {
this.showTinymce = true
this.commidyVisible = true
this.commId = row.productId
this.$nextTick(() => {
this.$refs.child?.details()
})
// this.$router.push({
// name: 'addCommodity',
// params: { productId: row.productId }
// })
// let res = await getClassifyGetById({ productId: row.productId });
// console.log(res);
},
// 设置会员价
setVipPrice(row) {
var _ = this
this.productId = row.productId
getProductMembers({ productId: row.productId }).then(res => {
if (res.code === '') {
_.productData = res.data
if (_.productData && _.productData.length > 0) {
_.gradeList = []
const _memberPrices = _.productData[0].memberPrices
_memberPrices.forEach(function(item, i) {
_.gradeList.push(item.memberLevelName)
})
_.mode = _.productData[0].mode ? _.productData[0].mode : 1
_.vipPriceVisible = true
}
}
})
},
// 设置会员价提交
vipPriceSubmit() {
for (let i = 0; i < this.productData.length; i++) {
this.productData[i].mode = this.mode
for (let j = 0; j < this.productData[i].memberPrices.length; j++) {
this.productData[i].memberPrices[j].mode = this.mode
var _price = this.productData[i].memberPrices[j].price
if (!_price) {
this.$message({
type: 'warning',
message: '会员价格不能为空!'
})
return false
} else if (this.productData[i].mode === 1) {
if (_price <= 0 || _price >= 10) {
this.$message({
type: 'warning',
message: '会员折数必须大于0小于10!'
})
return false
}
} else if (this.productData[i].mode === 2) {
if (_price > this.productData[i].price) {
this.$message({
type: 'warning',
message: '会员价格不能大于原价!'
})
return false
}
}
}
}
setProductMember({ productId: this.productId, members: this.productData }).then(res => {
if (res.code === '') {
this.$message({
type: 'success',
message: '设置成功!'
})
this.getAll(this.formInline)
this.vipPriceVisible = false
}
})
},
// 清空会员价
clearProductMember() {
console.log(this.productData, 'this.productData')
this.$confirm('该商品所有旧设置的会员价将被清除, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
clearProductMember({ productId: this.productId }).then(res => {
if (res.code === '') {
this.productData.map(item => {
item.memberPrices.map(itemChild => {
itemChild.price = null
})
})
this.$message.success('清除成功')
// this.vipPriceVisible = false
}
})
})
},
// 删除商品
del(row) {
this.$confirm('选中数据将被永久删除, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
.then(() => {
getClassifyDelete({ productId: row.productId }).then(res => {
if (res.code === '') {
this.$message({
type: 'success',
message: '删除成功!'
})
this.getAll(this.formInline)
}
})
})
.catch(() => {})
},
// 商品上下架
async down(row) {
console.log(row.shelveState)
if (row.shelveState) {
console.log(111)
const res = await getClassifyStart({
productId: row.productId,
shelveState: 0
})
if (res.code === '') {
this.$message({
type: 'success',
message: '下架成功!'
})
this.getAll(this.formInline)
}
} else {
const res = await getClassifyStart({
productId: row.productId,
shelveState: 1
})
if (res.code === '') {
this.$message({
type: 'success',
message: '上架成功!'
})
this.getAll(this.formInline)
}
}
},
// 初始化查询所有数据
async getAll(formInline) {
this.formInline.classifyId =
this.formInline.classifyId[2] ||
this.formInline.classifyId[1] ||
this.formInline.classifyId[0] ||
this.formInline.classifyId
const res = await getClassifyGetAll(formInline)
this.total = res.data.total
this.tableData = res.data.list
console.log(this.tableData)
},
// 初始化查询所有分类
async queryAllCategory() {
const res = await getClassify()
this.categoryList = res.data
this.handleRemoveEmptyChild(this.categoryList)
},
handleRemoveEmptyChild(arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i].childs && Array.isArray(arr[i].childs)) {
this.handleRemoveEmptyChild(arr[i].childs)
}
if (arr[i].childs && Array.isArray(arr[i].childs) && arr[i].childs.length === 0) {
delete arr[i].childs
}
}
},
// *********************导入部分
UploadUrls: function() {
// 因为action参数是必填项,我们使用二次确认进行文件上传时,直接填上传文件的url会因为没有参数导致api报404,所以这里将action设置为一个返回为空的方法就行,避免抛错
return ''
},
// 下载模板
poDownload() {
downloadTemplate().then(res => {
console.log(res, 1111)
// const content = res
const blob = new Blob([res])
const fileName = '批量导入商品模板.xlsx'
if ('download' in document.createElement('a')) {
// 非IE下载
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
} else {
// IE10+下载
navigator.msSaveBlob(blob, fileName)
}
})
},
// 导入
uploadFile() {
if (this.batchFileList.length === 0) {
this.$message.warning('请上传文件')
} else {
const formDate = new FormData()
formDate.append('file', this.batchFileList[0])
console.log(formDate.get('file'))
importProduct(formDate).then(res => {
console.log(res)
if (res.code === '') {
this.$message.success('导入成功')
this.batchAdd = false
this.batchFileList = []
this.getAll(this.formInline)
}
})
}
},
// 上传文件之前的钩子, 参数为上传的文件,若返回 false 或者返回 Promise 且被 reject,则停止上传
beforeUploadFile(file) {
console.log('before upload')
console.log(file)
const extension = file.name.substring(file.name.lastIndexOf('.') + 1)
const size = file.size / 1024 / 1024
if (extension !== 'xlsx') {
this.$message.warning('只能上传后缀是.xlsx的文件')
}
if (size > 10) {
this.$message.warning('文件大小不得超过10M')
}
},
// 文件状态改变时的钩子
fileChange(file, batchFileList) {
console.log(file.raw)
this.batchFileList.push(file.raw)
console.log(this.batchFileList)
},
batchRemove(file, batchFileList) {
this.batchFileList = []
},
// 文件超出个数限制时的钩子
exceedFile(files, batchFileList) {
this.$message.warning(`只能选择1个文件`)
},
// 文件上传成功时的钩子
handleSuccess(res, file, batchFileList) {
this.$message.success('文件上传成功')
},
// 文件上传失败时的钩子
handleError() {
this.$message.error('文件上传失败')
},
// 切换优惠方式清除内容
modeChange() {
this.productData.forEach(item => {
item.memberPrices.forEach(itemJ => {
itemJ.price = ''
})
})
},
handleSelectionChange(val) {
console.log(val)
let zuheId = ''
for(let i=0;i<val.length;i++){
zuheId += val[i].productId + ','
}
this.canst = zuheId
}
}
}
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css类
@import url("../../../styles/elDialog.scss");
.tab_show {
padding: 0 20px 20px 20px;
background-color:#fff;
}
.fenye {
margin-top: 20px;
display: flex;
justify-content: flex-end;
position: relative;
}
::v-deep .el-pagination__total {
position: absolute;
left: 10px;
}
.formSearch {
display: flex;
width: 100%;
font-size: 14px;
justify-content: space-between;
}
.uploadDialog{
// display: flex;
// flex-direction: column;
// justify-content: center;
>>>.el-upload__tip{
display: inline-block;
width: 360px;
margin: auto;
}
}
.vipDialog{
.priceTable{
table{
width: 100%;
text-align: center;
border-left: 1px solid #EBEEF5;
border-bottom: 1px solid #EBEEF5;
font-size: 14px;
color: #606266;
border-collapse: collapse;
tr{
border-top: 1px solid #EBEEF5;
th{
padding: 12px 0;
background: #EEF3FF;
color: #333;
border-right: 1px solid #EBEEF5;
}
td{
padding: 12px 0;
border-right: 1px solid #EBEEF5;
&.td-input{
.el-input{
width: 80px;
margin-right: 0;
}
}
&:nth-child(1),&:nth-child(2){
width: 80px;
}
.el-input{
width: 100px;
margin-right: 10px;
}
}
}
}
}
.clearBtn{
margin-top: 20px;
}
}
</style>