challenge-manage.vue 18 KB

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