// 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 学生的姓名 student.present 是否出席 student.isasking :0:没有提问,1:提问,2:请回答 */ presence: null, origin_presences: [], students: [], class: null, presences : [], class_id: null, asking_presence: null, my_presence: null, data: { //以下数据的同步由该代码文件实现,数据的呈现由classroom.wxml实现 students: [], // 该课程所有的学生, 元素类型见上文中的student current_user: null, //目前登陆的用户,类型为对象,属性有objectId,username, name teacher: null, //该课程教员,类型为对象 //请在进入界面时判断用户是否为教员以给与权限或更改界面 //属性如下:objectId, username, name class_name: "", //课程名称 class_id: "", //该课程的主键(编号) }, sco1: function () { //打1分的函数 }, sco2: function () { //打2分的函数 }, sco3: function () { //打3分的函数 }, ask: function (event) { //学生提问的函数 console.log(event); set_asking() }, rep: function (event) { console.log(event); //点这个学生要回答他问题的函数 }, set_asking: function(userid, status){ this.asking_present = this.presents.filter(present=>present.get("user").id==userid)[0]; this.asking_present.isasking = status; this.asking_present.save(); }, add_mark: function(mark){ if(this.asking_present==null){ console.warn("没有选择学生"); }else{ user = this.asking_present.get("user"); studentClassMap = this.studentClassMaps.filter(studentClassMap=>studentClassMap.get("user")) } }, pull_presence: function(){ var query = new AV.Query("Presence"); //console.log(this.class); query.equalTo("class", this.class); query.include("user"); set_presence = this.set_presence.bind(this); return AV.Promise.all([query.find().then(set_presence), query.subscribe()]).then(([presents, subscription])=>{ this.PresentSubscription = subscription; if(this.presentUnbind) this.presentUnbind(); this.presentUnbind = bind(subscription, presents, set_presence); }).catch(error=> console.error(error.message)); }, set_presence: function(presences){ console.log("set_presence"); console.log(presences); this.presences = presences; students = this.presences.map((presence)=>{ student = presence.get("user"); if(student.get("username")==null){ students = this.students.filter(stu=>stu.id==student.id) if(students.length==1){ student = students[0]; }else{ student.fetch(); } } student.set("ispresent", presence.get("present")); student.set("isasking",presence.get("isasking")); student.set("mark", presence.get("mark")) if(student.id==AV.User.current().id){ this.my_presence = presence; } return student; }); this.students = students; console.log(students); this.setData(jsonify({students})); return presences; }, fetch_class: function(class_id){ this.class = AV.Object.createWithoutData("Class_", class_id); //this.class.include("teacher"); return this.class.fetch().then((class_) => { //console.log(this.class); teacher = this.class.get("teacher"); console.log("fetch_class"); //console.log(teacher); if(typeof teacher.toJson == "function"){ teacher = teacher.toJson() } this.setData({ class_name: this.class.get("name"), class_id: this.class.get("objectId"), teacher: teacher }); }).catch(error=>console.error(error.message)) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.setData({current_user: AV.User.current()}); this.class_id = options.class_id; this.fetch_class.bind(this)(this.class_id).then(this.pull_presence.bind(this)).then((presences)=>{this.origin_presences=presences, this.my_presence.enter()}); console.log("onLoad"); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { console.log("onReady"); }, /** * 生命周期函数--监听页面显示 */ onShow: function () { console.log("onShow"); console.log("enter class when onShow"); console.log(AV.User.current()); if(this.my_presence){ this.my_presence.enter(); } }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { this.my_presence.leave(); console.log("onHide"); }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { this.my_presence.leave(); console.log("onUnload"); this.PresentSubscription.unsubscribe(); this.presentUnbind(); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { this.pull_presence.bind(this)(); }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { console.log("onShareAppMessage"); } })