search.vue 10 KB

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