| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- <template>
- <div class="app-container course-random-red-packet">
- <el-card shadow="never">
- <div slot="header" class="card-header">
- <div>
- <div class="title">配置中心 - 课程随机红包</div>
- <div class="desc">
- 绑定课程项目fs_user_course.project,仅白名单企业可参与;权重总和需为100%,多个企业ID逗号分隔,填写ALL代表全部企业随机红包
- </div>
- </div>
- <el-tag type="info" size="small">红包模块 v2.0</el-tag>
- </div>
- <el-tabs v-model="activeTab" type="card" class="config-tabs">
- <el-tab-pane label="单项目配置" name="single" />
- <el-tab-pane label="批量配置" name="batch" />
- </el-tabs>
- <el-form
- v-show="activeTab === 'single'"
- ref="singleFormRef"
- :model="singleForm"
- :rules="singleRules"
- label-width="120px"
- v-loading="loading"
- >
- <el-form-item label="课程项目" prop="project">
- <el-select
- v-model="singleForm.project"
- placeholder="请选择课程项目"
- filterable
- clearable
- style="width: 420px"
- >
- <el-option
- v-for="item in projectOptions"
- :key="item.dictValue"
- :label="item.dictLabel"
- :value="Number(item.dictValue)"
- />
- </el-select>
- </el-form-item>
- <el-divider content-position="left">红包开关配置</el-divider>
- <el-form-item label="开启随机红包">
- <el-switch
- v-model="singleForm.enableRandom"
- active-color="#13ce66"
- inactive-color="#ff4949"
- />
- </el-form-item>
- <template v-if="singleForm.enableRandom">
- <div v-for="(tier, index) in singleForm.tiers" :key="'single-' + index" class="tier-row">
- <el-form-item
- label="红包金额"
- :prop="'tiers.' + index + '.amount'"
- :rules="[{ required: true, message: '请填写金额', trigger: 'blur' }]"
- >
- <el-input-number
- v-model="tier.amount"
- :min="0.01"
- :max="200"
- :step="0.01"
- :precision="2"
- controls-position="right"
- />
- <span class="unit">元</span>
- </el-form-item>
- <el-form-item
- label="权重占比"
- :prop="'tiers.' + index + '.weight'"
- :rules="[
- { required: true, message: '请填写权重', trigger: 'blur' },
- { type: 'number', min: 1, message: '权重不能小于0', trigger: 'blur' }
- ]"
- >
- <el-input-number
- v-model="tier.weight"
- :min="1"
- :max="100"
- controls-position="right"
- />
- <span class="unit">%</span>
- </el-form-item>
- <el-button
- type="text"
- icon="el-icon-delete"
- class="delete-btn"
- :disabled="singleForm.tiers.length <= 1"
- @click="removeTier('singleForm', index)"
- />
- </div>
- <el-form-item>
- <el-button type="primary" plain icon="el-icon-plus" @click="addTier('singleForm')">新增档位</el-button>
- <span class="weight-total" :class="{ error: singleTotalWeight !== 100 }">
- 权重总和:{{ singleTotalWeight }}%
- </span>
- </el-form-item>
- </template>
- <el-divider content-position="left">企业白名单</el-divider>
- <el-form-item label="允许企业">
- <el-select
- v-model="singleWhitelistCompanyIds"
- multiple
- filterable
- clearable
- collapse-tags
- placeholder="不选择代表全部企业走随机红包"
- style="width: 100%"
- @change="handleWhitelistChange('single')"
- >
- <el-option label="全部企业 (ALL)" value="ALL" />
- <el-option
- v-for="item in companyOptions"
- :key="item.dictValue"
- :label="item.dictLabel"
- :value="String(item.dictValue)"
- />
- </el-select>
- <div class="tip">多个企业ID逗号分隔,内部存储ALL代表全部企业可参与随机红包</div>
- </el-form-item>
- <el-form-item>
- <el-button type="primary" :loading="submitting" @click="submitSingleForm">保存配置</el-button>
- <el-button @click="resetSingleForm">重置</el-button>
- </el-form-item>
- </el-form>
- <el-form
- v-show="activeTab === 'batch'"
- ref="batchFormRef"
- :model="batchForm"
- :rules="batchRules"
- label-width="120px"
- v-loading="batchLoading"
- >
- <el-form-item label="课程项目" prop="projects">
- <el-select
- v-model="batchForm.projects"
- placeholder="请选择课程项目(可多选)"
- filterable
- clearable
- multiple
- collapse-tags
- style="width: 420px"
- >
- <el-option
- v-for="item in projectOptions"
- :key="item.dictValue"
- :label="item.dictLabel"
- :value="Number(item.dictValue)"
- />
- </el-select>
- </el-form-item>
- <el-divider content-position="left">红包开关配置</el-divider>
- <el-form-item label="开启随机红包">
- <el-switch
- v-model="batchForm.enableRandom"
- active-color="#13ce66"
- inactive-color="#ff4949"
- />
- </el-form-item>
- <template v-if="batchForm.enableRandom">
- <div v-for="(tier, index) in batchForm.tiers" :key="'batch-' + index" class="tier-row">
- <el-form-item
- label="红包金额"
- :prop="'tiers.' + index + '.amount'"
- :rules="[{ required: true, message: '请填写金额', trigger: 'blur' }]"
- >
- <el-input-number
- v-model="tier.amount"
- :min="0.01"
- :max="200"
- :step="0.01"
- :precision="2"
- controls-position="right"
- />
- <span class="unit">元</span>
- </el-form-item>
- <el-form-item
- label="权重占比"
- :prop="'tiers.' + index + '.weight'"
- :rules="[
- { required: true, message: '请填写权重', trigger: 'blur' },
- { type: 'number', min: 1, message: '权重不能小于0', trigger: 'blur' }
- ]"
- >
- <el-input-number
- v-model="tier.weight"
- :min="1"
- :max="100"
- controls-position="right"
- />
- <span class="unit">%</span>
- </el-form-item>
- <el-button
- type="text"
- icon="el-icon-delete"
- class="delete-btn"
- :disabled="batchForm.tiers.length <= 1"
- @click="removeTier('batchForm', index)"
- />
- </div>
- <el-form-item>
- <el-button type="primary" plain icon="el-icon-plus" @click="addTier('batchForm')">新增档位</el-button>
- <span class="weight-total" :class="{ error: batchTotalWeight !== 100 }">
- 权重总和:{{ batchTotalWeight }}%
- </span>
- </el-form-item>
- </template>
- <el-divider content-position="left">企业白名单</el-divider>
- <el-form-item label="允许企业">
- <el-select
- v-model="batchWhitelistCompanyIds"
- multiple
- filterable
- clearable
- collapse-tags
- placeholder="不选择代表全部企业走随机红包"
- style="width: 100%"
- @change="handleWhitelistChange('batch')"
- >
- <el-option label="全部企业 (ALL)" value="ALL" />
- <el-option
- v-for="item in companyOptions"
- :key="item.dictValue"
- :label="item.dictLabel"
- :value="String(item.dictValue)"
- />
- </el-select>
- <div class="tip">多个企业ID逗号分隔,内部存储ALL代表全部企业可参与随机红包</div>
- </el-form-item>
- <el-form-item>
- <el-button type="primary" :loading="submitting" @click="submitBatchForm">批量保存</el-button>
- <el-button @click="resetBatchForm">重置</el-button>
- </el-form-item>
- </el-form>
- </el-card>
- </div>
- </template>
- <script>
- import {
- getCourseRandomRedPacket,
- saveCourseRandomRedPacket,
- batchSaveCourseRandomRedPacket
- } from '@/api/course/courseRandomRedPacket'
- import { allList } from '@/api/company/company'
- const defaultTier = () => ({ amount: 1.88, weight: 50 })
- const defaultConfig = () => ({
- enableRandom: false,
- companyWhitelist: '',
- tiers: [defaultTier()]
- })
- export default {
- name: 'CourseRandomRedPacket',
- data() {
- return {
- activeTab: 'single',
- loading: false,
- batchLoading: false,
- submitting: false,
- projectOptions: [],
- companyOptions: [],
- singleWhitelistCompanyIds: [],
- batchWhitelistCompanyIds: [],
- batchReferenceProject: null,
- singleForm: {
- project: null,
- ...defaultConfig()
- },
- batchForm: {
- projects: [],
- ...defaultConfig()
- },
- singleRules: {
- project: [{ required: true, message: '请选择课程项目', trigger: 'change' }]
- },
- batchRules: {
- projects: [{ type: 'array', required: true, min: 1, message: '请至少选择一个课程项目', trigger: 'change' }]
- }
- }
- },
- computed: {
- singleTotalWeight() {
- return this.calcTotalWeight(this.singleForm.tiers)
- },
- batchTotalWeight() {
- return this.calcTotalWeight(this.batchForm.tiers)
- }
- },
- watch: {
- 'singleForm.project'(project) {
- if (project) {
- this.loadSingleConfig(project)
- } else {
- this.applyDefaultConfig(this.singleForm)
- }
- },
- 'batchForm.projects': {
- handler(projects) {
- if (!projects || !projects.length) {
- this.batchReferenceProject = null
- this.applyDefaultConfig(this.batchForm)
- return
- }
- const referenceProject = projects[0]
- if (referenceProject === this.batchReferenceProject) {
- return
- }
- this.batchReferenceProject = referenceProject
- this.loadBatchConfig(referenceProject)
- },
- deep: true
- }
- },
- created() {
- this.getDicts('sys_course_project').then(response => {
- this.projectOptions = response.data || []
- })
- allList().then(res => {
- this.companyOptions = res.rows || []
- })
- if (this.$route.query.project) {
- this.singleForm.project = Number(this.$route.query.project)
- }
- },
- methods: {
- calcTotalWeight(tiers) {
- return (tiers || []).reduce((sum, item) => sum + (Number(item.weight) || 0), 0)
- },
- applyDefaultConfig(form) {
- const defaults = defaultConfig()
- form.enableRandom = defaults.enableRandom
- form.companyWhitelist = defaults.companyWhitelist
- form.tiers = JSON.parse(JSON.stringify(defaults.tiers))
- this.parseWhitelistToUi(form === this.singleForm ? 'single' : 'batch')
- },
- applyConfigData(form, data) {
- if (data) {
- form.enableRandom = !!data.enableRandom
- form.companyWhitelist = data.companyWhitelist || ''
- form.tiers = (data.tiers && data.tiers.length)
- ? JSON.parse(JSON.stringify(data.tiers.map(item => ({
- amount: Number(item.amount),
- weight: Number(item.weight)
- }))))
- : JSON.parse(JSON.stringify(defaultConfig().tiers))
- } else {
- this.applyDefaultConfig(form)
- return
- }
- this.parseWhitelistToUi(form === this.singleForm ? 'single' : 'batch')
- },
- parseWhitelistToUi(mode) {
- const form = mode === 'single' ? this.singleForm : this.batchForm
- const whitelist = (form.companyWhitelist || '').trim()
- const idsKey = mode === 'single' ? 'singleWhitelistCompanyIds' : 'batchWhitelistCompanyIds'
- if (!whitelist || whitelist.toUpperCase() === 'ALL') {
- this[idsKey] = whitelist.toUpperCase() === 'ALL' ? ['ALL'] : []
- return
- }
- this[idsKey] = whitelist.split(',').map(item => item.trim()).filter(Boolean)
- },
- buildWhitelistFromUi(mode) {
- const companyIds = mode === 'single' ? this.singleWhitelistCompanyIds : this.batchWhitelistCompanyIds
- if (companyIds.includes('ALL')) {
- return 'ALL'
- }
- if (companyIds && companyIds.length) {
- return companyIds.join(',')
- }
- return ''
- },
- handleWhitelistChange(mode) {
- const idsKey = mode === 'single' ? 'singleWhitelistCompanyIds' : 'batchWhitelistCompanyIds'
- const companyIds = this[idsKey]
- if (!companyIds.length) {
- return
- }
- if (companyIds.includes('ALL')) {
- this[idsKey] = ['ALL']
- return
- }
- this[idsKey] = companyIds.filter(id => id !== 'ALL')
- },
- loadSingleConfig(project) {
- this.loading = true
- getCourseRandomRedPacket(project).then(res => {
- this.applyConfigData(this.singleForm, res.data)
- }).finally(() => {
- this.loading = false
- })
- },
- loadBatchConfig(project) {
- this.batchLoading = true
- getCourseRandomRedPacket(project).then(res => {
- this.applyConfigData(this.batchForm, res.data)
- }).finally(() => {
- this.batchLoading = false
- })
- },
- validateConfig(form, totalWeight) {
- if (form.enableRandom) {
- if (totalWeight !== 100) {
- this.$message.warning('权重总和必须为 100%')
- return false
- }
- if (!form.tiers.length) {
- this.$message.warning('至少保留一个红包档位')
- return false
- }
- }
- return true
- },
- buildPayload(form, mode) {
- return {
- enableRandom: form.enableRandom,
- companyWhitelist: this.buildWhitelistFromUi(mode),
- tiers: form.enableRandom ? form.tiers : []
- }
- },
- addTier(formName) {
- this[formName].tiers.push({ amount: 1.88, weight: 10 })
- },
- removeTier(formName, index) {
- this[formName].tiers.splice(index, 1)
- },
- resetSingleForm() {
- this.singleForm.project = null
- this.applyDefaultConfig(this.singleForm)
- this.$nextTick(() => {
- if (this.$refs.singleFormRef) {
- this.$refs.singleFormRef.clearValidate()
- }
- })
- },
- resetBatchForm() {
- this.batchForm.projects = []
- this.batchReferenceProject = null
- this.applyDefaultConfig(this.batchForm)
- this.$nextTick(() => {
- if (this.$refs.batchFormRef) {
- this.$refs.batchFormRef.clearValidate()
- }
- })
- },
- submitSingleForm() {
- this.$refs.singleFormRef.validate(valid => {
- if (!valid) {
- return
- }
- if (!this.validateConfig(this.singleForm, this.singleTotalWeight)) {
- return
- }
- this.submitting = true
- saveCourseRandomRedPacket({
- project: this.singleForm.project,
- ...this.buildPayload(this.singleForm, 'single')
- }).then(() => {
- this.$message.success('保存成功')
- }).finally(() => {
- this.submitting = false
- })
- })
- },
- submitBatchForm() {
- this.$refs.batchFormRef.validate(valid => {
- if (!valid) {
- return
- }
- if (!this.validateConfig(this.batchForm, this.batchTotalWeight)) {
- return
- }
- this.submitting = true
- batchSaveCourseRandomRedPacket({
- projects: this.batchForm.projects,
- ...this.buildPayload(this.batchForm, 'batch')
- }).then(() => {
- this.$message.success(`已成功配置 ${this.batchForm.projects.length} 个项目`)
- }).finally(() => {
- this.submitting = false
- })
- })
- }
- }
- }
- </script>
- <style scoped>
- .course-random-red-packet .card-header {
- display: flex;
- justify-content: space-between;
- align-items: flex-start;
- }
- .course-random-red-packet .title {
- font-size: 18px;
- font-weight: 600;
- margin-bottom: 8px;
- }
- .course-random-red-packet .desc,
- .course-random-red-packet .tip {
- color: #909399;
- font-size: 13px;
- line-height: 1.6;
- }
- .course-random-red-packet .config-tabs {
- margin-bottom: 16px;
- }
- .course-random-red-packet .tier-row {
- display: flex;
- align-items: flex-start;
- flex-wrap: wrap;
- gap: 8px;
- margin-bottom: 8px;
- }
- .course-random-red-packet .unit {
- margin-left: 8px;
- color: #606266;
- }
- .course-random-red-packet .delete-btn {
- margin-top: 6px;
- color: #f56c6c;
- }
- .course-random-red-packet .weight-total {
- margin-left: 16px;
- color: #67c23a;
- font-weight: 600;
- }
- .course-random-red-packet .weight-total.error {
- color: #f56c6c;
- }
- </style>
|