//图书馆数据,数据库接口获取
counttimeLib:"10",//去图书馆次数
OverflowLib1:"超过",//次数
percentLib1:"90",//超过百分比,次数
countbookLib:"100",//借书次数
OverflowLib2:"超过",//借书数量
percentLib2:"90",//超过百分比,借书数量
//成绩数据,数据库接口获取
firstyearRank:"100",//第一学期排名
secondyearRank:"90",//第二学期排名
currentRank:"90",//预测的期末排名
完成剩下的数据接口的使用,
1. 首先把成绩数据以及借书记录预处理上传到服务器。
2. 部署新的云函数
2.1 新的合并json对象封装函数
利用arguments实现一次合并多个
function extend() {
var length = arguments.length;
if(length == 0)return {};
if(length == 1)return arguments[0];
var target = arguments[0] || {};
for (var i = 1; i < length; i++) {
var source = arguments[i];
for (var key in source) {
if (source.hasOwnProperty(key)) {
target[key] = source[key];
}
}
}
return target;
}
如:我在部署云函数时,将不同的模块分为了task1,task2,task3,…最后用该函数一波合成。
结果如下:
var tasks = extend(task1,task2,task3,task4)
2.2 云函数端
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
function extend() {
var length = arguments.length;
if(length == 0)return {};
if(length == 1)return arguments[0];
var target = arguments[0] || {};
for (var i = 1; i < length; i++) {
var source = arguments[i];
for (var key in source) {
if (source.hasOwnProperty(key)) {
target[key] = source[key];
}
}
}
return target;
}
/*小数转百分数*/
/*Number()数据类型转换,指定截取转换后的小数点后几位(四舍五入)*/
function toPercent(point){
var str=Number(point*100).toFixed(2);
str+="%";
return str;
}
// 云函数入口函数
exports.main = async (event, context) => {
const $ = db.command.aggregate
//指定学期,指定学号消费总额
const total_consumption = await db.collection('consumption').aggregate().match({
uid:event.uid,
学期 : event.学期
}).group({
_id : null,
totalprice : $.sum('$account')
}).end()
//指定学期,指定学号食堂消费总额
const restaurant_consumption = await db.collection('consumption').aggregate().match({
uid:event.uid,
学期 : event.学期,
title : "食堂"
}).group({
_id : null,
totalprice : $.sum('$account')
}).end()
const total1 = total_consumption.list[0].totalprice
console.log(total1)
const total2 = restaurant_consumption.list[0].totalprice
//查询小于或等于该同学食堂消费总额的人数
const smallerNum = await db.collection('consumption').aggregate().match({
学期 : event.学期,
title : "食堂"
}).limit(1000000).group({
_id : '$uid',
totalprice: $.sum('$account')
}).match({
totalprice:_.lte(total2)
}).count('超过的同学数目').end()
const total3 = smallerNum.list[0].超过的同学数目
//查询消费集部分用户数目
const usercount = await db.collection('consumption').aggregate()
.group({
_id: '$uid',
}).count('number')
.end()
const total4 = usercount.list[0].number
const percent_1 = toPercent(total3/total4)
var task1 = {'Totalcost':total1,'cafeprice':total2,'cafeovflow':'超过','cafepercente':percent_1}
console.log("task1:",task1)
/**
* 图书馆数据
* 1. 获得特定uid特定学期 图书馆门禁次数
* 2. 获得limit很大 以uid group match比1数量少的同学数目
* 3. 计算百分比
*/
const library_specified = await db.collection('Library_safe').where({
uid: event.uid,
学期: event.学期
}).count()
console.log('library_specified:',library_specified)
const library_specified_count = library_specified.total
const library_specified_below = await db.collection('Library_safe').aggregate().match({
学期 : event.学期
}).limit(1000000).group({
_id : '$uid',
totalCount: $.sum(1)
}).match({
totalCount:_.lte(library_specified_count)
}).count('library_specified_below_count').end()
console.log('library_specified_below:',library_specified_below)
const library_specified_below_count = library_specified_below.list[0].library_specified_below_count
const library_user = await db.collection('Library_safe').aggregate()
.group({
_id: '$uid',
}).count('library_user_count')
.end()
console.log('library_user:',library_user)
const library_user_count = library_user.list[0].library_user_count
const library_percent = toPercent(library_specified_below_count/library_user_count)
var task2 = {'counttimeLib':library_specified_count,'OverflowLib1':'超过','percentLib1':library_percent}
console.log("task2:",task2)
/**
* 借书数据
* 1. 获得特定uid特定学期 借书次数
* 2. 获得limit很大 以uid group match比1数量少的同学数目
* 3. 计算百分比
*/
const books_specified = await db.collection('books').where({
uid: event.uid,
学期: event.学期
}).count()
console.log('books_specified:',books_specified)
const books_specified_count = books_specified.total
const books_specified_below = await db.collection('books').aggregate().match({
学期 : event.学期
}).limit(1000000).group({
_id : '$uid',
totalCount: $.sum(1)
}).match({
totalCount:_.lte(books_specified_count)
}).count('books_specified_below_count').end()
console.log('books_specified_below:',books_specified_below)
const books_specified_below_count = books_specified_below.list[0].books_specified_below_count
const books_user = await db.collection('books').aggregate()
.group({
_id: '$uid',
}).count('books_user_count')
.end()
console.log('books_user:',books_user)
const books_user_count = books_user.list[0].books_user_count
const books_percent = toPercent(books_specified_below_count/books_user_count)
var task3 = {'countbookLib':books_specified_count,'OverflowLib2':'超过','percentLib2':books_percent}
console.log("task3:",task3)
/**
* 成绩数据
* 1.获得第一学期排名
* 2.获得第二学期排名
* 3. 获得预测排名
*/
const rank1 = await db.collection('rank').where({
uid:event.uid,
学期:'1'
}).field({
排名:true,
_id:false
}).get()
console.log('rank1:',rank1)
const rank1_num = rank1.data[0].排名
const rank2 = await db.collection('rank').where({
uid:event.uid,
学期:'2'
}).field({
排名:true,
_id:false
}).get()
console.log('rank2:',rank2)
const rank2_num = rank2.data[0].排名
var task4 = {'firstyearRank':rank1_num,'secondyearRank':rank2_num,'currentRank':rank2_num}
console.log('task4:',task4)
var tasks = extend(task1,task2,task3,task4)
return tasks
}
2.3 小程序端对接
onShow: function () {
let that = this;
var uid = wx.getStorageSync('uid');
// var that = this
//这里默认学期2
wx.cloud.callFunction({
name:"score_all",
data:{
uid : uid,
学期:'2'
},
complete: res => {
console.log('callFunction test result: ', res)
// var data = this.extend(res.result,{token: this.data.cardInfo});
// var data = this.extend(res.result,this.data.cardInfo);
// console.log('data',data)
that.setData({
//消费数据,数据库获取
Totalcost: res.result.Totalcost,//总支出,数据库获取
cafeprice:res.result.cafeprice,//食堂花费,数据库获取
cafeovflow: res.result.cafeovflow,//取值为 超过或者低于,从数据库获取。
cafepercente:res.result.cafepercente,//取值为百分比,数据库获取
//图书馆数据,数据库接口获取
counttimeLib:res.result.counttimeLib,//去图书馆次数
OverflowLib1:res.result.OverflowLib1,//次数
percentLib1:res.result.percentLib1,//超过百分比,次数
countbookLib:res.result.countbookLib,//借书次数
OverflowLib2:res.result.OverflowLib2,//借书数量
percentLib2:res.result.percentLib2,//超过百分比,借书数量
//成绩数据,数据库接口获取
firstyearRank:res.result.firstyearRank,//第一学期排名
secondyearRank:res.result.secondyearRank,//第二学期排名
currentRank:res.result.currentRank,//预测的期末排名
})
// console.log('percentLib2',this.data.percentLib2)
}
})
}