|
|
|
|
// 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")
|
|
|
|
|
const Message = require("../../model/message")
|
|
|
|
|
|
|
|
|
|
Page({
|
|
|
|
|
/**
|
|
|
|
|
页面的初始数据
|
|
|
|
|
students: 该课程所有的学生
|
|
|
|
|
元素student
|
|
|
|
|
student.objectId 该学生的主键,唯一
|
|
|
|
|
student.username 学生的用户名,是唯一,就像微信号一样
|
|
|
|
|
student.name 学生的姓名
|
|
|
|
|
student.present 是否出席
|
|
|
|
|
student.isasking :0:没有提问,1:提问,2:请回答
|
|
|
|
|
*/
|
|
|
|
|
class:null,
|
|
|
|
|
students: [],
|
|
|
|
|
presences: [],
|
|
|
|
|
my_presence: null,
|
|
|
|
|
asking_presence: null,
|
|
|
|
|
idStudentMap: {},
|
|
|
|
|
data: {
|
|
|
|
|
loading: true,
|
|
|
|
|
students: [], // 该课程所有的学生, 元素类型见上文中的student
|
|
|
|
|
student_num: 0,
|
|
|
|
|
current_user: null, //目前登陆的用户,类型为对象,属性有objectId,username, name
|
|
|
|
|
teacher: null,
|
|
|
|
|
select_stu_id: '',
|
|
|
|
|
//该课程教员,类型为对象
|
|
|
|
|
//请在进入界面时判断用户是否为教员以给与权限或更改界面
|
|
|
|
|
//属性如下:objectId, username, name
|
|
|
|
|
class_name: "", //课程名称
|
|
|
|
|
class_id: "", //该课程的主键(编号)
|
|
|
|
|
message_text: "",
|
|
|
|
|
messages: [],
|
|
|
|
|
show_conversation: true
|
|
|
|
|
},
|
|
|
|
|
pull_messages: function(){
|
|
|
|
|
query = new AV.Query("Message");
|
|
|
|
|
query.equalTo("class", this.class);
|
|
|
|
|
query.include("sender");
|
|
|
|
|
set_messages = this.set_messages.bind(this);
|
|
|
|
|
return AV.Promise.all([query.find().then(set_messages), query.subscribe()]).then(([messages, subscription]) => {
|
|
|
|
|
this.messageSubscription = subscription;
|
|
|
|
|
if (this.messageUnbind) this.messageUnbind();
|
|
|
|
|
this.messageUnbind = bind(subscription, messages, set_messages);
|
|
|
|
|
}).catch(error => console.error(error.message));
|
|
|
|
|
},
|
|
|
|
|
set_messages: function(messages){
|
|
|
|
|
console.log("set_messages");
|
|
|
|
|
messages = messages.map(message=>{
|
|
|
|
|
sender = message.get("sender");
|
|
|
|
|
console.log(this.idStudentMap);
|
|
|
|
|
console.log(sender.id);
|
|
|
|
|
console.log(!sender.get("name") && sender.id in this.idStudentMap)
|
|
|
|
|
if(!sender.get("name")&&sender.id in this.idStudentMap){
|
|
|
|
|
sender = this.idStudentMap[sender.id];
|
|
|
|
|
}
|
|
|
|
|
message.set("sender_name", sender.get("name"));
|
|
|
|
|
message.set("sender_username", sender.get("username"));
|
|
|
|
|
return message;
|
|
|
|
|
})
|
|
|
|
|
console.log(jsonify({ messages }));
|
|
|
|
|
this.setData(jsonify({messages}));
|
|
|
|
|
return messages;
|
|
|
|
|
},
|
|
|
|
|
send_message:function(event){
|
|
|
|
|
let {message_text} = this.data;
|
|
|
|
|
if(message_text.trim()==""){return;}
|
|
|
|
|
message = new Message({
|
|
|
|
|
text: message_text,
|
|
|
|
|
class: this.class,
|
|
|
|
|
sender: AV.User.current()
|
|
|
|
|
})
|
|
|
|
|
message.save().then(()=>{
|
|
|
|
|
this.setData({message_text: ""});
|
|
|
|
|
console.log("send_message")
|
|
|
|
|
}).catch(console.error);
|
|
|
|
|
},
|
|
|
|
|
close_conversation: function(event){
|
|
|
|
|
console.log("close conversation");
|
|
|
|
|
console.log(event);
|
|
|
|
|
this.setData({show_conversation: false});
|
|
|
|
|
this.messageSubscription.unsubscribe();
|
|
|
|
|
this.messageUnbind();
|
|
|
|
|
},
|
|
|
|
|
update_message:function({detail:{value}}){
|
|
|
|
|
this.setData({message_text: value});
|
|
|
|
|
},
|
|
|
|
|
enter_setting: function(event){
|
|
|
|
|
wx.navigateTo({
|
|
|
|
|
url: '../classsetting/classsetting?class_id='+this.data.class_id+"&teacher_id="+this.data.teacher.objectId+"&classname="+this.data.class_name,
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
enter_detail: function(event){
|
|
|
|
|
wx.navigateTo({
|
|
|
|
|
url: '../classdetail/classdetail?class_id='+this.data.class_id,
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
select_stu: function(event){
|
|
|
|
|
id = event.currentTarget.dataset.id;
|
|
|
|
|
if(id=='none'){
|
|
|
|
|
id="";
|
|
|
|
|
}
|
|
|
|
|
this.setData({select_stu_id: id});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
set_isasking: function(event){
|
|
|
|
|
userid = event.currentTarget.dataset.id;
|
|
|
|
|
status = event.currentTarget.dataset.status;
|
|
|
|
|
console.log("set_isasking");
|
|
|
|
|
this.asking_presence = this.presences.filter(presence=>presence.get("user").id==userid)[0];
|
|
|
|
|
console.log(this.asking_presence);
|
|
|
|
|
this.asking_presence.isasking = status;
|
|
|
|
|
wx.showLoading({
|
|
|
|
|
title: '请稍候',
|
|
|
|
|
})
|
|
|
|
|
this.asking_presence.save().then(()=>{
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
}, ()=>{
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: '操作失败',
|
|
|
|
|
icon: "none"
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
add_mark: function(event){
|
|
|
|
|
console.log("add_mark");
|
|
|
|
|
id = event.currentTarget.dataset.id;
|
|
|
|
|
mark = event.currentTarget.dataset.mark;
|
|
|
|
|
console.log(this.presences);
|
|
|
|
|
presence = this.presences.filter(presence => presence.get("user").id == id)[0];
|
|
|
|
|
if(presence==null){
|
|
|
|
|
console.warn("没有选择学生");
|
|
|
|
|
}
|
|
|
|
|
presence.increment("mark", mark).save().then(()=>{
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: '操作成功',
|
|
|
|
|
icon:"none"
|
|
|
|
|
})
|
|
|
|
|
}).catch(error=>console.error(error.message));
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
pull_presence: function(){
|
|
|
|
|
var query = new AV.Query("Presence");
|
|
|
|
|
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(([presences, subscription])=>{
|
|
|
|
|
this.presenceSubscription = subscription;
|
|
|
|
|
if(this.presenceUnbind) this.presenceUnbind();
|
|
|
|
|
console.log("shwo set_presence");
|
|
|
|
|
console.log(presences);
|
|
|
|
|
console.log(set_presence);
|
|
|
|
|
this.presenceUnbind = bind(subscription, presences, set_presence);
|
|
|
|
|
}).catch(error=> console.error(error.message));
|
|
|
|
|
},
|
|
|
|
|
set_presence: function(presences){
|
|
|
|
|
console.log("set_presence");
|
|
|
|
|
console.log(presences);
|
|
|
|
|
pull_presence = this.pull_presence.bind(this);
|
|
|
|
|
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{
|
|
|
|
|
console.warn("pull_presence because new student join");
|
|
|
|
|
pull_presence();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.idStudentMap[student.id]=student;
|
|
|
|
|
student.set("present", 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;
|
|
|
|
|
this.setData({current_user:student.toJSON()});
|
|
|
|
|
}
|
|
|
|
|
return student;
|
|
|
|
|
});
|
|
|
|
|
this.students = students;
|
|
|
|
|
this.setData(jsonify({students}));
|
|
|
|
|
this.setData({loading:false})
|
|
|
|
|
return presences;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
pull_class: function(class_id){
|
|
|
|
|
query = new AV.Query("Class_");
|
|
|
|
|
query.equalTo("objectId", class_id);
|
|
|
|
|
query.include("teacher");
|
|
|
|
|
return query.find().then((classes) => {
|
|
|
|
|
this.class = classes[0];
|
|
|
|
|
teacher = this.class.get("teacher");
|
|
|
|
|
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) {
|
|
|
|
|
wx.showLoading({
|
|
|
|
|
title: '加载中'
|
|
|
|
|
})
|
|
|
|
|
this.setData({current_user: AV.User.current().toJSON()});
|
|
|
|
|
this.class_id = options.class_id;
|
|
|
|
|
this.class = AV.Object.createWithoutData("Class_", this.class_id);
|
|
|
|
|
this.pull_presence.bind(this)().then((presences) => {
|
|
|
|
|
this.my_presence.enter();
|
|
|
|
|
});
|
|
|
|
|
this.pull_class.bind(this)(this.class_id)
|
|
|
|
|
.then(() => {
|
|
|
|
|
wx.setNavigationBarTitle({
|
|
|
|
|
title: this.data.class_name,
|
|
|
|
|
})
|
|
|
|
|
wx.hideLoading();//better place??
|
|
|
|
|
});
|
|
|
|
|
this.pull_messages.bind(this)();
|
|
|
|
|
console.log("onLoad");
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生命周期函数--监听页面初次渲染完成
|
|
|
|
|
*/
|
|
|
|
|
onReady: function () {
|
|
|
|
|
console.log("onReady");
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生命周期函数--监听页面显示
|
|
|
|
|
*/
|
|
|
|
|
onShow: function () {
|
|
|
|
|
console.log("onShow");
|
|
|
|
|
if(this.my_presence){
|
|
|
|
|
console.log("enter class when onShow");
|
|
|
|
|
this.my_presence.enter();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生命周期函数--监听页面隐藏
|
|
|
|
|
*/
|
|
|
|
|
onHide: function () {
|
|
|
|
|
this.my_presence.leave();
|
|
|
|
|
console.log("onHide");
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生命周期函数--监听页面卸载
|
|
|
|
|
*/
|
|
|
|
|
onUnload: function () {
|
|
|
|
|
this.my_presence.leave();
|
|
|
|
|
console.log("onUnload");
|
|
|
|
|
this.presenceSubscription.unsubscribe();
|
|
|
|
|
this.presenceUnbind();
|
|
|
|
|
this.messageSubscription.unsubscribe();
|
|
|
|
|
this.messageUnbind();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 页面相关事件处理函数--监听用户下拉动作
|
|
|
|
|
*/
|
|
|
|
|
onPullDownRefresh: function () {
|
|
|
|
|
console.log(this.idStudentMap);
|
|
|
|
|
this.pull_presence.bind(this)();
|
|
|
|
|
this.pull_class.bind(this)(this.class_id)
|
|
|
|
|
.then(() => {
|
|
|
|
|
wx.setNavigationBarTitle({
|
|
|
|
|
title: this.data.class_name,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 页面上拉触底事件的处理函数
|
|
|
|
|
*/
|
|
|
|
|
onReachBottom: function () {
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用户点击右上角分享
|
|
|
|
|
*/
|
|
|
|
|
onShareAppMessage: function () {
|
|
|
|
|
console.log("onShareAppMessage");
|
|
|
|
|
}
|
|
|
|
|
})
|