liveCoupon.vue 16 KB

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