challenge-manage.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. <template>
  2. <view class="challenge-manage-page">
  3. <BaseLoading :loading="loading" text="加载中..." />
  4. <view v-if="!loading" class="page-content">
  5. <!-- 模板推荐区 -->
  6. <view class="template-section" v-if="hasFamily">
  7. <view class="section-title-row">
  8. <text class="section-title">快速创建</text>
  9. <text class="section-hint">选择模板,自定义参数</text>
  10. </view>
  11. <scroll-view class="template-scroll" scroll-x>
  12. <view class="template-scroll-inner">
  13. <view class="template-card" v-for="tpl in templates" :key="tpl.type" @click="createFromTemplate(tpl)">
  14. <view class="template-icon">{{ getTemplateEmoji(tpl.type) }}</view>
  15. <text class="template-name">{{ tpl.title }}</text>
  16. </view>
  17. <view class="template-card template-card-custom" @click="createFromTemplate(null)">
  18. <view class="template-icon">✏️</view>
  19. <text class="template-name">自定义</text>
  20. </view>
  21. </view>
  22. </scroll-view>
  23. </view>
  24. <view v-else>
  25. <FamilyEmptyState type="card" />
  26. </view>
  27. <!-- 挑战列表 -->
  28. <view class="list-section" v-if="hasFamily">
  29. <view class="section-title-row">
  30. <text class="section-title">我的挑战</text>
  31. <text class="list-add-btn" @click="openCreateModal(null)">+ 新建</text>
  32. </view>
  33. <!-- 进行中 -->
  34. <view class="group" v-if="activeChallenges.length > 0">
  35. <text class="group-label">进行中</text>
  36. <view class="challenge-item" v-for="ch in activeChallenges" :key="ch.id" @click="viewChallenge(ch)">
  37. <view class="challenge-item-header">
  38. <text class="challenge-item-title">{{ ch.title }}</text>
  39. <view class="challenge-item-actions">
  40. <text class="action-btn action-edit" @click.stop="openEditModal(ch)">编辑</text>
  41. <text class="action-btn action-delete" @click.stop="deleteChallenge(ch.id)">删除</text>
  42. </view>
  43. </view>
  44. <text class="challenge-item-desc" v-if="ch.description">{{ ch.description }}</text>
  45. <view class="challenge-item-footer">
  46. <text class="tag">{{ ch.targetMode === 'aggregate' ? '全家目标' : '每人目标' }}</text>
  47. <text class="tag tag-days">{{ ch.durationDays }}天</text>
  48. <text class="tag tag-points" v-if="ch.rewardPoints">🏆 +{{ ch.rewardPoints }}分</text>
  49. </view>
  50. <FamilyChallengeCard :challenge="ch" />
  51. </view>
  52. </view>
  53. <!-- 已完成 -->
  54. <view class="group" v-if="historyChallenges.length > 0">
  55. <text class="group-label">已完成</text>
  56. <view class="challenge-item" v-for="ch in historyChallenges" :key="ch.id" @click="viewChallenge(ch)">
  57. <view class="challenge-item-header">
  58. <text class="challenge-item-title">{{ ch.title }}</text>
  59. <text class="status-badge status-completed">已完成</text>
  60. </view>
  61. <text class="challenge-item-desc" v-if="ch.description">{{ ch.description }}</text>
  62. </view>
  63. </view>
  64. <!-- 空状态 -->
  65. <view class="empty-state" v-if="activeChallenges.length === 0 && historyChallenges.length === 0">
  66. <text class="empty-icon">🎯</text>
  67. <text class="empty-title">还没有挑战</text>
  68. <text class="empty-desc">创建第一个家庭挑战,让全家一起成长!</text>
  69. <view class="empty-btn" @click="openCreateModal(null)">立即创建</view>
  70. </view>
  71. </view>
  72. </view>
  73. <!-- 创建/编辑弹窗 -->
  74. <view class="modal-overlay" v-if="showModal" @click="closeModal">
  75. <view class="modal-content" @click.stop>
  76. <view class="modal-header">
  77. <text class="modal-title">{{ editMode ? '编辑挑战' : '创建挑战' }}</text>
  78. <text class="modal-close" @click="closeModal">×</text>
  79. </view>
  80. <view class="modal-body">
  81. <view class="form-item">
  82. <text class="form-label">挑战标题 *</text>
  83. <input class="form-input" v-model="form.title" placeholder="输入挑战标题" maxlength="50" />
  84. </view>
  85. <view class="form-item">
  86. <text class="form-label">挑战描述</text>
  87. <textarea class="form-textarea" v-model="form.description" placeholder="描述挑战内容" maxlength="200" />
  88. </view>
  89. <view class="form-item">
  90. <text class="form-label">挑战类型</text>
  91. <view class="type-picker">
  92. <text class="type-item" :class="{ active: form.challengeType === t.type }"
  93. v-for="t in templates" :key="t.type"
  94. @click="form.challengeType = t.type">{{ t.title }}</text>
  95. </view>
  96. </view>
  97. <view class="form-row">
  98. <view class="form-item" style="flex:1">
  99. <text class="form-label">目标模式 *</text>
  100. <view class="mode-picker">
  101. <text class="mode-item" :class="{ active: form.targetMode === 'aggregate' }"
  102. @click="form.targetMode = 'aggregate'">全家合计</text>
  103. <text class="mode-item" :class="{ active: form.targetMode === 'all_members' }"
  104. @click="form.targetMode = 'all_members'">每人目标</text>
  105. </view>
  106. </view>
  107. <view class="form-item" style="flex:1">
  108. <text class="form-label">目标值 *</text>
  109. <input class="form-input" type="digit" v-model="form.targetValue" placeholder="如: 300" />
  110. </view>
  111. </view>
  112. <view class="form-row">
  113. <view class="form-item" style="flex:1">
  114. <text class="form-label">持续天数</text>
  115. <input class="form-input" type="number" v-model="form.durationDays" placeholder="7" />
  116. </view>
  117. <view class="form-item" style="flex:1">
  118. <text class="form-label">奖励积分</text>
  119. <input class="form-input" type="number" v-model="form.rewardPoints" placeholder="50" />
  120. </view>
  121. </view>
  122. </view>
  123. <view class="modal-footer">
  124. <view class="modal-btn modal-btn-cancel" @click="closeModal">取消</view>
  125. <view class="modal-btn modal-btn-submit" @click="submitForm">{{ editMode ? '保存' : '创建' }}</view>
  126. </view>
  127. </view>
  128. </view>
  129. </view>
  130. </template>
  131. <script>
  132. import { getChallengeList, getChallengeHistory, createChallenge, updateChallenge, deleteChallenge, getChallengeTemplates, getParentDashboard } from '../../utils/api.js'
  133. import FamilyChallengeCard from '../../components/FamilyChallengeCard.vue'
  134. export default {
  135. components: { FamilyChallengeCard },
  136. data() {
  137. return {
  138. loading: true,
  139. familyId: null,
  140. templates: [],
  141. activeChallenges: [],
  142. historyChallenges: [],
  143. showModal: false,
  144. editMode: false,
  145. editingId: null,
  146. form: {
  147. title: '',
  148. description: '',
  149. challengeType: 'custom',
  150. targetMode: 'all_members',
  151. targetValue: '7',
  152. durationDays: '7',
  153. rewardPoints: '50'
  154. }
  155. }
  156. },
  157. computed: {
  158. hasFamily: function() {
  159. if (this.loading) return true
  160. if (this.familyId) return true
  161. return this.$store.getters.hasFamily
  162. }
  163. },
  164. onLoad() {
  165. var role = this.getUserRole()
  166. if (role !== 'parent') {
  167. uni.showToast({ title: '仅家长可管理挑战', icon: 'none' })
  168. setTimeout(function() { uni.navigateBack() }, 1000)
  169. return
  170. }
  171. this.loadFamilyId()
  172. this.loadData()
  173. this.loadTemplates()
  174. },
  175. onShow() {
  176. if (this.familyId) {
  177. this.loadData()
  178. }
  179. },
  180. methods: {
  181. getUserRole: function() {
  182. // store state 顶层有 role 字段(见 store/index.js:15)
  183. return this.$store && this.$store.state ? this.$store.state.role : ''
  184. },
  185. loadFamilyId: function() {
  186. var self = this
  187. try {
  188. var dashPromise = getParentDashboard()
  189. dashPromise.then(function(res) {
  190. var data = res.data || {}
  191. if (data.familyId) {
  192. self.familyId = data.familyId
  193. }
  194. }).catch(function() {})
  195. } catch (e) {
  196. console.log('获取familyId失败', e)
  197. }
  198. },
  199. loadData: function() {
  200. var self = this
  201. if (!self.familyId) return
  202. self.loading = true
  203. Promise.all([
  204. getChallengeList({}),
  205. getChallengeHistory({})
  206. ]).then(function(results) {
  207. self.activeChallenges = Array.isArray(results[0].data) ? results[0].data : []
  208. self.historyChallenges = Array.isArray(results[1].data) ? results[1].data : []
  209. }).catch(function() {
  210. self.activeChallenges = []
  211. self.historyChallenges = []
  212. }).finally(function() {
  213. self.loading = false
  214. })
  215. },
  216. loadTemplates: function() {
  217. var self = this
  218. getChallengeTemplates().then(function(res) {
  219. if (res.code === 200) {
  220. self.templates = res.data || []
  221. }
  222. }).catch(function() {
  223. self.templates = [
  224. { type: 'full_checkin', title: '全员打卡', targetMode: 'all_members', targetValue: '3', durationDays: '3', rewardPoints: '50', description: '全家连续打卡' },
  225. { type: 'sports', title: '运动PK', targetMode: 'aggregate', targetValue: '300', durationDays: '7', rewardPoints: '100', description: '全家运动时长' },
  226. { type: 'reading', title: '阅读挑战', targetMode: 'aggregate', targetValue: '200', durationDays: '7', rewardPoints: '80', description: '全家阅读时长' },
  227. { type: 'gratitude', title: '感恩日记', targetMode: 'all_members', targetValue: '7', durationDays: '7', rewardPoints: '40', description: '每天感恩记录' }
  228. ]
  229. })
  230. },
  231. getTemplateEmoji: function(type) {
  232. var map = {
  233. full_checkin: '✅',
  234. sports: '🏃',
  235. health_week: '💚',
  236. reading: '📚',
  237. no_screen: '📵',
  238. gratitude: '🙏',
  239. custom: '✏️'
  240. }
  241. return map[type] || '🎯'
  242. },
  243. createFromTemplate: function(template) {
  244. this.editMode = false
  245. this.editingId = null
  246. this.form = {
  247. title: template ? template.title : '',
  248. description: template ? template.description : '',
  249. challengeType: template ? template.type : 'custom',
  250. targetMode: template ? template.targetMode : 'all_members',
  251. targetValue: template ? String(template.targetValue) : '7',
  252. durationDays: template ? String(template.durationDays) : '7',
  253. rewardPoints: template ? String(template.rewardPoints) : '50'
  254. }
  255. this.showModal = true
  256. },
  257. openCreateModal: function(template) {
  258. this.createFromTemplate(template)
  259. },
  260. openEditModal: function(challenge) {
  261. this.editMode = true
  262. this.editingId = challenge.id
  263. this.form = {
  264. title: challenge.title || '',
  265. description: challenge.description || '',
  266. challengeType: challenge.challengeType || 'custom',
  267. targetMode: challenge.targetMode || 'all_members',
  268. targetValue: String(challenge.targetValue || '7'),
  269. durationDays: String(challenge.durationDays || '7'),
  270. rewardPoints: String(challenge.rewardPoints || '50')
  271. }
  272. this.showModal = true
  273. },
  274. closeModal: function() {
  275. this.showModal = false
  276. },
  277. submitForm: function() {
  278. var self = this
  279. var title = self.form.title && self.form.title.trim()
  280. if (!title) {
  281. uni.showToast({ title: '请填写挑战标题', icon: 'none' })
  282. return
  283. }
  284. var targetValue = parseInt(self.form.targetValue || '0', 10)
  285. if (targetValue <= 0) {
  286. uni.showToast({ title: '目标值必须大于0', icon: 'none' })
  287. return
  288. }
  289. var data = {
  290. title: title,
  291. description: self.form.description && self.form.description.trim(),
  292. challengeType: self.form.challengeType,
  293. targetMode: self.form.targetMode,
  294. targetValue: targetValue,
  295. durationDays: parseInt(self.form.durationDays || '7', 10),
  296. rewardPoints: parseInt(self.form.rewardPoints || '0', 10)
  297. }
  298. self.loading = true
  299. var promise
  300. if (self.editMode && self.editingId) {
  301. promise = updateChallenge(self.editingId, data)
  302. } else {
  303. promise = createChallenge(data)
  304. }
  305. promise.then(function(res) {
  306. if (res.code === 200) {
  307. var msg = self.editMode ? '更新成功' : '创建成功'
  308. uni.showToast({ title: msg, icon: 'success' })
  309. self.closeModal()
  310. self.loadData()
  311. } else {
  312. uni.showToast({ title: res.message || '操作失败', icon: 'none' })
  313. }
  314. }).catch(function(e) {
  315. uni.showToast({ title: '操作失败', icon: 'none' })
  316. console.log('挑战操作失败', e)
  317. }).finally(function() {
  318. self.loading = false
  319. })
  320. },
  321. deleteChallenge: function(id) {
  322. var self = this
  323. uni.showModal({
  324. title: '确认删除',
  325. content: '确定要取消这个挑战吗?',
  326. success: function(r) {
  327. if (r.confirm) {
  328. deleteChallenge(id).then(function(res) {
  329. if (res.code === 200) {
  330. uni.showToast({ title: '已删除', icon: 'success' })
  331. self.loadData()
  332. } else {
  333. uni.showToast({ title: res.message || '删除失败', icon: 'none' })
  334. }
  335. }).catch(function() {
  336. uni.showToast({ title: '删除失败', icon: 'none' })
  337. })
  338. }
  339. }
  340. })
  341. },
  342. viewChallenge: function(challenge) {
  343. uni.showToast({ title: challenge.title, icon: 'none' })
  344. }
  345. }
  346. }
  347. </script>
  348. <style scoped>
  349. .challenge-manage-page {
  350. min-height: 100vh;
  351. background: #F5F9FC;
  352. padding: 20rpx 24rpx 40rpx;
  353. }
  354. .page-content {
  355. display: flex;
  356. flex-direction: column;
  357. gap: 28rpx;
  358. }
  359. /* 模板推荐区 */
  360. .template-section {
  361. margin-bottom: 8rpx;
  362. }
  363. .section-title-row {
  364. display: flex;
  365. justify-content: space-between;
  366. align-items: center;
  367. margin-bottom: 12rpx;
  368. }
  369. .section-title {
  370. font-size: 30rpx;
  371. font-weight: 700;
  372. color: #1E293B;
  373. }
  374. .section-hint {
  375. font-size: 22rpx;
  376. color: #94A3B8;
  377. }
  378. .template-scroll {
  379. width: 100%;
  380. }
  381. .template-scroll-inner {
  382. display: flex;
  383. gap: 12rpx;
  384. }
  385. .template-card {
  386. flex-shrink: 0;
  387. width: 130rpx;
  388. padding: 16rpx 12rpx;
  389. background: #fff;
  390. border-radius: 16rpx;
  391. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  392. display: flex;
  393. flex-direction: column;
  394. align-items: center;
  395. gap: 6rpx;
  396. }
  397. .template-card:active { opacity: 0.8; }
  398. .template-card-custom {
  399. background: linear-gradient(135deg, #10B981, #34D399);
  400. }
  401. .template-icon {
  402. font-size: 40rpx;
  403. line-height: 1;
  404. }
  405. .template-name {
  406. font-size: 22rpx;
  407. color: #475569;
  408. font-weight: 600;
  409. }
  410. .template-card-custom .template-name {
  411. color: #fff;
  412. }
  413. /* 列表区 */
  414. .list-section {
  415. display: flex;
  416. flex-direction: column;
  417. }
  418. .list-add-btn {
  419. font-size: 24rpx;
  420. color: #10B981;
  421. font-weight: 600;
  422. }
  423. .list-add-btn:active { opacity: 0.7; }
  424. .group {
  425. margin-bottom: 20rpx;
  426. }
  427. .group-label {
  428. font-size: 26rpx;
  429. font-weight: 600;
  430. color: #64748B;
  431. margin-bottom: 10rpx;
  432. }
  433. .challenge-item {
  434. background: #fff;
  435. border-radius: 16rpx;
  436. padding: 16rpx;
  437. margin-bottom: 12rpx;
  438. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.04);
  439. }
  440. .challenge-item-header {
  441. display: flex;
  442. justify-content: space-between;
  443. align-items: center;
  444. margin-bottom: 4rpx;
  445. }
  446. .challenge-item-title {
  447. font-size: 26rpx;
  448. font-weight: 700;
  449. color: #1E293B;
  450. }
  451. .challenge-item-actions {
  452. display: flex;
  453. gap: 12rpx;
  454. }
  455. .action-btn {
  456. font-size: 22rpx;
  457. padding: 4rpx 12rpx;
  458. border-radius: 8rpx;
  459. }
  460. .action-edit {
  461. color: #5B9BD5;
  462. background: rgba(91,155,213,0.1);
  463. }
  464. .action-delete {
  465. color: #EF4444;
  466. background: rgba(239,68,68,0.1);
  467. }
  468. .challenge-item-desc {
  469. font-size: 22rpx;
  470. color: #64748B;
  471. margin-bottom: 6rpx;
  472. }
  473. .challenge-item-footer {
  474. display: flex;
  475. gap: 8rpx;
  476. margin-bottom: 4rpx;
  477. }
  478. .tag {
  479. font-size: 20rpx;
  480. padding: 2rpx 10rpx;
  481. border-radius: 999rpx;
  482. background: #E2E8F0;
  483. color: #475569;
  484. }
  485. .tag-days { background: #DBEAFE; color: #2563EB; }
  486. .tag-points { background: #FEF3C7; color: #D97706; }
  487. .status-badge {
  488. font-size: 20rpx;
  489. padding: 2rpx 10rpx;
  490. border-radius: 999rpx;
  491. }
  492. .status-completed {
  493. background: #F1F5F9;
  494. color: #64748B;
  495. }
  496. /* 空状态 */
  497. .empty-state {
  498. display: flex;
  499. flex-direction: column;
  500. align-items: center;
  501. padding: 60rpx 0 20rpx;
  502. }
  503. .empty-icon { font-size: 80rpx; margin-bottom: 16rpx; }
  504. .empty-title { font-size: 32rpx; font-weight: 700; color: #1E293B; }
  505. .empty-desc { font-size: 24rpx; color: #94A3B8; margin: 8rpx 0 24rpx; }
  506. .empty-btn {
  507. background: #10B981;
  508. color: #fff;
  509. font-size: 26rpx;
  510. font-weight: 600;
  511. padding: 16rpx 40rpx;
  512. border-radius: 44rpx;
  513. }
  514. /* 弹窗 */
  515. .modal-overlay {
  516. position: fixed;
  517. top: 0; left: 0; right: 0; bottom: 0;
  518. background: rgba(0,0,0,0.5);
  519. display: flex;
  520. align-items: flex-end;
  521. justify-content: center;
  522. z-index: 999;
  523. }
  524. .modal-content {
  525. width: 100%;
  526. background: #fff;
  527. border-radius: 32rpx 32rpx 0 0;
  528. padding: 24rpx;
  529. box-sizing: border-box;
  530. }
  531. .modal-header {
  532. display: flex;
  533. justify-content: space-between;
  534. align-items: center;
  535. margin-bottom: 20rpx;
  536. }
  537. .modal-title {
  538. font-size: 32rpx;
  539. font-weight: 700;
  540. color: #1E293B;
  541. }
  542. .modal-close {
  543. font-size: 40rpx;
  544. color: #94A3B8;
  545. line-height: 1;
  546. }
  547. .modal-body {
  548. display: flex;
  549. flex-direction: column;
  550. gap: 16rpx;
  551. margin-bottom: 20rpx;
  552. max-height: 70vh;
  553. overflow-y: auto;
  554. }
  555. .form-item {
  556. display: flex;
  557. flex-direction: column;
  558. gap: 6rpx;
  559. }
  560. .form-label {
  561. font-size: 24rpx;
  562. color: #475569;
  563. font-weight: 600;
  564. }
  565. .form-input {
  566. background: #F8FAFC;
  567. border: 1rpx solid #E2E8F0;
  568. border-radius: 12rpx;
  569. padding: 14rpx 16rpx;
  570. font-size: 26rpx;
  571. color: #1E293B;
  572. }
  573. .form-textarea {
  574. background: #F8FAFC;
  575. border: 1rpx solid #E2E8F0;
  576. border-radius: 12rpx;
  577. padding: 14rpx 16rpx;
  578. font-size: 26rpx;
  579. color: #1E293B;
  580. height: 80rpx;
  581. width: 100%;
  582. box-sizing: border-box;
  583. }
  584. .form-row {
  585. display: flex;
  586. gap: 12rpx;
  587. }
  588. .type-picker {
  589. display: flex;
  590. flex-wrap: wrap;
  591. gap: 8rpx;
  592. }
  593. .type-item {
  594. font-size: 22rpx;
  595. padding: 6rpx 14rpx;
  596. border-radius: 999rpx;
  597. background: #F1F5F9;
  598. color: #475569;
  599. }
  600. .type-item.active {
  601. background: #10B981;
  602. color: #fff;
  603. font-weight: 600;
  604. }
  605. .mode-picker {
  606. display: flex;
  607. gap: 8rpx;
  608. }
  609. .mode-item {
  610. flex: 1;
  611. font-size: 24rpx;
  612. padding: 10rpx 0;
  613. border-radius: 12rpx;
  614. background: #F1F5F9;
  615. color: #475569;
  616. text-align: center;
  617. }
  618. .mode-item.active {
  619. background: #10B981;
  620. color: #fff;
  621. font-weight: 600;
  622. }
  623. .modal-footer {
  624. display: flex;
  625. gap: 12rpx;
  626. }
  627. .modal-btn {
  628. flex: 1;
  629. text-align: center;
  630. padding: 16rpx 0;
  631. border-radius: 44rpx;
  632. font-size: 28rpx;
  633. font-weight: 600;
  634. }
  635. .modal-btn-cancel {
  636. background: #F1F5F9;
  637. color: #64748B;
  638. }
  639. .modal-btn-submit {
  640. background: #10B981;
  641. color: #fff;
  642. }
  643. </style>