liveCoupon.vue 17 KB

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