search.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template>
  2. <view class="use-page">
  3. <!-- 搜索内容区 -->
  4. <view class="search-area pos-r w-full padding-lr dflex-b">
  5. <view class="h-full flex1 dflex-c">
  6. <view class="icon-search pos-a">
  7. <text class="iconfont iconsousuo-01"></text>
  8. </view>
  9. <input type="text" class="pos-a padding-left padding-tb-xs border-radius-lg box-sizing-b" maxlength="20"
  10. placeholder="请输入关键字" @input="shuru" v-model="keyword" />
  11. </view>
  12. <view class="bg-base-sou border-radius-big padding-tb-xs padding-lr margin-left" @click="search">搜索</view>
  13. </view>
  14. <view class="gap"></view>
  15. <!-- 搜索历史区 -->
  16. <view class="padding-lr w-full padding-top-lg" v-if="historyDatas">
  17. <view class="dflex-b">
  18. <view class="dflex">
  19. <text>搜索历史</text>
  20. </view>
  21. <view class="iconfont iconfont iconlajitong-01 dflex-c ft-dark padding-sm" @click="clear"></view>
  22. </view>
  23. <view class="dflex dflex-wrap-w">
  24. <view
  25. class="item margin-right-sm margin-bottom-sm dflex bg-drak border-radius-lg padding-tb-xs padding-lr"
  26. v-for="(item,index) in historyDatas" :key="index" @click="search('history', item)">
  27. <text>{{item}}</text>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- 热门搜索区 -->
  32. <view class="padding-lr w-full padding-top-lg" v-if="hotDatas">
  33. <view class="padding-bottom-sm dflex-b">
  34. <view class="dflex">
  35. <text>热门搜索</text>
  36. </view>
  37. </view>
  38. <view class="dflex dflex-wrap-w">
  39. <view
  40. class="item margin-right-sm margin-bottom-sm dflex bg-drak border-radius-lg padding-tb-xs padding-lr"
  41. v-for="(item, index) in hotDatas" :key="index" @click="search('hot', item)">
  42. <text>{{item.keyword}}</text>
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. import {
  50. searchlist,
  51. searchhot,
  52. searchclear
  53. } from '../../../utils/api_goods.js'
  54. const _history = 'usemall-search-history'
  55. const _hot = 'usemall-search-hot'
  56. import { mapState } from 'vuex';
  57. export default {
  58. computed: {
  59. ...mapState(['islogin'])
  60. },
  61. data() {
  62. return {
  63. // 搜索关键字
  64. keyword: '',
  65. // 历史搜索
  66. historyDatas: [],
  67. // 热门搜索
  68. hotDatas: [],
  69. };
  70. },
  71. onShow() {
  72. // 初始化
  73. this.keyword = '';
  74. // 加载数据
  75. this.loadData();
  76. },
  77. methods: {
  78. loadData() {
  79. let _self=this
  80. //搜索历史列表
  81. let history = uni.getStorageSync('historyKeyword');
  82. console.log(history,'history')
  83. if (history) {
  84. _self.historyDatas = history.split(',');
  85. }
  86. console.log(_self.historyDatas,'_self.historyDatas')
  87. // searchlist().then((res) => {
  88. // if(res.success){
  89. // _self.historyDatas = res.data.list
  90. // }else{
  91. // _self.$message.warning('没有符合条件的数据!')
  92. // }
  93. // })
  94. //热门搜索列表
  95. searchhot().then((res) => {
  96. if(res.success){
  97. _self.hotDatas = res.data.list
  98. }else{
  99. _self.$message.warning('没有符合条件的数据!')
  100. }
  101. })
  102. },
  103. //判断条数和去重
  104. removeRepeat: function (value) {
  105. for (let i = 0; i < this.historyDatas.length; i++) {
  106. if (value === this.historyDatas[i]) {
  107. this.historyDatas.splice(i, 1);
  108. }
  109. }
  110. if (this.historyDatas.length < 10) {
  111. this.historyDatas.unshift(value);
  112. } else {
  113. this.historyDatas.pop();
  114. this.historyDatas.unshift(value);
  115. }
  116. },
  117. shuru(event){
  118. this.keyword=event.detail.value
  119. },
  120. // 搜索
  121. search(type, item) {
  122. switch (type) {
  123. case 'history':
  124. this.removeRepeat(item);
  125. uni.setStorageSync('historyKeyword', this.historyDatas.toString());
  126. this.$api.togoodslist({
  127. keyword: item,
  128. // userId:userId,
  129. // searchCnt:searchCnt,
  130. // createBy:createBy,
  131. // createTime:createTime,
  132. });
  133. break;
  134. case 'hot':
  135. this.removeRepeat(item.keyword);
  136. uni.setStorageSync('historyKeyword', this.historyDatas.toString());
  137. this.keyword=item.keyword
  138. // for( var i=0;i<this.historyDatas.length;i++){
  139. // if(item.keyword=this.historyDatas[i].keyword){
  140. // var id=this.historyDatas[i].id
  141. // var userId=this.historyDatas[i].userId
  142. // var searchCnt=parseInt(this.historyDatas[i].searchCnt)+1
  143. // var createBy=this.historyDatas[i].createBy
  144. // var createTime=this.historyDatas[i].createTime
  145. // }
  146. // }
  147. console.log()
  148. this.$api.togoodslist({
  149. keyword: item.keyword,
  150. // userId:userId,
  151. // searchCnt:searchCnt,
  152. // createBy:createBy,
  153. // createTime:createTime,
  154. });
  155. break;
  156. default:
  157. this.removeRepeat(this.keyword);
  158. uni.setStorageSync('historyKeyword', this.historyDatas.toString());
  159. this.$api.togoodslist({
  160. keyword: this.keyword,
  161. searchCnt:1,
  162. });
  163. break;
  164. }
  165. },
  166. // 清空搜索历史
  167. clear() {
  168. let _this = this;
  169. var ids=[]
  170. _this.historyDatas.forEach(data => {
  171. ids.push(data.id)
  172. })
  173. uni.showModal({
  174. title: '提示',
  175. content: '清空搜索历史',
  176. success: function(res) {
  177. if (res.confirm) {
  178. uni.removeStorageSync('historyKeyword') //删除缓存
  179. _this.historyDatas=[]
  180. } else if (res.cancel) {
  181. console.log('用户点击取消');
  182. }
  183. }
  184. });
  185. }
  186. }
  187. };
  188. </script>
  189. <style lang="scss">
  190. @import url('/packageShang/components/iconfont/iconfont.css');
  191. @import url('/packageShang/common/common.scss');
  192. .search-area {
  193. height: 120rpx;
  194. input {
  195. width: 560rpx;
  196. padding-left: 100rpx;
  197. height: 64rpx;
  198. line-height: 64rpx;
  199. background-color: #f5f5f5;
  200. padding-top: 10px;
  201. }
  202. .icon-search {
  203. top: 50%;
  204. left: 40rpx;
  205. transform: translate(0, -50%);
  206. z-index: 1;
  207. text {
  208. color: #c0c0c0;
  209. }
  210. }
  211. }
  212. </style>