Children.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. <!-- 搜索筛选 -->
  17. <el-form :inline="true" :model="searchForm" style="margin-bottom: 20px">
  18. <el-form-item label="昵称">
  19. <el-input
  20. v-model="searchForm.nickname"
  21. placeholder="请输入昵称"
  22. clearable
  23. @keyup.enter.native="handleSearch"
  24. ></el-input>
  25. </el-form-item>
  26. <el-form-item label="家庭ID">
  27. <el-input
  28. v-model="searchForm.familyId"
  29. placeholder="请输入家庭ID"
  30. clearable
  31. @keyup.enter.native="handleSearch"
  32. ></el-input>
  33. </el-form-item>
  34. <el-form-item>
  35. <el-button type="primary" @click="handleSearch">搜索</el-button>
  36. <el-button @click="handleReset">重置</el-button>
  37. </el-form-item>
  38. </el-form>
  39. <!-- 孩子列表 -->
  40. <el-table :data="tableData" border v-loading="loading" style="width: 100%" :max-height="tableHeight">
  41. <el-table-column prop="id" label="ID" width="80"></el-table-column>
  42. <el-table-column prop="nickname" label="昵称" width="150"></el-table-column>
  43. <el-table-column prop="age" label="年龄" width="80"></el-table-column>
  44. <el-table-column prop="totalPoints" label="当前积分" width="120">
  45. <template slot-scope="scope">
  46. <el-tag type="success">{{ scope.row.totalPoints || 0 }}</el-tag>
  47. </template>
  48. </el-table-column>
  49. <el-table-column prop="streakDays" label="连续打卡" width="100">
  50. <template slot-scope="scope">
  51. <span>{{ scope.row.streakDays || 0 }}天</span>
  52. </template>
  53. </el-table-column>
  54. <el-table-column prop="familyId" label="家庭ID" width="100"></el-table-column>
  55. <el-table-column prop="penaltyEnabled" label="扣分" width="80">
  56. <template slot-scope="scope">
  57. <el-tag :type="scope.row.penaltyEnabled ? 'danger' : 'info'" size="small">
  58. {{ scope.row.penaltyEnabled ? '开启' : '关闭' }}
  59. </el-tag>
  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. </el-table-column>
  67. <el-table-column label="操作" width="180" fixed="right">
  68. <template slot-scope="{ row }">
  69. <el-button size="mini" type="primary" @click="handleEdit(row)">编辑</el-button>
  70. <el-dropdown trigger="hover" @command="(cmd) => handleActionCmd(row, cmd)">
  71. <el-button size="mini">
  72. 更多<i class="el-icon-arrow-down el-icon--right"></i>
  73. </el-button>
  74. <el-dropdown-menu slot="dropdown">
  75. <el-dropdown-item command="adjustPoints" icon="el-icon-edit">调分</el-dropdown-item>
  76. </el-dropdown-menu>
  77. </el-dropdown>
  78. </template>
  79. </el-table-column>
  80. </el-table>
  81. <!-- 分页 -->
  82. <el-pagination
  83. style="margin-top: 20px; text-align: right"
  84. @size-change="handleSizeChange"
  85. @current-change="handleCurrentChange"
  86. :current-page="pagination.current"
  87. :page-sizes="[10, 20, 50, 100]"
  88. :page-size="pagination.size"
  89. :total="pagination.total"
  90. layout="total, sizes, prev, pager, next, jumper"
  91. ></el-pagination>
  92. </el-card>
  93. <!-- 调整积分对话框 -->
  94. <el-dialog title="调整积分" :visible.sync="dialogVisible" width="400px">
  95. <el-form :model="pointsForm" :rules="pointsRules" ref="pointsForm" label-width="100px">
  96. <el-form-item label="当前积分">
  97. <span>{{ currentChild ? currentChild.totalPoints : 0 }}</span>
  98. </el-form-item>
  99. <el-form-item label="调整积分" prop="amount">
  100. <el-input-number
  101. v-model="pointsForm.amount"
  102. :min="-1000"
  103. :max="1000"
  104. placeholder="正数为加分,负数为扣分"
  105. ></el-input-number>
  106. </el-form-item>
  107. <el-form-item label="原因" prop="reason">
  108. <el-input
  109. type="textarea"
  110. v-model="pointsForm.reason"
  111. placeholder="请输入调整原因"
  112. :rows="3"
  113. ></el-input>
  114. </el-form-item>
  115. </el-form>
  116. <div slot="footer">
  117. <el-button @click="dialogVisible = false">取消</el-button>
  118. <el-button type="primary" @click="submitPoints" :loading="submitting">确定</el-button>
  119. </div>
  120. </el-dialog>
  121. <!-- 编辑孩子对话框 -->
  122. <el-dialog title="编辑孩子" :visible.sync="editDialogVisible" width="500px">
  123. <el-form :model="editForm" :rules="editRules" ref="editForm" label-width="100px">
  124. <el-form-item label="昵称" prop="nickname">
  125. <el-input v-model="editForm.nickname" placeholder="请输入昵称"></el-input>
  126. </el-form-item>
  127. <el-form-item label="年龄" prop="age">
  128. <el-input-number v-model="editForm.age" :min="0" :max="18"></el-input-number>
  129. </el-form-item>
  130. <el-form-item label="扣分开关">
  131. <el-switch v-model="editForm.penaltyEnabled" :active-value="1" :inactive-value="0"></el-switch>
  132. </el-form-item>
  133. </el-form>
  134. <div slot="footer">
  135. <el-button @click="editDialogVisible = false">取消</el-button>
  136. <el-button type="primary" @click="submitEdit" :loading="submitting">确定</el-button>
  137. </div>
  138. </el-dialog>
  139. </div>
  140. </template>
  141. <script>
  142. import { getChildrenList, adjustChildPoints, updateChild } from '@/api/admin'
  143. export default {
  144. name: 'Children',
  145. computed: {
  146. tableHeight() {
  147. return window.innerHeight - 340
  148. }
  149. },
  150. data() {
  151. return {
  152. loading: false,
  153. submitting: false,
  154. tableData: [],
  155. searchForm: {
  156. nickname: '',
  157. familyId: ''
  158. },
  159. pagination: {
  160. current: 1,
  161. size: 10,
  162. total: 0
  163. },
  164. dialogVisible: false,
  165. editDialogVisible: false,
  166. currentChild: null,
  167. pointsForm: {
  168. amount: 0,
  169. reason: ''
  170. },
  171. pointsRules: {
  172. amount: [
  173. { required: true, message: '请输入调整积分', trigger: 'blur' }
  174. ],
  175. reason: [
  176. { required: true, message: '请输入调整原因', trigger: 'blur' }
  177. ]
  178. },
  179. editForm: {
  180. id: null,
  181. nickname: '',
  182. age: 0,
  183. penaltyEnabled: 1
  184. },
  185. editRules: {
  186. nickname: [
  187. { required: true, message: '请输入昵称', trigger: 'blur' }
  188. ],
  189. age: [
  190. { required: true, message: '请输入年龄', trigger: 'blur' }
  191. ]
  192. }
  193. }
  194. },
  195. mounted() {
  196. this.loadChildren()
  197. },
  198. methods: {
  199. handleActionCmd(row, cmd) {
  200. switch (cmd) {
  201. case 'adjustPoints': this.handleAdjustPoints(row); break;
  202. case 'edit': this.handleEdit(row); break;
  203. }
  204. },
  205. async loadChildren() {
  206. this.loading = true
  207. try {
  208. const params = {
  209. page: this.pagination.current,
  210. size: this.pagination.size,
  211. ...this.searchForm
  212. }
  213. const res = await getChildrenList(params)
  214. this.tableData = res.data.records || res.data || []
  215. this.pagination.total = res.data.total || this.tableData.length
  216. } catch (error) {
  217. this.$message.error(error.message || '加载孩子列表失败')
  218. } finally {
  219. this.loading = false
  220. }
  221. },
  222. handleSearch() {
  223. this.pagination.current = 1
  224. this.loadChildren()
  225. },
  226. handleReset() {
  227. this.searchForm = {
  228. nickname: '',
  229. familyId: ''
  230. }
  231. this.handleSearch()
  232. },
  233. handleRefresh() {
  234. this.loadChildren()
  235. },
  236. handleSizeChange(val) {
  237. this.pagination.size = val
  238. this.pagination.current = 1
  239. this.loadChildren()
  240. },
  241. handleCurrentChange(val) {
  242. this.pagination.current = val
  243. this.loadChildren()
  244. },
  245. handleAdjustPoints(row) {
  246. this.currentChild = row
  247. this.pointsForm = {
  248. amount: 0,
  249. reason: ''
  250. }
  251. this.dialogVisible = true
  252. },
  253. async submitPoints() {
  254. this.$refs.pointsForm.validate(async (valid) => {
  255. if (valid) {
  256. this.submitting = true
  257. try {
  258. await adjustChildPoints(this.currentChild.id, this.pointsForm)
  259. this.$message.success('积分调整成功')
  260. this.dialogVisible = false
  261. this.loadChildren()
  262. } catch (error) {
  263. this.$message.error(error.message || '积分调整失败')
  264. } finally {
  265. this.submitting = false
  266. }
  267. }
  268. })
  269. },
  270. handleEdit(row) {
  271. this.editForm = {
  272. id: row.id,
  273. nickname: row.nickname,
  274. age: row.age,
  275. penaltyEnabled: row.penaltyEnabled
  276. }
  277. this.editDialogVisible = true
  278. },
  279. async submitEdit() {
  280. this.$refs.editForm.validate(async (valid) => {
  281. if (valid) {
  282. this.submitting = true
  283. try {
  284. await updateChild(this.editForm.id, this.editForm)
  285. this.$message.success('更新成功')
  286. this.editDialogVisible = false
  287. this.loadChildren()
  288. } catch (error) {
  289. this.$message.error(error.message || '更新失败')
  290. } finally {
  291. this.submitting = false
  292. }
  293. }
  294. })
  295. },
  296. formatDate(dateStr) {
  297. if (!dateStr) return '-'
  298. const date = new Date(dateStr)
  299. return date.toLocaleString('zh-CN')
  300. }
  301. }
  302. }
  303. </script>
  304. <style scoped>
  305. </style>