search.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.keyword}}</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. searchlist().then((res) => {
  82. if(res.success){
  83. _self.historyDatas = res.data.list
  84. }else{
  85. _self.$message.warning('没有符合条件的数据!')
  86. }
  87. })
  88. //热门搜索列表
  89. searchhot().then((res) => {
  90. if(res.success){
  91. _self.hotDatas = res.data.list
  92. }else{
  93. _self.$message.warning('没有符合条件的数据!')
  94. }
  95. })
  96. },
  97. shuru(event){
  98. this.keyword=event.detail.value
  99. console.log(this.keyword,'res')
  100. },
  101. // 搜索
  102. search(type, item) {
  103. switch (type) {
  104. case 'history':
  105. this.keyword=item.keyword
  106. for( var i=0;i<this.historyDatas.length;i++){
  107. if(item.keyword=this.historyDatas[i].keyword){
  108. var id=this.historyDatas[i].id
  109. var userId=this.historyDatas[i].userId
  110. var searchCnt=parseInt(this.historyDatas[i].searchCnt)+1
  111. var createBy=this.historyDatas[i].createBy
  112. var createTime=this.historyDatas[i].createTime
  113. }
  114. }
  115. this.$api.togoodslist({
  116. keyword: item.keyword,
  117. userId:userId,
  118. searchCnt:searchCnt,
  119. createBy:createBy,
  120. createTime:createTime,
  121. });
  122. break;
  123. case 'hot':
  124. this.keyword=item.keyword
  125. for( var i=0;i<this.historyDatas.length;i++){
  126. if(item.keyword=this.historyDatas[i].keyword){
  127. var id=this.historyDatas[i].id
  128. var userId=this.historyDatas[i].userId
  129. var searchCnt=parseInt(this.historyDatas[i].searchCnt)+1
  130. var createBy=this.historyDatas[i].createBy
  131. var createTime=this.historyDatas[i].createTime
  132. }
  133. }
  134. this.$api.togoodslist({
  135. keyword: item.keyword,
  136. userId:userId,
  137. searchCnt:searchCnt,
  138. createBy:createBy,
  139. createTime:createTime,
  140. });
  141. break;
  142. default:
  143. this.$api.togoodslist({
  144. keyword: this.keyword,
  145. searchCnt:1,
  146. });
  147. break;
  148. }
  149. },
  150. // 清空搜索历史
  151. clear() {
  152. let _this = this;
  153. var ids=[]
  154. _this.historyDatas.forEach(data => {
  155. ids.push(data.id)
  156. })
  157. uni.showModal({
  158. title: '提示',
  159. content: '清空搜索历史',
  160. success: function(res) {
  161. if (res.confirm) {
  162. var data=ids
  163. searchclear(data).then((res) => {
  164. if(res.success){
  165. _this.historyDatas = [];
  166. return;
  167. }
  168. _this.$api.msg(res.msg)
  169. })
  170. } else if (res.cancel) {
  171. console.log('用户点击取消');
  172. }
  173. }
  174. });
  175. }
  176. }
  177. };
  178. </script>
  179. <style lang="scss">
  180. @import url('/packageShang/components/iconfont/iconfont.css');
  181. @import url('/packageShang/common/common.scss');
  182. .search-area {
  183. height: 120rpx;
  184. input {
  185. width: 560rpx;
  186. padding-left: 100rpx;
  187. height: 64rpx;
  188. line-height: 64rpx;
  189. background-color: #f5f5f5;
  190. padding-top: 10px;
  191. }
  192. .icon-search {
  193. top: 50%;
  194. left: 40rpx;
  195. transform: translate(0, -50%);
  196. z-index: 1;
  197. text {
  198. color: #c0c0c0;
  199. }
  200. }
  201. }
  202. </style>