challenge-manage.vue 18 KB

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