Rewards.vue 18 KB

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