import GoodApi from '../../services/good' import Navi from '../../utils/navi' // pages/search/search.ts Page({ /** * 页面的初始数据 */ data: { tags: [] as string[], focusable: false, showResult: false, }, /** * 生命周期函数--监听页面加载 */ onLoad(query: { from: string }) { if (query.from === 'cate') { this.setData({ showResult: true, }) } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() {}, /** * 生命周期函数--监听页面显示 */ onShow() { this.setData({ focusable: true, }) this.handleReload() }, /** * 生命周期函数--监听页面隐藏 */ onHide() { this.setData({ focusable: false, }) }, /** * 生命周期函数--监听页面卸载 */ onUnload() {}, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() {}, /** * 页面上拉触底事件的处理函数 */ onReachBottom() {}, /** * 用户点击右上角分享 */ onShareAppMessage() {}, handleInput(e: WechatMiniprogram.CustomEvent<{ value: string }>) { console.log(e.detail.value) const keyword = e.detail.value const cacheSearch = wx.getStorageSync('searchkeys') let keywords: string[] = [] if (cacheSearch) { keywords = keywords.concat(JSON.parse(cacheSearch)) } if (keyword && keyword.replace(/ /gi, '').length > 0 && keywords.indexOf(keyword) < 0) { keywords.splice(0, 0, keyword) wx.setStorageSync('searchkeys', JSON.stringify(keywords)) this.setData({ tags: keywords, }) } this.goes(keyword) }, handleClick(e: WechatMiniprogram.CustomEvent<{}, {}, { keyword: string }>) { this.goes(e.currentTarget.dataset.keyword) }, handleReload() { const cacheSearch = wx.getStorageSync('searchkeys') let keywords: string[] = [] if (cacheSearch) { keywords = keywords.concat(JSON.parse(cacheSearch)) } this.setData({ tags: keywords, }) }, goes(keyword: string) { if (this.data.showResult) { Navi.navigateTo({ url: '/pages/search/result?keyword=' + keyword, }) } else { const pages = getCurrentPages() //获取小程序页面栈 const beforePage = pages[pages.length - 2] //获取上个页面的实例对象 beforePage.research(keyword) //触发上个页面自定义的shuaxin方法 wx.navigateBack({ delta: 1, }) } }, })