task-detail.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // pages/task-detail/task-detail.js
  2. const app = getApp();
  3. const api = require('../../utils/api.js');
  4. Page({
  5. data: {
  6. taskId: 0,
  7. task: null,
  8. loading: true,
  9. canComplete: false,
  10. canReview: false,
  11. remainingTime: '',
  12. photoUrl: '',
  13. voiceUrl: ''
  14. },
  15. onLoad(options) {
  16. const taskId = options.id || 0;
  17. this.setData({ taskId });
  18. this.loadTaskDetail();
  19. },
  20. onShow() {
  21. this.loadTaskDetail();
  22. },
  23. // 加载任务详情
  24. loadTaskDetail() {
  25. this.setData({ loading: true });
  26. // 模拟数据
  27. const mockTask = {
  28. id: this.data.taskId,
  29. title: '写作业',
  30. description: '完成数学和语文作业,认真书写,仔细检查',
  31. points: 2,
  32. deadline: '2026-03-31 20:00',
  33. status: 'pending',
  34. category: '学习',
  35. needReview: true,
  36. reviewByCategory: false,
  37. createdAt: '2026-03-31 08:00',
  38. creatorName: '妈妈',
  39. childName: '小明',
  40. completedAt: null,
  41. reviewedAt: null
  42. };
  43. setTimeout(() => {
  44. this.setData({
  45. task: mockTask,
  46. loading: false,
  47. canComplete: mockTask.status === 'pending',
  48. canReview: mockTask.status === 'completed' && mockTask.needReview
  49. });
  50. this.updateRemainingTime();
  51. wx.setNavigationBarTitle({ title: mockTask.title });
  52. }, 500);
  53. },
  54. // 更新剩余时间
  55. updateRemainingTime() {
  56. if (!this.data.task) return;
  57. const now = new Date();
  58. const deadline = new Date(this.data.task.deadline);
  59. const diff = deadline - now;
  60. if (diff <= 0) {
  61. this.setData({ remainingTime: '已逾期' });
  62. return;
  63. }
  64. const hours = Math.floor(diff / (1000 * 60 * 60));
  65. const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
  66. this.setData({
  67. remainingTime: `${hours}小时${minutes}分钟`
  68. });
  69. },
  70. // 选择照片
  71. onChoosePhoto() {
  72. wx.chooseImage({
  73. count: 1,
  74. sizeType: ['compressed'],
  75. sourceType: ['album', 'camera'],
  76. success: (res) => {
  77. const tempFilePath = res.tempFilePaths[0];
  78. this.setData({ photoUrl: tempFilePath });
  79. // 这里应该上传到服务器
  80. }
  81. });
  82. },
  83. // 选择语音
  84. onChooseVoice() {
  85. wx.chooseMessageFile({
  86. count: 1,
  87. type: 'file',
  88. extension: ['mp3', 'wav', 'm4a'],
  89. success: (res) => {
  90. const tempFilePath = res.tempFilePaths[0];
  91. this.setData({ voiceUrl: tempFilePath });
  92. // 这里应该上传到服务器
  93. }
  94. });
  95. },
  96. // 完成任务
  97. onCompleteTask() {
  98. wx.showModal({
  99. title: '确认完成',
  100. content: '确定要完成这个任务吗?',
  101. success: (res) => {
  102. if (res.confirm) {
  103. this.doCompleteTask();
  104. }
  105. }
  106. });
  107. },
  108. doCompleteTask() {
  109. wx.showLoading({ title: '提交中...' });
  110. // 模拟API调用
  111. setTimeout(() => {
  112. wx.hideLoading();
  113. // 更新任务状态
  114. const task = this.data.task;
  115. task.status = 'completed';
  116. task.completedAt = new Date().toLocaleString();
  117. this.setData({
  118. task,
  119. canComplete: false,
  120. canReview: task.needReview
  121. });
  122. // 显示积分动画
  123. this.showPointsAnimation();
  124. wx.showToast({
  125. title: '任务完成',
  126. icon: 'success'
  127. });
  128. }, 1000);
  129. },
  130. // 显示积分动画
  131. showPointsAnimation() {
  132. wx.showModal({
  133. title: '🎉 恭喜完成任务',
  134. content: `获得 ${this.data.task.points} 积分`,
  135. showCancel: false,
  136. confirmText: '太棒了'
  137. });
  138. },
  139. // 审核通过
  140. onReviewPass() {
  141. wx.showModal({
  142. title: '审核确认',
  143. content: '确定通过这个任务吗?',
  144. success: (res) => {
  145. if (res.confirm) {
  146. this.doReviewTask('pass');
  147. }
  148. }
  149. });
  150. },
  151. // 审核拒绝
  152. onReviewReject() {
  153. wx.showModal({
  154. title: '审核确认',
  155. content: '确定拒绝这个任务吗?',
  156. success: (res) => {
  157. if (res.confirm) {
  158. this.doReviewTask('reject');
  159. }
  160. }
  161. });
  162. },
  163. doReviewTask(result) {
  164. wx.showLoading({ title: '提交中...' });
  165. // 模拟API调用
  166. setTimeout(() => {
  167. wx.hideLoading();
  168. // 更新任务状态
  169. const task = this.data.task;
  170. task.status = result === 'pass' ? 'completed' : 'pending';
  171. task.reviewedAt = new Date().toLocaleString();
  172. this.setData({
  173. task,
  174. canReview: false
  175. });
  176. wx.showToast({
  177. title: result === 'pass' ? '审核通过' : '已拒绝',
  178. icon: 'success'
  179. });
  180. }, 1000);
  181. },
  182. // 分享
  183. onShareAppMessage() {
  184. return {
  185. title: `${this.data.task?.childName}完成了任务「${this.data.task?.title}」`,
  186. path: `/pages/task-detail/task-detail?id=${this.data.taskId}`
  187. };
  188. }
  189. });