index.vue 13 KB
<!-- 商品管理-集团 -->
<template>
  <div class="marketing pro-group p-t-20">
    <div class="m-b-20">
      <!-- <el-button :size="size" type="primary" class="el-icon-plus" @click="handleEdit(1)">新建商品</el-button> -->
      <GLButton :size="size" type="primary" fn="新建商品" class="el-icon-plus" @click="handleEdit(1)">新建商品</GLButton>
    </div>
    <div class="container">
      <div>
        <glsx-combi-form ref="showSearch" :formObj="searchForm" :formData="searchFormData">
          <span slot="footer" class="footer-btns">
            <el-button :size="size" type="default" @click="handleReset">重置</el-button>
            <el-button :size="size" type="primary" @click="handleSearch">查询</el-button>
          </span>
        </glsx-combi-form>
      </div>

      <!-- 表格区域 开始 -->
      <GLTable ref="proGroupRef" class="table-img" :getListApi="getListApi" :noDataImg="url" :searchForm="tableSearchForm" :tableParams="tableParams" :pageConf="pageConf" :beforeSearchFn="beforeSearchFn" @setTableFinishFun="setTableFinishFun">
        <div slot="goodsImage" class="table-image" slot-scope="scope">
          <img class="img-60" :src="scope.scope.goodsImage" />
        </div>
        <div class="table-btns" slot="auth-button" slot-scope="scope">
          <el-button type="text" @click="handleUpOrDown(scope.scope)">{{ scope.scope.goodsStatus ? '下架' : '上架' }}</el-button>
          <!-- <el-button v-if="!scope.scope.goodsStatus" type="text" @click="handleEdit(2, scope.scope)">编辑</el-button>
          <el-button v-if="scope.scope.goodsStatus" type="text" @click="handleEdit(3, scope.scope)">查看</el-button> -->
          <GLButton v-if="!scope.scope.goodsStatus" type="text" fn="编辑" @click="handleEdit(2, scope.scope)">编辑</GLButton>
          <GLButton v-if="scope.scope.goodsStatus" type="text" fn="查看" @click="handleEdit(3, scope.scope)">查看</GLButton>
          <el-button type="text" @click="handleTop(scope.scope)">置顶</el-button>
          <el-button type="text" @click="handleShare(scope.scope)">分享</el-button>
        </div>
      </GLTable>
      <!-- 表格区域 end -->
    </div>
    <!-- 下架 -->
    <glsx-dialog title="下架" :isVisible.sync="isDownVisible" @confirmDialog="handleDownConfirm" @closeDialog="handleDownClose">
      <el-form ref="downRef" label-width="100px" :rules="downRules" :model="downForm">
        <el-form-item label="下架备注" prop="offReason">
          <el-input :size="size" v-model="downForm.offReason" placeholder="请输入下架备注" maxlength="30"></el-input>
        </el-form-item>
      </el-form>
    </glsx-dialog>
    <!-- 分享 -->
    <share-dialog :isShareVisible="isShareVisible" :shareForm="shareForm" @handleShareClose="handleShareClose"></share-dialog>
  </div>
</template>

