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.
136 lines
3.6 KiB
136 lines
3.6 KiB
// pages/mall/search-list/index.js
|
|
const util = require('../../../utils/util')
|
|
const app = getApp()
|
|
|
|
Page({
|
|
/**
|
|
* 页面的初始数据
|
|
*/
|
|
data: {
|
|
height: app.globalData.fragmentHeight - 100,
|
|
loading: false,
|
|
finished: false,
|
|
requesting: false,
|
|
top: 0,
|
|
rearchStringList: [],
|
|
taskList: [],
|
|
form: {
|
|
name: '',
|
|
pageSize: 10,
|
|
pageNum: 1
|
|
},
|
|
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(',') })
|
|
}
|
|
})
|
|
},
|
|
onChange(e) {
|
|
this.setData({ ['form.name']: e.detail })
|
|
},
|
|
/**
|
|
* 生命周期函数--监听页面显示
|
|
*/
|
|
onSearch: 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
|
|
// /product/list产品列表
|
|
searchProductList(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 = ((num - 1) * this.data.form.pageSize + respList.length) >= result.data.total
|
|
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,
|
|
['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)
|
|
});
|
|
},
|
|
clearHistory: function (e) {
|
|
var that = this
|
|
wx.removeStorage({
|
|
key: 'mall-search',
|
|
success (res) {
|
|
that.setData({
|
|
rearchStringList: []
|
|
})
|
|
}
|
|
})
|
|
},
|
|
tagClick: function(e){
|
|
this.setData({ ['form.name']: this.data.rearchStringList[e.currentTarget.dataset.index] })
|
|
this.onSearch(null)
|
|
},
|
|
onUnload() {
|
|
if(this.data.rearchStringList.length){
|
|
try {
|
|
wx.setStorageSync('mall-search', this.data.rearchStringList.toString())
|
|
} catch (e) {
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
})
|