1 parent
88db96c374
commit
ae9ed6c2b9
15 files changed
+1542
-638
No files matched your search
@@ -0,0 +1,457 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
import { onMounted, reactive, ref } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { useRbac } from '@/stores/rbac'
|
||||
import {
|
||||
createRewardActivityApi,
|
||||
createRewardTaskApi,
|
||||
getRewardRulesApi,
|
||||
manualRewardGrantApi,
|
||||
pageRewardActivitiesApi,
|
||||
pageRewardGrantsApi,
|
||||
pageRewardTasksApi,
|
||||
saveRewardRulesApi,
|
||||
updateRewardActivityApi,
|
||||
updateRewardActivityStatusApi,
|
||||
updateRewardTaskApi,
|
||||
updateRewardTaskStatusApi
|
||||
} from '@/api/admin/reward'
|
||||
|
||||
/** 奖励中心后台页面状态与操作。 */
|
||||
export function useRewardManagement() {
|
||||
const { can } = useRbac()
|
||||
const activeTab = ref('tasks')
|
||||
|
||||
const typeOptions = [
|
||||
['DAILY_CHECK_IN', '每日签到'],
|
||||
['WATCH_DURATION', '观看时长'],
|
||||
['WATCH_EPISODES', '观看集数'],
|
||||
['FIRST_LOGIN', '首次登录'],
|
||||
['COMPLETE_PROFILE', '完善资料'],
|
||||
['FIRST_PURCHASE', '首次购买'],
|
||||
['FOLLOW_SOCIAL', '关注社交账号'],
|
||||
['INVITE_FRIEND', '邀请好友'],
|
||||
['CUSTOM_LINK', '自定义任务']
|
||||
]
|
||||
const cycleOptions = [
|
||||
['ONCE', '一次性'],
|
||||
['DAILY', '每日'],
|
||||
['WEEKLY', '每周']
|
||||
]
|
||||
const sectionOptions = [
|
||||
['CHECK_IN', '签到奖励'],
|
||||
['DAILY', '每日任务'],
|
||||
['NEWBIE', '新手任务'],
|
||||
['ACTIVITY', '活动任务']
|
||||
]
|
||||
const actionOptions = [
|
||||
['NONE', '无动作'],
|
||||
['OPEN_HOME', '打开首页'],
|
||||
['OPEN_PROFILE', '打开个人资料'],
|
||||
['OPEN_WALLET', '打开钱包'],
|
||||
['OPEN_URL', '打开链接']
|
||||
]
|
||||
|
||||
const tasks = reactive({ rows: [], total: 0, page: 1, size: 10, loading: false })
|
||||
const taskFilters = reactive({
|
||||
keyword: '',
|
||||
type: undefined,
|
||||
sectionCode: undefined,
|
||||
status: undefined
|
||||
})
|
||||
const taskDrawerVisible = ref(false)
|
||||
const taskSaving = ref(false)
|
||||
const editingTaskId = ref(null)
|
||||
const taskForm = reactive(emptyTaskForm())
|
||||
|
||||
const activities = reactive({ rows: [], total: 0, page: 1, size: 10, loading: false })
|
||||
const activityFilters = reactive({ keyword: '', status: undefined })
|
||||
const activityDrawerVisible = ref(false)
|
||||
const activitySaving = ref(false)
|
||||
const editingActivityId = ref(null)
|
||||
const activityForm = reactive(emptyActivityForm())
|
||||
|
||||
const rules = ref([])
|
||||
const rulesLoading = ref(false)
|
||||
const rulesSaving = ref(false)
|
||||
const grants = reactive({
|
||||
rows: [],
|
||||
total: 0,
|
||||
page: 1,
|
||||
size: 10,
|
||||
loading: false,
|
||||
userId: '',
|
||||
status: undefined
|
||||
})
|
||||
const grantVisible = ref(false)
|
||||
const grantSaving = ref(false)
|
||||
const grantForm = reactive({ userId: '', taskId: '', amount: 10, idempotentKey: '', reason: '' })
|
||||
const tabLoaded = { tasks: true, activities: false, rules: false, grants: false }
|
||||
|
||||
function emptyTaskForm() {
|
||||
return {
|
||||
code: '',
|
||||
sectionCode: 'DAILY',
|
||||
type: 'WATCH_DURATION',
|
||||
verifyMode: 'SERVER',
|
||||
targetValue: 1,
|
||||
rewardCoin: 10,
|
||||
cycleType: 'DAILY',
|
||||
cycleLimit: 1,
|
||||
actionType: 'OPEN_HOME',
|
||||
actionUrl: '',
|
||||
iconUrl: '',
|
||||
titleId: '',
|
||||
titleEn: '',
|
||||
titleZh: '',
|
||||
subtitleId: '',
|
||||
subtitleEn: '',
|
||||
subtitleZh: '',
|
||||
ctaId: 'Mulai',
|
||||
ctaEn: 'Start',
|
||||
ctaZh: '去完成',
|
||||
sectionTitleId: 'Tugas harian',
|
||||
sectionTitleEn: 'Daily tasks',
|
||||
sectionTitleZh: '每日任务',
|
||||
configJson: '',
|
||||
sort: 0,
|
||||
startAt: null,
|
||||
endAt: null
|
||||
}
|
||||
}
|
||||
|
||||
function emptyActivityForm() {
|
||||
return {
|
||||
code: '',
|
||||
coverUrl: '',
|
||||
actionType: 'NONE',
|
||||
actionUrl: '',
|
||||
titleId: '',
|
||||
titleEn: '',
|
||||
titleZh: '',
|
||||
summaryId: '',
|
||||
summaryEn: '',
|
||||
summaryZh: '',
|
||||
ctaId: '',
|
||||
ctaEn: '',
|
||||
ctaZh: '',
|
||||
sort: 0,
|
||||
startAt: null,
|
||||
endAt: null
|
||||
}
|
||||
}
|
||||
|
||||
async function loadTasks() {
|
||||
tasks.loading = true
|
||||
try {
|
||||
const res = await pageRewardTasksApi({
|
||||
pageIndex: tasks.page,
|
||||
pageSize: tasks.size,
|
||||
keyword: taskFilters.keyword.trim() || undefined,
|
||||
type: taskFilters.type,
|
||||
sectionCode: taskFilters.sectionCode,
|
||||
status: taskFilters.status
|
||||
})
|
||||
tasks.rows = res.data?.list || []
|
||||
tasks.total = res.data?.total || 0
|
||||
} finally {
|
||||
tasks.loading = false
|
||||
}
|
||||
}
|
||||
|
||||
function queryTasks() {
|
||||
tasks.page = 1
|
||||
loadTasks()
|
||||
}
|
||||
|
||||
function updateTaskFilter(name, value) {
|
||||
taskFilters[name] = value
|
||||
}
|
||||
|
||||
function changeTaskPage(page) {
|
||||
tasks.page = page
|
||||
loadTasks()
|
||||
}
|
||||
|
||||
function resetTaskFilters() {
|
||||
Object.assign(taskFilters, {
|
||||
keyword: '',
|
||||
type: undefined,
|
||||
sectionCode: undefined,
|
||||
status: undefined
|
||||
})
|
||||
queryTasks()
|
||||
}
|
||||
|
||||
function openCreateTask() {
|
||||
editingTaskId.value = null
|
||||
Object.assign(taskForm, emptyTaskForm(), { sort: tasks.total + 1 })
|
||||
taskDrawerVisible.value = true
|
||||
}
|
||||
|
||||
function openEditTask(row) {
|
||||
editingTaskId.value = row.id
|
||||
Object.assign(taskForm, emptyTaskForm(), row)
|
||||
taskDrawerVisible.value = true
|
||||
}
|
||||
|
||||
async function saveTask(data) {
|
||||
if (!data.code?.trim() || !data.titleId?.trim() || !data.sectionTitleId?.trim())
|
||||
return ElMessage.warning('请填写任务编码、印尼语标题和分组标题')
|
||||
if (data.targetValue <= 0 || data.rewardCoin < 0)
|
||||
return ElMessage.warning('目标值和奖励金币不符合要求')
|
||||
taskSaving.value = true
|
||||
try {
|
||||
const payload = {
|
||||
...data,
|
||||
code: data.code.trim(),
|
||||
titleId: data.titleId.trim(),
|
||||
sectionTitleId: data.sectionTitleId.trim()
|
||||
}
|
||||
if (editingTaskId.value) await updateRewardTaskApi(editingTaskId.value, payload)
|
||||
else await createRewardTaskApi(payload)
|
||||
ElMessage.success(editingTaskId.value ? '任务已更新' : '任务已创建,默认下线')
|
||||
taskDrawerVisible.value = false
|
||||
await loadTasks()
|
||||
} finally {
|
||||
taskSaving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function toggleTask(row) {
|
||||
await updateRewardTaskStatusApi(row.id, row.status === 1 ? 0 : 1)
|
||||
ElMessage.success(row.status === 1 ? '任务已下线' : '任务已上线')
|
||||
await loadTasks()
|
||||
}
|
||||
|
||||
async function loadActivities() {
|
||||
activities.loading = true
|
||||
try {
|
||||
const res = await pageRewardActivitiesApi({
|
||||
pageIndex: activities.page,
|
||||
pageSize: activities.size,
|
||||
keyword: activityFilters.keyword.trim() || undefined,
|
||||
status: activityFilters.status
|
||||
})
|
||||
activities.rows = res.data?.list || []
|
||||
activities.total = res.data?.total || 0
|
||||
} finally {
|
||||
activities.loading = false
|
||||
}
|
||||
}
|
||||
|
||||
function queryActivities() {
|
||||
activities.page = 1
|
||||
loadActivities()
|
||||
}
|
||||
|
||||
function updateActivityFilter(name, value) {
|
||||
activityFilters[name] = value
|
||||
}
|
||||
|
||||
function changeActivityPage(page) {
|
||||
activities.page = page
|
||||
loadActivities()
|
||||
}
|
||||
|
||||
function resetActivityFilters() {
|
||||
Object.assign(activityFilters, { keyword: '', status: undefined })
|
||||
queryActivities()
|
||||
}
|
||||
|
||||
function openCreateActivity() {
|
||||
editingActivityId.value = null
|
||||
Object.assign(activityForm, emptyActivityForm(), { sort: activities.total + 1 })
|
||||
activityDrawerVisible.value = true
|
||||
}
|
||||
|
||||
function openEditActivity(row) {
|
||||
editingActivityId.value = row.id
|
||||
Object.assign(activityForm, emptyActivityForm(), row)
|
||||
activityDrawerVisible.value = true
|
||||
}
|
||||
|
||||
async function saveActivity(data) {
|
||||
if (!data.code?.trim() || !data.titleId?.trim())
|
||||
return ElMessage.warning('请填写活动编码和印尼语标题')
|
||||
activitySaving.value = true
|
||||
try {
|
||||
const payload = { ...data, code: data.code.trim(), titleId: data.titleId.trim() }
|
||||
if (editingActivityId.value) await updateRewardActivityApi(editingActivityId.value, payload)
|
||||
else await createRewardActivityApi(payload)
|
||||
ElMessage.success(editingActivityId.value ? '活动已更新' : '活动已创建,默认下线')
|
||||
activityDrawerVisible.value = false
|
||||
await loadActivities()
|
||||
} finally {
|
||||
activitySaving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function toggleActivity(row) {
|
||||
await updateRewardActivityStatusApi(row.id, row.status === 1 ? 0 : 1)
|
||||
ElMessage.success(row.status === 1 ? '活动已下线' : '活动已上线')
|
||||
await loadActivities()
|
||||
}
|
||||
|
||||
async function loadRules() {
|
||||
rulesLoading.value = true
|
||||
try {
|
||||
rules.value = (await getRewardRulesApi()).data || []
|
||||
} finally {
|
||||
rulesLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function saveRules() {
|
||||
rulesSaving.value = true
|
||||
try {
|
||||
await saveRewardRulesApi(
|
||||
rules.value.map(({ dayIndex, rewardCoin }) => ({ dayIndex, rewardCoin }))
|
||||
)
|
||||
ElMessage.success('签到规则已保存')
|
||||
} finally {
|
||||
rulesSaving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function updateRule(dayIndex, rewardCoin) {
|
||||
const rule = rules.value.find((item) => item.dayIndex === dayIndex)
|
||||
if (rule) rule.rewardCoin = rewardCoin
|
||||
}
|
||||
|
||||
async function loadGrants() {
|
||||
grants.loading = true
|
||||
try {
|
||||
const res = await pageRewardGrantsApi({
|
||||
pageIndex: grants.page,
|
||||
pageSize: grants.size,
|
||||
userId: grants.userId || undefined,
|
||||
status: grants.status
|
||||
})
|
||||
grants.rows = res.data?.list || []
|
||||
grants.total = res.data?.total || 0
|
||||
} finally {
|
||||
grants.loading = false
|
||||
}
|
||||
}
|
||||
|
||||
function queryGrants() {
|
||||
grants.page = 1
|
||||
loadGrants()
|
||||
}
|
||||
|
||||
function updateGrantFilter(name, value) {
|
||||
grants[name] = value
|
||||
}
|
||||
|
||||
function resetGrantFilters() {
|
||||
Object.assign(grants, { userId: '', status: undefined })
|
||||
queryGrants()
|
||||
}
|
||||
|
||||
function changeGrantPage(page) {
|
||||
grants.page = page
|
||||
loadGrants()
|
||||
}
|
||||
|
||||
function openGrant() {
|
||||
Object.assign(grantForm, {
|
||||
userId: grants.userId,
|
||||
taskId: '',
|
||||
amount: 10,
|
||||
idempotentKey: '',
|
||||
reason: ''
|
||||
})
|
||||
grantVisible.value = true
|
||||
}
|
||||
|
||||
async function confirmGrant() {
|
||||
if (!grantForm.userId || grantForm.amount <= 0)
|
||||
return ElMessage.warning('请填写用户 ID 和补发金币')
|
||||
grantSaving.value = true
|
||||
try {
|
||||
await manualRewardGrantApi({
|
||||
...grantForm,
|
||||
taskId: grantForm.taskId || undefined,
|
||||
idempotentKey: grantForm.idempotentKey || undefined
|
||||
})
|
||||
ElMessage.success('奖励已通过钱包流水补发')
|
||||
grantVisible.value = false
|
||||
await loadGrants()
|
||||
} finally {
|
||||
grantSaving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function handleTab(name) {
|
||||
if (tabLoaded[name]) return
|
||||
tabLoaded[name] = true
|
||||
if (name === 'activities') loadActivities()
|
||||
if (name === 'rules') loadRules()
|
||||
if (name === 'grants') loadGrants()
|
||||
}
|
||||
|
||||
function optionLabel(options, value) {
|
||||
return options.find((item) => item[0] === value)?.[1] || value || '-'
|
||||
}
|
||||
|
||||
onMounted(loadTasks)
|
||||
|
||||
return {
|
||||
can,
|
||||
activeTab,
|
||||
typeOptions,
|
||||
cycleOptions,
|
||||
sectionOptions,
|
||||
actionOptions,
|
||||
tasks,
|
||||
taskFilters,
|
||||
taskDrawerVisible,
|
||||
taskSaving,
|
||||
editingTaskId,
|
||||
taskForm,
|
||||
activities,
|
||||
activityFilters,
|
||||
activityDrawerVisible,
|
||||
activitySaving,
|
||||
editingActivityId,
|
||||
activityForm,
|
||||
rules,
|
||||
rulesLoading,
|
||||
rulesSaving,
|
||||
grants,
|
||||
grantVisible,
|
||||
grantSaving,
|
||||
grantForm,
|
||||
loadTasks,
|
||||
queryTasks,
|
||||
resetTaskFilters,
|
||||
updateTaskFilter,
|
||||
changeTaskPage,
|
||||
openCreateTask,
|
||||
openEditTask,
|
||||
saveTask,
|
||||
toggleTask,
|
||||
loadActivities,
|
||||
queryActivities,
|
||||
resetActivityFilters,
|
||||
updateActivityFilter,
|
||||
changeActivityPage,
|
||||
openCreateActivity,
|
||||
openEditActivity,
|
||||
saveActivity,
|
||||
toggleActivity,
|
||||
saveRules,
|
||||
updateRule,
|
||||
loadGrants,
|
||||
queryGrants,
|
||||
updateGrantFilter,
|
||||
resetGrantFilters,
|
||||
changeGrantPage,
|
||||
openGrant,
|
||||
confirmGrant,
|
||||
handleTab,
|
||||
optionLabel
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user