纯属娱乐学习项目,偶尔记录下开发中遇到的问题和想法,不定期更新,如果你有什么建议也请告诉我。项目中自己有封装一些组件。
目前豆瓣搜索接口已经没有免费的可以使用了,本人提供的接口部署在 Vercel,未备案不可添加到后台,项目同时提供了 mock 数据可使用。小程序个人开发功能限制太多,无法完全上线。如若喜欢可以克隆项目自己运行看看。
功能 Features
• 云函数实现微信登录
• 云函数定时任务实现每日卡片
• 云函数聚合查询实现卡片收藏
• Grid 多列表格布局
• Grid 布局实现瀑布流
• 云函数爬取 GitHub Trending
• 关于页背景音频播放
• 分别使用 template 和 Component 实现公用组件
• CSS3 属性动画
表
for (article : articles) {
isLike = ( select * from relation where articleId = article.id && userId = 'userId' )
likeCount = ( select count(*) from relation where articleId = article.id )
article.isLike = isLike
article.likeCount = likeCount
}
const db = cloud.database()
const getArticles = async (event, context) => {
const { userInfo: { openId } } = event
return db.collection('articles').get().then(({ data }) => {
let articles = []
for (let i = 0, length = data.length; i < length; ++i) {
await Promise.all([
db.collection('relation').where({
articleId: data[i].id,
}).count(),
db.collection('relation').where({
articleId: data[i].id,
userId: openId,
}).count()
]).then(([likeCount, liked]) => {
articles.push({
...data[i],
likeCount,
liked: !!liked,
})
})
}
return {
data: articles,
message: 'success',
}
}).catch( err => {
console.error(err.errMsg)
return Promise.reject({
data: [],
message: err.errMsg,
})
})
}
res = [] // 最终结果
for (id : articleIds) {
details = ( select * from articles where articleId = id )
likeCount = ( select count(*) from relation where articleId = id )
res.push({
articleId: id,
title: details.title,
likeCount: likeCount,
})
}
const db = cloud.database()
const _ = db.command
const getFavArticles = async (event, context) => {
const { userInfo: { openId } } = event
return db.collection('relation').where({
userId: openId,
}).field({
articleId: true,
}).get().then(({ data }) => {
return db.collection('articles').where({
id: _in(data.map( item => item.articleId )),
}).then(({ data: articles }) => {
let result = []
for (let i = 0, length = articles.length; i < length; ++i) {
await db.collection('relation').where({
articleId: articles[i].id,
}).count().then(({ total }) => {
result.push({
...articles,
likeCount: total,
})
})
}
return {
data: result,
message: 'success',
}
})
}).catch( err => {
console.error(err)
return Promise.reject({
data: [],
message: err.errMsg,
})
})
}
select title, content, likeCount from articles
select count(*) from relation where articleId = 'articleId' && userId = 'userId'
const db = cloud.database()
const getArticleDetails = (event, context) => {
const { userInfo: { openId }, id } = event
return Promise.all([
// 如果直接使用微信自带的 _id 索引可直接使用
// db.collection('articles').doc(id)
db.collection('articles').where({ id }),
db.collection('relation').where({
userId: openId,
articleId: id,
}).count()
]).then(([details, total]) => {
// 注意使用 where 查询后这里的 details 是个数组
if (details.length) {
return {
data: {
...details[0],
liked: !!total,
},
message: 'success',
}
}
}).catch( err => {
console.error(err)
return Promise.reject({
data: {},
message: err.errMsg,
})
})
}