// pages/message/index.js const request = require('../../../utils/request'); //导入模块 const util = require('../../../utils/util'); const event = require('../../../utils/event.js') const float = require('../../../utils/floatObj'); const app = getApp() const formate = 'Y-M-D' Page({ /** * 页面的初始数据 */ data: { height: app.globalData.fragmentHeight, CustomBar: app.globalData.CustomBar, loading: false, finished: false, requesting: false, top: 0, rearchStringList: [], taskList: [], form: { name: '', pageSize: 10, pageNum: 1 }, clearClick: false, focus: true }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.setData({ height: app.globalData.fragmentHeight - 100, CustomBar: app.globalData.CustomBar, }) var that = this wx.getStorage({ key: 'mall-search', success (res) { that.setData({ rearchStringList: res.data.split(',') }) } }) }, onEvent: function (message) { console.log('index.onEvent', message) }, bindSearch: function (e) { var word = e.detail.value if(this.data.clearClick && util.isEmpty(this.data.form.name)){ this.data.clearClick = false this.setData({ focus: true }) return } if(word.indexOf(',') >= 0){ word = word.replace(',', '') } this.setData({ ['form.name']: word }) }, bindFocus: function (e) { this.setData({ focus: true }) }, clearHistory: function (e) { var that = this wx.removeStorage({ key: 'mall-search', success (res) { that.setData({ rearchStringList: [] }) } }) }, clearInput: function (e) { this.data.clearClick = true this.setData({ ['form.name']: '', focus: true }) var that = this setTimeout(function () { that.data.clearClick = false }, 100); }, startSearch: function (e) { if(util.isEmpty(this.data.form.name)){ util.showToast('请输入要搜索的关键字') return } this.setData({ taskList: [], ['form.pageNum']: 1, finished: false }) this.fetchTaskList() }, fetchTaskList: function () { if (this.data.requesting || this.data.finished) { return } if (this.data.form.pageNum == 1 && this.data.taskList.length == 0) { this.setData({ loading: true }) } this.data.requesting = true // wx.showLoading({ title: '搜索中', mask: true }) // /product/list产品列表 request.get('/bxe-mall/product/list', this.data.form).then(result => { //成功回调 this.data.requesting = false if(this.data.form.pageNum == 1){ this.setData({ focus: false, top: 0 }) } else { this.setData({ focus: false }) } if (result.data.records.length) { var respList = result.data.records let nowList = `taskList[${this.data.taskList.length}]` var num = this.data.form.pageNum var finished = this.data.form.pageNum >= result.data.pages var strList = this.data.rearchStringList if(!util.isEmpty(this.data.form.name) && strList.indexOf(this.data.form.name) < 0){ strList.splice(0, 0, this.data.form.name) } this.setData({ rearchStringList: strList, [nowList]: respList, total: result.data.total, ['form.pageNum']: (num + 1), finished, loading: false }) } else { var strList = this.data.rearchStringList if(!util.isEmpty(this.data.form.name) && strList.indexOf(this.data.form.name) < 0){ strList.splice(0, 0, this.data.form.name) } this.setData({ rearchStringList: strList, focus: false, finished: true, loading: false }) } // wx.hideLoading() }).catch(err => { //异常回调 // wx.hideLoading() this.setData({ loading: false }) this.data.requesting = false var strList = this.data.rearchStringList if(strList.indexOf(this.data.form.name) < 0){ strList.splice(0, 0, this.data.form.name) } this.setData({ rearchStringList: strList, focus: false }) util.showToast(err) }); }, tagClick: function (e) { this.setData({ ['form.name']: this.data.rearchStringList[e.currentTarget.dataset.index] }) this.startSearch(null) }, lookItem: function (e) { var item = this.data.taskList[e.currentTarget.dataset.page][e.currentTarget.dataset.index] wx.navigateTo({ url: '/pages/mall/order-info/index?id=' + item.id }) }, onUnload() { if(this.data.rearchStringList.length){ try { wx.setStorageSync('mall-search', this.data.rearchStringList.toString()) } catch (e) { } } } })