batchRedPacket.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <el-dialog
  3. title="批量设置红包"
  4. :visible.sync="dialogVisible"
  5. width="800px"
  6. append-to-body
  7. @close="handleClose"
  8. >
  9. <el-table
  10. v-loading="loading"
  11. :data="tableData"
  12. border
  13. style="width: 100%"
  14. >
  15. <el-table-column
  16. type="index"
  17. label="序号"
  18. width="80"
  19. align="center"
  20. />
  21. <el-table-column
  22. prop="periodName"
  23. label="营期"
  24. align="center"
  25. />
  26. <el-table-column
  27. label="金额(0.1-0.3元)"
  28. align="center"
  29. width="200"
  30. >
  31. <template slot-scope="scope">
  32. <el-input-number
  33. v-model="scope.row.amount"
  34. :min="0.1"
  35. :max="0.3"
  36. :precision="2"
  37. :step="0.01"
  38. size="small"
  39. style="width: 150px"
  40. />
  41. </template>
  42. </el-table-column>
  43. </el-table>
  44. <div slot="footer" class="dialog-footer">
  45. <el-button @click="handleClose">取 消</el-button>
  46. <el-button type="primary" @click="handleSave">保 存</el-button>
  47. </div>
  48. </el-dialog>
  49. </template>
  50. <script>
  51. import { batchSaveRedPacketByPeriod } from "@/api/course/userCoursePeriod";
  52. export default {
  53. name: 'BatchRedPacket',
  54. props: {
  55. visible: {
  56. type: Boolean,
  57. default: false
  58. },
  59. selectedData: {
  60. type: Array,
  61. default: () => []
  62. }
  63. },
  64. data() {
  65. return {
  66. loading: false,
  67. tableData: [],
  68. dialogVisible: false
  69. }
  70. },
  71. watch: {
  72. visible(val) {
  73. this.dialogVisible = val;
  74. if (val) {
  75. this.initTableData()
  76. }
  77. },
  78. dialogVisible(val) {
  79. if (!val) {
  80. // 当弹窗关闭时,通知父组件
  81. this.$emit('update:visible', false);
  82. }
  83. }
  84. },
  85. methods: {
  86. // 初始化表格数据
  87. initTableData() {
  88. this.tableData = this.selectedData.map(item => ({
  89. ...item,
  90. amount: 0.1
  91. }))
  92. },
  93. // 保存
  94. handleSave() {
  95. // 验证金额范围
  96. const invalidItems = this.tableData.filter(item => item.amount < 0.1 || item.amount > 0.3);
  97. if (invalidItems.length > 0) {
  98. this.$message.error('红包金额需要在0.1元至0.3元之间');
  99. return;
  100. }
  101. this.$confirm(`是否确定?确定后营期下的所有公司红包金额都将设置成对应值`, '提示', {
  102. confirmButtonText: '确定',
  103. cancelButtonText: '取消',
  104. type: 'warning'
  105. }).then(() => {
  106. this.loading = true;
  107. const saveData = this.tableData.map(item => ({
  108. periodId: item.periodId,
  109. redPacketMoney: item.amount
  110. }));
  111. batchSaveRedPacketByPeriod(saveData).then(response => {
  112. if (response.code === 200) {
  113. this.$message.success('设置成功');
  114. this.$emit('success');
  115. this.handleClose();
  116. } else {
  117. this.$message.error(response.msg || '批量设置失败');
  118. }
  119. }).catch(error => {
  120. this.$message.error('批量设置失败:' + error.message);
  121. }).finally(() => {
  122. this.loading = false;
  123. });
  124. })
  125. },
  126. // 关闭
  127. handleClose() {
  128. this.dialogVisible = false;
  129. }
  130. }
  131. }
  132. </script>
  133. <style scoped>
  134. .el-input-number {
  135. width: 100%;
  136. }
  137. </style>