我們搭建自己的博客、或者個人網(wǎng)站,往往需要統(tǒng)計服務。統(tǒng)計服務分為兩種,一種是類似百度、友盟的統(tǒng)計,可以根據(jù)數(shù)據(jù)來看 PV/UV 或者人群畫像。但是有時候也需要展示每一篇文章的閱讀數(shù)給“讀者”看。閱讀統(tǒng)計服務既可以服務后者的,也可以服務自己。
可以參考 serverless實踐:打造自己的閱讀計數(shù)組件 這篇文章。
核心代碼如下:
'use strict';
const tcb = require('tcb-admin-node')
const app = tcb.init({
env: "你的環(huán)境 ID"
})
const db = app.database()
const _ = db.command
exports.main = async (event, context) => {
let coll = "read_count"
let path = decodeURIComponent(event.queryStringParameters.path || '')
let host = decodeURIComponent(event.queryStringParameters.host || '')
if(!path && !host){
return {
status: 0,
info: '必須傳入 host 和 path 參數(shù)'
}
}
let data = await db.collection(coll).where({
host: host,
path: path
}).limit(1).get()
//更新
if(data.data.length){
let doc_id = data.data[0]._id
await db.collection(coll).doc(doc_id).update({
count: _.inc(1)
})
let obj = await db.collection(coll).doc(doc_id).get()
return {
status: 1,
count: obj.data[0].count
}
}
//增加
else{
let o = await db.collection(coll).add({
host: host,
path: path,
count: 1
})
return {
status: 1,
count: 1
}
}
};
如果自己不想開發(fā)和搭建,但是想使用現(xiàn)成的服務,可以參考閱讀 https://github.com/serverless80/tongji,已經(jīng)部署為獨立服務了。
2018 年,開發(fā)了一款小程序名叫「大學之巔」,里面包含2600 + 所高校數(shù)據(jù);2019 年將小程序數(shù)據(jù)庫改造成了「小程序云開發(fā)」,當然目前還有些圖片是存在 OSS 上,沒有搬遷??梢話叽a體驗 。
小程序效果如下:
當然,這個小程序的數(shù)據(jù)開源了,在 https://github.com/vczero/serverless-colleage。有了數(shù)據(jù),再使用云開發(fā)造一個小程序見識很 easy 的事了。
有很多美好的回憶,有很多重要的圖片,圖片管理難很棘手。手機一年換一部,圖片傳輸費勁還經(jīng)常丟失。經(jīng)常提示空間不足,刪了一遍又一遍。于是開發(fā)了一款小程序:小小收藏夾,用來管理自己需要收藏的私人圖片。可以掃描體驗:
小程序效果如下:
代碼已經(jīng)開源,請參考:https://github.com/vczero/CloudPhoto。
擁抱 Serverless,擁抱云開發(fā)吧,用時代的生產(chǎn)力,去做一些有趣的事情......
更多建議: