search.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. console.log(this.keyword,'res')
  120. },
  121. // 搜索
  122. search(type, item) {
  123. switch (type) {
  124. case 'history':
  125. this.removeRepeat(item);
  126. uni.setStorageSync('historyKeyword', this.historyDatas.toString());
  127. this.$api.togoodslist({
  128. keyword: item,
  129. userId:userId,
  130. searchCnt:searchCnt,
  131. createBy:createBy,
  132. createTime:createTime,
  133. });
  134. break;
  135. case 'hot':
  136. this.keyword=item.keyword
  137. for( var i=0;i<this.historyDatas.length;i++){
  138. if(item.keyword=this.historyDatas[i].keyword){
  139. var id=this.historyDatas[i].id
  140. var userId=this.historyDatas[i].userId
  141. var searchCnt=parseInt(this.historyDatas[i].searchCnt)+1
  142. var createBy=this.historyDatas[i].createBy
  143. var createTime=this.historyDatas[i].createTime
  144. }
  145. }
  146. this.$api.togoodslist({
  147. keyword: item.keyword,
  148. userId:userId,
  149. searchCnt:searchCnt,
  150. createBy:createBy,
  151. createTime:createTime,
  152. });
  153. break;
  154. default:
  155. this.removeRepeat(this.keyword);
  156. uni.setStorageSync('historyKeyword', this.historyDatas.toString());
  157. this.$api.togoodslist({
  158. keyword: this.keyword,
  159. searchCnt:1,
  160. });
  161. break;
  162. }
  163. },
  164. // 清空搜索历史
  165. clear() {
  166. let _this = this;
  167. var ids=[]
  168. _this.historyDatas.forEach(data => {
  169. ids.push(data.id)
  170. })
  171. uni.showModal({
  172. title: '提示',
  173. content: '清空搜索历史',
  174. success: function(res) {
  175. if (res.confirm) {
  176. uni.removeStorageSync('historyKeyword') //删除缓存
  177. _this.historyDatas=[]
  178. } else if (res.cancel) {
  179. console.log('用户点击取消');
  180. }
  181. }
  182. });
  183. }
  184. }
  185. };
  186. </script>
  187. <style lang="scss">
  188. @import url('/packageShang/components/iconfont/iconfont.css');
  189. @import url('/packageShang/common/common.scss');
  190. .search-area {
  191. height: 120rpx;
  192. input {
  193. width: 560rpx;
  194. padding-left: 100rpx;
  195. height: 64rpx;
  196. line-height: 64rpx;
  197. background-color: #f5f5f5;
  198. padding-top: 10px;
  199. }
  200. .icon-search {
  201. top: 50%;
  202. left: 40rpx;
  203. transform: translate(0, -50%);
  204. z-index: 1;
  205. text {
  206. color: #c0c0c0;
  207. }
  208. }
  209. }
  210. </style>