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.

101 lines
2.6 KiB

const app = getApp();
Component({
properties: {
},
data: {
list: [],
subList:[],
subjects:[],
subCurrent:-1
},
pageLifetimes:{
show(){
if(!this.disciplines)
this.pullDisciplines();
}
},
attached(){
this.options = {page:1, limit:10};
this.pullDisciplines();
this.pullSubjects({refresh:1});
},
methods: {
pullSubjects({refresh=0}={}){
if(refresh){
if(refresh==1){
this.options.page = 1;
var { options } = this;
}else if(refresh==2){
var {page, per_page} = this.options;
var options = {...this.options,page:1, per_page: page*per_page};
}
}else{
this.options.page++;
var {options} = this;
}
return app.api("weapps.paths")(options).then(({subjects})=>{
let length = subjects.length;
if (!refresh){
subjects = this.data.subjects.concat(subjects);
var status = length>0?200:204;
}else{
var status = length>0?200:205;
}
this.setData({ subjects, status});
}).catch(e=>{
this.setData({subjects:[], status:e.code});
})
},
onPullDownRefresh(){
this.pullSubjects({refresh:2});
},
onReachBottom(){
if(this.data.status==200)
this.pullSubjects();
},
pullDisciplines(){
this.disciplines = [];
app.api("disciplines")({source:"subject"})
.then(res=>{
;
this.disciplines = res.disciplines;
this.disciplines.unshift({id:"",name:"全部", sub_disciplines:[]});
this.setNavList({disciplines: this.disciplines, key:"list"});
//this.setNavList({disciplines: this.disciplines[0].sub_disciplines, key:"subList"});
}).catch(e=>{
this.disciplines = null;
})
},
setNavList({disciplines, key}){
let list = disciplines.map(i=>{
return {text: i.name, id: i.id};
})
this.setData({[key]: list});
},
onCateChange({detail}){
let {current, value} = detail;
this.options.discipline_id = value.id;
this.setNavList({disciplines: this.disciplines[current].sub_disciplines, key:"subList"});
this.setData({subCurrent:-1});
delete this.options.sub_discipline_id;
this.pullSubjects({refresh:1});
},
onSubCateChange({detail}){
let {current, value, source} = detail;
if(source!="touch")
return;
this.setData({subCurrent:current});
if(value.id)
this.options.sub_discipline_id = value.id;
else
delete this.options.sub_discipline_id;
this.pullSubjects({refresh:1});
}
}
})