yj
2024-12-05 ac3234c308e86f20cc63465573f321561ee00690
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
<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.title" clearable class="filter-item" style="width: 200px;" placeholder="输入商品名称" />
      <el-input v-model="listQuery.barcode" clearable class="filter-item" style="width: 200px;" placeholder="输入商品条形码" />
      <el-select v-model="listQuery.status" clearable style="width: 200px" class="filter-item" placeholder="选择商品状态" >
        <el-option v-for="(item,index) in goodsStatusMap" :key="index" :label="item.name" :value="item.value" />
      </el-select>
      <el-cascader :props="{label:'title', value:'id', children:'childrenList', checkStrictly: true}" :options="categoryOptions" placeholder="请选择类目" clearable class="filter-item" filterable style="width: 200px;" @change="handleCategoryOption" />
      <el-button v-permission="['product:product:list']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button v-permission="['product:product:create']" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
      <el-button v-permission="['product:product:batchdelete']" :disabled="selectedIds.length === 0" class="filter-item" type="danger" icon="el-icon-delete" @click="handleBatchDelete">批量删除</el-button>
      <el-button v-permission="['product:product:rebuild']" :loading="rebuilding" class="filter-item" type="primary" @click="handleProductRebuild">重建商品缓存</el-button>
    </div>
 
    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      size="small"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
 
      <el-table-column align="center" type="selection" width="55" />
 
      <el-table-column type="expand" label="sku信息">
        <template slot-scope="props">
          <el-table
            v-loading="listLoading"
            :data="props.row.skuList"
            size="small"
            border
          >
            <el-table-column align="center" label="商品条码" prop="barCode" />
            <el-table-column align="center" label="商品规格" prop="title" />
            <el-table-column align="center" label="商品库存" prop="stock" />
            <el-table-column align="center" label="原价" prop="originalPrice" >
              <template slot-scope="scope">{{ scope.row.originalPrice /100 }}</template>
            </el-table-column>
            <el-table-column align="center" label="现价" prop="price" >
              <template slot-scope="scope">{{ scope.row.price /100 }}</template>
            </el-table-column>
            <el-table-column align="center" label="VIP价格" prop="vipPrice" >
              <template slot-scope="scope">{{ scope.row.vipPrice /100 }}</template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
 
      <el-table-column align="center" label="商品ID" prop="id" width="80" />
 
      <el-table-column align="center" min-width="220" label="名称" prop="title" />
 
      <el-table-column align="center" label="图片" width="80" prop="img">
        <template slot-scope="scope">
          <el-popover placement="right" trigger="hover">
            <!--trigger属性值:hover、click、focus 和 manual-->
            <img :src="scope.row.img" height="230" >
            <img slot="reference" :src="scope.row.img" height="23" >
          </el-popover>
        </template>
      </el-table-column>
 
      <el-table-column align="center" label="富文本" prop="detail" width="80">
        <template slot-scope="scope">
          <el-dialog :visible.sync="detailDialogVisible" title="商品详情">
            <div v-html="goodsDetail" />
          </el-dialog>
          <el-button type="text" size="mini" @click="showDetail(scope.row)">查看</el-button>
        </template>
      </el-table-column>
 
      <el-table-column align="center" label="类目" prop="categoryFullTitle" />
 
      <el-table-column align="center" label="邮费模板" prop="freightTemplateTitle" />
 
      <el-table-column align="center" label="销量" prop="sales" width="80" />
 
      <el-table-column align="center" label="价格" prop="sales" width="80" >
        <template slot-scope="scope">¥{{ scope.row.price / 100.0 }}</template>
      </el-table-column>
 
      <el-table-column align="center" label="VIP格" prop="sales" width="80" >
        <template slot-scope="scope">¥{{ scope.row.vipPrice / 100.0 }}</template>
      </el-table-column>
 
      <el-table-column align="center" label="单位" prop="unit" width="80" />
 
      <el-table-column align="center" label="状态" prop="status" width="90">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.status == 1 ? 'success' : 'info' "
          >{{ scope.row.status == 1 ? '在售' : '未售' }}</el-tag>
        </template>
      </el-table-column>
 
      <el-table-column align="center" label="操作" width="250" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-permission="['product:product:edit']" type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-permission="['product:product:edit']" v-if=" scope.row.status == 1" type="warning" size="mini" @click="freezeOrActivationBtn(scope.row,'freeze')">下架</el-button>
          <el-button v-permission="['product:product:edit']" v-else type="success" size="mini" @click="freezeOrActivationBtn(scope.row,'activation')">上架</el-button>
          <el-button v-permission="['product:product:delete']" type="danger" style="margin:5px" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
 
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
 
    <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibility-height="100" />
    </el-tooltip>
  </div>
