goods.vue 17 KB

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