feat: 完善见素起名小程序功能
- 添加收藏锦囊功能,支持查看和删除收藏 - 实现积分系统,每日赠送5次灵感次数 - 添加静心阅读功能,阅读15秒可获得额外次数 - 实现灵感广场,展示用户分享的名字 - 添加字源溯源组件,长按汉字查看详情 - 优化空状态和结语卡片样式统一 - 添加音频控制(静音/风铃/雨落/古琴/白噪音/森林/溪流) - 优化名字生成逻辑,确保每次返回5个不重复名字 - 修复卡片翻转样式问题 - 移除首页动态提醒气泡
This commit is contained in:
184
miniprogram/pages/square/square.js
Normal file
184
miniprogram/pages/square/square.js
Normal file
@@ -0,0 +1,184 @@
|
||||
// 灵感广场页面
|
||||
Page({
|
||||
data: {
|
||||
posts: [],
|
||||
leftPosts: [],
|
||||
rightPosts: [],
|
||||
page: 0,
|
||||
size: 20,
|
||||
loading: false,
|
||||
hasMore: true,
|
||||
currentTag: '',
|
||||
sortType: 'latest',
|
||||
showDetail: false,
|
||||
selectedPost: {}
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.loadPosts();
|
||||
},
|
||||
|
||||
onShow() {
|
||||
// 刷新列表
|
||||
if (this.data.posts.length === 0) {
|
||||
this.loadPosts();
|
||||
}
|
||||
},
|
||||
|
||||
// 加载帖子列表
|
||||
loadPosts(reset = false) {
|
||||
if (this.data.loading) return;
|
||||
|
||||
if (reset) {
|
||||
this.setData({
|
||||
page: 0,
|
||||
posts: [],
|
||||
leftPosts: [],
|
||||
rightPosts: [],
|
||||
hasMore: true
|
||||
});
|
||||
}
|
||||
|
||||
this.setData({ loading: true });
|
||||
|
||||
const apiBaseUrl = getApp().globalData.apiBaseUrl;
|
||||
const { page, size, currentTag, sortType } = this.data;
|
||||
|
||||
let url = `${apiBaseUrl}/api/square/posts?page=${page}&size=${size}&sort=${sortType}`;
|
||||
if (currentTag) {
|
||||
url += `&tag=${encodeURIComponent(currentTag)}`;
|
||||
}
|
||||
|
||||
wx.request({
|
||||
url,
|
||||
success: (res) => {
|
||||
if (res.data && res.data.success) {
|
||||
const newPosts = res.data.data || [];
|
||||
const allPosts = reset ? newPosts : [...this.data.posts, ...newPosts];
|
||||
|
||||
// 分配到左右两列
|
||||
const leftPosts = [];
|
||||
const rightPosts = [];
|
||||
allPosts.forEach((post, index) => {
|
||||
if (index % 2 === 0) {
|
||||
leftPosts.push(post);
|
||||
} else {
|
||||
rightPosts.push(post);
|
||||
}
|
||||
});
|
||||
|
||||
this.setData({
|
||||
posts: allPosts,
|
||||
leftPosts,
|
||||
rightPosts,
|
||||
hasMore: res.data.hasMore,
|
||||
page: page + 1,
|
||||
loading: false
|
||||
});
|
||||
} else {
|
||||
this.setData({ loading: false });
|
||||
}
|
||||
},
|
||||
fail: () => {
|
||||
this.setData({ loading: false });
|
||||
wx.showToast({ title: '加载失败', icon: 'none' });
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// 点击标签
|
||||
onTagTap(e) {
|
||||
const tag = e.currentTarget.dataset.tag;
|
||||
this.setData({ currentTag: tag });
|
||||
this.loadPosts(true);
|
||||
},
|
||||
|
||||
// 切换排序
|
||||
onSortTap(e) {
|
||||
const type = e.currentTarget.dataset.type;
|
||||
this.setData({ sortType: type });
|
||||
this.loadPosts(true);
|
||||
},
|
||||
|
||||
// 点击帖子
|
||||
onPostTap(e) {
|
||||
const post = e.currentTarget.dataset.post;
|
||||
this.setData({
|
||||
selectedPost: post,
|
||||
showDetail: true
|
||||
});
|
||||
},
|
||||
|
||||
// 关闭详情
|
||||
onCloseDetail() {
|
||||
this.setData({ showDetail: false });
|
||||
},
|
||||
|
||||
// 阻止冒泡
|
||||
onDetailTap() {
|
||||
// 什么都不做
|
||||
},
|
||||
|
||||
// 点赞
|
||||
onLikeTap() {
|
||||
const { selectedPost } = this.data;
|
||||
const apiBaseUrl = getApp().globalData.apiBaseUrl;
|
||||
|
||||
wx.request({
|
||||
url: `${apiBaseUrl}/api/square/posts/${selectedPost.id}/like`,
|
||||
method: 'POST',
|
||||
success: (res) => {
|
||||
if (res.data && res.data.success) {
|
||||
// 更新本地数据
|
||||
const updatedPost = { ...selectedPost, likeCount: (selectedPost.likeCount || 0) + 1 };
|
||||
this.setData({ selectedPost: updatedPost });
|
||||
|
||||
// 更新列表中的数据
|
||||
const posts = this.data.posts.map(p =>
|
||||
p.id === selectedPost.id ? updatedPost : p
|
||||
);
|
||||
this.setData({ posts });
|
||||
|
||||
wx.showToast({ title: '已共鸣', icon: 'none' });
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// 同款跳转(卡片上)
|
||||
onCopyTap(e) {
|
||||
const { keyword, mode } = e.currentTarget.dataset;
|
||||
this.navigateToHome(keyword, mode);
|
||||
},
|
||||
|
||||
// 同款跳转(详情中)
|
||||
onDetailCopyTap() {
|
||||
const { keyword, mode } = this.data.selectedPost;
|
||||
this.navigateToHome(keyword, mode);
|
||||
},
|
||||
|
||||
// 跳转到首页
|
||||
navigateToHome(keyword, mode) {
|
||||
if (!keyword) {
|
||||
wx.showToast({ title: '无法获取关键词', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
|
||||
wx.reLaunch({
|
||||
url: `/pages/home/home?keyword=${encodeURIComponent(keyword)}&mode=${mode || 'poetic'}`
|
||||
});
|
||||
},
|
||||
|
||||
// 下拉刷新
|
||||
onPullDownRefresh() {
|
||||
this.loadPosts(true);
|
||||
wx.stopPullDownRefresh();
|
||||
},
|
||||
|
||||
// 上拉加载更多
|
||||
onReachBottom() {
|
||||
if (this.data.hasMore && !this.data.loading) {
|
||||
this.loadPosts();
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user