index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <template>
  2. <div class="app-container course-random-red-packet">
  3. <el-card shadow="never">
  4. <div slot="header" class="card-header">
  5. <div>
  6. <div class="title">配置中心 - 课程随机红包</div>
  7. <div class="desc">
  8. 绑定课程项目fs_user_course.project,仅白名单企业可参与;权重总和需为100%,多个企业ID逗号分隔,填写ALL代表全部企业随机红包
  9. </div>
  10. </div>
  11. <el-tag type="info" size="small">红包模块 v2.0</el-tag>
  12. </div>
  13. <el-tabs v-model="activeTab" type="card" class="config-tabs">
  14. <el-tab-pane label="单项目配置" name="single" />
  15. <el-tab-pane label="批量配置" name="batch" />
  16. </el-tabs>
  17. <el-form
  18. v-show="activeTab === 'single'"
  19. ref="singleFormRef"
  20. :model="singleForm"
  21. :rules="singleRules"
  22. label-width="120px"
  23. v-loading="loading"
  24. >
  25. <el-form-item label="课程项目" prop="project">
  26. <el-select
  27. v-model="singleForm.project"
  28. placeholder="请选择课程项目"
  29. filterable
  30. clearable
  31. style="width: 420px"
  32. >
  33. <el-option
  34. v-for="item in projectOptions"
  35. :key="item.dictValue"
  36. :label="item.dictLabel"
  37. :value="Number(item.dictValue)"
  38. />
  39. </el-select>
  40. </el-form-item>
  41. <el-divider content-position="left">红包开关配置</el-divider>
  42. <el-form-item label="开启随机红包">
  43. <el-switch
  44. v-model="singleForm.enableRandom"
  45. active-color="#13ce66"
  46. inactive-color="#ff4949"
  47. />
  48. </el-form-item>
  49. <template v-if="singleForm.enableRandom">
  50. <div v-for="(tier, index) in singleForm.tiers" :key="'single-' + index" class="tier-row">
  51. <el-form-item
  52. label="红包金额"
  53. :prop="'tiers.' + index + '.amount'"
  54. :rules="[{ required: true, message: '请填写金额', trigger: 'blur' }]"
  55. >
  56. <el-input-number
  57. v-model="tier.amount"
  58. :min="0.01"
  59. :max="200"
  60. :step="0.01"
  61. :precision="2"
  62. controls-position="right"
  63. />
  64. <span class="unit">元</span>
  65. </el-form-item>
  66. <el-form-item
  67. label="权重占比"
  68. :prop="'tiers.' + index + '.weight'"
  69. :rules="[
  70. { required: true, message: '请填写权重', trigger: 'blur' },
  71. { type: 'number', min: 1, message: '权重不能小于0', trigger: 'blur' }
  72. ]"
  73. >
  74. <el-input-number
  75. v-model="tier.weight"
  76. :min="1"
  77. :max="100"
  78. controls-position="right"
  79. />
  80. <span class="unit">%</span>
  81. </el-form-item>
  82. <el-button
  83. type="text"
  84. icon="el-icon-delete"
  85. class="delete-btn"
  86. :disabled="singleForm.tiers.length <= 1"
  87. @click="removeTier('singleForm', index)"
  88. />
  89. </div>
  90. <el-form-item>
  91. <el-button type="primary" plain icon="el-icon-plus" @click="addTier('singleForm')">新增档位</el-button>
  92. <span class="weight-total" :class="{ error: singleTotalWeight !== 100 }">
  93. 权重总和:{{ singleTotalWeight }}%
  94. </span>
  95. </el-form-item>
  96. </template>
  97. <el-divider content-position="left">企业白名单</el-divider>
  98. <el-form-item label="允许企业">
  99. <el-select
  100. v-model="singleWhitelistCompanyIds"
  101. multiple
  102. filterable
  103. clearable
  104. collapse-tags
  105. placeholder="不选择代表全部企业走随机红包"
  106. style="width: 100%"
  107. @change="handleWhitelistChange('single')"
  108. >
  109. <el-option label="全部企业 (ALL)" value="ALL" />
  110. <el-option
  111. v-for="item in companyOptions"
  112. :key="item.dictValue"
  113. :label="item.dictLabel"
  114. :value="String(item.dictValue)"
  115. />
  116. </el-select>
  117. <div class="tip">多个企业ID逗号分隔,内部存储ALL代表全部企业可参与随机红包</div>
  118. </el-form-item>
  119. <el-form-item>
  120. <el-button type="primary" :loading="submitting" @click="submitSingleForm">保存配置</el-button>
  121. <el-button @click="resetSingleForm">重置</el-button>
  122. </el-form-item>
  123. </el-form>
  124. <el-form
  125. v-show="activeTab === 'batch'"
  126. ref="batchFormRef"
  127. :model="batchForm"
  128. :rules="batchRules"
  129. label-width="120px"
  130. v-loading="batchLoading"
  131. >
  132. <el-form-item label="课程项目" prop="projects">
  133. <el-select
  134. v-model="batchForm.projects"
  135. placeholder="请选择课程项目(可多选)"
  136. filterable
  137. clearable
  138. multiple
  139. collapse-tags
  140. style="width: 420px"
  141. >
  142. <el-option
  143. v-for="item in projectOptions"
  144. :key="item.dictValue"
  145. :label="item.dictLabel"
  146. :value="Number(item.dictValue)"
  147. />
  148. </el-select>
  149. </el-form-item>
  150. <el-divider content-position="left">红包开关配置</el-divider>
  151. <el-form-item label="开启随机红包">
  152. <el-switch
  153. v-model="batchForm.enableRandom"
  154. active-color="#13ce66"
  155. inactive-color="#ff4949"
  156. />
  157. </el-form-item>
  158. <template v-if="batchForm.enableRandom">
  159. <div v-for="(tier, index) in batchForm.tiers" :key="'batch-' + index" class="tier-row">
  160. <el-form-item
  161. label="红包金额"
  162. :prop="'tiers.' + index + '.amount'"
  163. :rules="[{ required: true, message: '请填写金额', trigger: 'blur' }]"
  164. >
  165. <el-input-number
  166. v-model="tier.amount"
  167. :min="0.01"
  168. :max="200"
  169. :step="0.01"
  170. :precision="2"
  171. controls-position="right"
  172. />
  173. <span class="unit">元</span>
  174. </el-form-item>
  175. <el-form-item
  176. label="权重占比"
  177. :prop="'tiers.' + index + '.weight'"
  178. :rules="[
  179. { required: true, message: '请填写权重', trigger: 'blur' },
  180. { type: 'number', min: 1, message: '权重不能小于0', trigger: 'blur' }
  181. ]"
  182. >
  183. <el-input-number
  184. v-model="tier.weight"
  185. :min="1"
  186. :max="100"
  187. controls-position="right"
  188. />
  189. <span class="unit">%</span>
  190. </el-form-item>
  191. <el-button
  192. type="text"
  193. icon="el-icon-delete"
  194. class="delete-btn"
  195. :disabled="batchForm.tiers.length <= 1"
  196. @click="removeTier('batchForm', index)"
  197. />
  198. </div>
  199. <el-form-item>
  200. <el-button type="primary" plain icon="el-icon-plus" @click="addTier('batchForm')">新增档位</el-button>
  201. <span class="weight-total" :class="{ error: batchTotalWeight !== 100 }">
  202. 权重总和:{{ batchTotalWeight }}%
  203. </span>
  204. </el-form-item>
  205. </template>
  206. <el-divider content-position="left">企业白名单</el-divider>
  207. <el-form-item label="允许企业">
  208. <el-select
  209. v-model="batchWhitelistCompanyIds"
  210. multiple
  211. filterable
  212. clearable
  213. collapse-tags
  214. placeholder="不选择代表全部企业走随机红包"
  215. style="width: 100%"
  216. @change="handleWhitelistChange('batch')"
  217. >
  218. <el-option label="全部企业 (ALL)" value="ALL" />
  219. <el-option
  220. v-for="item in companyOptions"
  221. :key="item.dictValue"
  222. :label="item.dictLabel"
  223. :value="String(item.dictValue)"
  224. />
  225. </el-select>
  226. <div class="tip">多个企业ID逗号分隔,内部存储ALL代表全部企业可参与随机红包</div>
  227. </el-form-item>
  228. <el-form-item>
  229. <el-button type="primary" :loading="submitting" @click="submitBatchForm">批量保存</el-button>
  230. <el-button @click="resetBatchForm">重置</el-button>
  231. </el-form-item>
  232. </el-form>
  233. </el-card>
  234. </div>
  235. </template>
  236. <script>
  237. import {
  238. getCourseRandomRedPacket,
  239. saveCourseRandomRedPacket,
  240. batchSaveCourseRandomRedPacket
  241. } from '@/api/course/courseRandomRedPacket'
  242. import { allList } from '@/api/company/company'
  243. const defaultTier = () => ({ amount: 1.88, weight: 50 })
  244. const defaultConfig = () => ({
  245. enableRandom: false,
  246. companyWhitelist: '',
  247. tiers: [defaultTier()]
  248. })
  249. export default {
  250. name: 'CourseRandomRedPacket',
  251. data() {
  252. return {
  253. activeTab: 'single',
  254. loading: false,
  255. batchLoading: false,
  256. submitting: false,
  257. projectOptions: [],
  258. companyOptions: [],
  259. singleWhitelistCompanyIds: [],
  260. batchWhitelistCompanyIds: [],
  261. batchReferenceProject: null,
  262. singleForm: {
  263. project: null,
  264. ...defaultConfig()
  265. },
  266. batchForm: {
  267. projects: [],
  268. ...defaultConfig()
  269. },
  270. singleRules: {
  271. project: [{ required: true, message: '请选择课程项目', trigger: 'change' }]
  272. },
  273. batchRules: {
  274. projects: [{ type: 'array', required: true, min: 1, message: '请至少选择一个课程项目', trigger: 'change' }]
  275. }
  276. }
  277. },
  278. computed: {
  279. singleTotalWeight() {
  280. return this.calcTotalWeight(this.singleForm.tiers)
  281. },
  282. batchTotalWeight() {
  283. return this.calcTotalWeight(this.batchForm.tiers)
  284. }
  285. },
  286. watch: {
  287. 'singleForm.project'(project) {
  288. if (project) {
  289. this.loadSingleConfig(project)
  290. } else {
  291. this.applyDefaultConfig(this.singleForm)
  292. }
  293. },
  294. 'batchForm.projects': {
  295. handler(projects) {
  296. if (!projects || !projects.length) {
  297. this.batchReferenceProject = null
  298. this.applyDefaultConfig(this.batchForm)
  299. return
  300. }
  301. const referenceProject = projects[0]
  302. if (referenceProject === this.batchReferenceProject) {
  303. return
  304. }
  305. this.batchReferenceProject = referenceProject
  306. this.loadBatchConfig(referenceProject)
  307. },
  308. deep: true
  309. }
  310. },
  311. created() {
  312. this.getDicts('sys_course_project').then(response => {
  313. this.projectOptions = response.data || []
  314. })
  315. allList().then(res => {
  316. this.companyOptions = res.rows || []
  317. })
  318. if (this.$route.query.project) {
  319. this.singleForm.project = Number(this.$route.query.project)
  320. }
  321. },
  322. methods: {
  323. calcTotalWeight(tiers) {
  324. return (tiers || []).reduce((sum, item) => sum + (Number(item.weight) || 0), 0)
  325. },
  326. applyDefaultConfig(form) {
  327. const defaults = defaultConfig()
  328. form.enableRandom = defaults.enableRandom
  329. form.companyWhitelist = defaults.companyWhitelist
  330. form.tiers = JSON.parse(JSON.stringify(defaults.tiers))
  331. this.parseWhitelistToUi(form === this.singleForm ? 'single' : 'batch')
  332. },
  333. applyConfigData(form, data) {
  334. if (data) {
  335. form.enableRandom = !!data.enableRandom
  336. form.companyWhitelist = data.companyWhitelist || ''
  337. form.tiers = (data.tiers && data.tiers.length)
  338. ? JSON.parse(JSON.stringify(data.tiers.map(item => ({
  339. amount: Number(item.amount),
  340. weight: Number(item.weight)
  341. }))))
  342. : JSON.parse(JSON.stringify(defaultConfig().tiers))
  343. } else {
  344. this.applyDefaultConfig(form)
  345. return
  346. }
  347. this.parseWhitelistToUi(form === this.singleForm ? 'single' : 'batch')
  348. },
  349. parseWhitelistToUi(mode) {
  350. const form = mode === 'single' ? this.singleForm : this.batchForm
  351. const whitelist = (form.companyWhitelist || '').trim()
  352. const idsKey = mode === 'single' ? 'singleWhitelistCompanyIds' : 'batchWhitelistCompanyIds'
  353. if (!whitelist || whitelist.toUpperCase() === 'ALL') {
  354. this[idsKey] = whitelist.toUpperCase() === 'ALL' ? ['ALL'] : []
  355. return
  356. }
  357. this[idsKey] = whitelist.split(',').map(item => item.trim()).filter(Boolean)
  358. },
  359. buildWhitelistFromUi(mode) {
  360. const companyIds = mode === 'single' ? this.singleWhitelistCompanyIds : this.batchWhitelistCompanyIds
  361. if (companyIds.includes('ALL')) {
  362. return 'ALL'
  363. }
  364. if (companyIds && companyIds.length) {
  365. return companyIds.join(',')
  366. }
  367. return ''
  368. },
  369. handleWhitelistChange(mode) {
  370. const idsKey = mode === 'single' ? 'singleWhitelistCompanyIds' : 'batchWhitelistCompanyIds'
  371. const companyIds = this[idsKey]
  372. if (!companyIds.length) {
  373. return
  374. }
  375. if (companyIds.includes('ALL')) {
  376. this[idsKey] = ['ALL']
  377. return
  378. }
  379. this[idsKey] = companyIds.filter(id => id !== 'ALL')
  380. },
  381. loadSingleConfig(project) {
  382. this.loading = true
  383. getCourseRandomRedPacket(project).then(res => {
  384. this.applyConfigData(this.singleForm, res.data)
  385. }).finally(() => {
  386. this.loading = false
  387. })
  388. },
  389. loadBatchConfig(project) {
  390. this.batchLoading = true
  391. getCourseRandomRedPacket(project).then(res => {
  392. this.applyConfigData(this.batchForm, res.data)
  393. }).finally(() => {
  394. this.batchLoading = false
  395. })
  396. },
  397. validateConfig(form, totalWeight) {
  398. if (form.enableRandom) {
  399. if (totalWeight !== 100) {
  400. this.$message.warning('权重总和必须为 100%')
  401. return false
  402. }
  403. if (!form.tiers.length) {
  404. this.$message.warning('至少保留一个红包档位')
  405. return false
  406. }
  407. }
  408. return true
  409. },
  410. buildPayload(form, mode) {
  411. return {
  412. enableRandom: form.enableRandom,
  413. companyWhitelist: this.buildWhitelistFromUi(mode),
  414. tiers: form.enableRandom ? form.tiers : []
  415. }
  416. },
  417. addTier(formName) {
  418. this[formName].tiers.push({ amount: 1.88, weight: 10 })
  419. },
  420. removeTier(formName, index) {
  421. this[formName].tiers.splice(index, 1)
  422. },
  423. resetSingleForm() {
  424. this.singleForm.project = null
  425. this.applyDefaultConfig(this.singleForm)
  426. this.$nextTick(() => {
  427. if (this.$refs.singleFormRef) {
  428. this.$refs.singleFormRef.clearValidate()
  429. }
  430. })
  431. },
  432. resetBatchForm() {
  433. this.batchForm.projects = []
  434. this.batchReferenceProject = null
  435. this.applyDefaultConfig(this.batchForm)
  436. this.$nextTick(() => {
  437. if (this.$refs.batchFormRef) {
  438. this.$refs.batchFormRef.clearValidate()
  439. }
  440. })
  441. },
  442. submitSingleForm() {
  443. this.$refs.singleFormRef.validate(valid => {
  444. if (!valid) {
  445. return
  446. }
  447. if (!this.validateConfig(this.singleForm, this.singleTotalWeight)) {
  448. return
  449. }
  450. this.submitting = true
  451. saveCourseRandomRedPacket({
  452. project: this.singleForm.project,
  453. ...this.buildPayload(this.singleForm, 'single')
  454. }).then(() => {
  455. this.$message.success('保存成功')
  456. }).finally(() => {
  457. this.submitting = false
  458. })
  459. })
  460. },
  461. submitBatchForm() {
  462. this.$refs.batchFormRef.validate(valid => {
  463. if (!valid) {
  464. return
  465. }
  466. if (!this.validateConfig(this.batchForm, this.batchTotalWeight)) {
  467. return
  468. }
  469. this.submitting = true
  470. batchSaveCourseRandomRedPacket({
  471. projects: this.batchForm.projects,
  472. ...this.buildPayload(this.batchForm, 'batch')
  473. }).then(() => {
  474. this.$message.success(`已成功配置 ${this.batchForm.projects.length} 个项目`)
  475. }).finally(() => {
  476. this.submitting = false
  477. })
  478. })
  479. }
  480. }
  481. }
  482. </script>
  483. <style scoped>
  484. .course-random-red-packet .card-header {
  485. display: flex;
  486. justify-content: space-between;
  487. align-items: flex-start;
  488. }
  489. .course-random-red-packet .title {
  490. font-size: 18px;
  491. font-weight: 600;
  492. margin-bottom: 8px;
  493. }
  494. .course-random-red-packet .desc,
  495. .course-random-red-packet .tip {
  496. color: #909399;
  497. font-size: 13px;
  498. line-height: 1.6;
  499. }
  500. .course-random-red-packet .config-tabs {
  501. margin-bottom: 16px;
  502. }
  503. .course-random-red-packet .tier-row {
  504. display: flex;
  505. align-items: flex-start;
  506. flex-wrap: wrap;
  507. gap: 8px;
  508. margin-bottom: 8px;
  509. }
  510. .course-random-red-packet .unit {
  511. margin-left: 8px;
  512. color: #606266;
  513. }
  514. .course-random-red-packet .delete-btn {
  515. margin-top: 6px;
  516. color: #f56c6c;
  517. }
  518. .course-random-red-packet .weight-total {
  519. margin-left: 16px;
  520. color: #67c23a;
  521. font-weight: 600;
  522. }
  523. .course-random-red-packet .weight-total.error {
  524. color: #f56c6c;
  525. }
  526. </style>