RewardsPanel.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <template>
  2. <div class="rewards-panel">
  3. <el-tabs v-model="activeTab" @tab-click="handleTabClick">
  4. <!-- 奖励列表 -->
  5. <el-tab-pane label="奖励列表" name="list">
  6. <!-- 搜索筛选(家庭锁定) -->
  7. <el-form :inline="true" :model="searchForm" class="mb-20">
  8. <el-form-item label="家庭">
  9. <el-input :value="familyId" disabled class="family-lock-input"></el-input>
  10. </el-form-item>
  11. <el-form-item label="状态">
  12. <el-select v-model="searchForm.status" placeholder="全部状态" clearable @change="handleSearch">
  13. <el-option label="可用" value="available"></el-option>
  14. <el-option label="待审批" value="pending"></el-option>
  15. <el-option label="已兑换" value="exchanged"></el-option>
  16. <el-option label="已拒绝" value="rejected"></el-option>
  17. </el-select>
  18. </el-form-item>
  19. <el-form-item>
  20. <el-button type="primary" @click="handleSearch">搜索</el-button>
  21. <el-button @click="handleReset">重置</el-button>
  22. </el-form-item>
  23. </el-form>
  24. <div class="adm-table-wrap">
  25. <el-table :data="tableData" border v-loading="loading" class="full-width" height="100%">
  26. <el-table-column prop="id" label="ID" width="80">
  27. <template slot="header">
  28. <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>
  29. </template>
  30. </el-table-column>
  31. <el-table-column prop="title" label="奖励名称" min-width="200">
  32. <template slot="header">
  33. <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>
  34. </template>
  35. </el-table-column>
  36. <el-table-column prop="pointsRequired" label="所需积分" width="120">
  37. <template slot-scope="scope">
  38. <StatusTag type="warning" :text="String(scope.row.pointsRequired)" />
  39. </template>
  40. <template slot="header">
  41. <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>
  42. </template>
  43. </el-table-column>
  44. <el-table-column prop="category" label="分类" width="100">
  45. <template slot="header">
  46. <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>
  47. </template>
  48. </el-table-column>
  49. <el-table-column prop="familyId" label="家庭ID" width="80">
  50. <template slot="header">
  51. <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>
  52. </template>
  53. </el-table-column>
  54. <el-table-column prop="status" label="状态" width="100">
  55. <template slot-scope="scope">
  56. <StatusTag :type="getStatusType(scope.row.status)" :text="getStatusText(scope.row.status)" size="small" />
  57. </template>
  58. <template slot="header">
  59. <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>
  60. </template>
  61. </el-table-column>
  62. <el-table-column prop="createdAt" label="创建时间" width="180">
  63. <template slot-scope="scope">
  64. {{ formatDate(scope.row.createdAt) }}
  65. </template>
  66. <template slot="header">
  67. <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>
  68. </template>
  69. </el-table-column>
  70. <el-table-column label="操作" width="200" fixed="right">
  71. <template slot-scope="{ row }">
  72. <el-button
  73. v-if="row.status === 'pending'"
  74. size="mini"
  75. type="primary"
  76. @click="handleApprove(row, true)"
  77. >
  78. 通过
  79. </el-button>
  80. <el-dropdown v-if="row.status === 'pending'" trigger="hover" @command="(cmd) => handleActionCmd(row, cmd)">
  81. <el-button size="mini">
  82. 更多<i class="el-icon-arrow-down el-icon--right"></i>
  83. </el-button>
  84. <el-dropdown-menu slot="dropdown">
  85. <el-dropdown-item command="reject" icon="el-icon-close">拒绝</el-dropdown-item>
  86. </el-dropdown-menu>
  87. </el-dropdown>
  88. </template>
  89. </el-table-column>
  90. </el-table>
  91. </div>
  92. <el-pagination
  93. class="pagination-mt20"
  94. @size-change="handleSizeChange"
  95. @current-change="handleCurrentChange"
  96. :current-page="pagination.current"
  97. :page-sizes="[10, 20, 50, 100]"
  98. :page-size="pagination.size"
  99. :total="pagination.total"
  100. layout="total, sizes, prev, pager, next, jumper"
  101. ></el-pagination>
  102. </el-tab-pane>
  103. <!-- 奖励模板(全局配置,照搬原页面) -->
  104. <el-tab-pane label="奖励模板" name="templates">
  105. <el-button type="primary" size="small" class="mb-15" @click="showCreateTemplate">
  106. 新建模板
  107. </el-button>
  108. <div class="adm-table-wrap">
  109. <el-table :data="templates" border v-loading="loadingTemplates" class="full-width" height="100%">
  110. <el-table-column prop="id" label="ID" width="80"></el-table-column>
  111. <el-table-column prop="title" label="模板名称" min-width="200"></el-table-column>
  112. <el-table-column prop="pointsRequired" label="所需积分" width="120">
  113. <template slot-scope="scope">
  114. <StatusTag type="warning" :text="String(scope.row.pointsRequired)" />
  115. </template>
  116. </el-table-column>
  117. <el-table-column prop="category" label="分类" width="100"></el-table-column>
  118. <el-table-column prop="createdAt" label="创建时间" width="180">
  119. <template slot-scope="scope">
  120. {{ formatDate(scope.row.createdAt) }}
  121. </template>
  122. </el-table-column>
  123. <el-table-column label="操作" width="150" fixed="right">
  124. <template slot-scope="{ row }">
  125. <el-button size="mini" @click="handleDeleteTemplate(row)">删除</el-button>
  126. </template>
  127. </el-table-column>
  128. </el-table>
  129. </div>
  130. </el-tab-pane>
  131. </el-tabs>
  132. <!-- 创建奖励模板对话框 -->
  133. <el-dialog title="新建奖励模板" :visible.sync="dialogVisible" width="500px">
  134. <el-form :model="templateForm" label-width="100px">
  135. <el-form-item label="奖励名称">
  136. <el-input v-model="templateForm.title" placeholder="请输入奖励名称"></el-input>
  137. </el-form-item>
  138. <el-form-item label="所需积分">
  139. <el-input-number v-model="templateForm.pointsRequired" :min="1" :max="10000"></el-input-number>
  140. </el-form-item>
  141. <el-form-item label="分类">
  142. <el-select v-model="templateForm.category" placeholder="请选择分类">
  143. <el-option label="玩具" value="玩具"></el-option>
  144. <el-option label="图书" value="图书"></el-option>
  145. <el-option label="美食" value="美食"></el-option>
  146. <el-option label="娱乐" value="娱乐"></el-option>
  147. <el-option label="其他" value="其他"></el-option>
  148. </el-select>
  149. </el-form-item>
  150. </el-form>
  151. <div slot="footer">
  152. <el-button @click="dialogVisible = false">取消</el-button>
  153. <el-button type="primary" @click="handleCreateTemplate" :loading="creating">确定</el-button>
  154. </div>
  155. </el-dialog>
  156. </div>
  157. </template>
  158. <script>
  159. import SortMixin from '@/mixins/SortMixin'
  160. import { getRewardsList, getRewardTemplates, createRewardTemplate, deleteRewardTemplate, approveReward } from '@/api/admin'
  161. export default {
  162. name: 'RewardsPanel',
  163. mixins: [SortMixin],
  164. props: {
  165. familyId: {
  166. type: [Number, String],
  167. default: null
  168. }
  169. },
  170. data() {
  171. return {
  172. activeTab: 'list',
  173. loading: false,
  174. loadingTemplates: false,
  175. tableData: [],
  176. templates: [],
  177. sortableColumns: { id: 'ID', title: '奖励名称', pointsRequired: '所需积分', category: '分类', familyId: '家庭ID', status: '状态', createdAt: '创建时间' },
  178. searchForm: {
  179. status: ''
  180. },
  181. pagination: {
  182. current: 1,
  183. size: 10,
  184. total: 0
  185. },
  186. dialogVisible: false,
  187. creating: false,
  188. templateForm: {
  189. title: '',
  190. pointsRequired: 100,
  191. category: ''
  192. }
  193. }
  194. },
  195. mounted() {
  196. this.loadRewards()
  197. },
  198. methods: {
  199. handleActionCmd(row, cmd) {
  200. switch (cmd) {
  201. case 'approve': this.handleApprove(row, true); break;
  202. case 'reject': this.handleApprove(row, false); break;
  203. case 'deleteTemplate': this.handleDeleteTemplate(row); break;
  204. }
  205. },
  206. async loadRewards() {
  207. this.loading = true
  208. try {
  209. const params = {
  210. page: this.pagination.current,
  211. size: this.pagination.size,
  212. status: this.searchForm.status
  213. }
  214. if (this.familyId != null) params.familyId = this.familyId
  215. if (this.sortState.length > 0) params.sort = this.sortState
  216. const res = await getRewardsList(params)
  217. this.tableData = res.data.records || res.data || []
  218. this.pagination.total = res.data.total || this.tableData.length
  219. } catch (error) {
  220. this.$message.error(error.message || '加载奖励列表失败')
  221. } finally {
  222. this.loading = false
  223. }
  224. },
  225. async loadTemplates() {
  226. this.loadingTemplates = true
  227. try {
  228. const res = await getRewardTemplates()
  229. this.templates = res.data || []
  230. } catch (error) {
  231. this.$message.error(error.message || '加载模板失败')
  232. } finally {
  233. this.loadingTemplates = false
  234. }
  235. },
  236. handleTabClick(tab) {
  237. if (tab.name === 'templates' && this.templates.length === 0) {
  238. this.loadTemplates()
  239. }
  240. },
  241. handleSearch() {
  242. this.pagination.current = 1
  243. this.loadRewards()
  244. },
  245. handleReset() {
  246. this.searchForm = {
  247. status: ''
  248. }
  249. this.handleSearch()
  250. },
  251. handleSizeChange(val) {
  252. this.pagination.size = val
  253. this.pagination.current = 1
  254. this.loadRewards()
  255. },
  256. handleCurrentChange(val) {
  257. this.pagination.current = val
  258. this.loadRewards()
  259. },
  260. async handleApprove(row, approved) {
  261. this.$confirm(
  262. approved ? `确定通过 "${row.title}" 的兑换申请吗?` : `确定拒绝 "${row.title}" 的兑换申请吗?`,
  263. '提示',
  264. {
  265. confirmButtonText: '确定',
  266. cancelButtonText: '取消',
  267. type: 'warning'
  268. }
  269. ).then(async () => {
  270. try {
  271. await approveReward(row.id, { approved })
  272. this.$message.success(approved ? '已通过' : '已拒绝')
  273. this.loadRewards()
  274. } catch (error) {
  275. this.$message.error(error.message || '操作失败')
  276. }
  277. }).catch(() => {})
  278. },
  279. getStatusType(status) {
  280. const types = {
  281. available: 'success',
  282. pending: 'warning',
  283. exchanged: 'info',
  284. rejected: 'danger'
  285. }
  286. return types[status] || 'info'
  287. },
  288. getStatusText(status) {
  289. const texts = {
  290. available: '可用',
  291. pending: '待审批',
  292. exchanged: '已兑换',
  293. rejected: '已拒绝'
  294. }
  295. return texts[status] || status
  296. },
  297. showCreateTemplate() {
  298. this.templateForm = {
  299. title: '',
  300. pointsRequired: 100,
  301. category: ''
  302. }
  303. this.dialogVisible = true
  304. },
  305. async handleCreateTemplate() {
  306. if (!this.templateForm.title) {
  307. this.$message.warning('请输入奖励名称')
  308. return
  309. }
  310. if (!this.templateForm.category) {
  311. this.$message.warning('请选择分类')
  312. return
  313. }
  314. this.creating = true
  315. try {
  316. await createRewardTemplate(this.templateForm)
  317. this.$message.success('创建成功')
  318. this.dialogVisible = false
  319. this.loadTemplates()
  320. } catch (error) {
  321. this.$message.error(error.message || '创建失败')
  322. } finally {
  323. this.creating = false
  324. }
  325. },
  326. handleDeleteTemplate(row) {
  327. this.$confirm('确定要删除该模板吗?', '提示', {
  328. confirmButtonText: '确定',
  329. cancelButtonText: '取消',
  330. type: 'warning'
  331. }).then(async () => {
  332. try {
  333. await deleteRewardTemplate(row.id)
  334. this.$message.success('删除成功')
  335. this.loadTemplates()
  336. } catch (error) {
  337. this.$message.error(error.message || '删除失败')
  338. }
  339. }).catch(() => {})
  340. }
  341. }
  342. }
  343. </script>
  344. <style scoped>
  345. .family-lock-input { width: 140px; }
  346. .mb-20 { margin-bottom: 24px; }
  347. .mb-15 { margin-bottom: 15px; }
  348. .cursor-pointer { cursor: pointer; }
  349. .pagination-mt15 { margin-top: 15px; text-align: right; }
  350. .pagination-mt20 { margin-top: 24px; text-align: right; }
  351. .full-width { width: 100%; }
  352. </style>