123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- // pages/index/search/search.js
- const app = getApp();
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- hotsearch:"在家兼职",
- hotsearchList:[],
- searchValue:"",
- searchRecordList:[],
- isSearch:false,
- searchResList:[]
- },
- // 点击搜索结果的每项
- clickRes(e){
- console.log("1")
- wx.navigateTo({
- url: '../../commodity-detail/commodity-detail?id=' + e.currentTarget.dataset.id,
- })
- },
- //清除事件
- clearValue(e){
- this.setData({
- searchValue: ""
- });
- },
- //输入事件
- input(e){
- this.setData({
- searchValue: e.detail.value
- });
- },
- // 从数组头添加数据
- prepend(arr, item) {
- return [item].concat(arr);
- // console.log(arr)
- // console.log(item)
- // if(arr.length == 0){
- // console.log("不包含1")
- // this.setData({
- // searchRecordList: [item].concat(arr)
- // });
- // }else{
- // for (let i = 0; i < arr.length; i++) {
- // //如果找到相同的
- // if (arr[i].message === item.message) {
- // console.log("包含")
- // //删除该重复元素
- // var newarr = arr.splice(i+1, 1)
- // console.log(newarr)
- // //再添加到首项并赋值
- // this.setData({
- // searchRecordList: [item].concat(newarr)
- // });
- // break
- // }
- // }
- // }
-
- },
- // 删除搜索历史
- deleteRecord(e){
- this.setData({
- searchRecordList: []
- });
- wx.setStorage({
- key: "searchRecordList",
- data: []
- })
- },
- // 点击搜索按钮
- search(e){
- var _this = this
- wx.request({
- url: app.globalData.baseURL + '/job/search',
- method: 'GET',
- data: {
- "key": _this.data.searchValue
- },
- success(res) {
- _this.setData({
- searchResList: res.data.data,
- });
- //如果当前为未搜索时
- if (!_this.data.isSearch){
- _this.setData({
- isSearch: !_this.data.isSearch
- });
- }
- console.log(res.data.data)
- }
- })
- },
- // 点击热门搜索热词
- clickHot(e){
- //获取点击的词的内容,并赋值给搜索框
- var value = this.data.hotsearchList[e.currentTarget.dataset.index].message
- //赋值给搜索历史数组并更新缓存
- var item = { message: value}
- this.setData({
- searchValue: value,
- searchRecordList: this.prepend(this.data.searchRecordList, item)
- });
- wx.setStorage({
- key: "searchRecordList",
- data: this.data.searchRecordList
- })
-
- },
- // 点击搜索历史记录词
- clickRecord(e){
- // console.log(this.data.searchRecordList)
- this.setData({
- searchValue: this.data.searchRecordList[e.currentTarget.dataset.index].message
- });
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- var _this = this
- wx.request({
- url: app.globalData.baseURL + '/job/hot',
- method: 'GET',
- success(res) {
- _this.setData({
- hotsearchList: res.data.data
- });
- }
- })
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- try {
- var value = wx.getStorageSync('searchRecordList')
- if (value) {
- // Do something with return value
- this.setData({
- searchRecordList: value,
- });
- }
- } catch (e) {
- // Do something when catch error
- }
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
|