Skip to content

Commit 807623b

Browse files
committed
feat: save page views data
1 parent 2a43514 commit 807623b

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

cloudfunctions/setCookbook/index.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ const db = cloud.database()
44
const _ = db.command
55
const collection = db.collection('cookbook')
66

7-
const getLikes = async (id) => {
8-
const { data } = await collection.where({ id }).get();
9-
const { likeds = [] } = data[0]
10-
11-
return likeds.filter(item => !item.isDel).length
12-
}
13-
14-
157
// 云函数入口函数
168
exports.main = async (event) => {
179
const { id, type } = event
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const cloud = require('wx-server-sdk')
2+
cloud.init()
3+
const db = cloud.database()
4+
const _ = db.command
5+
const collection = db.collection('cookbook_views')
6+
7+
// 云函数入口函数
8+
exports.main = async (event) => {
9+
const { id } = event;
10+
const { stats } = await collection.where({ id }).update({
11+
data: {
12+
views: _.inc(1)
13+
}
14+
})
15+
if (stats.updated !== 1) {
16+
await collection.add({
17+
data: {
18+
id,
19+
createTime: new Date(),
20+
views: 1
21+
}
22+
})
23+
}
24+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "updateViews",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"wx-server-sdk": "~2.5.3"
13+
}
14+
}

miniprogram/pages/detail/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Page({
3131
})
3232
}
3333
this.getData()
34+
this.updateViews()
3435
}
3536
}
3637
},
@@ -71,6 +72,14 @@ Page({
7172
}
7273
},
7374

75+
updateViews() {
76+
const { id } = this.data;
77+
wx.cloud.callFunction({
78+
name: 'updateViews',
79+
data: { id }
80+
})
81+
},
82+
7483
toMyCenter() {
7584
wx.navigateTo({
7685
url: '/pages/my/index'

0 commit comments

Comments
 (0)