community.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // pages/community/community.js
  2. const app = getApp()
  3. Page({
  4. data: {
  5. theme: {},
  6. containerStyle: '',
  7. postContent: '',
  8. todayDuration: 0,
  9. showCommentId: null,
  10. currentComment: '',
  11. posts: [],
  12. isChecked: false,
  13. continuousDays: 0,
  14. totalDuration: 0
  15. },
  16. onLoad: function() {
  17. // 计算今日专注时长
  18. this.calculateTodayDuration()
  19. // 加载主题
  20. this.setData({
  21. theme: app.globalData.theme,
  22. containerStyle: `background-color: ${app.globalData.theme.bgColor}; color: ${app.globalData.theme.textColor};`
  23. })
  24. // 加载签到数据
  25. this.loadCheckInData()
  26. // 加载帖子数据
  27. this.loadPostsData()
  28. // 监听主题变化
  29. if (app.eventBus) {
  30. app.eventBus.on('themeChange', (theme) => {
  31. this.setData({
  32. theme,
  33. containerStyle: `background-color: ${theme.bgColor}; color: ${theme.textColor};`
  34. })
  35. })
  36. }
  37. },
  38. onShow: function() {
  39. // 更新导航栏颜色
  40. this.updateNavigationBarColor()
  41. },
  42. updateNavigationBarColor: function() {
  43. const { theme } = this.data
  44. if (theme && theme.primaryColor && theme.textColor) {
  45. wx.setNavigationBarColor({
  46. frontColor: theme.textColor === '#333' ? '#000000' : '#ffffff',
  47. backgroundColor: theme.primaryColor,
  48. animation: {
  49. duration: 300,
  50. timingFunc: 'easeIn'
  51. }
  52. })
  53. }
  54. },
  55. calculateTodayDuration: function() {
  56. // 从存储中获取今日专注数据
  57. const logs = wx.getStorageSync('logs') || []
  58. let duration = 0
  59. const today = new Date().toDateString()
  60. logs.forEach(log => {
  61. if (new Date(log.date).toDateString() === today) {
  62. duration += parseInt(log.time) || 0
  63. }
  64. })
  65. this.setData({ todayDuration: duration })
  66. },
  67. loadCheckInData: function() {
  68. // 从存储中获取签到数据
  69. const checkins = wx.getStorageSync('checkins') || {}
  70. const today = new Date().toDateString()
  71. // 计算连续签到天数
  72. let continuousDays = 0
  73. let date = new Date()
  74. while (true) {
  75. const dateStr = date.toDateString()
  76. if (checkins[dateStr]) {
  77. continuousDays++
  78. date.setDate(date.getDate() - 1)
  79. } else {
  80. break
  81. }
  82. }
  83. // 计算总专注时长
  84. const logs = wx.getStorageSync('logs') || []
  85. let totalDuration = 0
  86. logs.forEach(log => {
  87. totalDuration += parseInt(log.time) || 0
  88. })
  89. this.setData({
  90. isChecked: !!checkins[today],
  91. continuousDays,
  92. totalDuration
  93. })
  94. },
  95. loadPostsData: function() {
  96. // 从存储中获取帖子数据,或使用预设数据
  97. const savedPosts = wx.getStorageSync('communityPosts') || []
  98. // 如果没有保存的帖子,使用预设数据
  99. if (savedPosts.length === 0) {
  100. const defaultPosts = [
  101. {
  102. id: 1,
  103. user: {
  104. name: '番茄达人',
  105. avatar: '../../images/avatar1.png'
  106. },
  107. content: '今天专注学习了3小时,完成了所有任务!',
  108. duration: 180,
  109. focusTime: '14:30',
  110. time: '2小时前',
  111. likes: 12,
  112. isLiked: false,
  113. comments: [
  114. { id: 1, user: '用户A', content: '太棒了!向你学习' },
  115. { id: 2, user: '用户B', content: '我也要加油了' }
  116. ]
  117. },
  118. {
  119. id: 2,
  120. user: {
  121. name: '学习小能手',
  122. avatar: '../../images/avatar2.png'
  123. },
  124. content: '连续专注5个番茄钟,效率超高!',
  125. duration: 125,
  126. focusTime: '上午',
  127. time: '5小时前',
  128. likes: 8,
  129. isLiked: true,
  130. comments: [
  131. { id: 1, user: '用户C', content: '怎么做到的?求经验' }
  132. ]
  133. }
  134. ]
  135. wx.setStorageSync('communityPosts', defaultPosts)
  136. this.setData({ posts: defaultPosts })
  137. } else {
  138. this.setData({ posts: savedPosts })
  139. }
  140. },
  141. checkIn: function() {
  142. if (this.data.isChecked) {
  143. wx.showToast({ title: '今日已签到', icon: 'none' })
  144. return
  145. }
  146. // 更新签到状态
  147. const today = new Date().toDateString()
  148. const checkins = wx.getStorageSync('checkins') || {}
  149. checkins[today] = true
  150. wx.setStorageSync('checkins', checkins)
  151. // 更新页面数据
  152. this.setData({
  153. isChecked: true,
  154. continuousDays: this.data.continuousDays + 1
  155. })
  156. // 显示签到成功提示
  157. wx.showToast({ title: '签到成功', icon: 'success' })
  158. // 可以在此处添加签到奖励逻辑
  159. },
  160. onInputContent: function(e) {
  161. this.setData({ postContent: e.detail.value })
  162. },
  163. createPost: function() {
  164. if (!this.data.postContent.trim()) {
  165. wx.showToast({ title: '请输入内容', icon: 'none' })
  166. return
  167. }
  168. const newPost = {
  169. id: Date.now(),
  170. user: {
  171. name: app.globalData.userInfo ? app.globalData.userInfo.nickName : '匿名用户',
  172. avatar: app.globalData.userInfo ? app.globalData.userInfo.avatarUrl : '../../images/avatar-default.png'
  173. },
  174. content: this.data.postContent,
  175. duration: this.data.todayDuration,
  176. focusTime: new Date().toLocaleTimeString(),
  177. time: '刚刚',
  178. likes: 0,
  179. isLiked: false,
  180. comments: []
  181. }
  182. // 更新帖子列表
  183. const posts = [newPost, ...this.data.posts]
  184. this.setData({
  185. posts,
  186. postContent: ''
  187. })
  188. // 保存到本地存储
  189. wx.setStorageSync('communityPosts', posts)
  190. wx.showToast({ title: '发布成功', icon: 'success' })
  191. },
  192. toggleLike: function(e) {
  193. const postId = e.currentTarget.dataset.id
  194. const posts = this.data.posts.map(post => {
  195. if (post.id === postId) {
  196. return {
  197. ...post,
  198. likes: post.isLiked ? post.likes - 1 : post.likes + 1,
  199. isLiked: !post.isLiked
  200. }
  201. }
  202. return post
  203. })
  204. this.setData({ posts })
  205. wx.setStorageSync('communityPosts', posts)
  206. },
  207. showComments: function(e) {
  208. const postId = e.currentTarget.dataset.id
  209. this.setData({
  210. showCommentId: this.data.showCommentId === postId ? null : postId
  211. })
  212. },
  213. onCommentInput: function(e) {
  214. this.setData({
  215. currentComment: e.detail.value
  216. })
  217. },
  218. addComment: function(e) {
  219. const postId = e.currentTarget.dataset.id
  220. if (!this.data.currentComment.trim()) {
  221. wx.showToast({ title: '请输入评论内容', icon: 'none' })
  222. return
  223. }
  224. const posts = this.data.posts.map(post => {
  225. if (post.id === postId) {
  226. return {
  227. ...post,
  228. comments: [
  229. ...post.comments,
  230. {
  231. id: Date.now(),
  232. user: app.globalData.userInfo ? app.globalData.userInfo.nickName : '匿名用户',
  233. content: this.data.currentComment
  234. }
  235. ]
  236. }
  237. }
  238. return post
  239. })
  240. this.setData({
  241. posts,
  242. currentComment: '',
  243. showCommentId: postId // 保持评论框打开
  244. })
  245. wx.setStorageSync('communityPosts', posts)
  246. },
  247. // 页面分享功能
  248. onShareAppMessage: function() {
  249. return {
  250. title: '我在番茄社区分享了我的专注成果,快来看看吧!',
  251. path: '/pages/community/community',
  252. imageUrl: '../../images/share.png' // 需要准备分享图片
  253. }
  254. }
  255. })