Rewards.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <template>
  2. <div class="page-container">
  3. <el-card>
  4. <PageHeader slot="header" title="奖励管理">
  5. <el-button type="primary" size="small" @click="handleRefresh" :loading="loading">刷新</el-button>
  6. </PageHeader>
  7. <el-tabs v-model="activeTab" @tab-click="handleTabClick">
  8. <!-- 奖励列表 -->
  9. <el-tab-pane label="奖励列表" name="list">
  10. <!-- 搜索筛选 -->
  11. <el-form :inline="true" :model="searchForm" class="mb-20">
  12. <el-form-item label="家庭">
  13. <el-input
  14. v-model="searchForm.familyName"
  15. placeholder="点击选择家庭"
  16. readonly
  17. @click.native="showFamilySelector"
  18. >
  19. <el-button
  20. slot="append"
  21. icon="el-icon-close"
  22. v-if="searchForm.familyId"
  23. @click.stop="clearFamily"
  24. class="padding-8"
  25. ></el-button>
  26. <i slot="suffix" class="el-input__icon el-icon-arrow-down" @click.stop="showFamilySelector"></i>
  27. </el-input>
  28. </el-form-item>
  29. <el-form-item label="状态">
  30. <el-select v-model="searchForm.status" placeholder="全部状态" clearable @change="handleSearch">
  31. <el-option label="可用" value="available"></el-option>
  32. <el-option label="待审批" value="pending"></el-option>
  33. <el-option label="已兑换" value="exchanged"></el-option>
  34. <el-option label="已拒绝" value="rejected"></el-option>
  35. </el-select>
  36. </el-form-item>
  37. <el-form-item>
  38. <el-button type="primary" @click="handleSearch">搜索</el-button>
  39. <el-button @click="handleReset">重置</el-button>
  40. </el-form-item>
  41. </el-form>
  42. <!-- 家庭选择对话框 -->
  43. <el-dialog title="选择家庭" :visible.sync="familyDialogVisible" width="600px">
  44. <el-table :max-height="tableHeight" class="cursor-pointer" :data="familyList" border v-loading="familyLoading" @row-click="selectFamily">
  45. <el-table-column prop="id" label="ID" width="80"></el-table-column>
  46. <el-table-column prop="name" label="家庭名称" min-width="150"></el-table-column>
  47. <el-table-column prop="inviteCode" label="邀请码" width="120"></el-table-column>
  48. <el-table-column prop="createdAt" label="创建时间" width="180">
  49. <template slot-scope="scope">
  50. {{ formatDate(scope.row.createdAt) }}
  51. </template>
  52. </el-table-column>
  53. <el-pagination
  54. class="pagination-mt15"
  55. @current-change="loadFamilies"
  56. :current-page="familyPagination.current"
  57. :page-size="familyPagination.size"
  58. :total="familyPagination.total"
  59. layout="total, prev, pager, next"
  60. ></el-pagination>
  61. </el-dialog>
  62. <div class="adm-table-wrap">
  63. <el-table :data="tableData" border v-loading="loading" class="full-width" height="100%">
  64. <el-table-column prop="id" label="ID" width="80">
  65. <template slot="header">
  66. <span class="sort-header" @click.stop="onSortClick('id', $event)">ID<span v-if="sortIndex('id') !== null" class="sort-index" :class="sortClass('id') === 'sort-asc' ? 'sort-asc-badge' : 'sort-desc-badge'">{{ sortIndex('id') }}</span>{{ sortIcon('id') }}</span>
  67. </template>
  68. </el-table-column>
  69. <el-table-column prop="title" label="奖励名称" min-width="200">
  70. <template slot="header">
  71. <span class="sort-header" @click.stop="onSortClick('title', $event)">奖励名称<span v-if="sortIndex('title') !== null" class="sort-index" :class="sortClass('title') === 'sort-asc' ? 'sort-asc-badge' : 'sort-desc-badge'">{{ sortIndex('title') }}</span>{{ sortIcon('title') }}</span>
  72. </template>
  73. </el-table-column>
  74. <el-table-column prop="pointsRequired" label="所需积分" width="120">
  75. <template slot-scope="scope">
  76. <StatusTag type="warning" :text="String(scope.row.pointsRequired)" />
  77. </template>
  78. <template slot="header">
  79. <span class="sort-header" @click.stop="onSortClick('pointsRequired', $event)">所需积分<span v-if="sortIndex('pointsRequired') !== null" class="sort-index" :class="sortClass('pointsRequired') === 'sort-asc' ? 'sort-asc-badge' : 'sort-desc-badge'">{{ sortIndex('pointsRequired') }}</span>{{ sortIcon('pointsRequired') }}</span>
  80. </template>
  81. </el-table-column>
  82. <el-table-column prop="category" label="分类" width="100">
  83. <template slot="header">
  84. <span class="sort-header" @click.stop="onSortClick('category', $event)">分类<span v-if="sortIndex('category') !== null" class="sort-index" :class="sortClass('category') === 'sort-asc' ? 'sort-asc-badge' : 'sort-desc-badge'">{{ sortIndex('category') }}</span>{{ sortIcon('category') }}</span>
  85. </template>
  86. </el-table-column>
  87. <el-table-column prop="familyId" label="家庭ID" width="80">
  88. <template slot="header">
  89. <span class="sort-header" @click.stop="onSortClick('familyId', $event)">家庭ID<span v-if="sortIndex('familyId') !== null" class="sort-index" :class="sortClass('familyId') === 'sort-asc' ? 'sort-asc-badge' : 'sort-desc-badge'">{{ sortIndex('familyId') }}</span>{{ sortIcon('familyId') }}</span>
  90. </template>
  91. </el-table-column>
  92. <el-table-column prop="status" label="状态" width="100">
  93. <template slot-scope="scope">
  94. <StatusTag :type="getStatusType(scope.row.status)" :text="getStatusText(scope.row.status)" size="small" />
  95. </template>
  96. <template slot="header">
  97. <span class="sort-header" @click.stop="onSortClick('status', $event)">状态<span v-if="sortIndex('status') !== null" class="sort-index" :class="sortClass('status') === 'sort-asc' ? 'sort-asc-badge' : 'sort-desc-badge'">{{ sortIndex('status') }}</span>{{ sortIcon('status') }}</span>
  98. </template>
  99. </el-table-column>
  100. <el-table-column prop="createdAt" label="创建时间" width="180">
  101. <template slot-scope="scope">
  102. {{ formatDate(scope.row.createdAt) }}
  103. </template>
  104. <template slot="header">
  105. <span class="sort-header" @click.stop="onSortClick('createdAt', $event)">创建时间<span v-if="sortIndex('createdAt') !== null" class="sort-index" :class="sortClass('createdAt') === 'sort-asc' ? 'sort-asc-badge' : 'sort-desc-badge'">{{ sortIndex('createdAt') }}</span>{{ sortIcon('createdAt') }}</span>
  106. </template>
  107. </el-table-column>
  108. <el-table-column label="操作" width="200" fixed="right">
  109. <template slot-scope="{ row }">
  110. <el-button
  111. v-if="row.status === 'pending'"
  112. size="mini"
  113. type="primary"
  114. @click="handleApprove(row, true)"
  115. >
  116. 通过
  117. </el-button>
  118. <el-dropdown v-if="row.status === 'pending'" trigger="hover" @command="(cmd) => handleActionCmd(row, cmd)">
  119. <el-button size="mini">
  120. 更多<i class="el-icon-arrow-down el-icon--right"></i>
  121. </el-button>
  122. <el-dropdown-menu slot="dropdown">
  123. <el-dropdown-item command="reject" icon="el-icon-close">拒绝</el-dropdown-item>
  124. </el-dropdown-menu>
  125. </el-dropdown>
  126. </template>
  127. </el-table-column>
  128. </el-table>
  129. </div>
  130. <el-pagination
  131. class="pagination-mt20"
  132. @size-change="handleSizeChange"
  133. @current-change="handleCurrentChange"
  134. :current-page="pagination.current"
  135. :page-sizes="[10, 20, 50, 100]"
  136. :page-size="pagination.size"
  137. :total="pagination.total"
  138. layout="total, sizes, prev, pager, next, jumper"
  139. ></el-pagination>
  140. </el-tab-pane>
  141. <!-- 奖励模板 -->
  142. <el-tab-pane label="奖励模板" name="templates">
  143. <el-button type="primary" size="small" class="mb-15" @click="showCreateTemplate">
  144. 新建模板
  145. </el-button>
  146. <div class="adm-table-wrap">
  147. <el-table :data="templates" border v-loading="loadingTemplates" class="full-width" height="100%">
  148. <el-table-column prop="id" label="ID" width="80"></el-table-column>
  149. <el-table-column prop="title" label="模板名称" min-width="200"></el-table-column>
  150. <el-table-column prop="pointsRequired" label="所需积分" width="120">
  151. <template slot-scope="scope">
  152. <StatusTag type="warning" :text="String(scope.row.pointsRequired)" />
  153. </template>
  154. </el-table-column>
  155. <el-table-column prop="category" label="分类" width="100"></el-table-column>
  156. <el-table-column prop="createdAt" label="创建时间" width="180">
  157. <template slot-scope="scope">
  158. {{ formatDate(scope.row.createdAt) }}
  159. </template>
  160. </el-table-column>
  161. <el-table-column label="操作" width="150" fixed="right">
  162. <template slot-scope="{ row }">
  163. <el-button size="mini" @click="handleDeleteTemplate(row)">删除</el-button>
  164. </template>
  165. </el-table-column>
  166. </el-tab-pane>
  167. </el-tabs>
  168. </el-card>
  169. <!-- 创建奖励模板对话框 -->
  170. <el-dialog title="新建奖励模板" :visible.sync="dialogVisible" width="500px">
  171. <el-form :model="templateForm" label-width="100px">
  172. <el-form-item label="奖励名称">
  173. <el-input v-model="templateForm.title" placeholder="请输入奖励名称"></el-input>
  174. </el-form-item>
  175. <el-form-item label="所需积分">
  176. <el-input-number v-model="templateForm.pointsRequired" :min="1" :max="10000"></el-input-number>
  177. </el-form-item>
  178. <el-form-item label="分类">
  179. <el-select v-model="templateForm.category" placeholder="请选择分类">
  180. <el-option label="玩具" value="玩具"></el-option>
  181. <el-option label="图书" value="图书"></el-option>
  182. <el-option label="美食" value="美食"></el-option>
  183. <el-option label="娱乐" value="娱乐"></el-option>
  184. <el-option label="其他" value="其他"></el-option>
  185. </el-select>
  186. </el-form-item>
  187. </el-form>
  188. <div slot="footer">
  189. <el-button @click="dialogVisible = false">取消</el-button>
  190. <el-button type="primary" @click="handleCreateTemplate" :loading="creating">确定</el-button>
  191. </div>
  192. </el-dialog>
  193. </el-table>
  194. </div>
  195. </el-table>
  196. </div></template>
  197. <script>
  198. import SortMixin from '@/mixins/SortMixin'
  199. import ListStateMixin from '@/mixins/ListStateMixin'
  200. import { getRewardsList, getRewardTemplates, createRewardTemplate, deleteRewardTemplate, approveReward, getFamilyList } from '@/api/admin'
  201. export default {
  202. name: 'Rewards',
  203. mixins: [SortMixin, ListStateMixin],
  204. computed: {
  205. tableHeight() {
  206. return window.innerHeight - 340
  207. }
  208. },
  209. data() {
  210. return {
  211. activeTab: 'list',
  212. loading: false,
  213. loadingTemplates: false,
  214. tableData: [],
  215. templates: [],
  216. sortableColumns: { id: 'ID', title: '奖励名称', pointsRequired: '所需积分', category: '分类', familyId: '家庭ID', status: '状态', createdAt: '创建时间' },
  217. searchForm: {
  218. status: '',
  219. familyId: '',
  220. familyName: ''
  221. },
  222. pagination: {
  223. current: 1,
  224. size: 10,
  225. total: 0
  226. },
  227. // 家庭选择对话框
  228. familyDialogVisible: false,
  229. familyLoading: false,
  230. familyList: [],
  231. familyPagination: {
  232. current: 1,
  233. size: 10,
  234. total: 0
  235. },
  236. // 创建模板对话框
  237. dialogVisible: false,
  238. creating: false,
  239. templateForm: {
  240. title: '',
  241. pointsRequired: 100,
  242. category: ''
  243. }
  244. }
  245. },
  246. mounted() {
  247. this.loadRewards()
  248. },
  249. methods: {
  250. loadList() {
  251. this.loadRewards()
  252. },
  253. handleActionCmd(row, cmd) {
  254. switch (cmd) {
  255. case 'approve': this.handleApprove(row, true); break;
  256. case 'reject': this.handleApprove(row, false); break;
  257. case 'deleteTemplate': this.handleDeleteTemplate(row); break;
  258. }
  259. },
  260. async loadRewards() {
  261. this.loading = true
  262. try {
  263. const params = {
  264. page: this.pagination.current,
  265. size: this.pagination.size,
  266. status: this.searchForm.status,
  267. familyId: this.searchForm.familyId || undefined
  268. }
  269. if (this.sortState.length > 0) params.sort = this.sortState
  270. const res = await getRewardsList(params)
  271. this.tableData = res.data.records || res.data || []
  272. this.pagination.total = res.data.total || this.tableData.length
  273. } catch (error) {
  274. this.$message.error(error.message || '加载奖励列表失败')
  275. } finally {
  276. this.loading = false
  277. }
  278. },
  279. async loadFamilies(page = 1) {
  280. this.familyLoading = true
  281. try {
  282. const res = await getFamilyList({
  283. page: page,
  284. size: this.familyPagination.size
  285. })
  286. this.familyList = res.data.records || res.data || []
  287. this.familyPagination.total = res.data.total || this.familyList.length
  288. this.familyPagination.current = page
  289. } catch (error) {
  290. this.$message.error(error.message || '加载家庭列表失败')
  291. } finally {
  292. this.familyLoading = false
  293. }
  294. },
  295. selectFamily(row) {
  296. this.searchForm.familyId = row.id
  297. this.searchForm.familyName = row.name
  298. this.familyDialogVisible = false
  299. this.handleSearch()
  300. },
  301. showFamilySelector() {
  302. this.familyDialogVisible = true
  303. this.loadFamilies()
  304. },
  305. clearFamily() {
  306. this.searchForm.familyId = ''
  307. this.searchForm.familyName = ''
  308. this.handleSearch()
  309. },
  310. async loadTemplates() {
  311. this.loadingTemplates = true
  312. try {
  313. const res = await getRewardTemplates()
  314. this.templates = res.data || []
  315. } catch (error) {
  316. this.$message.error(error.message || '加载模板失败')
  317. } finally {
  318. this.loadingTemplates = false
  319. }
  320. },
  321. handleTabClick(tab) {
  322. if (tab.name === 'templates' && this.templates.length === 0) {
  323. this.loadTemplates()
  324. }
  325. },
  326. handleSearch() {
  327. this.pagination.current = 1
  328. this.loadRewards()
  329. },
  330. handleReset() {
  331. this.searchForm = {
  332. status: ''
  333. }
  334. this.handleSearch()
  335. },
  336. handleRefresh() {
  337. if (this.activeTab === 'list') {
  338. this.loadRewards()
  339. } else {
  340. this.loadTemplates()
  341. }
  342. },
  343. handleSizeChange(val) {
  344. this.pagination.size = val
  345. this.pagination.current = 1
  346. this.loadRewards()
  347. },
  348. handleCurrentChange(val) {
  349. this.pagination.current = val
  350. this.loadRewards()
  351. },
  352. async handleApprove(row, approved) {
  353. this.$confirm(
  354. approved ? `确定通过 "${row.title}" 的兑换申请吗?` : `确定拒绝 "${row.title}" 的兑换申请吗?`,
  355. '提示',
  356. {
  357. confirmButtonText: '确定',
  358. cancelButtonText: '取消',
  359. type: 'warning'
  360. }
  361. ).then(async () => {
  362. try {
  363. await approveReward(row.id, { approved })
  364. this.$message.success(approved ? '已通过' : '已拒绝')
  365. this.loadRewards()
  366. } catch (error) {
  367. this.$message.error(error.message || '操作失败')
  368. }
  369. }).catch(() => {})
  370. },
  371. getStatusType(status) {
  372. const types = {
  373. available: 'success',
  374. pending: 'warning',
  375. exchanged: 'info',
  376. rejected: 'danger'
  377. }
  378. return types[status] || 'info'
  379. },
  380. getStatusText(status) {
  381. const texts = {
  382. available: '可用',
  383. pending: '待审批',
  384. exchanged: '已兑换',
  385. rejected: '已拒绝'
  386. }
  387. return texts[status] || status
  388. },
  389. showCreateTemplate() {
  390. this.templateForm = {
  391. title: '',
  392. pointsRequired: 100,
  393. category: ''
  394. }
  395. this.dialogVisible = true
  396. },
  397. async handleCreateTemplate() {
  398. if (!this.templateForm.title) {
  399. this.$message.warning('请输入奖励名称')
  400. return
  401. }
  402. if (!this.templateForm.category) {
  403. this.$message.warning('请选择分类')
  404. return
  405. }
  406. this.creating = true
  407. try {
  408. await createRewardTemplate(this.templateForm)
  409. this.$message.success('创建成功')
  410. this.dialogVisible = false
  411. this.loadTemplates()
  412. } catch (error) {
  413. this.$message.error(error.message || '创建失败')
  414. } finally {
  415. this.creating = false
  416. }
  417. },
  418. handleDeleteTemplate(row) {
  419. this.$confirm('确定要删除该模板吗?', '提示', {
  420. confirmButtonText: '确定',
  421. cancelButtonText: '取消',
  422. type: 'warning'
  423. }).then(async () => {
  424. try {
  425. await deleteRewardTemplate(row.id)
  426. this.$message.success('删除成功')
  427. this.loadTemplates()
  428. } catch (error) {
  429. this.$message.error(error.message || '删除失败')
  430. }
  431. }).catch(() => {})
  432. },
  433. }
  434. }
  435. </script>
  436. <style scoped>
  437. .page-container { padding: 24px; }
  438. /* == C 批内联清理工具类 == */
  439. .mb-20 { margin-bottom: 24px; }
  440. .mb-15 { margin-bottom: 15px; }
  441. .padding-8 { padding: 8px; }
  442. .cursor-pointer { cursor: pointer; }
  443. .pagination-mt15 { margin-top: 15px; text-align: right; }
  444. .pagination-mt20 { margin-top: 24px; text-align: right; }
  445. .full-width { width: 100%; }
  446. </style>