const app = getApp(); Page({ data: { keyword:"", list:[ {text:"实训项目", type:"shixun"}, {text:"教学课堂", type:"course"} ] }, onTapNav({detail:{current, source , value}}){ if(source!='touch') return; this.setData({current}); this.options.type = value.type; this.search({refresh:1}) .then(()=>{ this.setData({scrollTop:0}); }); }, onSubmit({detail:{value}}){ this.options.keyword = value.keyword; this.search({refresh:1}); }, onConfirm({detail:{value}}){ this.options.keyword = value; this.search({refresh:1}); }, search({refresh=0}={}){ if(refresh==1) this.setData({loading:1}); if(refresh){ if(refresh==1){ this.options.page = 1; var {options} = this; }else if(refresh==2){ var {page,per_page,type, keyword} = this.options; var options = {page:1, per_page:page*per_page, type, keyword}; } }else{ this.options.page++; var {options} = this; } return app.api("search")(options) .then(res=>{ console.error(res); let {results,count} = res; if(refresh==1&&results.length==0) wx.showToast({ title: '没有找到相关内容',icon:"none" }); if(!refresh) results = this.data.results.concat(results); this.setData({count, results, loading:0}); }) .catch(e=>{ console.error(e); this.setData({loading:0}); }) }, onLoad: function (options) { var {type="shixun", keyword=""} = options; this.options = {page:1, per_page:20, type, keyword}; this.search({ refresh: 1 }); for (var current=this.data.list.length-1; current >=0; current--){ if(this.data.list[current].type==type) break; } this.setData({keyword, current}); console.log(this.data); }, _onReachBottom: function () { this.search({refresh:0}); }, onShareAppMessage: function () { let {type="shixun",keyword=""} = this.options; return app.shareApp({ path: `/pages/search/search?type=${type}&keyword=${keyword}` }) } })