<script>
import * as dayjs from 'dayjs'
import shareDialog from './components/shareDialog'
import url from "@/assets/empty.png"
export default {
  name: 'productGroup',
  data() {
    return {
      size: 'small',
      url: url,
      // 下架
      isDownVisible: false,
      downForm: {
        id: '',
        offReason: '',
        goodsStatus: 0
      },
      downRules: {
        offReason: [{ required: true, message: '请输入下架备注', trigger: 'change' }]
      },
      // 分享
      isShareVisible: false,
      shareForm: {},
      // 表格
      searchForm: {
        goodsName: '',
        createDate: [],
        goodsStatus: ''
      },
      tableSearchForm: {},
      // 搜索
      searchFormData: [
        {
          label: '商品名称',
          name: 'goodsName',
          type: 'input',
          maxlength: 20
        },
        {
          label: '创建日期',
          name: 'createDate',
          type: 'datePicker',
          isClear: true,
          pickerOptions: 'beforeRangePickerOptions'
        },
        {
          label: '商品状态',
          name: 'goodsStatus',
          type: 'select',
          optionList: [],
          optionId: 'confId',
          optionLabel: 'confName',
          optionsName: 'goodsStatusList'
        }
      ],
      // 表格
      getListApi: this.$apis.proManage.getListApi,
      tableParams: {
        listName: 'list',
        tableOption: true,
        tableColumns: [
          {
            label: '商品名称',
            prop: 'goodsName',
            line: 2
          },
          {
            label: '缩略图',
            prop: 'goodsImage',
            addHtml: 'goodsImage'
          },
          {
            label: '浏览量(次)',
            name: 'goodsStatistics',
            width: '180',
            formatter: cellValue => {
              return cellValue.goodsStatistics ? cellValue.goodsStatistics.viewAmount || '0' : '0'
            }
          },
          {
            label: '销量',
            name: 'goodsStatistics',
            formatter: cellValue => {
              return cellValue.goodsStatistics ? cellValue.goodsStatistics.saleAmount || '0' : '0'
            }
          },
          {
            label: '零售价(元)',
            name: 'retailPrice',
            width: '180',
            formatter: cellValue => {
              return cellValue.retailPrice
            }
          },
          {
            label: '库存',
            name: 'goodsStock',
            formatter: cellValue => {
              return cellValue.goodsStock || '0'
            }
          },
          {
            label: '创建日期',
            prop: 'createdDate',
            width: '180',
            formatter: cellValue => {
              return dayjs(cellValue.createdDate).format('YYYY-MM-DD HH:mm:ss')
            }
          },
          {
            label: '商品状态',
            name: 'goodsStatus',
            formatter: cellValue => {
              return this.baseData.goodsStatusList.find(item => item.confId == cellValue.goodsStatus).confName
            }
          }
        ]
      },
      pageConf: { pageSize: 'pageSize', pageNum: 'pageNum', totalCount: 'total' },
      baseData: {
        goodsStatusList: [
          {
            confId: '0',
            confName: '已下架'
          },
          {
            confId: '1',
            confName: '已上架'
          }
        ]
      }
    }
  },

  components: {
    shareDialog
  },

  mounted() {
    this.getOptionsList('searchFormData')
  },

  methods: {
    getParams() {
      var params = JSON.parse(JSON.stringify(this.searchForm))
      if (this.searchForm.createDate.length) {
        params.startTime = new Date(this.searchForm.createDate[0]).toLocaleDateString().replace(/\//g, '-') + ' 00:00:00'
        params.endTime = new Date(this.searchForm.createDate[1]).toLocaleDateString().replace(/\//g, '-') + ' 23:59:59'
      }
      delete params.createDate
      return params
    },
    beforeSearchFn() {
      return new Promise(resolve => {
        this.tableSearchForm = this.getParams()
        delete this.tableSearchForm.createDate
        resolve()
      })
    },

    secondConfirm(title, txt, apiModule, apiName, apiParams, fn) {
      this.$confirm(txt, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        callback: action => {
          if (action == 'confirm') {
            this.$apis[apiModule][apiName](apiParams).then(res => {
              this.$message({
                message: res.code == '0' ? '操作成功' : res.message,
                type: res.code == '0' ? 'success' : 'error'
              })
              if (res.code == '0') {
                fn && fn()
                this.handleSearch()
              }
            })
          }
        }
      })
    },
    // addOrEdit:1-新增;2-编辑;3-查看
    // groupOrStore:1-集团;2-门店
    handleEdit(addOrEdit, row) {
      // addOrEdit == 1 ? this.$router.push('/productGroup/editProduct/' + addOrEdit + '/' + 1) : this.$router.push('/productGroup/editProduct/' + addOrEdit + '/' + 1 + '/' + row.id)
      var titleObj = {
        1: '新建商品',
        2: '编辑商品',
        3: '查看商品',
      }
      var params = {
          addOrEdit: addOrEdit,
          groupOrStore: 1,
          $title:encodeURI(titleObj[addOrEdit])
      }
      addOrEdit != 1 && (params.id = row.id)
      this.$router.push({
        path: '/productGroup/editProduct',
        query: params
      })
    },
    handleUpOrDown(row) {
      if (row.goodsStatus) {
        this.downForm.id = row.id
        this.dwonVisibleChange()
        return true
      }
      this.secondConfirm('上架', '是否确认上架该商品?', 'proManage', 'updateStatusProApi', { id: row.id, goodsStatus: 1 })
    },
    dwonVisibleChange() {
      this.$refs.downRef && this.$refs.downRef.clearValidate()
      this.isDownVisible = !this.isDownVisible
    },
    handleDownClose() {
      this.dwonVisibleChange()
      this.downForm = {
        id: '',
        offReason: '',
        goodsStatus: 0
      }
    },
    handleDownConfirm() {
      this.$refs.downRef.validate(valid => {
        if (valid) {
          this.$apis.proManage.updateStatusProApi(this.downForm).then(res => {
            this.$message({
              message: res.code == '0' ? '操作成功' : res.message,
              type: res.code == '0' ? 'success' : 'error'
            })
            if (res.code == '0') {
              this.handleDownClose()
              this.handleSearch()
            }
          })
        }
      })
    },
    // 置顶
    handleTop(row) {
      this.secondConfirm('置顶', '是否确认置顶该商品?', 'proManage', 'topProApi', { id: row.id })
    },

    handleShare(row) {
      this.$apis.proManage.getProShareApi({ id: row.id }).then(res => {
        this.shareForm = res
        this.handleShareClose()
      })
    },

    handleShareClose() {
      this.isShareVisible = !this.isShareVisible
    },

    getOptionsList(formName, hasAll = true, refreshSigle) {
      if (refreshSigle) {
        var item = this[formName].find(k => k.name == refreshSigle)
        var index = this[formName].findIndex(k => k.name == refreshSigle)
        this.$set(this[formName][index], 'optionList', this.$common.utils.deepClone(this.baseData[item.optionsName]))
        if (hasAll && this.baseData[item.optionsName].length && this.baseData[item.optionsName][0][this[formName][index].optionLabel] !== '全部') {
          const temp = {}
          temp[this[formName][index].optionId] = ''
          temp[this[formName][index].optionLabel] = '全部'
          this[formName][index].optionList.unshift(temp)
        }
      } else {
        this[formName].forEach((item, index) => {
          if (item.type === 'select' && this.baseData[item.optionsName].length) {
            this.$set(this[formName][index], 'optionList', this.$common.utils.deepClone(this.baseData[item.optionsName]))
            if (hasAll && this.baseData[item.optionsName].length && this.baseData[item.optionsName][0][this[formName][index].optionLabel] !== '全部') {
              const temp = {}
              temp[this[formName][index].optionId] = ''
              temp[this[formName][index].optionLabel] = '全部'
              this[formName][index].optionList.unshift(temp)
            }
          }
        })
      }
    },
    
    setTableFinishFun() {
      //接收到的数据为空
      if (!this.$refs.proGroupRef.data.length) {
        this.$refs.proGroupRef.setTableHeightFun()
      } else {
        this.$refs.proGroupRef.tableHeightValues = ''
        this.$refs.proGroupRef.data.forEach(item => {
          item.saleAmount = item.goodsStatistics.saleAmount
          if (item.specificationDetailList.length > 1) {
            let retailPriceList = item.specificationDetailList.sort(function(a, b) {
              return a.retailPrice - b.retailPrice
            })
            let stockeList = item.specificationDetailList.sort(function(a, b) {
              return a.stock - b.stock
            })
            let minRatilPrice = retailPriceList[0].retailPrice / 100
            let maxRatilPrice = retailPriceList[retailPriceList.length - 1].retailPrice / 100
            let minStock = stockeList[0].stock
            let maxStock = stockeList[stockeList.length - 1].stock
            item.retailPrice = minRatilPrice == maxRatilPrice ? minRatilPrice.toFixed(2) : minRatilPrice.toFixed(2) + '-' + maxRatilPrice.toFixed(2)
            item.minRatilPrice = minRatilPrice.toFixed(2)
            item.goodsStock = minStock == maxStock ? minStock : minStock + '-' + maxStock
          } else if (item.specificationDetailList.length == 1) {
            item.retailPrice = (item.specificationDetailList[0].retailPrice / 100).toFixed(2)
            item.minRatilPrice = item.retailPrice
            item.goodsStock = item.specificationDetailList[0].stock
          } else {
            item.retailPrice = (item.retailPrice / 100).toFixed(2)
            item.minRatilPrice = item.retailPrice
          }
        })
      }
    },

    handleSearch() {
      this.$refs.proGroupRef.refreshTable()
    },

    handleReset() {
      this.searchForm = {
        goodsName: '',
        createDate: [],
        goodsStatus: ''
      }
      this.handleSearch()
    }
  }
}
</script>

<style scoped></style>