goods.vue 18 KB

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