File tree Expand file tree Collapse file tree 4 files changed +47
-8
lines changed
Expand file tree Collapse file tree 4 files changed +47
-8
lines changed Original file line number Diff line number Diff line change @@ -4,14 +4,6 @@ const db = cloud.database()
44const _ = db . command
55const 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// 云函数入口函数
168exports . main = async ( event ) => {
179 const { id, type } = event
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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'
You can’t perform that action at this time.
0 commit comments