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