liveCoupon.vue 17 KB

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