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.

135 lines
3.9 KiB

const app = getApp();
/*
statuses:[]
*/
Component({
data: {
attachDir: global.config.attachDir,
categories: [{ text: "我学习的", value: "study" }, { text: "我管理的", value: "manage" }],
statuses: [{ text: "正在进行", value: "processing" }, { text: "已结束", value: "end" }],
courses: [],
status: 0,
user: {},
loading: true,
current_cate: -1
},
attached() {
let user = app.user();
let key = `cache-weapps.home-${user.user_id}-${user.is_teacher}`
let cache_courses = wx.getStorageSync(key);
if(cache_courses)
this.setData({courses:cache_courses}); // set local cache;
this.options = { page: 1, limit: 15 };
if (user.is_teacher)
var current_cate = 1;
else
var current_cate = 0;
this.options["category"] = this.data.categories[current_cate].value;
this.pullCourses({refresh:1})
.then(res=>{
if(res.courses){
wx.setStorageSync(key, res.courses);
}
this.loaded = 1;
});
this.setData({current_cate});
},
pageLifetimes: {
show: function () {
if (this.loaded >= 0) {
console.log("show pull")
this.pullCourses({ refresh: 2, showError:0 });
}
this.setData({user_id: app.user().user_id});
}
},
methods: {
onTapButton(){
if(this.data.user_id==2)
app.navigateTo({url:"{account}"});
else
this.setData({showModal:1});
},
onCategoryChange: function ({ detail: { current, value, source } }) {
if(source!='touch') return;
this.options["category"] = value.value;
this.pullCourses({ refresh: 1 });
this.setData({current_cate: current});
},
onStatusChange: function ({ detail: { value } }) {
this.options["status"] = value.value;
this.pullCourses({ refresh: 1 });
},
addCourse() {
if(app.user().user_id==2)
return wx.showToast({
title: '请先登录'
});
return this.setData({ showModal: 1 });
/*
if (app.user().user_identity == "学生") {
this.setData({ showModal: 1 });
} else {
wx.showActionSheet({
itemList: ["加入课堂", '创建课堂'],
success: res => {
if (res.tapIndex == 0) {
this.setData({ showModal: 1 });
} else {
app.navigateTo({ url: "{course_setting}?intent=create" });
}
}
})
}
*/
},
enter_course: function (event) {
let { id, course_name } = event.currentTarget.dataset;
wx.navigateTo({
url: "/course/pages/course/course?course_id=" + id + "&course_name=" + course_name,
})
},
pullCourses: function ({ refresh = 0, showError=1} = {}) {
if (refresh) {
if (refresh == 1) {
this.options.page = 1;
var { options: data } = this;
} else if (refresh == 2) {
var { limit, page, status, category } = this.options;
var data = { limit: page * limit, page: 1, status, category };
}
} else {
this.options.page++;
var { options: data } = this;
}
return app.callApi({
name: "weapps.home", data
})
.then(res => {
let { courses } = res;
if (data.status)
courses = courses.filter(i => {
return i.is_end == (data.status == "end")
});
if (!refresh)
courses = this.data.courses.concat(courses);
this.setData({ courses, loading: false });
return res;
}).catch(e => {
if(e.code==401&&refresh)
this.setData({courses:[]});
if(showError&&e.code!=401)
app.showError(e);
this.setData({loading: false });
return e;
});
},
onReachBottom() {
this.pullCourses();
},
onPullDownRefresh: function () {
this.pullCourses({ refresh: 2 });
},
}
})