search.vue 8.9 KB

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