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.

144 lines
3.3 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: [],
current_user: null,
data: {
loading: true,
classes: []
},
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");
return 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 = [];
current_user = this.current_user;
this.classes.map(class_=>{
teacher = class_.get("teacher");
if(present_class_ids.indexOf(class_.id)>-1 || current_user.id==teacher.id){
class_.set("present", true);
}else{
class_.set("present", false);
classes.push(class_);
}
});
console.log(classes);
this.setData(jsonify({classes}));
this.setData({loading: false});
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
wx.showLoading({
title: '加载中',
})
this.current_user = AV.User.current();
this.pull_classes().then(this.pull_presences).then(wx.hideLoading);
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
this.pull_classes().then(this.pull_presences);
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})