const cloud = require("@cloudbase/node-sdk");
const axios = require('axios')
const APP = '$$$小程序'
function save_token(app, access_token) {
app.database().collection("token").where({ app: APP }).update({
access_token: access_token,
validity_time: new Date(new Date().getTime() + 7100 * 1000)
})
.then(res => { console.log(res) })
.catch(err => { console.log(err) })
}
exports.main = async (event, context) => {
console.log("event", event)
const app = cloud.init({
env: cloud.SYMBOL_CURRENT_ENV,
});
let config = await app.database().collection("token").where({ app: APP }).get().then(res => { return res.data[0] })
let access_token = config.access_token
if (new Date(config.validity_time).getTime() < new Date().getTime()) {
let url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" + "&appid=" + config.appid + "&secret=" + config.secret
await axios.get(url).then(res => {
save_token(app, res.data.access_token)
access_token = res.data.access_token
})
.catch(err => { console.log(err) })
}
let url = 'https://api.weixin.qq.com/tcb/databasequery?access_token=' + access_token
let data = {
"env": config.env,
"query": `db.collection("money").aggregate().group({_id:null,total:db.command.aggregate.sum("$realCount")}).end()`
}
console.log(data)
let result = {}
await axios.post(url, data).then(res => {
console.log(res)
result = {
message: "ok",
code: "res"
}
}).catch(err => {
console.log("err", err)
result = {
message: "fail",
code: -1
}
})
return result
};
返回结果截图给一下
await db.collection('money').aggregate().group({_id:null,total:db.command.aggregate.sum("$realCount")}).end().then(res=>{
console.log("es",res)
})