// pages/courses/courses.js const app = getApp(); Page({ options: {}, data: { courses: [], loading: true, page: 1, show_join_course_modal:false, loaded_all: false, }, search_courses: function({detail: {value}}){ this.setData({ loaded_all: false }); this.options.search = value; this.fetch_courses(this.options).then(res => { console.log(res); this.set_courses(res.data.courses); if (res.data.courses.length<=10) { this.setData({ loaded_all: true }) } }) }, show_join_course_modal: function(event){ this.setData({show_join_course_modal: true}); }, cancel_join_course_modal: function(event){ this.setData({show_join_course_modal: false}); }, update_invite_code: function({detail: {value}}){ this.setData({invite_code: value}); }, update_identities: function({detail: {value}}){ this.setData({identities: value}) }, join_course: function (event) { const { invite_code, identities } = this.data; let data = { invite_code: invite_code }; for (var identity of identities) { data[identity] = 1; } console.log(data); console.log({ ...data }); app.client.join_course({ ...data }) .then(res => { if (res.data.status == 401) { wx.showToast({ title: "请先登陆", icon: "none" }); return; } console.log(res); wx.showToast({ title: res.data.message }) wx.navigateTo({ url: "../course/course?course_id=" + res.data.course_id }); this.cancel_join_course_modal(); }) .catch(error => { wx.showToast({ title: error.toString(), icon: "none" }); console.warn(error); }) }, fetch_courses: function(options){ return app.client.search_courses(options) }, set_courses: function(courses){ this.setData({courses: courses}); }, add_courses: function(courses){ this.setData({ courses:[...this.data.courses, ...courses], }) }, onLoad: function (options) { this.fetch_courses().then(res=>{ console.log(res); this.set_courses(res.data.courses); }) }, onReady: function () { }, onShow: function () { }, onHide: function () { }, onUnload: function () { }, onPullDownRefresh: function () { }, onReachBottom: function () { if(!this.data.loaded_all){ wx.pageScrollTo({ selector: ".loading" }) this.fetch_courses({ page: this.data.page + 1 ,...this.options}) .then(res => { this.setData({ page: this.data.page + 1}); if(res.data.courses.length==0){ this.setData({loaded_all: true}) }else{ this.add_courses(res.data.courses); } }) } }, onShareAppMessage: function () { } })