index.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <template>
  2. <view :class="['qn-page-' + theme]" class="search_out">
  3. <view class="search-box">
  4. <!-- 搜索框 -->
  5. <u-search
  6. bg-color="#f4f5f6"
  7. placeholder-color="#9fa8bd"
  8. search-icon-color="#9fa8bd"
  9. @search="doSearch(keyword)"
  10. @custom="doSearch(keyword)"
  11. placeholder="输入搜索商品发现更多好物…"
  12. v-model="keyword"
  13. :action-style="{
  14. color: '#FA6400',
  15. fontWeight: '400'
  16. }"
  17. ></u-search>
  18. <!-- 搜索框 end -->
  19. </view>
  20. <!-- 历史搜索 -->
  21. <view class="keyword-box" v-if="oldKeywordList.length">
  22. <view class="keyword-header clearfix">
  23. <view class="float_left tit-text">历史搜索</view>
  24. <view class="float_right clear-view" @tap="oldDelete">
  25. <text class="ibonfont ibonshanchu"></text>
  26. 清除历史
  27. </view>
  28. </view>
  29. <view class="keyword-ul">
  30. <view class="keyword-li" @tap="doSearch(keyword)" v-for="(keyword, index) in oldKeywordList" :key="index">{{ keyword }}</view>
  31. </view>
  32. </view>
  33. <!-- 热门搜索 -->
  34. <view class="keyword-box" v-if="hotKeywordList.length">
  35. <view class="keyword-header clearfix">
  36. <view class="float_left tit-text">
  37. <image class="ic-hot" src="https://onlineimg.qianniao.vip/ic-hot.png" mode="aspectFill"></image>
  38. 热门搜索
  39. </view>
  40. </view>
  41. <view class="keyword-ul hot-ul">
  42. <view class="keyword-li" v-for="(keyword, index) in hotKeywordList" @tap="doSearch(keyword.name)" :key="index">
  43. <image class="ic-hot-tag" src="https://onlineimg.qianniao.vip/ic-hot-tag.png" mode="aspectFill"></image>
  44. {{ keyword.name }}
  45. </view>
  46. </view>
  47. </view>
  48. <!-- 热销榜 -->
  49. <view class="hot-search-ranking" v-if="hot_goods_list.length">
  50. <view class="title"><image class="hot-search" src="https://onlineimg.qianniao.vip/hot-search.png" mode="aspectFill"></image></view>
  51. <view class="hot-search-ul">
  52. <view class="hot-search-li clearfix" v-for="(item, index) in hot_goods_list" :key="index" @click="goPage(`/pagesT/product/product?id=${item.id}`)">
  53. <view class="float_left goods-img">
  54. <view class="sort-tag">{{ index + 1 }}</view>
  55. <image :src="item.images[0]" mode="aspectFill"></image>
  56. </view>
  57. <view class="float_left goods-info">
  58. <view class="ellipsis goods-name">{{ item.title }}</view>
  59. <view class="hot-num">
  60. 人气值{{ item.realSalesNum + 999 }}
  61. <text class="ibonfont ibonshangsheng"></text>
  62. </view>
  63. </view>
  64. </view>
  65. </view>
  66. </view>
  67. </view>
  68. </template>
  69. <script>
  70. export default {
  71. data() {
  72. return {
  73. defaultKeyword: '',
  74. keyword: '',
  75. oldKeywordList: [],
  76. hotKeywordList: [],
  77. keywordList: [],
  78. hot_goods_list: [], // 热销榜
  79. forbid: '',
  80. isShowKeywordList: false
  81. };
  82. },
  83. onLoad() {
  84. this.init();
  85. },
  86. components: {},
  87. methods: {
  88. init() {
  89. this.loadDefaultKeyword();
  90. this.loadOldKeyword();
  91. this.loadHotKeyword();
  92. this.getGoodsByCategory();
  93. },
  94. blur() {
  95. uni.hideKeyboard();
  96. },
  97. //加载默认搜索关键字
  98. loadDefaultKeyword() {
  99. //定义默认搜索关键字,可以自己实现ajax请求数据再赋值,用户未输入时,以水印方式显示在输入框,直接不输入内容搜索会搜索默认关键字
  100. // this.defaultKeyword =this.hotKeywordList;
  101. },
  102. //加载历史搜索,自动读取本地Storage
  103. loadOldKeyword() {
  104. uni.getStorage({
  105. key: 'OldKeys',
  106. success: res => {
  107. var OldKeys = JSON.parse(res.data);
  108. this.oldKeywordList = OldKeys;
  109. }
  110. });
  111. },
  112. //加载热门搜索
  113. loadHotKeyword() {
  114. //定义热门搜索关键字,可以自己实现ajax请求数据再赋值
  115. this.$u.api
  116. .getBasicField({
  117. field: 'recommend'
  118. })
  119. .then(res => {
  120. this.hotKeywordList = res.data.recommend;
  121. });
  122. },
  123. //顶置关键字
  124. setkeyword(data) {
  125. this.keyword = data.keyword;
  126. },
  127. //清除历史搜索
  128. oldDelete() {
  129. uni.showModal({
  130. content: '确定清除历史搜索记录?',
  131. success: res => {
  132. if (res.confirm) {
  133. console.log('用户点击确定');
  134. this.oldKeywordList = [];
  135. uni.removeStorage({
  136. key: 'OldKeys'
  137. });
  138. } else if (res.cancel) {
  139. console.log('用户点击取消');
  140. }
  141. }
  142. });
  143. },
  144. //热门搜索开关
  145. hotToggle() {
  146. this.forbid = this.forbid ? '' : '_forbid';
  147. },
  148. //执行搜索
  149. doSearch(key) {
  150. if (!key) {
  151. this.$api.msg('请输入您要搜索的商品');
  152. return;
  153. }
  154. key = key ? key : this.keyword ? this.keyword : this.defaultKeyword;
  155. this.keyword = key;
  156. //保存为历史
  157. this.saveKeyword(key);
  158. //跳转搜索
  159. this.goPage('/pagesT/search/SearchResult?key=' + this.keyword);
  160. },
  161. //保存关键字到历史记录
  162. saveKeyword(keyword) {
  163. uni.getStorage({
  164. key: 'OldKeys',
  165. success: res => {
  166. var OldKeys = JSON.parse(res.data);
  167. console.log(OldKeys);
  168. var findIndex = OldKeys.indexOf(keyword);
  169. if (findIndex == -1) {
  170. OldKeys.unshift(keyword);
  171. } else {
  172. OldKeys.splice(findIndex, 1);
  173. OldKeys.unshift(keyword);
  174. }
  175. //最多10个纪录
  176. OldKeys.length > 10 && OldKeys.pop();
  177. uni.setStorage({
  178. key: 'OldKeys',
  179. data: JSON.stringify(OldKeys)
  180. });
  181. this.oldKeywordList = OldKeys;
  182. console.log(this.oldKeywordList);
  183. //更新历史搜索
  184. },
  185. fail: e => {
  186. var OldKeys = [keyword];
  187. uni.setStorage({
  188. key: 'OldKeys',
  189. data: JSON.stringify(OldKeys)
  190. });
  191. this.oldKeywordList = OldKeys;
  192. //更新历史搜索
  193. }
  194. });
  195. },
  196. // 热销榜
  197. getGoodsByCategory() {
  198. this.$u.api
  199. .getGoodsByCategory({
  200. page: 1,
  201. pageSize: 10,
  202. sort: 1
  203. })
  204. .then(res => {
  205. this.hot_goods_list = res.data;
  206. });
  207. }
  208. }
  209. };
  210. </script>
  211. <style>
  212. .search_out .u-input {
  213. font-size: 24rpx !important;
  214. }
  215. page {
  216. background: #fff;
  217. }
  218. </style>
  219. <style scoped lang="scss">
  220. .search-box {
  221. padding: 24rpx 30rpx 32rpx;
  222. }
  223. .keyword-box {
  224. padding: 0 30rpx 24rpx 14rpx;
  225. .keyword-header {
  226. padding-bottom: 24rpx;
  227. padding-left: 16rpx;
  228. line-height: 40rpx;
  229. .tit-text {
  230. color: #000000;
  231. font-size: 28rpx;
  232. font-weight: 600;
  233. .ic-hot {
  234. width: 32rpx;
  235. height: 32rpx;
  236. margin-right: 8rpx;
  237. vertical-align: middle;
  238. transform: translateY(-4rpx);
  239. }
  240. }
  241. .clear-view {
  242. color: #9c9c9c;
  243. font-size: 24rpx;
  244. .ibonshanchu {
  245. font-size: 24rpx;
  246. margin-right: 10rpx;
  247. }
  248. }
  249. }
  250. .keyword-ul {
  251. display: flex;
  252. flex-wrap: wrap;
  253. .keyword-li {
  254. line-height: 40rpx;
  255. padding: 0 34rpx;
  256. border-radius: 40rpx;
  257. margin-bottom: 16rpx;
  258. margin-left: 16rpx;
  259. background-color: #f5f6f7;
  260. color: #6b6b6b;
  261. font-size: 22rpx;
  262. }
  263. }
  264. .hot-ul {
  265. .keyword-li {
  266. border-radius: 0px 21rpx 21rpx 21rpx;
  267. position: relative;
  268. .ic-hot-tag {
  269. position: absolute;
  270. left: 0;
  271. top: 0;
  272. width: 24rpx;
  273. height: 24rpx;
  274. display: block;
  275. }
  276. }
  277. }
  278. }
  279. .hot-search-ranking {
  280. width: 690rpx;
  281. background: linear-gradient(180deg, rgba(255, 218, 194, 0.22) 0%, rgba(255, 248, 244, 0.45) 29%, #ffffff 100%);
  282. border-radius: 24rpx;
  283. margin: 0 auto;
  284. .title {
  285. padding: 24rpx;
  286. .hot-search {
  287. width: 124rpx;
  288. height: 34rpx;
  289. }
  290. }
  291. .hot-search-ul {
  292. display: flex;
  293. flex-wrap: wrap;
  294. padding: 0 24rpx;
  295. .hot-search-li {
  296. width: 320rpx;
  297. margin-bottom: 16rpx;
  298. .goods-img {
  299. margin-right: 8rpx;
  300. position: relative;
  301. .sort-tag {
  302. position: absolute;
  303. width: 24rpx;
  304. height: 26rpx;
  305. background: #ffb482;
  306. border-radius: 8rpx 0px 8rpx 0px;
  307. top: 0;
  308. font-family: DIN-Medium;
  309. left: 0;
  310. font-size: 16rpx;
  311. color: #ffffff;
  312. line-height: 26rpx;
  313. text-align: center;
  314. }
  315. image {
  316. display: block;
  317. width: 88rpx;
  318. height: 88rpx;
  319. background: #ffffff;
  320. border-radius: 8rpx;
  321. }
  322. }
  323. .goods-info {
  324. width: calc(100% - 96rpx);
  325. .goods-name {
  326. line-height: 50rpx;
  327. -webkit-line-clamp: 1;
  328. font-size: 24rpx;
  329. color: #111111;
  330. }
  331. .hot-num {
  332. font-size: 20rpx;
  333. font-family: DINPro-Regular;
  334. color: #9c9c9c;
  335. line-height: 28rpx;
  336. margin-right: 10rpx;
  337. .ibonshangsheng {
  338. font-size: 18rpx;
  339. color: #e02020;
  340. }
  341. .ibonxiajiang {
  342. color: #64d992;
  343. }
  344. }
  345. }
  346. &:first-child {
  347. .goods-img {
  348. .sort-tag {
  349. background: #e02020;
  350. }
  351. }
  352. }
  353. &:nth-child(2) {
  354. .goods-img {
  355. .sort-tag {
  356. background: #fa6400;
  357. }
  358. }
  359. }
  360. &:nth-child(3) {
  361. .goods-img {
  362. .sort-tag {
  363. background: #f7b500;
  364. }
  365. }
  366. }
  367. }
  368. }
  369. }
  370. </style>