123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- // pages/community/community.js
- const app = getApp()
- Page({
- data: {
- theme: {},
- containerStyle: '',
- postContent: '',
- todayDuration: 0,
- showCommentId: null,
- currentComment: '',
- posts: [],
- isChecked: false,
- continuousDays: 0,
- totalDuration: 0
- },
- onLoad: function() {
- // 计算今日专注时长
- this.calculateTodayDuration()
-
- // 加载主题
- this.setData({
- theme: app.globalData.theme,
- containerStyle: `background-color: ${app.globalData.theme.bgColor}; color: ${app.globalData.theme.textColor};`
- })
-
- // 加载签到数据
- this.loadCheckInData()
-
- // 加载帖子数据
- this.loadPostsData()
-
- // 监听主题变化
- if (app.eventBus) {
- app.eventBus.on('themeChange', (theme) => {
- this.setData({
- theme,
- containerStyle: `background-color: ${theme.bgColor}; color: ${theme.textColor};`
- })
- })
- }
- },
- onShow: function() {
- // 更新导航栏颜色
- this.updateNavigationBarColor()
- },
- updateNavigationBarColor: function() {
- const { theme } = this.data
- if (theme && theme.primaryColor && theme.textColor) {
- wx.setNavigationBarColor({
- frontColor: theme.textColor === '#333' ? '#000000' : '#ffffff',
- backgroundColor: theme.primaryColor,
- animation: {
- duration: 300,
- timingFunc: 'easeIn'
- }
- })
- }
- },
- calculateTodayDuration: function() {
- // 从存储中获取今日专注数据
- const logs = wx.getStorageSync('logs') || []
- let duration = 0
- const today = new Date().toDateString()
-
- logs.forEach(log => {
- if (new Date(log.date).toDateString() === today) {
- duration += parseInt(log.time) || 0
- }
- })
-
- this.setData({ todayDuration: duration })
- },
- loadCheckInData: function() {
- // 从存储中获取签到数据
- const checkins = wx.getStorageSync('checkins') || {}
- const today = new Date().toDateString()
-
- // 计算连续签到天数
- let continuousDays = 0
- let date = new Date()
-
- while (true) {
- const dateStr = date.toDateString()
- if (checkins[dateStr]) {
- continuousDays++
- date.setDate(date.getDate() - 1)
- } else {
- break
- }
- }
-
- // 计算总专注时长
- const logs = wx.getStorageSync('logs') || []
- let totalDuration = 0
- logs.forEach(log => {
- totalDuration += parseInt(log.time) || 0
- })
-
- this.setData({
- isChecked: !!checkins[today],
- continuousDays,
- totalDuration
- })
- },
- loadPostsData: function() {
- // 从存储中获取帖子数据,或使用预设数据
- const savedPosts = wx.getStorageSync('communityPosts') || []
-
- // 如果没有保存的帖子,使用预设数据
- if (savedPosts.length === 0) {
- const defaultPosts = [
- {
- id: 1,
- user: {
- name: '番茄达人',
- avatar: '../../images/avatar1.png'
- },
- content: '今天专注学习了3小时,完成了所有任务!',
- duration: 180,
- focusTime: '14:30',
- time: '2小时前',
- likes: 12,
- isLiked: false,
- comments: [
- { id: 1, user: '用户A', content: '太棒了!向你学习' },
- { id: 2, user: '用户B', content: '我也要加油了' }
- ]
- },
- {
- id: 2,
- user: {
- name: '学习小能手',
- avatar: '../../images/avatar2.png'
- },
- content: '连续专注5个番茄钟,效率超高!',
- duration: 125,
- focusTime: '上午',
- time: '5小时前',
- likes: 8,
- isLiked: true,
- comments: [
- { id: 1, user: '用户C', content: '怎么做到的?求经验' }
- ]
- }
- ]
-
- wx.setStorageSync('communityPosts', defaultPosts)
- this.setData({ posts: defaultPosts })
- } else {
- this.setData({ posts: savedPosts })
- }
- },
- checkIn: function() {
- if (this.data.isChecked) {
- wx.showToast({ title: '今日已签到', icon: 'none' })
- return
- }
-
- // 更新签到状态
- const today = new Date().toDateString()
- const checkins = wx.getStorageSync('checkins') || {}
- checkins[today] = true
- wx.setStorageSync('checkins', checkins)
-
- // 更新页面数据
- this.setData({
- isChecked: true,
- continuousDays: this.data.continuousDays + 1
- })
-
- // 显示签到成功提示
- wx.showToast({ title: '签到成功', icon: 'success' })
-
- // 可以在此处添加签到奖励逻辑
- },
- onInputContent: function(e) {
- this.setData({ postContent: e.detail.value })
- },
- createPost: function() {
- if (!this.data.postContent.trim()) {
- wx.showToast({ title: '请输入内容', icon: 'none' })
- return
- }
-
- const newPost = {
- id: Date.now(),
- user: {
- name: app.globalData.userInfo ? app.globalData.userInfo.nickName : '匿名用户',
- avatar: app.globalData.userInfo ? app.globalData.userInfo.avatarUrl : '../../images/avatar-default.png'
- },
- content: this.data.postContent,
- duration: this.data.todayDuration,
- focusTime: new Date().toLocaleTimeString(),
- time: '刚刚',
- likes: 0,
- isLiked: false,
- comments: []
- }
-
- // 更新帖子列表
- const posts = [newPost, ...this.data.posts]
- this.setData({
- posts,
- postContent: ''
- })
-
- // 保存到本地存储
- wx.setStorageSync('communityPosts', posts)
-
- wx.showToast({ title: '发布成功', icon: 'success' })
- },
- toggleLike: function(e) {
- const postId = e.currentTarget.dataset.id
- const posts = this.data.posts.map(post => {
- if (post.id === postId) {
- return {
- ...post,
- likes: post.isLiked ? post.likes - 1 : post.likes + 1,
- isLiked: !post.isLiked
- }
- }
- return post
- })
-
- this.setData({ posts })
- wx.setStorageSync('communityPosts', posts)
- },
- showComments: function(e) {
- const postId = e.currentTarget.dataset.id
- this.setData({
- showCommentId: this.data.showCommentId === postId ? null : postId
- })
- },
- onCommentInput: function(e) {
- this.setData({
- currentComment: e.detail.value
- })
- },
- addComment: function(e) {
- const postId = e.currentTarget.dataset.id
- if (!this.data.currentComment.trim()) {
- wx.showToast({ title: '请输入评论内容', icon: 'none' })
- return
- }
-
- const posts = this.data.posts.map(post => {
- if (post.id === postId) {
- return {
- ...post,
- comments: [
- ...post.comments,
- {
- id: Date.now(),
- user: app.globalData.userInfo ? app.globalData.userInfo.nickName : '匿名用户',
- content: this.data.currentComment
- }
- ]
- }
- }
- return post
- })
-
- this.setData({
- posts,
- currentComment: '',
- showCommentId: postId // 保持评论框打开
- })
-
- wx.setStorageSync('communityPosts', posts)
- },
- // 页面分享功能
- onShareAppMessage: function() {
- return {
- title: '我在番茄社区分享了我的专注成果,快来看看吧!',
- path: '/pages/community/community',
- imageUrl: '../../images/share.png' // 需要准备分享图片
- }
- }
- })
|