</template>
 
<script>
import { listProduct, deleteProduct, detailProduct, freezeOrActivtion, batchDeleteProduct } from '@/api/product'
import BackToTop from '@/components/BackToTop'
import { categoryTree } from '@/api/category'
import { rebuildProductCache } from '@/api/product'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
 
export default {
  name: 'ProductList',
  components: { BackToTop, Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      categoryOptions: [],
      goodsStatusMap: [{ value: 0, name: '下架' }, { value: 1, name: '正常' }],
      listQuery: {
        page: 1,
        limit: 20,
        title: undefined,
        status: undefined,
        barcode: undefined,
        categoryId: undefined
      },
      goodsDetail: '',
      detailDialogVisible: false,
      selectedIds: [],
      rebuilding: false
    }
  },
  created() {
    this.getList()
    this.refreshCategoryOptions()
  },
  methods: {
    getList() {
      this.listLoading = true
      listProduct(this.listQuery)
        .then(response => {
          response.data.data.items.forEach(item => {
            item.price = item.price
            item.originalPrice = item.originalPrice
            item.vipPrice = item.vipPrice
          })
          this.list = response.data.data.items
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push({ path: '/product/upsert' })
    },
    handleUpdate(row) {
      this.$router.push({ path: '/product/upsert', query: { id: row.id }})
    },
    showDetail(row) {
      if (!this.detailDialogVisible) {
        detailProduct(row.id)
          .then(response => {
            this.goodsDetail = response.data.data.detail.replace(/<img/g, "<img style='max-width:100%;height:auto;line-height:0px'")
            this.detailDialogVisible = true
          })
      }
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除商品--' + row.title + '--, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteProduct(row.id)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.errmsg
            })
          })
      }).catch(() => {
        return false
      })
    },
    refreshCategoryOptions() {
      if (this.categoryOptions.length === 0) {
        categoryTree().then(response => {
          this.categoryOptions = response.data.data
        })
      }
    },
    handleCategoryOption(e) {
      if (e !== undefined) {
        const tag = e[e.length - 1]
        this.listQuery.categoryId = tag // 回调指定分类
      } else {
        this.listQuery.categoryId = undefined
      }
    },
    freezeOrActivationBtn(row, title) {
      var status = 0
      var sign = ''
      if (title === 'activation') {
        status = 1
        sign = '激活'
      } else if (title === 'freeze') {
        status = 0
        sign = '冻结'
      } else {
        return false
      }
 
      this.$confirm('此操作将' + sign + '商品--' + row.title + '--, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        freezeOrActivtion(row.id, status).then(response => {
          this.$notify.success({
            title: '成功',
            message: sign + '成功'
          })
          this.getList()
        }).catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
      }).catch(() => {
        return false
      })
    },
    handleSelectionChange(e) {
      const temp = []
      e.forEach(item => {
        temp.push(item.id)
      })
      this.selectedIds = temp
    },
    handleBatchDelete(e) {
      // 执行批量删除
      this.$confirm('此操作将永久删除选中的' + this.selectedIds.length + '件商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        batchDeleteProduct(this.selectedIds)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除成功'
            })
            this.getList()
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.errmsg
            })
          })
      }).catch(() => {
        return false
      })
    },
    handleProductRebuild() {
      this.$confirm('重建商品缓存,在短期内有损系统性能,是否继续?', '提示', {
        confirmButtonText: '立即重建',
        cancelButtonText: '再想想',
        type: 'warning',
        customClass: 'custom-notify'
      }).then(() => {
        this.rebuilding = true
        rebuildProductCache().then(res => {
          this.$notify.success({
            title: '成功',
            customClass: 'custom-notify',
            message: '正在重建1分钟内完成'
          })
          this.rebuilding = false
        }).catch(res => {
          this.rebuilding = false
        })
      })
    }
  }
}
</script>