search.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <template>
  2. <view class="content">
  3. <view class="search-box">
  4. <!-- mSearch组件 如果使用原样式,删除组件元素-->
  5. <mSearch ref='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/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/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. props: {
  72. searchcontent: {
  73. type: String,
  74. default: ''
  75. }
  76. },
  77. data() {
  78. return {
  79. defaultKeyword: "请输入作物名称或用户名称",
  80. keyword: "",
  81. keyword2: "",
  82. oldKeywordList: [],
  83. hotKeywordList: [],
  84. keywordList: [],
  85. forbid: '',
  86. isShowKeywordList: false
  87. };
  88. },
  89. components: {
  90. //引用mSearch组件,如不需要删除即可
  91. mSearch
  92. },
  93. mounted() {
  94. this.init();
  95. },
  96. watch: {
  97. // searchcontent:{
  98. // handler(oldValue, newValue) {
  99. // this.keyword=oldValue
  100. // // if (val != '') {
  101. // // this.place = val
  102. // // }
  103. // },
  104. // deep:true,
  105. // immediate:true,
  106. // },
  107. // keyword:{
  108. // handler(oldValue, newValue) {
  109. // this.keyword2=oldValue
  110. // console.log(oldValue, newValue,11111111)
  111. // // if (val != '') {
  112. // // this.place = val
  113. // // }
  114. // },
  115. // deep:true,
  116. // immediate:true,
  117. // },
  118. },
  119. methods: {
  120. cancel() {
  121. this.$emit('searchVal', '')
  122. },
  123. init() {
  124. this.$refs.msearch.getFocus()
  125. console.log(this.$refs.msearch)
  126. this.loadDefaultKeyword();
  127. this.loadOldKeyword();
  128. this.loadHotKeyword();
  129. },
  130. blur() {
  131. uni.hideKeyboard()
  132. },
  133. //加载默认搜索关键字
  134. loadDefaultKeyword() {
  135. //定义默认搜索关键字,可以自己实现ajax请求数据再赋值,用户未输入时,以水印方式显示在输入框,直接不输入内容搜索会搜索默认关键字
  136. // this.defaultKeyword = "默认关键字";
  137. },
  138. //加载历史搜索,自动读取本地Storage
  139. loadOldKeyword() {
  140. uni.getStorage({
  141. key: 'OldKeys',
  142. success: (res) => {
  143. var OldKeys = JSON.parse(res.data);
  144. this.oldKeywordList = OldKeys;
  145. }
  146. });
  147. },
  148. //加载热门搜索
  149. loadHotKeyword() {
  150. //定义热门搜索关键字,可以自己实现ajax请求数据再赋值
  151. this.hotKeywordList = ['大豆', '玉米', '高粱', '白小麦', '混合麦', '水稻', '玉米淀粉', '豆粕', '大麦', '小麦'];
  152. },
  153. //监听输入
  154. inputChange(event) {
  155. //兼容引入组件时传入参数情况
  156. var keyword = event.detail ? event.detail.value : event;
  157. if (!keyword) {
  158. this.keywordList = [];
  159. this.isShowKeywordList = false;
  160. return;
  161. }
  162. this.isShowKeywordList = true;
  163. //以下示例截取淘宝的关键字,请替换成你的接口
  164. uni.request({
  165. url: 'https://suggest.taobao.com/sug?code=utf-8&q=' + keyword, //仅为示例
  166. success: (res) => {
  167. this.keywordList = [];
  168. // this.keywordList = this.drawCorrelativeKeyword(res.data.result, keyword);
  169. }
  170. });
  171. },
  172. //高亮关键字
  173. drawCorrelativeKeyword(keywords, keyword) {
  174. var len = keywords.length,
  175. keywordArr = [];
  176. for (var i = 0; i < len; i++) {
  177. var row = keywords[i];
  178. //定义高亮#9f9f9f
  179. var html = row[0].replace(keyword, "<span style='color: #9f9f9f;'>" + keyword + "</span>");
  180. html = '<div>' + html + '</div>';
  181. var tmpObj = {
  182. keyword: row[0],
  183. htmlStr: html
  184. };
  185. keywordArr.push(tmpObj)
  186. }
  187. return keywordArr;
  188. },
  189. //顶置关键字
  190. setKeyword(index) {
  191. this.keyword = this.keywordList[index].keyword;
  192. },
  193. //清除历史搜索
  194. oldDelete() {
  195. uni.showModal({
  196. content: '确定清除历史搜索记录?',
  197. success: (res) => {
  198. if (res.confirm) {
  199. console.log('用户点击确定');
  200. this.oldKeywordList = [];
  201. uni.removeStorage({
  202. key: 'OldKeys'
  203. });
  204. } else if (res.cancel) {
  205. console.log('用户点击取消');
  206. }
  207. }
  208. });
  209. },
  210. //热门搜索开关
  211. hotToggle() {
  212. this.forbid = this.forbid ? '' : '_forbid';
  213. },
  214. //执行搜索
  215. doSearch(keyword) {
  216. keyword = keyword === false ? this.keyword : keyword;
  217. this.keyword = keyword;
  218. this.saveKeyword(keyword); //保存为历史
  219. // uni.showToast({
  220. // title: keyword,
  221. // icon: 'none',
  222. // duration: 2000
  223. // });
  224. this.$emit('searchVal', this.keyword)
  225. //以下是示例跳转淘宝搜索,可自己实现搜索逻辑
  226. /*
  227. //#ifdef APP-PLUS
  228. plus.runtime.openURL(encodeURI('taobao://s.taobao.com/search?q=' + keyword));
  229. //#endif
  230. //#ifdef H5
  231. window.location.href = 'taobao://s.taobao.com/search?q=' + keyword
  232. //#endif
  233. */
  234. },
  235. //保存关键字到历史记录
  236. saveKeyword(keyword) {
  237. uni.getStorage({
  238. key: 'OldKeys',
  239. success: (res) => {
  240. var OldKeys = JSON.parse(res.data);
  241. var findIndex = OldKeys.indexOf(keyword);
  242. if (findIndex == -1) {
  243. OldKeys.unshift(keyword);
  244. } else {
  245. OldKeys.splice(findIndex, 1);
  246. OldKeys.unshift(keyword);
  247. }
  248. //最多10个纪录
  249. OldKeys.length > 10 && OldKeys.pop();
  250. uni.setStorage({
  251. key: 'OldKeys',
  252. data: JSON.stringify(OldKeys)
  253. });
  254. this.oldKeywordList = OldKeys; //更新历史搜索
  255. },
  256. fail: (e) => {
  257. var OldKeys = [keyword];
  258. uni.setStorage({
  259. key: 'OldKeys',
  260. data: JSON.stringify(OldKeys)
  261. });
  262. this.oldKeywordList = OldKeys; //更新历史搜索
  263. }
  264. });
  265. }
  266. }
  267. }
  268. </script>
  269. <style lang="scss" scoped>
  270. view {
  271. // display: block;
  272. }
  273. .content {
  274. overflow: hidden;
  275. background: #fff;
  276. }
  277. .cancel {
  278. font-size: 28rpx;
  279. color: #333333;
  280. }
  281. .search-box {
  282. width: 100%;
  283. background-color: #fff;
  284. padding: 15upx 2.5%;
  285. display: flex;
  286. justify-content: space-between;
  287. position: sticky;
  288. top: 0;
  289. align-items: center;
  290. }
  291. .search-box .mSearch-input-box {
  292. width: 90%;
  293. }
  294. .search-box .input-box {
  295. width: 85%;
  296. flex-shrink: 1;
  297. display: flex;
  298. justify-content: center;
  299. align-items: center;
  300. }
  301. .search-box .search-btn {
  302. width: 15%;
  303. margin: 0 0 0 2%;
  304. display: flex;
  305. justify-content: center;
  306. align-items: center;
  307. flex-shrink: 0;
  308. font-size: 28upx;
  309. color: #fff;
  310. background: linear-gradient(to right, #ff9801, #ff570a);
  311. border-radius: 60upx;
  312. }
  313. .search-box .input-box>input {
  314. width: 100%;
  315. height: 60upx;
  316. font-size: 32upx;
  317. border: 0;
  318. border-radius: 60upx;
  319. -webkit-appearance: none;
  320. -moz-appearance: none;
  321. appearance: none;
  322. padding: 0 3%;
  323. margin: 0;
  324. background-color: #ffffff;
  325. }
  326. .placeholder-class {
  327. color: #9e9e9e;
  328. }
  329. .search-keyword {
  330. width: 100%;
  331. background-color: rgb(242, 242, 242);
  332. }
  333. .keyword-list-box {
  334. height: calc(100vh - 110upx);
  335. padding-top: 10upx;
  336. border-radius: 20upx 20upx 0 0;
  337. background-color: #fff;
  338. }
  339. .keyword-entry-tap {
  340. background-color: #eee;
  341. }
  342. .keyword-entry {
  343. width: 94%;
  344. height: 80upx;
  345. margin: 0 3%;
  346. font-size: 30upx;
  347. color: #333;
  348. display: flex;
  349. justify-content: space-between;
  350. align-items: center;
  351. border-bottom: solid 1upx #e7e7e7;
  352. }
  353. .keyword-entry image {
  354. width: 60upx;
  355. height: 60upx;
  356. }
  357. .keyword-entry .keyword-text,
  358. .keyword-entry .keyword-img {
  359. height: 80upx;
  360. display: flex;
  361. align-items: center;
  362. }
  363. .keyword-entry .keyword-text {
  364. width: 90%;
  365. }
  366. .keyword-entry .keyword-img {
  367. width: 10%;
  368. justify-content: center;
  369. }
  370. .keyword-box {
  371. height: calc(100vh - 280rpx);
  372. background-color: #fff;
  373. }
  374. .keyword-box .keyword-block {
  375. padding: 10upx 0;
  376. }
  377. .keyword-box .keyword-block .keyword-list-header {
  378. width: 100%;
  379. padding: 10upx 3%;
  380. font-size: 27upx;
  381. color: #333;
  382. display: flex;
  383. justify-content: space-between;
  384. align-items: center;
  385. }
  386. .keyword-box .keyword-block .keyword-list-header image {
  387. width: 32upx;
  388. height: 32upx;
  389. }
  390. .keyword-box .keyword-block .keyword {
  391. width: 100%;
  392. padding: 3px 3%;
  393. display: flex;
  394. flex-flow: wrap;
  395. justify-content: flex-start;
  396. }
  397. .keyword-box .keyword-block .hide-hot-tis {
  398. display: flex;
  399. justify-content: center;
  400. font-size: 28upx;
  401. }
  402. .keyword-box .keyword-block .keyword>view {
  403. display: flex;
  404. justify-content: center;
  405. align-items: center;
  406. border-radius: 60upx;
  407. padding: 0 20upx;
  408. margin: 10upx 20upx 10upx 0;
  409. height: 56rpx;
  410. font-size: 24rpx;
  411. background-color: #F5F6F9;
  412. color: #333333;
  413. }
  414. </style>