const { tryLoginRenewal } = require('../../utils/subscribe'); const { itemsOf, totalOf, mergeUnique } = require('../../utils/list-pagination'); const { withDataCache } = require('../../utils/page-cache'); const { getLayout } = require('../../utils/layout'); const { api, getImageUrl } = require('../../utils/request'); Page(withDataCache({ onLoginSubscribeTap: function() { return tryLoginRenewal(); }, noop: function() {}, onResize: function() { this.setData(getLayout()); }, data: { loadError: false, diary: null, loading: true, comments: [], commentPage: 1, commentsLoadingMore: false, commentsMoreError: false, commentsTotal: 0, hasMoreComments: true, showCommentInput: false, commentText: '', commentInitialText: '', commentLength: 0, commentFocus: false, commentKeyboardHeight: 0, commentEditorHeight: 56, commentSending: false, replyTo: null, coverSwiperIndex: 0, previewImages: [], previewIndex: 0, showPreview: false, myUid: 0 }, onLoad: function(options) { this.setData(getLayout()); var diaryId = options.id; if (!diaryId) { wx.showToast({ title: '参数错误', icon: 'none' }); return; } this.setData({ diaryId: String(diaryId), myUid: wx.getStorageSync('userId') || 0 }); this.loadDiary(); this.loadComments(); }, onShow: function() { if (this.data.diaryId) { this.loadDiary(); this.loadComments(); } }, loadDiary: async function() { if (this._pageLoading) return; this._pageLoading = true; this.setData({loading: !this._hasCachedData, loadError:false}); try { var data = await api.diaryDetail(this.data.diaryId); data = this._formatDiary(data); this.setData({ diary: data, myUid: wx.getStorageSync('userId') || 0, loading: false }); } catch (e) { this.setData({loadError:true}); wx.showToast({ title: '加载失败', icon: 'none' }); this.setData({ loading: false }); } finally { this._pageLoading = false; this.setData({loading:false}); } }, _formatDiary: function(data) { var myUid = wx.getStorageSync('userId') || 0; data.creator_name = data.creator_name || (data.created_by == myUid ? '我' : 'TA'); data.creator_avatar = getImageUrl(data.creator_avatar); data.created_time = data.created_at ? data.created_at.substring(0, 16).replace('T', ' ') : ''; var imgs = data.images || []; if (typeof imgs === 'string') { try { imgs = JSON.parse(imgs); } catch(e) { imgs = []; } } if (!Array.isArray(imgs)) imgs = []; data.imageUrls = imgs.map(function(p) { return getImageUrl(p); }); data.comment_count = data.comment_count || 0; // 地理位置:优先显示地址名称,其次显示经纬度(经纬度为0的不显示) data.location_text = ''; var hasCoord = data.lat && data.lng && parseFloat(data.lat) !== 0 && parseFloat(data.lng) !== 0; if (data.location) { data.location_text = data.location; } else if (hasCoord) { data.location_text = data.lat + ', ' + data.lng; } return data; }, loadComments: function() { return this._loadCommentsPage(1); }, _loadCommentsPage: async function(page) { page = Number(page) || 1; if (this._commentsFirst || (page > 1 && (this.data.commentsLoadingMore || !this.data.hasMoreComments))) return; if (page === 1) { this._commentsFirst = true; this._commentsGeneration = (this._commentsGeneration || 0) + 1; } var generation = this._commentsGeneration; this.setData(page === 1 ? {commentsError:false,commentsMoreError:false,commentsLoadingMore:false} : {commentsLoadingMore:true,commentsMoreError:false}); try { var result = await api.diaryCommentList({diary_id:this.data.diaryId,page:page,page_size:20}); if (generation !== this._commentsGeneration) return; var added = itemsOf(result).map(function(comment) { return Object.assign({}, comment, {avatar_url:getImageUrl(comment.avatar_url || comment.avatar || ''),_time:String(comment.created_at || comment.create_time || '').slice(0,16).replace('T',' ')}); }); this.setData({comments:page===1?added:mergeUnique(this.data.comments,added),commentPage:page,commentsTotal:totalOf(result),hasMoreComments:!Array.isArray(result)&&(result.has_more != null?!!result.has_more:page*20