| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599 | <template>  <div class="el-container-md">    <div class="selection-toolbar">      <el-checkbox :indeterminate="isIndeterminate" v-model="allChecked" @change="toggleSelectAll">        {{ multipleSelection.length > 0 ? `已选 ${multipleSelection.length} 条` : '选中本页' }}      </el-checkbox>      <el-button  plain size="mini" @click="handleShelf">上架</el-button>      <el-button  plain size="mini" @click="handleUnshelf">下架</el-button>      <el-button  plain size="mini" @click="handleDeleteSelected">删除</el-button>      <el-button  plain type="mini" icon="el-icon-plus" @click="handleAddLiveGoods">添加商品</el-button>    </div>    <el-table      ref="goodTable"      :data="goodsLiveList"      style="width: 100%; "      v-loading="loading"      @selection-change="handleSelectionChange"    >      <el-table-column type="selection" width="55"></el-table-column>      <!-- 题干列:显示试题的主要内容 -->      <el-table-column        prop="goodsId"        label="商品id"        show-overflow-tooltip      ></el-table-column>      <el-table-column        label="商品图片"      >        <template slot-scope="scope">          <img            :src="scope.row.imgUrl"            style="display: block; max-width: 100%; width: 100px; height: 100px"          />        </template>      </el-table-column>      <el-table-column        prop="productName"        label="商品名称"      ></el-table-column>      <el-table-column        prop="price"        label="价格"      ></el-table-column>      <el-table-column        prop="stock"        label="库存"      ></el-table-column>      <el-table-column        prop="sales"        label="销量"      ></el-table-column>      <el-table-column        prop="isShow"        label="显示状态"      >        <template slot-scope="scope">          <el-switch            v-model="scope.row.isShow"            @click.native.capture.prevent="handleSwitchClick(scope.row)"            active-color="#13ce66"            inactive-color="#ff4949">          </el-switch>        </template>      </el-table-column>      <el-table-column        prop="status"        label="上下架"      >        <template slot-scope="scope">          <el-tag v-if="scope.row.status == 1">上架</el-tag>          <el-tag type="info" v-if="scope.row.status == 0">下架</el-tag>        </template>      </el-table-column>      <el-table-column        prop="status"        label="店铺上下架"      >        <template slot-scope="scope">          <el-tag v-if="scope.row.fsStatus == 1">上架</el-tag>          <el-tag type="info" v-if="scope.row.fsStatus == 0">下架</el-tag>        </template>      </el-table-column>      <!-- 操作列:包含编辑和删除按钮 -->      <el-table-column        label="操作"        width="180"        fixed="right"      >        <template slot-scope="scope">          <el-button            type="text"            size="small"            style="color: #0066FF;"            @click="handleGoodStock(scope.row)"          >调整库存</el-button>          <el-button            type="text"            size="small"            style="color: #0066FF;"            @click="handleGoodSale(scope.row)"          >调整销量</el-button>          <el-button            type="text"            size="small"            style="color: #F56C6C;"            @click="handleGoodDelete(scope.row)"          >删除</el-button>        </template>      </el-table-column>    </el-table>    <!-- 分页组件:用于分页展示试题列表 -->    <pagination      v-show="goodsLiveTotal > 0"      :total="goodsLiveTotal"      :page.sync="goodsParams.pageNum"      :limit.sync="goodsParams.pageSize"      @pagination="getLiveGoodsList"      style="margin-top: 20px;background-color: inherit;"    />    <!-- 添加商品弹窗 -->    <el-dialog      title="添加商品"      :visible.sync="goodsDialogVisible"      width="800px"      :close-on-click-modal="false"      :close-on-press-escape="false"    >      <div class="dialog-content">        <div style="text-align: right; margin-bottom: 20px;">          <el-input            v-model="searchTitle"            placeholder="请输入产品名称"            style="width: 300px;"            @input="handleGoodsSearch"          ></el-input>        </div>        <el-table          :data="goodsList"          style="width: 100%"          v-loading="goodsLoading"          @selection-change="handleGoodsChange"          @row-click="handleGoodsRowClick"          row-key="id"        >          <el-table-column            type="selection"            width="55"          >          </el-table-column>          <el-table-column            prop="storeName"            label="商铺名称"            class-name="clickable-column"          ></el-table-column>          <el-table-column            prop="productName"            label="产品"            class-name="clickable-column"          ></el-table-column>          <el-table-column            prop="price"            label="价格"            class-name="clickable-column"          ></el-table-column>          <el-table-column            prop="stock"            label="库存"            class-name="clickable-column"          ></el-table-column>        </el-table>        <pagination          v-show="goodsTotal > 0"          :total="goodsTotal"          :page.sync="queryGoodParams.pageNum"          :limit.sync="queryGoodParams.pageSize"          @pagination="getStoreProductLists"          style="margin-top: 20px;"        />      </div>      <div slot="footer" class="dialog-footer" style="background-color: inherit;">        <div style="display: flex; justify-content: space-between; align-items: center;">          <span class="selected-count">当前已选择 <span style="color: #00BFFF; font-style: italic;">{{ selectedGoods.length }}</span> 商品</span>          <div>            <el-button @click="goodsDialogVisible = false">取 消</el-button>            <el-button type="primary" @click="confirmAddGoods">确 定</el-button>          </div>        </div>      </div>    </el-dialog>    <el-dialog      title="调整库存"      :visible.sync="stockDialogVisible"      width="400px"      :close-on-click-modal="false"      :close-on-press-escape="false"    >      <el-form :model="stockForm" ref="stockForm" :rules="stockRules">        <el-form-item label="调整后库存" prop="newStock">          <el-input-number            v-model="stockForm.stock"            :min="0"            :max="999999"            controls-position="right"            style="width: 100%;"          ></el-input-number>        </el-form-item>      </el-form>      <div slot="footer" class="dialog-footer">        <el-button @click="stockDialogVisible = false">取 消</el-button>        <el-button type="primary" @click="confirmStockChange">确 定</el-button>      </div>    </el-dialog>    <el-dialog      title="调整销量"      :visible.sync="salesDialogVisible"      width="400px"      :close-on-click-modal="false"      :close-on-press-escape="false"    >      <el-form :model="salesForm" ref="salesForm" :rules="salesRules">        <el-form-item label="调整后销量" prop="newSales">          <el-input-number            v-model="salesForm.sales"            :min="0"            :max="999999"            controls-position="right"            style="width: 100%;"          ></el-input-number>        </el-form-item>      </el-form>      <div slot="footer" class="dialog-footer">        <el-button @click="salesDialogVisible = false">取 消</el-button>        <el-button type="primary" @click="confirmSalesChange">确 定</el-button>      </div>    </el-dialog>  </div></template><script>import {addLiveGoods, delLiveGoods, listLiveGoods, listStoreProduct, handleShelfOrUn, handleDeleteSelected,handleIsShowChange,updateGoodsStock,updateLiveGoods} from "@/api/live/liveGoods";export default {  data() {    return {      liveId: '',      loading: true,      searchTitle: '',      queryGoodParams: {        pageNum: 1,        pageSize: 10,        productName: null,      },      goodsLiveList: [],      goodsLiveTotal: 0,      goodsParams: {        pageNum: 1,        pageSize: 10,        liveId: null      },      goodsList: [],      goodsTotal: 0,      selectedGoods: [],      goodsLoading: false,      goodsDialogVisible: false,      multipleSelection: [],      allChecked: false,      isIndeterminate: false,      socket: null,      stockDialogVisible: false,      salesDialogVisible: false,      stockForm: {        goodsId: '',        stock: 0      },      salesForm: {        goodsId: '',        sales: 0,        productId: 0,      },      stockRules: {        stock: [          { required: true, message: '请输入库存数量', trigger: 'blur' },          { type: 'number', min: 0, message: '库存数量不能小于0', trigger: 'blur' }        ]      },      salesRules: {        sales: [          { required: true, message: '请输入销量数量', trigger: 'blur' },          { type: 'number', min: 0, message: '库存数量不能小于0', trigger: 'blur' }        ]      }    };  },  watch: {    // 监听路由的 query 参数变化    '$route.query': {      handler(newQuery) {        if (this.$route.params.liveId) {          this.liveId = this.$route.params.liveId;        }else {          this.liveId = this.$route.query.liveId;        }        this.goodsParams.liveId = this.liveId        if(this.liveId == null) {          return;        }        this.getLiveGoodsList();        this.socket = this.$store.state.liveWs[this.liveId]      },      // 初始化时立即执行一次      immediate: true    }  },  created() {    // if (this.$route.params.liveId) {    //   this.liveId = this.$route.params.liveId;    // }else {    //   this.liveId = this.$route.query.liveId;    // }    // this.goodsParams.liveId = this.liveId  },  methods: {    handleSwitchClick(row) {      // 1. 获取「即将切换到的目标状态」(当前状态取反)      const targetStatus = !row.isShow      const goodsList = [row.goodsId];      handleIsShowChange({"goodsIds":goodsList,"isShow":targetStatus,"liveId":this.liveId}).then(res=>{        if(res.code == 200){          row.isShow = res.isShow          if (res.msg == "目前仅支持单一物品展示") {            this.$message.error(res.msg)          }          if (this.socket == null) {            this.$message.error("请从直播间开启展示状态!");          } else {            const msg = {              cmd: 'goods',              data: {"liveId":this.liveId,"goodsId":goodsList[0],"status":targetStatus ? 1 : 0}            }            this.$store.state.liveWs[this.liveId].send(JSON.stringify(msg));          }        }      })    },    handleShelf(){      this.handleShelfOrUn(1)    },    handleUnshelf(){      this.handleShelfOrUn(0)    },    handleShelfOrUn(type){      if (this.multipleSelection.length > 0) {        const goodsList = this.getSelectedList();        handleShelfOrUn({"goodsIds":goodsList,"status":type,"liveId":this.liveId}).then(res=>{          this.dealResult(res)        })      } else {        this.$message.info("请选择下架商品!")      }    },    handleDeleteSelected(){      if (this.multipleSelection.length > 0) {        const goodsList = this.getSelectedList();        handleDeleteSelected({"goodsIds":goodsList,"liveId":this.liveId}).then(res=>{          this.dealResult(res)        })      } else {        this.$message.info("请选择被删除的商品!")      }    },    dealResult(res){      if (res.code == 200) {        this.getLiveGoodsList();        this.$refs.goodTable.clearSelection();      } else {        this.$message.error(res.msg);      }    },    getSelectedList(){      var goodsList = []      this.multipleSelection.forEach(item => {        goodsList.push(item.goodsId);      })      return goodsList;    },    // 全选或取消全选    toggleSelectAll(val) {      this.checked = val; // 更新 checkbox 的状态      if (val) {        // 如果 checkbox 被选中,则全选        this.toggleSelection(this.goodsLiveList);      } else {        // 如果 checkbox 被取消选中,则取消全选        this.toggleSelection();      }    },    toggleSelection(rows) {      if (rows && !this.isIndeterminate) {        rows.forEach(row => {          this.$refs.goodTable.toggleRowSelection(row);        });      } else {        this.$refs.goodTable.clearSelection();      }    },    // 多选框选中数据    handleSelectionChange(val) {      this.multipleSelection = val;      // 根据选择项的数量更新 checkbox 的状态      this.allChecked = val.length === this.goodsLiveList.length;      this.isIndeterminate = val.length > 0 && val.length < this.goodsLiveList.length;    },    getLiveGoodsList() {      this.loading = true      listLiveGoods(this.goodsParams).then(response => {        this.goodsLiveList = response.rows        this.goodsLiveTotal = response.total        this.loading = false      })    },    handleAddLiveGoods(){      this.goodsDialogVisible = true;      this.getStoreProductLists()    },    handleGoodsSearch(){      this.queryGoodParams.pageNum = 1      this.queryGoodParams.productName = this.searchTitle      this.getStoreProductLists()    },    handleGoodsChange(goods) {      this.selectedGoods = goods    },    confirmAddGoods(){      if (this.selectedGoods.length === 0) {        this.$message({          message: '请选择要添加的商品',          type: 'warning'        })        return      }      addLiveGoods({        liveId: this.liveId,        productsId: this.selectedGoods.map(item => item.productId).join(',')      }).then(response => {        if (response.code !== 200) {          this.$message({            message: response.msg,            type: 'warning'          });          return;        }        this.goodsDialogVisible = false;        this.getLiveGoodsList()      })    },    handleGoodDelete(row){      delLiveGoods(row.goodsId).then(response => {        this.getLiveGoodsList()      })    },    /** 处理行点击事件 */    handleGoodsRowClick(row, column) {      // 如果点击的是复选框列,不进行处理      if (column.type === 'selection') {        return      }      // 获取表格实例      const table = this.$refs.goodsTable[0]      if (!table) {        return      }      // 判断当前行是否已经被选中      const isSelected = this.selectedGoods.some(item => item.id === row.id)      // 切换选中状态      table.toggleRowSelection(row, !isSelected)    },    getStoreProductLists() {      listStoreProduct(this.queryGoodParams).then(response => {        this.goodsList = response.rows        this.goodsTotal = response.total        this.loading = false      })    },    handleGoodStock(row){      this.stockForm.goodsId = row.goodsId;      this.stockForm.stock = row.stock;      this.stockDialogVisible = true;    },    handleGoodSale(row){      this.salesForm.goodsId = row.goodsId;      this.salesForm.sales = row.sales;      this.salesForm.productId = row.productId;      this.salesDialogVisible = true;    },    // 添加确认修改库存方法    confirmStockChange() {      this.$refs.stockForm.validate((valid) => {        if (valid) {          updateGoodsStock({            goodsId: this.stockForm.goodsId,            stock: this.stockForm.stock          }).then(response => {            if (response.code === 200) {              this.$message.success('库存修改成功');              this.stockDialogVisible = false;              this.getLiveGoodsList(); // 重新获取列表数据            } else {              this.$message.error(response.msg || '库存修改失败');            }          });        }      });    },    // 添加确认修改库存方法    confirmSalesChange() {      this.$refs.salesForm.validate((valid) => {        if (valid) {          updateLiveGoods({            goodsId: this.salesForm.goodsId,            sales: this.salesForm.sales,            productId: this.salesForm.productId          }).then(response => {            if (response.code === 200) {              this.$message.success('销量修改成功');              this.salesDialogVisible = false;              this.getLiveGoodsList(); // 重新获取列表数据            } else {              this.$message.error(response.msg || '销量修改失败');            }          });        }      });    },  }};</script ><style lang="scss" scoped>.selection-toolbar {  display: flex;  align-items: center;  margin-bottom: 10px;  padding-left: 10px;  background-color: #f0f2f5;}.selection-toolbar .el-checkbox {  margin-right: 10px;  background-color: #f0f2f5;}/* 调整 checkbox 内部输入框的对齐 */.selection-toolbar .el-checkbox .el-checkbox__inner {  top: 8px; /* 根据实际需求调整 */  background-color: #f0f2f5;}/* 1. 让组件根容器继承 app-main 背景 */.goods-table-container {  background-color: inherit; /* 继承父容器(app-main)的背景色 */  padding: 20px; /* 可选:保留内边距,避免内容贴边 */}/* 2. 让 Element UI 容器/表格匹配背景(若其有默认白色背景) */.el-container-md {  background-color: inherit; /* 取消 el-container 的默认背景 */}.el-table {  background-color: inherit; /* 取消 el-table 的默认白色背景(可选,若需要表格透明) */  /* 若希望表格有背景,但与 app-main 协调:可改为 app-main 背景色的相近色 */  /* background-color: #f8f9fa; */}::v-deep .app-main .goods-table-container {  background-color: #f5f7fa; /* 与 app-main 全局背景统一 */}</style>
 |