You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// pages/classroom/classroom.js
const AV = require("../../lib/av-live-query-weapp-min")
const { jsonify } = require('../../utils/leancloudutils');
const bind = require("../../lib/live-query-binding")
Page({
/**
页面的初始数据
students: 该课程所有的学生
元素student
student.objectId 该学生的主键,唯一
student.username 学生的用户名,是唯一,就像微信号一样
student.name 学生的姓名
*/
presence: null,
students: [],
class: null,
class_id: null,
data: {
//以下数据的同步由该代码文件实现数据的呈现由classroom.wxml实现
//命名可能有点混乱......
present_students: [],
// 出席的学生的用户名即上文提及的student.username考虑弃用建议替换为presen_stu_ids
present_stu_ids: [], //出席的学生的主键objectId
students: [], // 该课程所有的学生, 元素类型见上文中的student
present_stus: [], //出席的学生元素类型与students中的一样
unpresent_students: [], //没有出席的学生的用户名,不建议使用该数据,弃用
class_name: "",//课程名称
class_id: "",//该课程的主键(编号)
},
pull_present: function(){
var query = new AV.Query("Presence");
//console.log(this.class);
query.equalTo("class", this.class);
query.include("user");
set_present = this.set_present.bind(this);
return AV.Promise.all([query.find().then(set_present), query.subscribe()]).then(([presents, subscription])=>{
this.subscription = subscription;
if(this.presenceUnbind) this.unbind();
this.presenceUnbind = bind(subscription, presents, set_present);
}).catch(error=> console.error(error.message));
},
set_present: function(presents){
//console.log("set_present");
//console.log(presents);
var student_ids = presents.map((present)=>{
user = present.get("user");
//console.log(user);
//console.log(typeof user);
return user.id;
//return user.get("username");
});
//console.log("student_ids:"+student_ids.toString());
//console.log("students:"+this.students);
present_stus = this.students.filter(student=> student_ids.indexOf(student.id)>-1)
//console.log(present_stus);
present_stu_usernames = present_stus.map(stu => stu.get("username"));
present_stu_ids = present_stus.map(stu=>stu.id)
this.setData({
present_stu_ids: present_stu_ids,
present_students: present_stu_usernames});
this.setData(jsonify({present_stus}));
return presents;
},
fetch_class: function(class_id){
this.class = AV.Object.createWithoutData("Class_", class_id);
return this.class.fetch().then((class_) => {
//console.log(this.class);
this.setData({
class_name: this.class.get("name"),
class_id: this.class.get("objectId")
});
}).catch(error=>console.error(error.message))
},
fetch_students: function(){
//console.log("fetch_students");
var query = new AV.Query("StudentClassMap");
query.equalTo("class", this.class);
query.include("user");
//console.log("fetch_students2")
return query.find();
},
set_students: function(studentClassMaps){
//console.log("set_students");
var students = []
studentClassMaps.forEach((scm, idx, a)=>{
console.log(a);
students.push(scm.get("user"));
});
this.students = students;
console.log("this.students");
console.log(students);
this.setData(jsonify((students)));
return students;
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.class_id = options.class_id;
//this.class = AV.Object.createWithoutData("Class_", this.class_id);
this.fetch_class.bind(this)(this.class_id).then(this.fetch_students).then(this.set_students.bind(this)).then(this.pull_present.bind(this));
console.log("onLoad");
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
console.log("onReady");
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
console.log("onShow");
console.log("enter class when onShow");
this.class.enter();
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
this.class.leave();
console.log("onHide");
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
this.class.leave();
console.log("onUnload");
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
this.pull_present.bind(this)();
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
console.log("onShareAppMessage");
}
})