search.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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"
  6. @search="doSearch(false)" @input="inputChange" @confirm="doSearch(false)" v-model="keyword"></mSearch>
  7. <view class="cancel" @click="cancel">
  8. 取消
  9. </view>
  10. <!-- 原样式 如果使用原样式,恢复下方注销代码 -->
  11. <!--
  12. <view class="input-box">
  13. <input type="text" :adjust-position="true" :placeholder="defaultKeyword" @input="inputChange" v-model="keyword" @confirm="doSearch(false)"
  14. placeholder-class="placeholder-class" confirm-type="search">
  15. </view>
  16. <view class="search-btn" @tap="doSearch(false)">搜索</view>
  17. -->
  18. <!-- 原样式 end -->
  19. </view>
  20. <view class="search-keyword">
  21. <scroll-view class="keyword-list-box" v-show="isShowKeywordList" scroll-y>
  22. <block v-for="(row,index) in keywordList" :key="index">
  23. <view class="keyword-entry" hover-class="keyword-entry-tap">
  24. <view class="keyword-text" @tap.stop="doSearch(keywordList[index].keyword)">
  25. <rich-text :nodes="row.htmlStr"></rich-text>
  26. </view>
  27. <view class="keyword-img" @tap.stop="setKeyword(keywordList[index].keyword)">
  28. <image src="/static/HM-search/back.png"></image>
  29. </view>
  30. </view>
  31. </block>
  32. </scroll-view>
  33. <scroll-view class="keyword-box" v-show="!isShowKeywordList" scroll-y>
  34. <view class="keyword-block" v-if="oldKeywordList.length>0">
  35. <view class="keyword-list-header">
  36. <view style="font-size: 32rpx; font-weight: 700;">最近搜索</view>
  37. <view>
  38. <image @tap="oldDelete" src="/static/HM-search/delete.png"></image>
  39. </view>
  40. </view>
  41. <view class="keyword">
  42. <view v-for="(keyword,index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">
  43. {{keyword}}
  44. </view>
  45. </view>
  46. </view>
  47. <view class="keyword-block">
  48. <view class="keyword-list-header">
  49. <view style="font-size: 32rpx; font-weight: 700;">推荐搜索</view>
  50. <view>
  51. <image @tap="hotToggle" :src="'/static/HM-search/attention'+forbid+'.png'"></image>
  52. </view>
  53. </view>
  54. <view class="keyword" v-if="forbid==''">
  55. <view v-for="(keyword,index) in hotKeywordList" @tap="doSearch(keyword)" :key="index">
  56. {{keyword}}
  57. </view>
  58. </view>
  59. <view class="hide-hot-tis" v-else>
  60. <view>当前搜热门搜索已隐藏</view>
  61. </view>
  62. </view>
  63. </scroll-view>
  64. </view>
  65. </view>
  66. </template>
  67. <script>
  68. import mSearch from '@/components/mehaotian-search-revision.vue';
  69. export default {
  70. name: "search",
  71. data() {
  72. return {
  73. defaultKeyword: "请输入作物名称或用户名称",
  74. keyword: "",
  75. oldKeywordList: [],
  76. hotKeywordList: [],
  77. keywordList: [],
  78. forbid: '',
  79. isShowKeywordList: false
  80. };
  81. },
  82. components: {
  83. //引用mSearch组件,如不需要删除即可
  84. mSearch
  85. },
  86. mounted() {
  87. this.init();
  88. },
  89. methods: {
  90. cancel() {
  91. this.$emit('searchVal', '')
  92. },
  93. init() {
  94. this.loadDefaultKeyword();
  95. this.loadOldKeyword();
  96. this.loadHotKeyword();
  97. },
  98. blur() {
  99. uni.hideKeyboard()
  100. },
  101. //加载默认搜索关键字
  102. loadDefaultKeyword() {
  103. //定义默认搜索关键字,可以自己实现ajax请求数据再赋值,用户未输入时,以水印方式显示在输入框,直接不输入内容搜索会搜索默认关键字
  104. // this.defaultKeyword = "默认关键字";
  105. },
  106. //加载历史搜索,自动读取本地Storage
  107. loadOldKeyword() {
  108. uni.getStorage({
  109. key: 'OldKeys',
  110. success: (res) => {
  111. var OldKeys = JSON.parse(res.data);
  112. this.oldKeywordList = OldKeys;
  113. }
  114. });
  115. },
  116. //加载热门搜索
  117. loadHotKeyword() {
  118. //定义热门搜索关键字,可以自己实现ajax请求数据再赋值
  119. this.hotKeywordList = ['键盘', '鼠标', '显示器', '电脑主机', '蓝牙音箱', '笔记本电脑', '鼠标垫', 'USB', 'USB3.0'];
  120. },
  121. //监听输入
  122. inputChange(event) {
  123. //兼容引入组件时传入参数情况
  124. var keyword = event.detail ? event.detail.value : event;
  125. if (!keyword) {
  126. this.keywordList = [];
  127. this.isShowKeywordList = false;
  128. return;
  129. }
  130. this.isShowKeywordList = true;
  131. //以下示例截取淘宝的关键字,请替换成你的接口
  132. uni.request({
  133. url: 'https://suggest.taobao.com/sug?code=utf-8&q=' + keyword, //仅为示例
  134. success: (res) => {
  135. this.keywordList = [];
  136. this.keywordList = this.drawCorrelativeKeyword(res.data.result, keyword);
  137. }
  138. });
  139. },
  140. //高亮关键字
  141. drawCorrelativeKeyword(keywords, keyword) {
  142. var len = keywords.length,
  143. keywordArr = [];
  144. for (var i = 0; i < len; i++) {
  145. var row = keywords[i];
  146. //定义高亮#9f9f9f
  147. var html = row[0].replace(keyword, "<span style='color: #9f9f9f;'>" + keyword + "</span>");
  148. html = '<div>' + html + '</div>';
  149. var tmpObj = {
  150. keyword: row[0],
  151. htmlStr: html
  152. };
  153. keywordArr.push(tmpObj)
  154. }
  155. return keywordArr;
  156. },
  157. //顶置关键字
  158. setKeyword(index) {
  159. this.keyword = this.keywordList[index].keyword;
  160. },
  161. //清除历史搜索
  162. oldDelete() {
  163. uni.showModal({
  164. content: '确定清除历史搜索记录?',
  165. success: (res) => {
  166. if (res.confirm) {
  167. console.log('用户点击确定');
  168. this.oldKeywordList = [];
  169. uni.removeStorage({
  170. key: 'OldKeys'
  171. });
  172. } else if (res.cancel) {
  173. console.log('用户点击取消');
  174. }
  175. }
  176. });
  177. },
  178. //热门搜索开关
  179. hotToggle() {
  180. this.forbid = this.forbid ? '' : '_forbid';
  181. },
  182. //执行搜索
  183. doSearch(keyword) {
  184. keyword = keyword === false ? this.keyword : keyword;
  185. this.keyword = keyword;
  186. this.saveKeyword(keyword); //保存为历史
  187. // uni.showToast({
  188. // title: keyword,
  189. // icon: 'none',
  190. // duration: 2000
  191. // });
  192. this.$emit('searchVal', this.keyword)
  193. //以下是示例跳转淘宝搜索,可自己实现搜索逻辑
  194. /*
  195. //#ifdef APP-PLUS
  196. plus.runtime.openURL(encodeURI('taobao://s.taobao.com/search?q=' + keyword));
  197. //#endif
  198. //#ifdef H5
  199. window.location.href = 'taobao://s.taobao.com/search?q=' + keyword
  200. //#endif
  201. */
  202. },
  203. //保存关键字到历史记录
  204. saveKeyword(keyword) {
  205. uni.getStorage({
  206. key: 'OldKeys',
  207. success: (res) => {
  208. var OldKeys = JSON.parse(res.data);
  209. var findIndex = OldKeys.indexOf(keyword);
  210. if (findIndex == -1) {
  211. OldKeys.unshift(keyword);
  212. } else {
  213. OldKeys.splice(findIndex, 1);
  214. OldKeys.unshift(keyword);
  215. }
  216. //最多10个纪录
  217. OldKeys.length > 10 && OldKeys.pop();
  218. uni.setStorage({
  219. key: 'OldKeys',
  220. data: JSON.stringify(OldKeys)
  221. });
  222. this.oldKeywordList = OldKeys; //更新历史搜索
  223. },
  224. fail: (e) => {
  225. var OldKeys = [keyword];
  226. uni.setStorage({
  227. key: 'OldKeys',
  228. data: JSON.stringify(OldKeys)
  229. });
  230. this.oldKeywordList = OldKeys; //更新历史搜索
  231. }
  232. });
  233. }
  234. }
  235. }
  236. </script>
  237. <style lang="scss" scoped>
  238. view {
  239. // display: block;
  240. }
  241. .content {
  242. overflow: hidden;
  243. background: #fff;
  244. }
  245. .cancel {
  246. font-size: 28rpx;
  247. color: #333333;
  248. }
  249. .search-box {
  250. width: 100%;
  251. background-color: #fff;
  252. padding: 15upx 2.5%;
  253. display: flex;
  254. justify-content: space-between;
  255. position: sticky;
  256. top: 0;
  257. align-items: center;
  258. }
  259. .search-box .mSearch-input-box {
  260. width: 90%;
  261. }
  262. .search-box .input-box {
  263. width: 85%;
  264. flex-shrink: 1;
  265. display: flex;
  266. justify-content: center;
  267. align-items: center;
  268. }
  269. .search-box .search-btn {
  270. width: 15%;
  271. margin: 0 0 0 2%;
  272. display: flex;
  273. justify-content: center;
  274. align-items: center;
  275. flex-shrink: 0;
  276. font-size: 28upx;
  277. color: #fff;
  278. background: linear-gradient(to right, #ff9801, #ff570a);
  279. border-radius: 60upx;
  280. }
  281. .search-box .input-box>input {
  282. width: 100%;
  283. height: 60upx;
  284. font-size: 32upx;
  285. border: 0;
  286. border-radius: 60upx;
  287. -webkit-appearance: none;
  288. -moz-appearance: none;
  289. appearance: none;
  290. padding: 0 3%;
  291. margin: 0;
  292. background-color: #ffffff;
  293. }
  294. .placeholder-class {
  295. color: #9e9e9e;
  296. }
  297. .search-keyword {
  298. width: 100%;
  299. background-color: rgb(242, 242, 242);
  300. }
  301. .keyword-list-box {
  302. height: calc(100vh - 110upx);
  303. padding-top: 10upx;
  304. border-radius: 20upx 20upx 0 0;
  305. background-color: #fff;
  306. }
  307. .keyword-entry-tap {
  308. background-color: #eee;
  309. }
  310. .keyword-entry {
  311. width: 94%;
  312. height: 80upx;
  313. margin: 0 3%;
  314. font-size: 30upx;
  315. color: #333;
  316. display: flex;
  317. justify-content: space-between;
  318. align-items: center;
  319. border-bottom: solid 1upx #e7e7e7;
  320. }
  321. .keyword-entry image {
  322. width: 60upx;
  323. height: 60upx;
  324. }
  325. .keyword-entry .keyword-text,
  326. .keyword-entry .keyword-img {
  327. height: 80upx;
  328. display: flex;
  329. align-items: center;
  330. }
  331. .keyword-entry .keyword-text {
  332. width: 90%;
  333. }
  334. .keyword-entry .keyword-img {
  335. width: 10%;
  336. justify-content: center;
  337. }
  338. .keyword-box {
  339. height: calc(100vh - 280rpx);
  340. background-color: #fff;
  341. }
  342. .keyword-box .keyword-block {
  343. padding: 10upx 0;
  344. }
  345. .keyword-box .keyword-block .keyword-list-header {
  346. width: 94%;
  347. padding: 10upx 3%;
  348. font-size: 27upx;
  349. color: #333;
  350. display: flex;
  351. justify-content: space-between;
  352. }
  353. .keyword-box .keyword-block .keyword-list-header image {
  354. width: 40upx;
  355. height: 40upx;
  356. }
  357. .keyword-box .keyword-block .keyword {
  358. width: 100%;
  359. padding: 3px 3%;
  360. display: flex;
  361. flex-flow: wrap;
  362. justify-content: flex-start;
  363. }
  364. .keyword-box .keyword-block .hide-hot-tis {
  365. display: flex;
  366. justify-content: center;
  367. font-size: 28upx;
  368. }
  369. .keyword-box .keyword-block .keyword>view {
  370. display: flex;
  371. justify-content: center;
  372. align-items: center;
  373. border-radius: 60upx;
  374. padding: 0 20upx;
  375. margin: 10upx 20upx 10upx 0;
  376. height: 56rpx;
  377. font-size: 24rpx;
  378. background-color: #F5F6F9;
  379. color: #333333;
  380. }
  381. </style>