const app = getApp(); const typeMap = { shixun:{current:0, type:"shixun"}, path: {current:1, type:"subject"}, subject:{current:1, type:"subject"}, course:{current:2, type:"course"} } Page({ data: { keyword:"", list:[ {text:"实训项目", type:"shixun"}, {text:"实践课程", type:"subject"}, {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=>{ 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=>{ this.setData({loading:0}); }) }, onLoad: function (options) { let {type:_type="shixun", keyword=""} = options; let {type, current} = typeMap[_type]||typeMap["shixun"]; this.options = {page:1, per_page:20, type, keyword}; this.search({ refresh: 1 }); this.setData({keyword, current}); }, _onReachBottom: function () { this.search({refresh:0}); }, onShareAppMessage: function () { let {type="shixun",keyword=""} = this.options; return app.shareApp({ title:"搜索", path: `/pages/search/search?type=${type}&keyword=${keyword}` }) } })