const cloud = require('wx-server-sdk'); //const got = require("got"); //const rp = require("request-promise") cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) const config = { submitPagesGapDay: 1, clearPageHistoryGapDay: 30 }; const milliSecondsPerDay = 24*60*60*1000; const collectionNameMap = { pageHistory: "pageHistory" } const PagePathMap = { shixun:"markdown/shixun/shixun/shixun", path:"markdown/path/path/path" } exports.main = async (event, context) => { const wxContext = cloud.getWXContext(); console.log(event); let {TriggerName, Time} = event; switch(TriggerName){ case "submitPages":{ return submitPages({Time}); } case "clearPageHistory":{ return clearPageHistory({Time}); } case "attandance":{ return attandance({Time}); } } } async function submitPages({Time}){ console.log("start submitPages"); const db = cloud.database(); const _ = db.command; const pageHistoryCollection = db.collection(collectionNameMap.pageHistory); let shixunPath = PagePathMap.shixun; let pathPath = PagePathMap.path; let timeEnd = new Date(Time); let timeStart = new Date(timeEnd.getTime() - config.submitPagesGapDay*milliSecondsPerDay); let shixunResult = await pageHistoryCollection.where({ page: shixunPath, status: 200, isCrawl: false, time: _.lt(timeEnd).and(_.gte(timeStart)) }).get(); let pathResult = await pageHistoryCollection.where({ page: pathPath, status: 200, isCrawl: false, time:_.lt(timeEnd).and(_.gte(timeStart)) }).get(); let shixunQueries = shixunResult.data.reduce(function(arr, v){ var query = Object.keys(v.options).map(k=>`${k}=${v.options[k]}`).join("&"); if(arr.indexOf(query)==-1) arr.push(query); return arr },[]); let pathQueries = pathResult.data.reduce(function(arr, v){ var query = Object.keys(v.options).map(k=>`${k}=${v.options[k]}`).join("&"); if(arr.indexOf(query)==-1) arr.push(query); return arr },[]); console.info("shixunQueries", shixunQueries.length, shixunQueries); console.info("pathQueries", pathQueries.length, pathQueries); if(shixunQueries.length>0){ var pages = shixunQueries.map(i=>{ return { path: shixunPath, query: i } }); var res = await cloud.openapi.search.submitPages({pages}); console.log("submit shixun pages",pages.length, res); console.info("arguments", pages); }else{ console.warn("no shixun history"); } if(pathQueries.length>0){ var pages = pathQueries.map(i=>{ return { path: pathPath, query: i } }); var res = await cloud.openapi.search.submitPages({pages}); console.log("submit path pages success", pages.length, res); console.info("arguments", pages); }else{ console.warn("no path history"); } return {code:0, message:"success"}; } async function clearPageHistory({Time}){ console.log("clearPageHistory", Time); let now = new Date(Time); let timeEnd = new Date(now.getTime() - config.clearPageHistoryGapDay*milliSecondsPerDay); const db = cloud.database(); const _ = cloud.command; const pageHistoryCollection = db.collection(collectionNameMap.pageHistory); let res = await pageHistoryCollection.where({ time: _.lt(timeEnd) }).remove(); console.log("remove page History result: ", res); return {code: 0, message:"success", data:res}; } async function attandance({Time}){ /**let postResponse = await got(url, { method: 'GET', //post请求 headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ }) })**/ }