耀实惠小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
8.3 KiB

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