search.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <template>
  2. <view class="rf-search">
  3. <u-navbar :back-text="i18n('Back')" :back-text-style="backStyle"
  4. back-icon-color="#fff" title-color="#fff"
  5. :title="i18n('Search')"
  6. :background="background">
  7. </u-navbar>
  8. <view class="search-box">
  9. <mSearch
  10. class="mSearch-input-box"
  11. :mode="1"
  12. button="inside"
  13. :placeholder="hotSearchDefault || ''"
  14. @search="doSearch(false)"
  15. @confirm="doSearch(false)"
  16. v-model="keyword"
  17. ></mSearch>
  18. </view>
  19. <view class="search-keyword" @tap="blur">
  20. <scroll-view class="keyword-list-box" v-if="isShowKeywordList" scroll-y>
  21. <view
  22. class="keyword-entry"
  23. hover-class="keyword-entry-tap"
  24. v-for="row in keywordList"
  25. :key="row.keyword"
  26. >
  27. <view class="keyword-text" @tap="doSearch(row.keyword)">
  28. <rf-parser lazy-load :html="row.htmlStr"></rf-parser>
  29. </view>
  30. <view class="keyword-img" @tap="setkeyword(row)">
  31. <!--<image src="/static/HM-search/back.png"></image>-->
  32. </view>
  33. </view>
  34. </scroll-view>
  35. <scroll-view class="keyword-box" v-if="!isShowKeywordList" scroll-y>
  36. <view class="keyword-block">
  37. <view class="keyword-list-header">
  38. <view>{{i18n('HistorySearch')}}</view>
  39. <view>
  40. <text
  41. class="iconfont iconiconfontshanchu1"
  42. @tap="oldDelete"
  43. ></text>
  44. </view>
  45. </view>
  46. <view class="keyword">
  47. <view
  48. v-for="(keyword, index) in oldKeywordList"
  49. @tap="doSearch(keyword)"
  50. :key="index"
  51. >{{ keyword }}</view
  52. >
  53. </view>
  54. <view class="hide-hot-tis" v-if="oldKeywordList.length === 0">
  55. <view>{{i18n('Norecord')}}</view>
  56. </view>
  57. </view>
  58. <view class="keyword-block" v-if="hotKeywordList">
  59. <view class="keyword-list-header">
  60. <view>{{i18n('Hotsearch')}}</view>
  61. <view>
  62. <text class="iconfont" :class="forbid" @tap="hotToggle"></text>
  63. </view>
  64. </view>
  65. <view class="keyword" v-if="forbid == 'iconai47'">
  66. <view
  67. v-for="(keyword, index) in hotKeywordList"
  68. @tap.stop="doSearch(keyword,'hot')"
  69. :key="index"
  70. >{{ i18n(keyword) }}</view
  71. >
  72. </view>
  73. <view class="hide-hot-tis" v-else>
  74. <view>{{$i18n.locale=='zh'?'当前搜热门搜索已隐藏':'Current popular search is hidden'}}</view>
  75. </view>
  76. </view>
  77. </scroll-view>
  78. </view>
  79. </view>
  80. </template>
  81. <script>
  82. import mSearch from '@/components/rf-search/rf-search';
  83. export default {
  84. data() {
  85. return {
  86. keyword: '',
  87. oldKeywordList: [],
  88. hotKeywordList: [],
  89. keywordList: [],
  90. background: {
  91. backgroundImage: 'linear-gradient(270deg, #4BC0E2 0%, #538BE7 100%)',
  92. },
  93. backStyle: {
  94. color: '#fff'
  95. },
  96. forbid: 'iconai47',
  97. isShowKeywordList: false,
  98. hotSearchDefault: '',
  99. type: null ,// 搜索类型
  100. hotSerch:['cleanenergy','Smartgrid','Energystorage','UHV','GEI']
  101. };
  102. },
  103. onLoad(options) {
  104. this.initData(options);
  105. },
  106. components: {
  107. mSearch
  108. },
  109. methods: {
  110. i18n (data) {
  111. return this.$t('common.'+data);
  112. },
  113. async initData(options) {
  114. if(options.data !=='undefined'){
  115. this.type = options.type;
  116. const search = JSON.parse(options.data || '{}');
  117. this.hotSearchDefault = search.hot_search_default || options.keyword;
  118. this.hotKeywordList = search.hot_search_list;
  119. this.loadOldKeyword();
  120. }else {
  121. this.hotKeywordList = this.hotSerch;
  122. this.loadOldKeyword();
  123. }
  124. },
  125. blur() {
  126. uni.hideKeyboard();
  127. },
  128. // 加载历史搜索,自动读取本地Storage
  129. loadOldKeyword() {
  130. uni.getStorage({
  131. key: 'OldKeys',
  132. success: res => {
  133. let OldKeys = JSON.parse(res.data);
  134. this.oldKeywordList = OldKeys;
  135. }
  136. });
  137. },
  138. // 顶置关键字
  139. setkeyword(data) {
  140. this.keyword = data.keyword;
  141. },
  142. // 清除历史搜索
  143. oldDelete() {
  144. uni.showModal({
  145. content: this.$i18n.locale=='zh'?'确定清除历史搜索记录?':'Are you sure you want to clear the history search',
  146. cancelText: this.$t('common.Cancel'), // 取消按钮的文字
  147. confirmText: this.$t('common.submit'), // 确认按钮文字
  148. success: res => {
  149. if (res.confirm) {
  150. this.oldKeywordList = [];
  151. uni.removeStorage({ key: 'OldKeys' });
  152. }
  153. }
  154. });
  155. },
  156. // 热门搜索开关
  157. hotToggle() {
  158. this.forbid = this.forbid === 'iconai47' ? 'iconyanjing' : 'iconai47';
  159. },
  160. // 执行搜索
  161. doSearch(key,hot) {
  162. key = key || this.keyword || this.defaultKeyword;
  163. if(hot) {
  164. key = this.i18n(key);
  165. }
  166. this.keyword = key;
  167. uni.navigateTo({
  168. url: '/pages/order/search/search?keyword=' + key,
  169. });
  170. this.saveKeyword(key);// 保存为历史
  171. },
  172. // 保存关键字到历史记录
  173. saveKeyword(keyword) {
  174. uni.getStorage({
  175. key: 'OldKeys',
  176. success: res => {
  177. let OldKeys = JSON.parse(res.data);
  178. let findIndex = OldKeys.indexOf(keyword);
  179. if (findIndex === -1) {
  180. OldKeys.unshift(keyword);
  181. } else {
  182. OldKeys.splice(findIndex, 1);
  183. OldKeys.unshift(keyword);
  184. }
  185. // 最多10个纪录
  186. OldKeys.length > 10 && OldKeys.pop();
  187. uni.setStorage({
  188. key: 'OldKeys',
  189. data: JSON.stringify(OldKeys)
  190. });
  191. this.oldKeywordList = OldKeys; // 更新历史搜索
  192. },
  193. fail: () => {
  194. let OldKeys = [keyword];
  195. uni.setStorage({
  196. key: 'OldKeys',
  197. data: JSON.stringify(OldKeys)
  198. });
  199. this.oldKeywordList = OldKeys; // 更新历史搜索
  200. }
  201. });
  202. }
  203. }
  204. };
  205. </script>
  206. <style lang="scss">
  207. .rf-search {
  208. .search-box {
  209. width: 100%;
  210. background-color: rgb(242, 242, 242);
  211. padding: 15upx 2.5%;
  212. display: flex;
  213. justify-content: space-between;
  214. .mSearch-input-box {
  215. width: 100%;
  216. }
  217. .input-box > input {
  218. width: 100%;
  219. height: 60upx;
  220. font-size: 32upx;
  221. border: 0;
  222. border-radius: 60upx;
  223. -webkit-appearance: none;
  224. -moz-appearance: none;
  225. appearance: none;
  226. padding: 0 3%;
  227. margin: 0;
  228. background-color: #ffffff;
  229. }
  230. }
  231. .search-keyword {
  232. width:100%;
  233. background-color: rgb(242, 242, 242);
  234. margin: 0 auto;
  235. .keyword-list-box {
  236. height: calc(100vh - 110upx);
  237. padding-top: 10upx;
  238. border-radius: 20upx 20upx 0 0;
  239. background-color: #fff;
  240. }
  241. .keyword-entry {
  242. width: 94%;
  243. height: 80upx;
  244. margin: 0 3%;
  245. font-size: 30upx;
  246. color: #333;
  247. display: flex;
  248. justify-content: space-between;
  249. align-items: center;
  250. border-bottom: solid 1upx #e7e7e7;
  251. image {
  252. width: 60upx;
  253. height: 60upx;
  254. }
  255. .keyword-text {
  256. width: 90%;
  257. }
  258. .keyword-img {
  259. width: 10%;
  260. justify-content: center;
  261. }
  262. }
  263. .keyword-box {
  264. border-radius: 20upx 20upx 0 0;
  265. background-color: #fff;
  266. .keyword-block {
  267. padding: 10upx;
  268. margin:10upx;
  269. .keyword-list-header {
  270. width: 100vw;
  271. padding: 10upx 3%;
  272. font-size: 27upx;
  273. color: #333;
  274. display: flex;
  275. justify-content: space-between;
  276. image {
  277. width: 40upx;
  278. height: 40upx;
  279. }
  280. }
  281. .keyword {
  282. width: 100vw;
  283. padding: 3px 3%;
  284. display: flex;
  285. flex-flow: wrap;
  286. justify-content: flex-start;
  287. }
  288. .hide-hot-tis {
  289. display: flex;
  290. justify-content: center;
  291. font-size: 28upx;
  292. color: #6b6b6b;
  293. }
  294. .keyword > view {
  295. display: flex;
  296. justify-content: center;
  297. align-items: center;
  298. border-radius: 60upx;
  299. padding: 0 20upx;
  300. margin: 10upx 20upx 10upx 0;
  301. height: 60upx;
  302. font-size: 28upx;
  303. background-color: rgb(242, 242, 242);
  304. color: #6b6b6b;
  305. }
  306. }
  307. }
  308. }
  309. .iconfont {
  310. font-size: $font-lg + 12upx;
  311. }
  312. }
  313. </style>