goods.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <template>
  2. <div class="el-container-md">
  3. <div class="selection-toolbar">
  4. <el-checkbox :indeterminate="isIndeterminate" v-model="allChecked" @change="toggleSelectAll">
  5. {{ multipleSelection.length > 0 ? `已选 ${multipleSelection.length} 条` : '选中本页' }}
  6. </el-checkbox>
  7. <el-button plain size="mini" @click="handleShelf">上架</el-button>
  8. <el-button plain size="mini" @click="handleUnshelf">下架</el-button>
  9. <el-button plain size="mini" @click="handleDeleteSelected">删除</el-button>
  10. <el-button plain type="mini" icon="el-icon-plus" @click="handleAddLiveGoods">添加商品</el-button>
  11. </div>
  12. <el-table
  13. ref="goodTable"
  14. :data="goodsLiveList"
  15. style="width: 100%; "
  16. v-loading="loading"
  17. @selection-change="handleSelectionChange"
  18. >
  19. <el-table-column type="selection" width="55"></el-table-column>
  20. <!-- 题干列:显示试题的主要内容 -->
  21. <el-table-column
  22. prop="goodsId"
  23. label="商品id"
  24. show-overflow-tooltip
  25. ></el-table-column>
  26. <el-table-column
  27. label="商品图片"
  28. >
  29. <template slot-scope="scope">
  30. <img
  31. :src="scope.row.imgUrl"
  32. style="display: block; max-width: 100%; width: 100px; height: 100px"
  33. />
  34. </template>
  35. </el-table-column>
  36. <el-table-column
  37. prop="productName"
  38. label="商品名称"
  39. ></el-table-column>
  40. <el-table-column
  41. prop="price"
  42. label="价格"
  43. ></el-table-column>
  44. <el-table-column
  45. prop="stock"
  46. label="库存"
  47. ></el-table-column>
  48. <el-table-column
  49. prop="sales"
  50. label="销量"
  51. ></el-table-column>
  52. <el-table-column
  53. prop="isShow"
  54. label="显示状态"
  55. >
  56. <template slot-scope="scope">
  57. <el-switch
  58. v-model="scope.row.isShow"
  59. @click.native.capture.prevent="handleSwitchClick(scope.row)"
  60. active-color="#13ce66"
  61. inactive-color="#ff4949">
  62. </el-switch>
  63. </template>
  64. </el-table-column>
  65. <el-table-column
  66. prop="status"
  67. label="上下架"
  68. >
  69. <template slot-scope="scope">
  70. <el-tag v-if="scope.row.status == 1">上架</el-tag>
  71. <el-tag v-if="scope.row.status == 0">下架</el-tag>
  72. </template>
  73. </el-table-column>
  74. <!-- 操作列:包含编辑和删除按钮 -->
  75. <el-table-column
  76. label="操作"
  77. width="180"
  78. fixed="right"
  79. >
  80. <template slot-scope="scope">
  81. <el-button
  82. type="text"
  83. size="small"
  84. style="color: #0066FF;"
  85. @click="handleGoodStock(scope.row)"
  86. >调整库存</el-button>
  87. <el-button
  88. type="text"
  89. size="small"
  90. style="color: #F56C6C;"
  91. @click="handleGoodDelete(scope.row)"
  92. >删除</el-button>
  93. </template>
  94. </el-table-column>
  95. </el-table>
  96. <!-- 分页组件:用于分页展示试题列表 -->
  97. <pagination
  98. v-show="goodsLiveTotal > 0"
  99. :total="goodsLiveTotal"
  100. :page.sync="goodsParams.pageNum"
  101. :limit.sync="goodsParams.pageSize"
  102. @pagination="getLiveGoodsList"
  103. style="margin-top: 20px;background-color: inherit;"
  104. />
  105. <!-- 添加商品弹窗 -->
  106. <el-dialog
  107. title="添加商品"
  108. :visible.sync="goodsDialogVisible"
  109. width="800px"
  110. :close-on-click-modal="false"
  111. :close-on-press-escape="false"
  112. >
  113. <div class="dialog-content">
  114. <div style="text-align: right; margin-bottom: 20px;">
  115. <el-input
  116. v-model="searchTitle"
  117. placeholder="请输入产品名称"
  118. style="width: 300px;"
  119. @input="handleGoodsSearch"
  120. ></el-input>
  121. </div>
  122. <el-table
  123. :data="goodsList"
  124. style="width: 100%"
  125. v-loading="goodsLoading"
  126. @selection-change="handleGoodsChange"
  127. @row-click="handleGoodsRowClick"
  128. row-key="id"
  129. >
  130. <el-table-column
  131. type="selection"
  132. width="55"
  133. >
  134. </el-table-column>
  135. <el-table-column
  136. prop="storeName"
  137. label="商铺名称"
  138. class-name="clickable-column"
  139. ></el-table-column>
  140. <el-table-column
  141. prop="productName"
  142. label="产品"
  143. class-name="clickable-column"
  144. ></el-table-column>
  145. <el-table-column
  146. prop="price"
  147. label="价格"
  148. class-name="clickable-column"
  149. ></el-table-column>
  150. <el-table-column
  151. prop="stock"
  152. label="库存"
  153. class-name="clickable-column"
  154. ></el-table-column>
  155. </el-table>
  156. <pagination
  157. v-show="goodsTotal > 0"
  158. :total="goodsTotal"
  159. :page.sync="queryGoodParams.pageNum"
  160. :limit.sync="queryGoodParams.pageSize"
  161. @pagination="getStoreProductLists"
  162. style="margin-top: 20px;"
  163. />
  164. </div>
  165. <div slot="footer" class="dialog-footer" style="background-color: inherit;">
  166. <div style="display: flex; justify-content: space-between; align-items: center;">
  167. <span class="selected-count">当前已选择 <span style="color: #00BFFF; font-style: italic;">{{ selectedGoods.length }}</span> 商品</span>
  168. <div>
  169. <el-button @click="goodsDialogVisible = false">取 消</el-button>
  170. <el-button type="primary" @click="confirmAddGoods">确 定</el-button>
  171. </div>
  172. </div>
  173. </div>
  174. </el-dialog>
  175. <el-dialog
  176. title="调整库存"
  177. :visible.sync="stockDialogVisible"
  178. width="400px"
  179. :close-on-click-modal="false"
  180. :close-on-press-escape="false"
  181. >
  182. <el-form :model="stockForm" ref="stockForm" :rules="stockRules">
  183. <el-form-item label="调整后库存" prop="newStock">
  184. <el-input-number
  185. v-model="stockForm.stock"
  186. :min="0"
  187. :max="999999"
  188. controls-position="right"
  189. style="width: 100%;"
  190. ></el-input-number>
  191. </el-form-item>
  192. </el-form>
  193. <div slot="footer" class="dialog-footer">
  194. <el-button @click="stockDialogVisible = false">取 消</el-button>
  195. <el-button type="primary" @click="confirmStockChange">确 定</el-button>
  196. </div>
  197. </el-dialog>
  198. </div>
  199. </template>
  200. <script>
  201. import {addLiveGoods, delLiveGoods, listLiveGoods, listStoreProduct, handleShelfOrUn, handleDeleteSelected,handleIsShowChange,updateGoodsStock} from "@/api/live/liveGoods";
  202. export default {
  203. data() {
  204. return {
  205. liveId: '',
  206. loading: true,
  207. searchTitle: '',
  208. queryGoodParams: {
  209. pageNum: 1,
  210. pageSize: 10,
  211. productName: null,
  212. liveId: null,
  213. storeId: null
  214. },
  215. goodsLiveList: [],
  216. goodsLiveTotal: 0,
  217. goodsParams: {
  218. pageNum: 1,
  219. pageSize: 10,
  220. liveId: null
  221. },
  222. goodsList: [],
  223. goodsTotal: 0,
  224. selectedGoods: [],
  225. goodsLoading: false,
  226. goodsDialogVisible: false,
  227. multipleSelection: [],
  228. allChecked: false,
  229. isIndeterminate: false,
  230. socket: null,
  231. stockDialogVisible: false,
  232. stockForm: {
  233. goodsId: '',
  234. stock: 0
  235. },
  236. stockRules: {
  237. stock: [
  238. { required: true, message: '请输入库存数量', trigger: 'blur' },
  239. { type: 'number', min: 0, message: '库存数量不能小于0', trigger: 'blur' }
  240. ]
  241. }
  242. };
  243. },
  244. watch: {
  245. // 监听路由的 query 参数变化
  246. '$route.query': {
  247. handler(newQuery) {
  248. if (this.$route.params.liveId) {
  249. this.liveId = this.$route.params.liveId;
  250. }else {
  251. this.liveId = this.$route.query.liveId;
  252. }
  253. this.goodsParams.liveId = this.liveId
  254. this.getLiveGoodsList();
  255. this.socket = this.$store.state.liveWs[this.liveId]
  256. },
  257. // 初始化时立即执行一次
  258. immediate: true
  259. }
  260. },
  261. created() {
  262. if (this.$route.params.liveId) {
  263. this.liveId = this.$route.params.liveId;
  264. }else {
  265. this.liveId = this.$route.query.liveId;
  266. }
  267. this.goodsParams.liveId = this.liveId
  268. this.getLiveGoodsList();
  269. this.socket = this.$store.state.liveWs[this.liveId]
  270. },
  271. methods: {
  272. handleSwitchClick(row) {
  273. // 1. 获取「即将切换到的目标状态」(当前状态取反)
  274. const targetStatus = !row.isShow
  275. const goodsList = [row.goodsId];
  276. handleIsShowChange({"goodsIds":goodsList,"isShow":targetStatus,"liveId":this.liveId}).then(res=>{
  277. if(res.code == 200){
  278. row.isShow = res.isShow
  279. if (res.msg == "目前仅支持单一物品展示") {
  280. this.$message.error(res.msg)
  281. }
  282. if (this.socket == null) {
  283. this.$message.error("请从直播间开启展示状态!");
  284. } else {
  285. const msg = {
  286. cmd: 'goods',
  287. data: {"liveId":this.liveId,"goodsId":goodsList[0],"status":targetStatus ? 1 : 0}
  288. }
  289. this.$store.state.liveWs[this.liveId].send(JSON.stringify(msg));
  290. }
  291. }
  292. })
  293. },
  294. handleShelf(){
  295. this.handleShelfOrUn(1)
  296. },
  297. handleUnshelf(){
  298. this.handleShelfOrUn(0)
  299. },
  300. handleShelfOrUn(type){
  301. if (this.multipleSelection.length > 0) {
  302. const goodsList = this.getSelectedList();
  303. handleShelfOrUn({"goodsIds":goodsList,"status":type,"liveId":this.liveId}).then(res=>{
  304. this.dealResult(res)
  305. })
  306. } else {
  307. this.$message.info("请选择下架商品!")
  308. }
  309. },
  310. handleDeleteSelected(){
  311. if (this.multipleSelection.length > 0) {
  312. const goodsList = this.getSelectedList();
  313. handleDeleteSelected({"goodsIds":goodsList,"liveId":this.liveId}).then(res=>{
  314. this.dealResult(res)
  315. })
  316. } else {
  317. this.$message.info("请选择被删除的商品!")
  318. }
  319. },
  320. dealResult(res){
  321. if (res.code == 200) {
  322. this.getLiveGoodsList();
  323. this.$refs.goodTable.clearSelection();
  324. } else {
  325. this.$message.error(res.msg);
  326. }
  327. },
  328. getSelectedList(){
  329. var goodsList = []
  330. this.multipleSelection.forEach(item => {
  331. goodsList.push(item.goodsId);
  332. })
  333. return goodsList;
  334. },
  335. // 全选或取消全选
  336. toggleSelectAll(val) {
  337. this.checked = val; // 更新 checkbox 的状态
  338. if (val) {
  339. // 如果 checkbox 被选中,则全选
  340. this.toggleSelection(this.goodsLiveList);
  341. } else {
  342. // 如果 checkbox 被取消选中,则取消全选
  343. this.toggleSelection();
  344. }
  345. },
  346. toggleSelection(rows) {
  347. if (rows && !this.isIndeterminate) {
  348. rows.forEach(row => {
  349. this.$refs.goodTable.toggleRowSelection(row);
  350. });
  351. } else {
  352. this.$refs.goodTable.clearSelection();
  353. }
  354. },
  355. // 多选框选中数据
  356. handleSelectionChange(val) {
  357. this.multipleSelection = val;
  358. // 根据选择项的数量更新 checkbox 的状态
  359. this.allChecked = val.length === this.goodsLiveList.length;
  360. this.isIndeterminate = val.length > 0 && val.length < this.goodsLiveList.length;
  361. },
  362. getLiveGoodsList() {
  363. this.loading = true
  364. listLiveGoods(this.goodsParams).then(response => {
  365. this.goodsLiveList = response.rows
  366. this.goodsLiveTotal = response.total
  367. this.loading = false
  368. })
  369. },
  370. handleAddLiveGoods(){
  371. this.goodsDialogVisible = true;
  372. this.getStoreProductLists()
  373. },
  374. handleGoodsSearch(){
  375. this.queryGoodParams.pageNum = 1
  376. this.queryGoodParams.productName = this.searchTitle
  377. this.getStoreProductLists()
  378. },
  379. handleGoodsChange(goods) {
  380. this.selectedGoods = goods
  381. },
  382. confirmAddGoods(){
  383. if (this.selectedGoods.length === 0) {
  384. this.$message({
  385. message: '请选择要添加的商品',
  386. type: 'warning'
  387. })
  388. return
  389. }
  390. addLiveGoods({
  391. liveId: this.liveId,
  392. productsId: this.selectedGoods.map(item => item.productId).join(',')
  393. }).then(response => {
  394. if (response.code !== 200) {
  395. this.$message({
  396. message: response.msg,
  397. type: 'warning'
  398. });
  399. return;
  400. }
  401. this.goodsDialogVisible = false;
  402. this.getLiveGoodsList()
  403. })
  404. },
  405. handleGoodDelete(row){
  406. delLiveGoods(row.goodsId).then(response => {
  407. this.getLiveGoodsList()
  408. })
  409. },
  410. /** 处理行点击事件 */
  411. handleGoodsRowClick(row, column) {
  412. // 如果点击的是复选框列,不进行处理
  413. if (column.type === 'selection') {
  414. return
  415. }
  416. // 获取表格实例
  417. const table = this.$refs.goodsTable[0]
  418. if (!table) {
  419. return
  420. }
  421. // 判断当前行是否已经被选中
  422. const isSelected = this.selectedGoods.some(item => item.id === row.id)
  423. // 切换选中状态
  424. table.toggleRowSelection(row, !isSelected)
  425. },
  426. getStoreProductLists() {
  427. this.queryGoodParams.liveId = this.liveId
  428. listStoreProduct(this.queryGoodParams).then(response => {
  429. this.goodsList = response.rows
  430. this.goodsTotal = response.total
  431. this.loading = false
  432. })
  433. },
  434. handleGoodStock(row){
  435. this.stockForm.goodsId = row.goodsId;
  436. this.stockForm.stock = row.stock;
  437. this.stockDialogVisible = true;
  438. },
  439. // 添加确认修改库存方法
  440. confirmStockChange() {
  441. this.$refs.stockForm.validate((valid) => {
  442. if (valid) {
  443. updateGoodsStock({
  444. goodsId: this.stockForm.goodsId,
  445. stock: this.stockForm.stock
  446. }).then(response => {
  447. if (response.code === 200) {
  448. this.$message.success('库存修改成功');
  449. this.stockDialogVisible = false;
  450. this.getLiveGoodsList(); // 重新获取列表数据
  451. } else {
  452. this.$message.error(response.msg || '库存修改失败');
  453. }
  454. });
  455. }
  456. });
  457. },
  458. }
  459. };
  460. </script >
  461. <style lang="scss" scoped>
  462. .selection-toolbar {
  463. display: flex;
  464. align-items: center;
  465. margin-bottom: 10px;
  466. padding-left: 10px;
  467. background-color: #f0f2f5;
  468. }
  469. .selection-toolbar .el-checkbox {
  470. margin-right: 10px;
  471. background-color: #f0f2f5;
  472. }
  473. /* 调整 checkbox 内部输入框的对齐 */
  474. .selection-toolbar .el-checkbox .el-checkbox__inner {
  475. top: 8px; /* 根据实际需求调整 */
  476. background-color: #f0f2f5;
  477. }
  478. /* 1. 让组件根容器继承 app-main 背景 */
  479. .goods-table-container {
  480. background-color: inherit; /* 继承父容器(app-main)的背景色 */
  481. padding: 20px; /* 可选:保留内边距,避免内容贴边 */
  482. }
  483. /* 2. 让 Element UI 容器/表格匹配背景(若其有默认白色背景) */
  484. .el-container-md {
  485. background-color: inherit; /* 取消 el-container 的默认背景 */
  486. }
  487. .el-table {
  488. background-color: inherit; /* 取消 el-table 的默认白色背景(可选,若需要表格透明) */
  489. /* 若希望表格有背景,但与 app-main 协调:可改为 app-main 背景色的相近色 */
  490. /* background-color: #f8f9fa; */
  491. }
  492. ::v-deep .app-main .goods-table-container {
  493. background-color: #f5f7fa; /* 与 app-main 全局背景统一 */
  494. }
  495. </style>