search.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <view class="content">
  3. <view class="search-box">
  4. <!-- mSearch组件 如果使用原样式,删除组件元素-->
  5. <mSearch class="mSearch-input-box" :mode="2" button="inside" :placeholder="defaultKeyword" @search="doSearch(false)" @input="inputChange" @confirm="doSearch(false)" v-model="keyword"></mSearch>
  6. <!-- 原样式 如果使用原样式,恢复下方注销代码 -->
  7. <!--
  8. <view class="input-box">
  9. <input type="text" :placeholder="defaultKeyword" @input="inputChange" v-model="keyword" @confirm="doSearch(false)"
  10. placeholder-class="placeholder-class" confirm-type="search">
  11. </view>
  12. <view class="search-btn" @tap="doSearch(false)">搜索</view>
  13. -->
  14. <!-- 原样式 end -->
  15. </view>
  16. <view class="search-keyword" @touchstart="blur">
  17. <scroll-view class="keyword-list-box" v-show="isShowKeywordList" scroll-y>
  18. <view class="keyword-entry" hover-class="keyword-entry-tap" v-for="row in keywordList" :key="row.keyword">
  19. <view class="keyword-text" @tap="doSearch(row.keyword)">
  20. <rich-text :nodes="row.htmlStr"></rich-text>
  21. </view>
  22. <view class="keyword-img" @tap="setkeyword(row)">
  23. <image src="/static/HM-search/back.png"></image>
  24. </view>
  25. </view>
  26. </scroll-view>
  27. <scroll-view class="keyword-box" v-show="!isShowKeywordList" scroll-y>
  28. <view class="keyword-block" v-if="oldKeywordList.length>0">
  29. <view class="keyword-list-header">
  30. <view>历史搜索</view>
  31. <view>
  32. <image @tap="oldDelete" src="/static/cart/delete.png"></image>
  33. </view>
  34. </view>
  35. <view class="keyword">
  36. <view v-for="(keyword,index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">{{keyword}}</view>
  37. </view>
  38. </view>
  39. <!-- <view class="keyword-block">
  40. <view class="keyword-list-header">
  41. <view>热门搜索</view>
  42. <view>
  43. <image @tap="hotToggle" :src="'/static/HM-search/attention'+forbid+'.png'"></image>
  44. </view>
  45. </view>
  46. <view class="keyword" v-if="forbid==''">
  47. <view v-for="(keyword,index) in hotKeywordList" @tap="doSearch(keyword)" :key="index">{{keyword}}</view>
  48. </view>
  49. <view class="hide-hot-tis" v-else>
  50. <view>当前搜热门搜索已隐藏</view>
  51. </view>
  52. </view> -->
  53. </scroll-view>
  54. </view>
  55. </view>
  56. </template>
  57. <script>
  58. //引用mSearch组件,如不需要删除即可
  59. import mSearch from '@/components/mehaotian-search-revision.vue';
  60. export default {
  61. data() {
  62. return {
  63. defaultKeyword: "新鲜,搜一下就涞了",
  64. keyword: "",
  65. oldKeywordList: [],
  66. hotKeywordList: [],
  67. keywordList: [],
  68. forbid: '',
  69. isShowKeywordList: false
  70. }
  71. },
  72. onLoad() {
  73. this.init();
  74. },
  75. components: {
  76. //引用mSearch组件,如不需要删除即可
  77. mSearch
  78. },
  79. methods: {
  80. init() {
  81. this.loadDefaultKeyword();
  82. this.loadOldKeyword();
  83. // this.loadHotKeyword();
  84. },
  85. blur(){
  86. uni.hideKeyboard()
  87. },
  88. //加载默认搜索关键字
  89. loadDefaultKeyword() {
  90. //定义默认搜索关键字,可以自己实现ajax请求数据再赋值,用户未输入时,以水印方式显示在输入框,直接不输入内容搜索会搜索默认关键字
  91. this.defaultKeyword = "默认关键字";
  92. },
  93. //加载历史搜索,自动读取本地Storage
  94. loadOldKeyword() {
  95. uni.getStorage({
  96. key: 'OldKeys',
  97. success: (res) => {
  98. var OldKeys = JSON.parse(res.data);
  99. this.oldKeywordList = OldKeys;
  100. }
  101. });
  102. },
  103. //加载热门搜索
  104. // loadHotKeyword() {
  105. // //定义热门搜索关键字,可以自己实现ajax请求数据再赋值
  106. // this.hotKeywordList = ['键盘', '鼠标', '显示器', '电脑主机', '蓝牙音箱', '笔记本电脑', '鼠标垫', 'USB', 'USB3.0'];
  107. // },
  108. //监听输入
  109. inputChange(event) {
  110. //兼容引入组件时传入参数情况
  111. var keyword = event.detail?event.detail.value:event;
  112. // if (!keyword) {
  113. // this.keywordList = [];
  114. // this.isShowKeywordList = false;
  115. // return;
  116. // }
  117. // this.isShowKeywordList = true;
  118. // //以下示例截取淘宝的关键字,请替换成你的接口
  119. // uni.request({
  120. // url: 'https://suggest.taobao.com/sug?code=utf-8&q=' + keyword, //仅为示例
  121. // success: (res) => {
  122. // this.keywordList = this.drawCorrelativeKeyword(res.data.result, keyword);
  123. // }
  124. // });
  125. },
  126. //高亮关键字
  127. drawCorrelativeKeyword(keywords, keyword) {
  128. var len = keywords.length,
  129. keywordArr = [];
  130. for (var i = 0; i < len; i++) {
  131. var row = keywords[i];
  132. //定义高亮#9f9f9f
  133. var html = row[0].replace(keyword, "<span style='color: #9f9f9f;'>" + keyword + "</span>");
  134. html = '<div>' + html + '</div>';
  135. var tmpObj = {
  136. keyword: row[0],
  137. htmlStr: html
  138. };
  139. keywordArr.push(tmpObj)
  140. }
  141. return keywordArr;
  142. },
  143. //顶置关键字
  144. setkeyword(data) {
  145. this.keyword = data.keyword;
  146. },
  147. //清除历史搜索
  148. oldDelete() {
  149. uni.showModal({
  150. content: '确定清除历史搜索记录?',
  151. success: (res) => {
  152. if (res.confirm) {
  153. console.log('用户点击确定');
  154. this.oldKeywordList = [];
  155. uni.removeStorage({
  156. key: 'OldKeys'
  157. });
  158. } else if (res.cancel) {
  159. console.log('用户点击取消');
  160. }
  161. }
  162. });
  163. },
  164. //热门搜索开关
  165. hotToggle() {
  166. this.forbid = this.forbid ? '' : '_forbid';
  167. },
  168. //执行搜索
  169. doSearch(key) {
  170. key = key ? key : this.keyword ? this.keyword : this.defaultKeyword;
  171. this.keyword = key;
  172. this.saveKeyword(key); //保存为历史
  173. uni.navigateTo({
  174. url: `/pages/product/list?keywords=${key}`
  175. })
  176. },
  177. //保存关键字到历史记录
  178. saveKeyword(keyword) {
  179. uni.getStorage({
  180. key: 'OldKeys',
  181. success: (res) => {
  182. console.log(res.data);
  183. var OldKeys = JSON.parse(res.data);
  184. var findIndex = OldKeys.indexOf(keyword);
  185. if (findIndex == -1) {
  186. OldKeys.unshift(keyword);
  187. } else {
  188. OldKeys.splice(findIndex, 1);
  189. OldKeys.unshift(keyword);
  190. }
  191. //最多10个纪录
  192. OldKeys.length > 10 && OldKeys.pop();
  193. uni.setStorage({
  194. key: 'OldKeys',
  195. data: JSON.stringify(OldKeys)
  196. });
  197. this.oldKeywordList = OldKeys; //更新历史搜索
  198. },
  199. fail: (e) => {
  200. var OldKeys = [keyword];
  201. uni.setStorage({
  202. key: 'OldKeys',
  203. data: JSON.stringify(OldKeys)
  204. });
  205. this.oldKeywordList = OldKeys; //更新历史搜索
  206. }
  207. });
  208. }
  209. }
  210. }
  211. </script>
  212. <style>
  213. view{display:block;}
  214. .search-box {width:100%;background-color:rgb(242,242,242);padding:15upx 2.5%;display:flex;justify-content:space-between;}
  215. .search-box .mSearch-input-box{width: 100%;}
  216. .search-box .input-box {width:85%;flex-shrink:1;display:flex;justify-content:center;align-items:center;}
  217. .search-box .search-btn {width:15%;margin:0 0 0 2%;display:flex;justify-content:center;align-items:center;flex-shrink:0;font-size:28upx;color:#fff;background:linear-gradient(to right,#ff9801,#ff570a);border-radius:60upx;}
  218. .search-box .input-box>input {width:100%;height:60upx;font-size:32upx;border:0;border-radius:60upx;-webkit-appearance:none;-moz-appearance:none;appearance:none;padding:0 3%;margin:0;background-color:#ffffff;}
  219. .placeholder-class {color:#9e9e9e;}
  220. .search-keyword {width:100%;background-color:rgb(242,242,242);}
  221. .keyword-list-box {height:calc(100vh - 110upx);padding-top:10upx;border-radius:20upx 20upx 0 0;background-color:#fff;}
  222. .keyword-entry-tap {background-color:#eee;}
  223. .keyword-entry {width:94%;height:80upx;margin:0 3%;font-size:30upx;color:#333;display:flex;justify-content:space-between;align-items:center;border-bottom:solid 1upx #e7e7e7;}
  224. .keyword-entry image {width:60upx;height:60upx;}
  225. .keyword-entry .keyword-text,.keyword-entry .keyword-img {height:80upx;display:flex;align-items:center;}
  226. .keyword-entry .keyword-text {width:90%;}
  227. .keyword-entry .keyword-img {width:10%;justify-content:center;}
  228. .keyword-box {height:calc(100vh - 110upx);border-radius:20upx 20upx 0 0;background-color:#fff;}
  229. .keyword-box .keyword-block {padding:10upx 0;}
  230. .keyword-box .keyword-block .keyword-list-header {width:94%;padding:10upx 3%;font-size:27upx;color:#333;display:flex;justify-content:space-between;}
  231. .keyword-box .keyword-block .keyword-list-header image {width:40upx;height:40upx;}
  232. .keyword-box .keyword-block .keyword {width:94%;padding:3px 3%;display:flex;flex-flow:wrap;justify-content:flex-start;}
  233. .keyword-box .keyword-block .hide-hot-tis {display:flex;justify-content:center;font-size:28upx;color:#6b6b6b;}
  234. .keyword-box .keyword-block .keyword>view {display:flex;justify-content:center;align-items:center;border-radius:60upx;padding:0 20upx;margin:10upx 20upx 10upx 0;height:60upx;font-size:28upx;background-color:rgb(242,242,242);color:#6b6b6b;}
  235. </style>