| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <template>
- <view class="use-page">
- <!-- 搜索内容区 -->
- <view class="search-area pos-r w-full padding-lr dflex-b">
- <view class="h-full flex1 dflex-c">
- <view class="icon-search pos-a">
- <text class="iconfont iconsousuo-01"></text>
- </view>
- <input type="text" class="pos-a padding-left padding-tb-xs border-radius-lg box-sizing-b" maxlength="20"
- placeholder="请输入关键字" @input="shuru" v-model="keyword" />
- </view>
- <view class="bg-base-sou border-radius-big padding-tb-xs padding-lr margin-left" @click="search">搜索</view>
- </view>
- <view class="gap"></view>
- <!-- 搜索历史区 -->
- <view class="padding-lr w-full padding-top-lg" v-if="historyDatas">
- <view class="dflex-b">
- <view class="dflex">
- <text>搜索历史</text>
- </view>
- <view class="iconfont iconfont iconlajitong-01 dflex-c ft-dark padding-sm" @click="clear"></view>
- </view>
- <view class="dflex dflex-wrap-w">
- <view
- class="item margin-right-sm margin-bottom-sm dflex bg-drak border-radius-lg padding-tb-xs padding-lr"
- v-for="(item,index) in historyDatas" :key="index" @click="search('history', item)">
- <text>{{item}}</text>
- </view>
- </view>
- </view>
- <!-- 热门搜索区 -->
- <view class="padding-lr w-full padding-top-lg" v-if="hotDatas">
- <view class="padding-bottom-sm dflex-b">
- <view class="dflex">
- <text>热门搜索</text>
- </view>
- </view>
- <view class="dflex dflex-wrap-w">
- <view
- class="item margin-right-sm margin-bottom-sm dflex bg-drak border-radius-lg padding-tb-xs padding-lr"
- v-for="(item, index) in hotDatas" :key="index" @click="search('hot', item)">
- <text>{{item.keyword}}</text>
- </view>
- </view>
- </view>
- </view>
- </template>
- <script>
- import {
- searchlist,
- searchhot,
- searchclear
- } from '../../../utils/api_goods.js'
- const _history = 'usemall-search-history'
- const _hot = 'usemall-search-hot'
- import { mapState } from 'vuex';
- export default {
- computed: {
- ...mapState(['islogin'])
- },
- data() {
- return {
- // 搜索关键字
- keyword: '',
- // 历史搜索
- historyDatas: [],
- // 热门搜索
- hotDatas: [],
- };
- },
- onShow() {
- // 初始化
- this.keyword = '';
- // 加载数据
- this.loadData();
- },
- methods: {
- loadData() {
- let _self=this
- //搜索历史列表
- let history = uni.getStorageSync('historyKeyword');
- console.log(history,'history')
- if (history) {
- _self.historyDatas = history.split(',');
- }
- console.log(_self.historyDatas,'_self.historyDatas')
- // searchlist().then((res) => {
- // if(res.success){
- // _self.historyDatas = res.data.list
- // }else{
- // _self.$message.warning('没有符合条件的数据!')
- // }
- // })
- //热门搜索列表
- searchhot().then((res) => {
- if(res.success){
- _self.hotDatas = res.data.list
- }else{
- _self.$message.warning('没有符合条件的数据!')
- }
- })
- },
- //判断条数和去重
- removeRepeat: function (value) {
- for (let i = 0; i < this.historyDatas.length; i++) {
- if (value === this.historyDatas[i]) {
- this.historyDatas.splice(i, 1);
- }
- }
- if (this.historyDatas.length < 10) {
- this.historyDatas.unshift(value);
- } else {
- this.historyDatas.pop();
- this.historyDatas.unshift(value);
- }
- },
- shuru(event){
- this.keyword=event.detail.value
- },
- // 搜索
- search(type, item) {
- switch (type) {
- case 'history':
- this.removeRepeat(item);
- uni.setStorageSync('historyKeyword', this.historyDatas.toString());
- this.$api.togoodslist({
- keyword: item,
- // userId:userId,
- // searchCnt:searchCnt,
- // createBy:createBy,
- // createTime:createTime,
- });
- break;
- case 'hot':
- this.removeRepeat(item.keyword);
- uni.setStorageSync('historyKeyword', this.historyDatas.toString());
- this.keyword=item.keyword
- // for( var i=0;i<this.historyDatas.length;i++){
- // if(item.keyword=this.historyDatas[i].keyword){
- // var id=this.historyDatas[i].id
- // var userId=this.historyDatas[i].userId
- // var searchCnt=parseInt(this.historyDatas[i].searchCnt)+1
- // var createBy=this.historyDatas[i].createBy
- // var createTime=this.historyDatas[i].createTime
- // }
- // }
- console.log()
- this.$api.togoodslist({
- keyword: item.keyword,
- // userId:userId,
- // searchCnt:searchCnt,
- // createBy:createBy,
- // createTime:createTime,
- });
- break;
- default:
- this.removeRepeat(this.keyword);
- uni.setStorageSync('historyKeyword', this.historyDatas.toString());
- this.$api.togoodslist({
- keyword: this.keyword,
- searchCnt:1,
- });
- break;
- }
- },
- // 清空搜索历史
- clear() {
- let _this = this;
- var ids=[]
- _this.historyDatas.forEach(data => {
- ids.push(data.id)
- })
- uni.showModal({
- title: '提示',
- content: '清空搜索历史',
- success: function(res) {
- if (res.confirm) {
- uni.removeStorageSync('historyKeyword') //删除缓存
- _this.historyDatas=[]
- } else if (res.cancel) {
- console.log('用户点击取消');
- }
- }
- });
- }
- }
- };
- </script>
- <style lang="scss">
- @import url('/packageShang/components/iconfont/iconfont.css');
- @import url('/packageShang/common/common.scss');
- .search-area {
- height: 120rpx;
- input {
- width: 560rpx;
- padding-left: 100rpx;
- height: 64rpx;
- line-height: 64rpx;
- background-color: #f5f5f5;
- padding-top: 10px;
- }
- .icon-search {
- top: 50%;
- left: 40rpx;
- transform: translate(0, -50%);
- z-index: 1;
- text {
- color: #c0c0c0;
- }
- }
- }
- </style>
|