1.分析功能
先看截圖

列表.png

詳細(xì)

評論.png
1. 朋友圈列表
2. 朋友圈詳細(xì)
3. 評論功能
2.數(shù)據(jù)庫建立
這里列表與詳細(xì),我們用一個(gè)表來實(shí)現(xiàn),評論用第二個(gè)表??傆?jì)2個(gè)表
post 表
我們可以直接這樣導(dǎo)入一個(gè)JSON,但由于首頁的資訊這樣操作,導(dǎo)致數(shù)據(jù)都一模一樣,這次我們使用云函數(shù)定時(shí)采集數(shù)據(jù)到數(shù)據(jù)庫。
采集數(shù)據(jù)
數(shù)據(jù)結(jié)構(gòu)地址:
var url = 'https://apiquan.ithome.com/api/post?categoryid=0&type=0&orderTime=1526981875455&visistCount=&pageLength='
如果手動(dòng)導(dǎo)入可以導(dǎo)入此數(shù)據(jù)結(jié)構(gòu)
[
{
"id":266716,
"t":"普通聯(lián)通卡可以轉(zhuǎn)米粉卡嗎?",
"c":"[雜談]",
"cn":"暢談",
"uid":100058,
"un":"中國火箭軍",
"rn":"諾基亞 Lumia 930",
"pt":"/Date(1527057744773)/",
"rt":"/Date(1527060538403)/",
"vc":0,
"rc":17,
"IC":false,
"IH":false,
"ot":"/Date(-62135596800000)/"
}
]
云函數(shù)獲取網(wǎng)址數(shù)據(jù)代碼
function onRequest(request, response, modules) {
var http = modules.oHttp;
var url = 'https://apiquan.ithome.com/api/post?categoryid=0&type=0&orderTime=1526981875455&visistCount=&pageLength='
http(url, function(error, res, body) {
response.end(body);
});
}
點(diǎn)擊測試看結(jié)果

結(jié)果
這樣就可以拿到目標(biāo)網(wǎng)址數(shù)據(jù),如何插入到數(shù)據(jù)庫,看下面代碼
db.insert({
"table":"XXX", //表名
"data":{"a":"XXXX","b":"XXXX"} //需要更新的數(shù)據(jù),格式為JSON
},function(err,data){ //回調(diào)函數(shù)
});
上面是插入單行數(shù)據(jù),如果批量插入,可以使用下面代碼
function onRequest(request, response, modules) {
var http = modules.oHttp;
var db = modules.oData;
var url = 'https://apiquan.ithome.com/api/post?categoryid=0&type=0&orderTime=1526981875455&visistCount=&pageLength='
http(url, function(error, res, body) {
//結(jié)果轉(zhuǎn)為對象類型
var results =JSON.parse(body);
var arr = [];
//循環(huán)更新數(shù)據(jù)
for (var i in results) {
newdata = {
"method": "post",
"path": "/1/classes/post",
"body":results[i]
};
arr.push(newdata)
}
//獲取數(shù)組對象
var bat = modules.oBatch;
//批量操作
bat.exec({
"data": {
"requests": arr
}
}, function (err, data) {
//回調(diào)函數(shù)
response.end(data);
return;
});
response.end(JSON.stringify(results.length));
});
}
//點(diǎn)擊測試,效果下如圖

效果
定時(shí)任務(wù)
當(dāng)然你也可以設(shè)置個(gè)定時(shí)任務(wù)
我這里設(shè)置結(jié)果
0 0 16 * * * 表示每天的16時(shí)0分0秒,觸發(fā)定時(shí)器一次

定時(shí)任務(wù)
當(dāng)然你也可以設(shè)置每小時(shí),每天,每分鐘,每秒,每隔5分鐘等等,規(guī)則可以自己定義,具體請看云函數(shù)文檔
文檔地址
這一節(jié)我們把朋友圈的數(shù)據(jù)表建立,并采集了相關(guān)數(shù)據(jù),下一節(jié)我們修改mpvue小程序代碼。