微信小程序优选商城首部搜索

<style lang="less">
  .searchpage{
    .searchbox{
      background-color: #f00;
      padding: 30rpx 0;
      text-align: center;
      .inputtag{
        display: inline-block;
        width: 80%;
        height: 80rpx;
        line-height: 80rpx;
        border-radius: 40rpx;
        padding: 0 40rpx;
        background-color: #fff;
        text-align: left;
        font-size: 26rpx;
        color: #666666;
      }
    }

    .databox{
      color: #666666;
      font-size: 28rpx;
      line-height: 60rpx;
      .dataitem{
        overflow: hidden;
        height: 60rpx;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
</style>

<template>
  <div class="searchpage">
    <view class="searchbox">
      <input type="text" v-model.trim="query" class="inputtag" placeholder="请输入搜索的内容" bindinput="inputHandle"/>
      <!-- <input type="text" v-model.trim="query" class="inputtag" placeholder="请输入搜索的内容" bindconfirm="searchHandle"/> -->
    </view>
    <view class="databox">
      <view class="dataitem" bindtap="toDetail" data-id="{{item.goods_id}}" wx:for="{{goodsData}}" wx:key="goods_id">{{item.goods_name}}</view>
    </view>
    <view class="historybox" wx:if="{{!goodsData.length && historyData.length}}">
      <button bindtap="deleteAll">删除全部</button>
            <view bindtap="searchHistory" data-item="{{item}}" class="historyitem"  wx:for="{{historyData}}" wx:key="*this"><text >{{item}}</text> <text catchtap="deleteSingle" data-index="{{index}}">删除</text></view>
      <!-- <view class="historyitem"  wx:for="{{historyData}}" wx:key="*this"><text bindtap="searchHistory" data-item="{{item}}">{{item}}</text> <text bindtap="deleteSingle" data-index="{{index}}">删除</text></view> -->
    </view>
  </div>
</template>

<script>
  import wepy from '@wepy/core'
  
  import axiosapi from '../request/request'
  wepy.page({
    data: {
      query: '',
      goodsData: [],
      historyData: [],
      timeInt: null
    },
    onShow(){
      // 从缓存获取数据 用于渲染
      this.historyData = wx.getStorageSync('history') || []
    },

    methods:{
      // 防抖 dobounce  节流 throgle

        inputHandle(){
          if(this.timeInt){
            clearTimeout(this.timeInt)
          }
          this.timeInt = setTimeout(()=>{
            this.searchHandle()
          },1000)
        },
      // 用户点击搜索
      searchHandle(){
        // 判断为空
        if(!this.query){
          wx.showToast({
            title: '不能为空',
            icon: "none"
          })
          this.goodsData = []
          return
        }

        // 判断重复
        let index = this.historyData.findIndex(item=>{
          return item == this.query
        })

        if(index>-1){
          this.historyData.splice(index,1)
        }

        // 最多只能添加8个
        if(this.historyData.length>=8){
          this.historyData.splice(this.historyData.length-1,1)
        }

        // 添加缓存

        // let arr = wx.getStorageSync('history') || []

        // arr.unshift(this.query) // 数组头部添加一个元素

        this.historyData.unshift(this.query)

        wx.setStorageSync('history',this.historyData)

        this.getData()
      },
      getData(){
        //   axiosapi({url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/qsearch?query='+this.query}).then(res=>{
        //     this.goodsData = res
        // })


        axiosapi({url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/qsearch',data: {
          query: this.query
        }}).then(res=>{
            this.goodsData = res
        })
      },
     
      searchHistory(e){
        let {item} = e.currentTarget.dataset
        this.query = item
        this.searchHandle()
      },
      //  删除单个
      deleteSingle(e){
        let {index} = e.currentTarget.dataset
        this.historyData.splice(index,1)
        wx.setStorageSync('history',this.historyData)
      },
      deleteAll(){
        this.historyData=[]
        wx.setStorageSync('history',this.historyData)
      },
      // 去详情
      toDetail(e){
        // let {id} = e.currentTarget.dataset
        wx.navigateTo({
          url: "/pages/detail?id="+e.currentTarget.dataset.id
        })
      }
    }

    
  });
</script>
<config>
{
    navigationBarTitleText: '优购商城',
    usingComponents: {
     
    }
}
</config>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值