|
|
|
@ -2,6 +2,7 @@
|
|
|
|
|
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({
|
|
|
|
|
/**
|
|
|
|
@ -14,10 +15,12 @@ Page({
|
|
|
|
|
student.present 是否出席
|
|
|
|
|
student.isasking :0:没有提问,1:提问,2:请回答
|
|
|
|
|
*/
|
|
|
|
|
class:null,
|
|
|
|
|
students: [],
|
|
|
|
|
presences: [],
|
|
|
|
|
my_presence: null,
|
|
|
|
|
is_askiny: null,
|
|
|
|
|
asking_presence: null,
|
|
|
|
|
idStudentMap: {},
|
|
|
|
|
data: {
|
|
|
|
|
//以下数据的同步由该代码文件实现,数据的呈现由classroom.wxml实现
|
|
|
|
|
students: [], // 该课程所有的学生, 元素类型见上文中的student
|
|
|
|
@ -29,6 +32,59 @@ Page({
|
|
|
|
|
//属性如下: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.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){
|
|
|
|
|
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({
|
|
|
|
@ -43,9 +99,9 @@ Page({
|
|
|
|
|
},
|
|
|
|
|
select_stu: function(event){
|
|
|
|
|
id = event.currentTarget.dataset.id;
|
|
|
|
|
if(id!='none'){
|
|
|
|
|
console.log(id);
|
|
|
|
|
}else{console.log("none");id=""}
|
|
|
|
|
if(id=='none'){
|
|
|
|
|
id="";
|
|
|
|
|
}
|
|
|
|
|
this.setData({select_stu_id: id});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
@ -53,14 +109,13 @@ Page({
|
|
|
|
|
userid = event.currentTarget.dataset.id;
|
|
|
|
|
status = event.currentTarget.dataset.status;
|
|
|
|
|
console.log("set_isasking");
|
|
|
|
|
console.log(status);
|
|
|
|
|
console.log(typeof status);
|
|
|
|
|
this.asking_present = this.presences.filter(presence=>presence.get("user").id==userid)[0];
|
|
|
|
|
this.asking_present.isasking = status;
|
|
|
|
|
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_present.save().then(()=>{
|
|
|
|
|
this.asking_presence.save().then(()=>{
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
}, ()=>{
|
|
|
|
|
wx.hideLoading();
|
|
|
|
@ -71,11 +126,10 @@ Page({
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
add_mark: function(event){
|
|
|
|
|
console.log("add_mark");
|
|
|
|
|
id = event.currentTarget.dataset.id;
|
|
|
|
|
mark = event.currentTarget.dataset.mark;
|
|
|
|
|
console.log("add_mark");
|
|
|
|
|
console.log(mark);
|
|
|
|
|
console.log(typeof mark);
|
|
|
|
|
console.log(this.presences);
|
|
|
|
|
presence = this.presences.filter(presence => presence.get("user").id == id)[0];
|
|
|
|
|
if(presence==null){
|
|
|
|
|
console.warn("没有选择学生");
|
|
|
|
@ -90,21 +144,22 @@ Page({
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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);
|
|
|
|
|
console.log(presences);
|
|
|
|
|
this.presences = presences;
|
|
|
|
|
console.log(this.presences);
|
|
|
|
|
students = this.presences.map((presence)=>{
|
|
|
|
|
student = presence.get("user");
|
|
|
|
|
if(student.get("username")==null){
|
|
|
|
@ -115,6 +170,7 @@ Page({
|
|
|
|
|
student.fetch();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.idStudentMap[student.id]=student;
|
|
|
|
|
student.set("present", presence.get("present"));
|
|
|
|
|
student.set("isasking",presence.get("isasking"));
|
|
|
|
|
student.set("mark", presence.get("mark"));
|
|
|
|
@ -125,7 +181,6 @@ Page({
|
|
|
|
|
return student;
|
|
|
|
|
});
|
|
|
|
|
this.students = students;
|
|
|
|
|
console.log(students);
|
|
|
|
|
this.setData(jsonify({students}));
|
|
|
|
|
return presences;
|
|
|
|
|
},
|
|
|
|
@ -135,23 +190,14 @@ Page({
|
|
|
|
|
query.equalTo("objectId", class_id);
|
|
|
|
|
query.include("teacher");
|
|
|
|
|
return query.find().then((classes) => {
|
|
|
|
|
//console.log(this.class);
|
|
|
|
|
this.class = classes[0];
|
|
|
|
|
teacher = this.class.get("teacher");
|
|
|
|
|
console.log("pull_class");
|
|
|
|
|
console.log(teacher);
|
|
|
|
|
teacher = teacher.toJSON();
|
|
|
|
|
// if(typeof teacher.toJson == "function"){
|
|
|
|
|
// teacher = teacher.toJson();
|
|
|
|
|
// console.log("toJson");
|
|
|
|
|
// }
|
|
|
|
|
this.setData({
|
|
|
|
|
class_name: this.class.get("name"),
|
|
|
|
|
class_id: this.class.get("objectId"),
|
|
|
|
|
teacher: teacher
|
|
|
|
|
});
|
|
|
|
|
console.log(teacher);
|
|
|
|
|
//this.setData(teacher);
|
|
|
|
|
}).catch(error=>console.error(error.message))
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
@ -164,16 +210,18 @@ Page({
|
|
|
|
|
})
|
|
|
|
|
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,
|
|
|
|
|
})
|
|
|
|
|
console.log("pull_presence");
|
|
|
|
|
this.pull_presence.bind(this)().then((presences) => { this.my_presence.enter(); this.presences = presences;});
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
console.log("hideLoading");
|
|
|
|
|
});
|
|
|
|
|
.then(() => {
|
|
|
|
|
wx.setNavigationBarTitle({
|
|
|
|
|
title: this.data.class_name,
|
|
|
|
|
})
|
|
|
|
|
wx.hideLoading();//better place??
|
|
|
|
|
});
|
|
|
|
|
this.pull_messages.bind(this)();
|
|
|
|
|
console.log("onLoad");
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
@ -189,9 +237,8 @@ Page({
|
|
|
|
|
*/
|
|
|
|
|
onShow: function () {
|
|
|
|
|
console.log("onShow");
|
|
|
|
|
console.log("enter class when onShow");
|
|
|
|
|
console.log(AV.User.current());
|
|
|
|
|
if(this.my_presence){
|
|
|
|
|
console.log("enter class when onShow");
|
|
|
|
|
this.my_presence.enter();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
@ -210,14 +257,17 @@ Page({
|
|
|
|
|
onUnload: function () {
|
|
|
|
|
this.my_presence.leave();
|
|
|
|
|
console.log("onUnload");
|
|
|
|
|
this.PresentSubscription.unsubscribe();
|
|
|
|
|
this.presentUnbind();
|
|
|
|
|
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(() => {
|
|
|
|
|