goods.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. @change="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="productName"
  153. label="产品"
  154. class-name="clickable-column"
  155. ></el-table-column>
  156. <el-table-column
  157. prop="price"
  158. label="价格"
  159. class-name="clickable-column"
  160. ></el-table-column>
  161. <el-table-column
  162. prop="stock"
  163. label="库存"
  164. class-name="clickable-column"
  165. ></el-table-column>
  166. </el-table>
  167. <pagination
  168. v-show="goodsTotal > 0"
  169. :total="goodsTotal"
  170. :page.sync="queryGoodParams.pageNum"
  171. :limit.sync="queryGoodParams.pageSize"
  172. @pagination="getStoreProductLists"
  173. style="margin-top: 20px;"
  174. />
  175. </div>
  176. <div slot="footer" class="dialog-footer" style="background-color: inherit;">
  177. <div style="display: flex; justify-content: space-between; align-items: center;">
  178. <span class="selected-count">当前已选择 <span style="color: #00BFFF; font-style: italic;">{{ selectedGoods.length }}</span> 商品</span>
  179. <div>
  180. <el-button @click="goodsDialogVisible = false">取 消</el-button>
  181. <el-button type="primary" @click="confirmAddGoods">确 定</el-button>
  182. </div>
  183. </div>
  184. </div>
  185. </el-dialog>
  186. <el-dialog
  187. title="调整库存"
  188. :visible.sync="stockDialogVisible"
  189. width="400px"
  190. :close-on-click-modal="false"
  191. :close-on-press-escape="false"
  192. >
  193. <el-form :model="stockForm" ref="stockForm" :rules="stockRules">
  194. <el-form-item label="调整后库存" prop="stock">
  195. <el-input-number
  196. v-model="stockForm.stock"
  197. :min="0"
  198. :max="999999"
  199. controls-position="right"
  200. style="width: 100%;"
  201. ></el-input-number>
  202. </el-form-item>
  203. </el-form>
  204. <div slot="footer" class="dialog-footer">
  205. <el-button @click="stockDialogVisible = false">取 消</el-button>
  206. <el-button type="primary" @click="confirmStockChange">确 定</el-button>
  207. </div>
  208. </el-dialog>
  209. <el-dialog
  210. title="调整销量"
  211. :visible.sync="salesDialogVisible"
  212. width="400px"
  213. :close-on-click-modal="false"
  214. :close-on-press-escape="false"
  215. >
  216. <el-form :model="salesForm" ref="salesForm" :rules="salesRules">
  217. <el-form-item label="调整后销量" prop="newSales">
  218. <el-input-number
  219. v-model="salesForm.sales"
  220. :min="0"
  221. :max="999999"
  222. controls-position="right"
  223. style="width: 100%;"
  224. ></el-input-number>
  225. </el-form-item>
  226. </el-form>
  227. <div slot="footer" class="dialog-footer">
  228. <el-button @click="salesDialogVisible = false">取 消</el-button>
  229. <el-button type="primary" @click="confirmSalesChange">确 定</el-button>
  230. </div>
  231. </el-dialog>
  232. </div>
  233. </div>
  234. </template>
  235. <script>
  236. import {addLiveGoods, delLiveGoods, listLiveGoods, listStoreProduct, handleShelfOrUn, handleDeleteSelected,handleIsShowChange,updateGoodsStock,updateLiveGoods} from "@/api/live/liveGoods";
  237. export default {
  238. data() {
  239. return {
  240. liveId: '',
  241. loading: true,
  242. searchTitle: '',
  243. queryGoodParams: {
  244. pageNum: 1,
  245. pageSize: 10,
  246. keywords: null,
  247. },
  248. goodsLiveList: [],
  249. goodsLiveTotal: 0,
  250. goodsParams: {
  251. pageNum: 1,
  252. pageSize: 10,
  253. liveId: null
  254. },
  255. goodsList: [],
  256. goodsTotal: 0,
  257. selectedGoods: [],
  258. goodsLoading: false,
  259. goodsDialogVisible: false,
  260. multipleSelection: [],
  261. allChecked: false,
  262. isIndeterminate: false,
  263. socket: null,
  264. stockDialogVisible: false,
  265. salesDialogVisible: false,
  266. stockForm: {
  267. goodsId: '',
  268. stock: 0
  269. },
  270. salesForm: {
  271. goodsId: '',
  272. sales: 0,
  273. productId: 0,
  274. },
  275. stockRules: {
  276. stock: [
  277. { required: true, message: '请输入库存数量', trigger: 'blur' },
  278. { type: 'number', min: 0, message: '库存数量不能小于0', trigger: 'blur' }
  279. ]
  280. },
  281. salesRules: {
  282. sales: [
  283. { required: true, message: '请输入销量数量', trigger: 'blur' },
  284. { type: 'number', min: 0, message: '库存数量不能小于0', trigger: 'blur' }
  285. ]
  286. }
  287. };
  288. },
  289. watch: {
  290. // 监听路由的 query 参数变化
  291. '$route.query': {
  292. handler(newQuery) {
  293. if (this.$route.params.liveId) {
  294. this.liveId = this.$route.params.liveId;
  295. }else {
  296. this.liveId = this.$route.query.liveId;
  297. }
  298. this.goodsParams.liveId = this.liveId
  299. if(this.liveId == null) {
  300. return;
  301. }
  302. this.getLiveGoodsList();
  303. this.socket = this.$store.state.liveWs[this.liveId]
  304. },
  305. // 初始化时立即执行一次
  306. immediate: true
  307. }
  308. },
  309. created() {
  310. // if (this.$route.params.liveId) {
  311. // this.liveId = this.$route.params.liveId;
  312. // }else {
  313. // this.liveId = this.$route.query.liveId;
  314. // }
  315. // this.goodsParams.liveId = this.liveId
  316. },
  317. methods: {
  318. handleSwitchClick(row) {
  319. // 1. 获取「即将切换到的目标状态」(当前状态取反)
  320. const targetStatus = row.isShow;
  321. const originalIsShow = !row.isShow
  322. const goodsList = [row.goodsId];
  323. if (this.socket == null) {
  324. this.$message.error("请从直播间开启展示状态!");
  325. this.$nextTick(() => {
  326. row.isShow = originalIsShow;
  327. });
  328. return;
  329. }
  330. handleIsShowChange({"goodsIds":goodsList,"isShow":targetStatus,"liveId":this.liveId}).then(res=>{
  331. if(res.code == 200){
  332. row.isShow = res.isShow
  333. if (res.msg == "目前仅支持单一物品展示") {
  334. this.$message.error(res.msg)
  335. }
  336. if (this.socket != null) {
  337. const msg = {
  338. cmd: 'goods',
  339. data: {"liveId":this.liveId,"goodsId":goodsList[0],"status":targetStatus ? 1 : 0}
  340. };
  341. this.socket.send(JSON.stringify(msg));
  342. } else {
  343. row.isShow = originalIsShow;
  344. }
  345. }
  346. }).catch(err => {
  347. // 网络错误时,同样恢复原始状态
  348. row.isShow = originalIsShow;
  349. // this.$set(row, 'isShow', originalIsShow); // 响应式修复
  350. console.error("状态切换失败:", err);
  351. });
  352. },
  353. handleShelf(){
  354. this.handleShelfOrUn(1)
  355. },
  356. handleUnshelf(){
  357. this.handleShelfOrUn(0)
  358. },
  359. handleShelfOrUn(type){
  360. if (this.multipleSelection.length > 0) {
  361. const goodsList = this.getSelectedList();
  362. handleShelfOrUn({"goodsIds":goodsList,"status":type,"liveId":this.liveId}).then(res=>{
  363. this.dealResult(res)
  364. })
  365. } else {
  366. this.$message.info("请选择下架商品!")
  367. }
  368. },
  369. handleDeleteSelected(){
  370. if (this.multipleSelection.length > 0) {
  371. const goodsList = this.getSelectedList();
  372. handleDeleteSelected({"goodsIds":goodsList,"liveId":this.liveId}).then(res=>{
  373. this.dealResult(res)
  374. })
  375. } else {
  376. this.$message.info("请选择被删除的商品!")
  377. }
  378. },
  379. dealResult(res){
  380. if (res.code == 200) {
  381. this.getLiveGoodsList();
  382. this.$refs.goodTable.clearSelection();
  383. } else {
  384. this.$message.error(res.msg);
  385. }
  386. },
  387. getSelectedList(){
  388. var goodsList = []
  389. this.multipleSelection.forEach(item => {
  390. goodsList.push(item.goodsId);
  391. })
  392. return goodsList;
  393. },
  394. // 全选或取消全选
  395. toggleSelectAll(val) {
  396. this.checked = val; // 更新 checkbox 的状态
  397. if (val) {
  398. // 如果 checkbox 被选中,则全选
  399. this.toggleSelection(this.goodsLiveList);
  400. } else {
  401. // 如果 checkbox 被取消选中,则取消全选
  402. this.toggleSelection();
  403. }
  404. },
  405. toggleSelection(rows) {
  406. if (rows && !this.isIndeterminate) {
  407. rows.forEach(row => {
  408. this.$refs.goodTable.toggleRowSelection(row);
  409. });
  410. } else {
  411. this.$refs.goodTable.clearSelection();
  412. }
  413. },
  414. // 多选框选中数据
  415. handleSelectionChange(val) {
  416. this.multipleSelection = val;
  417. // 根据选择项的数量更新 checkbox 的状态
  418. this.allChecked = val.length === this.goodsLiveList.length;
  419. this.isIndeterminate = val.length > 0 && val.length < this.goodsLiveList.length;
  420. },
  421. getLiveGoodsList() {
  422. this.loading = true
  423. listLiveGoods(this.goodsParams).then(response => {
  424. this.goodsLiveList = response.rows
  425. this.goodsLiveTotal = response.total
  426. this.loading = false
  427. })
  428. },
  429. handleAddLiveGoods(){
  430. this.goodsDialogVisible = true;
  431. this.getStoreProductLists()
  432. },
  433. handleGoodsSearch(){
  434. this.queryGoodParams.pageNum = 1
  435. this.queryGoodParams.keywords = this.searchTitle
  436. this.getStoreProductLists()
  437. },
  438. handleGoodsChange(goods) {
  439. this.selectedGoods = goods
  440. },
  441. confirmAddGoods(){
  442. if (this.selectedGoods.length === 0) {
  443. this.$message({
  444. message: '请选择要添加的商品',
  445. type: 'warning'
  446. })
  447. return
  448. }
  449. addLiveGoods({
  450. liveId: this.liveId,
  451. productsId: this.selectedGoods.map(item => item.productId).join(',')
  452. }).then(response => {
  453. if (response.code !== 200) {
  454. this.$message({
  455. message: response.msg,
  456. type: 'warning'
  457. });
  458. return;
  459. }
  460. this.goodsDialogVisible = false;
  461. this.getLiveGoodsList()
  462. })
  463. },
  464. handleGoodDelete(row){
  465. delLiveGoods(row.goodsId).then(response => {
  466. this.getLiveGoodsList()
  467. })
  468. },
  469. /** 处理行点击事件 */
  470. handleGoodsRowClick(row, column) {
  471. // 如果点击的是复选框列,不进行处理
  472. if (column.type === 'selection') {
  473. return
  474. }
  475. // 获取表格实例
  476. const table = this.$refs.goodsTable[0]
  477. if (!table) {
  478. return
  479. }
  480. // 判断当前行是否已经被选中
  481. const isSelected = this.selectedGoods.some(item => item.id === row.id)
  482. // 切换选中状态
  483. table.toggleRowSelection(row, !isSelected)
  484. },
  485. getStoreProductLists() {
  486. listStoreProduct(this.queryGoodParams).then(response => {
  487. this.goodsList = response.rows
  488. this.goodsTotal = response.total
  489. this.loading = false
  490. })
  491. },
  492. handleGoodStock(row){
  493. this.stockForm.goodsId = row.goodsId;
  494. this.stockForm.stock = row.stock;
  495. this.stockDialogVisible = true;
  496. },
  497. handleGoodSale(row){
  498. this.salesForm.goodsId = row.goodsId;
  499. this.salesForm.sales = row.sales;
  500. this.salesForm.productId = row.productId;
  501. this.salesDialogVisible = true;
  502. },
  503. // 添加确认修改库存方法
  504. confirmStockChange() {
  505. this.$refs.stockForm.validate((valid) => {
  506. if (valid) {
  507. updateGoodsStock({
  508. goodsId: this.stockForm.goodsId,
  509. stock: this.stockForm.stock
  510. }).then(response => {
  511. if (response.code === 200) {
  512. this.$message.success('库存修改成功');
  513. this.stockDialogVisible = false;
  514. this.getLiveGoodsList(); // 重新获取列表数据
  515. } else {
  516. this.$message.error(response.msg || '库存修改失败');
  517. }
  518. });
  519. }
  520. });
  521. },
  522. // 添加确认修改库存方法
  523. confirmSalesChange() {
  524. this.$refs.salesForm.validate((valid) => {
  525. if (valid) {
  526. updateLiveGoods({
  527. goodsId: this.salesForm.goodsId,
  528. sales: this.salesForm.sales,
  529. productId: this.salesForm.productId
  530. }).then(response => {
  531. if (response.code === 200) {
  532. this.$message.success('销量修改成功');
  533. this.salesDialogVisible = false;
  534. this.getLiveGoodsList(); // 重新获取列表数据
  535. } else {
  536. this.$message.error(response.msg || '销量修改失败');
  537. }
  538. });
  539. }
  540. });
  541. },
  542. }
  543. };
  544. </script >
  545. <style lang="scss" scoped>
  546. .selection-toolbar {
  547. display: flex;
  548. align-items: center;
  549. margin-bottom: 10px;
  550. padding-left: 10px;
  551. background-color: #f0f2f5;
  552. }
  553. .selection-toolbar .el-checkbox {
  554. margin-right: 10px;
  555. background-color: #f0f2f5;
  556. }
  557. /* 调整 checkbox 内部输入框的对齐 */
  558. .selection-toolbar .el-checkbox .el-checkbox__inner {
  559. top: 8px; /* 根据实际需求调整 */
  560. background-color: #f0f2f5;
  561. }
  562. /* 1. 让组件根容器继承 app-main 背景 */
  563. .goods-table-container {
  564. background-color: inherit; /* 继承父容器(app-main)的背景色 */
  565. padding: 20px; /* 可选:保留内边距,避免内容贴边 */
  566. }
  567. /* 2. 让 Element UI 容器/表格匹配背景(若其有默认白色背景) */
  568. .el-container-md {
  569. background-color: inherit; /* 取消 el-container 的默认背景 */
  570. }
  571. .el-table {
  572. background-color: inherit; /* 取消 el-table 的默认白色背景(可选,若需要表格透明) */
  573. /* 若希望表格有背景,但与 app-main 协调:可改为 app-main 背景色的相近色 */
  574. /* background-color: #f8f9fa; */
  575. }
  576. ::v-deep .app-main .goods-table-container {
  577. background-color: #f5f7fa; /* 与 app-main 全局背景统一 */
  578. }
  579. </style>