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.

169 lines
3.9 KiB

// pages/addclass/addclass.js
const AV = require("../../lib/av-live-query-weapp-min")
const Presence = require("../../model/presence")
const Class = require("../../model/class")
const { jsonify } = require('../../utils/leancloudutils');
Page({
/**
* 页面的初始数据
*/
classes: [],
data: {
new_classname:'',
new_class_teacher_username: '',
classes: []
},
updateName: function ({detail: {value}}){
this.setData({new_classname:value});
},
create_class: function(event){
const {new_classname} = this.data;
class_ = new Class({
name: new_classname,
teacher: AV.User.current()
})
presence = new Presence({
user: AV.User.current(),
class: class_
});
wx.showLoading({
title: '请稍候',
})
presence.save().then(()=>{
wx.hideLoading();
wx.showToast({
title: '创建成功,可在首页课程列表以教师身份进入课堂',
icon:"none",
duration: 4000
});
this.setData({new_classname: ""});
},
()=>{
wx.hideLoading();
wx.showToast({
title: '操作失败',
icon: "none"
});
});
},
add_class:function(event){
class_id = event.currentTarget.dataset.id;
class_name = event.currentTarget.dataset.name;
pull_presences = this.pull_presences.bind(this);
wx.showModal({
title: '确认',
content: '是否加入学习'+class_name,
success(res) {
if (res.confirm) {
class_ = AV.Object.createWithoutData("Class_", class_id);
presence = new Presence({
user: AV.User.current(),
class: class_
});
wx.showLoading({
title: '请稍候',
})
presence.save().then(()=>{
pull_presences();
wx.hideLoading();
wx.showToast({
title: '加入成功',
icon: "success"
})
},
() => {
wx.hideLoading();
wx.showToast({
title: '加入失败',
icon: "none"
})
}).catch(error=>console.error(error.message));
}
}
})
},
pull_classes: function(){
query = new AV.Query("Class_");
return query.find().then(classes=>{console.log(classes);this.classes=classes});
},
pull_presences: function(){
query = new AV.Query("Presence");
query.equalTo("user", AV.User.current());
query.include("class");
query.find().then(this.set_presences.bind(this));
},
set_presences: function(presences){
console.log("set_presences");
console.log(presences);
present_class_ids = presences.map(presence=>presence.get("class").id);
classes =this.classes.map(class_=>{
if(present_class_ids.indexOf(class_.id)>-1){
class_.set("present", true);
}else{
class_.set("present", false);
}
return class_;
});
console.log(classes);
this.setData(jsonify({classes}));
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.pull_classes().then(this.pull_presences);
this.setData({new_class_teacher_username: AV.User.current().get("username")});
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
this.pull_classes().then(this.pull_presences);
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})