searchPage.vue 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. <template>
  2. <view class="content">
  3. <u-navbar :background="navObj" :is-back="false">
  4. <!-- mSearch组件 如果使用原样式,删除组件元素-->
  5. <mSearch ref="mSearch" class="mSearch-input-box" @clickLeft="back" :mode="2" :placeholder="defaultKeyword" @search="doSearch(false)" @input="inputChange" @confirm="doSearch(false)"
  6. @SwitchType="doSearchSwitch()" v-model="keyword" :isFocusVal="!isShowSeachGoods"></mSearch>
  7. </u-navbar>
  8. <view class="search-keyword" v-if="!isShowSeachGoods">
  9. <scroll-view class="keyword-list-box" v-show="isShowKeywordList" scroll-y>
  10. <block v-for="(row, index) in keywordList" :key="index">
  11. <view class="keyword-entry" hover-class="keyword-entry-tap">
  12. <view class="keyword-text" @tap.stop="doSearch(keywordList[index].words)">
  13. <rich-text :nodes="row.words"></rich-text>
  14. </view>
  15. </view>
  16. </block>
  17. </scroll-view>
  18. <div class="keyword-box" v-show="!isShowKeywordList">
  19. <view class="keyword-block add1">
  20. <view class="keyword-list-header">
  21. <view class="u-tips">热门搜索</view>
  22. </view>
  23. <view class="keyword keywordBox">
  24. <view class="wes" v-for="(keyword, index) in hotKeywordList" @tap="doSearch(keyword)" :key="index">{{ keyword }}</view>
  25. </view>
  26. </view>
  27. <view class="keyword-block" v-if="oldKeywordList.length > 0">
  28. <view class="keyword-list-header">
  29. <view class="u-tips">搜索历史</view>
  30. </view>
  31. <div class="oldKeyList">
  32. <div class="oldKeyItem" v-if="keyword" v-for="(keyword, index) in oldKeywordList" :key="index" @click="doSearch(keyword)">
  33. <span>{{ keyword }} </span>
  34. </div>
  35. <div @click="showMore" v-if=" oldKeywordIndex > loadIndex" class="oldKeyItem">展示更多</div>
  36. </div>
  37. </view>
  38. <div class="clear mp-iphonex-bottom" @tap="oldDelete">清空搜索历史</div>
  39. </div>
  40. </view>
  41. <!-- 搜索 -->
  42. <view class="goods-content" v-if="isShowSeachGoods">
  43. <view class="navbar">
  44. <view class="nav-item" :class="{ current: filterIndex === 0 }" @click="tabClick(0)">综合排序</view>
  45. <view class="nav-item" :class="{ current: filterIndex === 3 }" @click="tabClick(3, 'buyCount')">
  46. <text>销量</text>
  47. <view class="p-box">
  48. <view class="index-nav-arrow">
  49. <image class="img" src="/static/index/arrow-up-1.png" v-if="params.sort === 'buyCount' && params.order === 'asc'" mode="aspectFit"></image>
  50. <image class="img" src="/static/index/arrow-up.png" v-else mode="aspectFit"></image>
  51. </view>
  52. <view class="index-nav-arrow">
  53. <image class="img" src="/static/index/arrow-down.png" v-if="params.sort === 'buyCount' && params.order === 'desc'" mode="aspectFit"></image>
  54. <image class="img" src="/static/index/arrow-down-1.png" v-else mode="aspectFit"></image>
  55. </view>
  56. </view>
  57. </view>
  58. <view class="nav-item" :class="{ current: filterIndex === 4 }" @click="tabClick(4, 'price')">
  59. <text>价格</text>
  60. <view class="p-box">
  61. <view class="index-nav-arrow">
  62. <image class="img" src="/static/index/arrow-up-1.png" v-if="params.sort === 'price' && params.order === 'asc'" mode="aspectFit"></image>
  63. <image class="img" src="/static/index/arrow-up.png" v-else mode="aspectFit"></image>
  64. </view>
  65. <view class="index-nav-arrow">
  66. <image class="img" src="/static/index/arrow-down.png" v-if="params.sort === 'price' && params.order === 'desc'" mode="aspectFit"></image>
  67. <image class="img" src="/static/index/arrow-down-1.png" v-else mode="aspectFit"></image>
  68. </view>
  69. </view>
  70. </view>
  71. <view class="nav-item" @click="sortGoods">筛选</view>
  72. </view>
  73. <!-- 一行一个商品展示 -->
  74. <div v-if="isSWitch">
  75. <scroll-view :style="{ height: goodsHeight }" enableBackToTop="true" lower-threshold="250" @scrolltolower="loadmore()" scroll-with-animation scroll-y class="scoll-page">
  76. <div class="goodsClass">
  77. <u-row v-for="(item, index) in goodsList" :key="index" class="goodsRow">
  78. <u-col :span="4" @click.native="navigateToDetailPage(item)" class="switchType1">
  79. <u-image width="182rpx" height="200rpx" class="imgGoods" :src="item.thumbnail">
  80. <u-loading slot="loading"></u-loading>
  81. </u-image>
  82. </u-col>
  83. <u-col :span="8" @click.native="navigateToDetailPage(item)" class="switchType2">
  84. <div class="title clamp3" style="">{{ item.goodsName }}</div>
  85. <view class="price-box">
  86. <div class="price" v-if="item.price!=undefined">
  87. ¥<span>{{ formatPrice(item.price )[0] }} </span>.{{
  88. formatPrice(item.price )[1]
  89. }}
  90. </div>
  91. </view>
  92. <div class="promotion">
  93. <div v-for="(promotionItem,promotionIndex) in getPromotion(item)" :key="promotionIndex">
  94. <span v-if="promotionItem.indexOf('COUPON') != -1">劵</span>
  95. <span v-if="promotionItem.indexOf('FULL_DISCOUNT') != -1">满减</span>
  96. <span v-if="promotionItem.indexOf('SECKILL') != -1">秒杀</span>
  97. </div>
  98. </div>
  99. <div style="overflow: hidden" class="countConfig">
  100. <span style="float: left; font-size: 22rpx">已售 {{ item.buyCount || '0' }}</span>
  101. <span style="float: right; font-size: 22rpx">{{ item.commentNum || '0' }}条评论</span>
  102. </div>
  103. </u-col>
  104. <u-col :span="12" class="storeSellerBox">
  105. <div class="storeSellerName" @click="clickTostore()">
  106. <div class="textHidden">
  107. <u-tag style="margin-right: 10rpx" size="mini" mode="dark" v-if="item.selfOperated" text="自营" type="error" />
  108. <span style="
  109. color: #333333;
  110. font-size: 28rpx;
  111. padding-left: 17rpx;
  112. ">{{ item.storeName }}</span>
  113. </div>
  114. <span>
  115. <u-icon name="arrow-right" color="#c5c5c5"></u-icon>
  116. </span>
  117. </div>
  118. </u-col>
  119. </u-row>
  120. </div>
  121. <uni-load-more :status="loadingType" @loadmore="loadmore()"></uni-load-more>
  122. </scroll-view>
  123. </div>
  124. <div class="empty" v-if="goodsList == [] || goodsList == '' || goodsList == null">
  125. <view>
  126. <image style="width: 320rpx; height: 240rpx" src="/static/nodata.png">
  127. </image>
  128. </view>
  129. <view>
  130. <p>没有找到相关的商品信息</p>
  131. <p>请换一个关键词试试吧</p>
  132. </view>
  133. </div>
  134. <!-- 一行两个商品展示 -->
  135. <div v-if="
  136. !isSWitch &&
  137. !(goodsList == [] || goodsList == '' || goodsList == null)
  138. ">
  139. <scroll-view :style="{ height: goodsHeight }" scroll-anchoring enableBackToTop="true" @scrolltolower="loadmore()" scroll-with-animation scroll-y lower-threshold="250" class="scoll-page">
  140. <view class="goods-list">
  141. <view v-for="(item, index) in goodsList" :key="index" class="goods-item" @click="navigateToDetailPage(item)">
  142. <view class="image-wrapper">
  143. <image :src="item.thumbnail" mode="aspectFill"></image>
  144. </view>
  145. <view class="goods-detail">
  146. <div class="title clamp">{{ item.goodsName }}</div>
  147. <view class="price-box">
  148. <div class="price" v-if="item.price!=undefined">
  149. ¥<span>{{ formatPrice(item.price )[0] }} </span>.{{
  150. formatPrice(item.price )[1]
  151. }}
  152. </div>
  153. </view>
  154. <div class="promotion">
  155. <div v-for="(promotionItem,promotionIndex) in getPromotion(item)" :key="promotionIndex">
  156. <span v-if="promotionItem.indexOf('COUPON') != -1">劵</span>
  157. <span v-if="promotionItem.indexOf('FULL_DISCOUNT') != -1">满减</span>
  158. <span v-if="promotionItem.indexOf('SECKILL') != -1">秒杀</span>
  159. </div>
  160. </div>
  161. <div class="countConfig">
  162. <span>已售 {{ item.buyCount || "0" }}</span>
  163. <span>{{ item.commentNum || "0" }}条评论</span>
  164. </div>
  165. <div class="storeSellerName">
  166. <div class="textHidden">
  167. <u-tag style="margin-right: 10rpx" size="mini" mode="dark" v-if="item.selfOperated == 1" text="自营" type="error" />
  168. <span>{{ item.storeName || "暂无" }}</span>
  169. </div>
  170. <span>
  171. <u-icon name="arrow-right"></u-icon>
  172. </span>
  173. </div>
  174. </view>
  175. </view>
  176. </view>
  177. <uni-load-more :status="loadingType"></uni-load-more>
  178. </scroll-view>
  179. </div>
  180. </view>
  181. <u-popup border-radius="20" mode="right" width="90%" v-model="sortPopup">
  182. <view class="status_bar"></view>
  183. <view class="sort-box ">
  184. <view class="sort-list">
  185. <view class="sort-item">
  186. <view class="sort-title"> 品牌 </view>
  187. <view class="flex" v-if="sortData.brands">
  188. <view class="sort-brand-item" :key="brandsIndex" v-for="(brand, brandsIndex) in sortData.brands" @click="handleSort(brand, brandsIndex, 'brand')">
  189. <view class="sort-radius" :class="{
  190. 'sort-active': brand.__selected,
  191. }">
  192. {{ brand.name }}
  193. </view>
  194. </view>
  195. </view>
  196. <!-- <u-empty v-else text="暂无品牌" mode="list"></u-empty> -->
  197. </view>
  198. <view class="sort-item">
  199. <view class="sort-title"> 全部分类 </view>
  200. <view class="flex" style="flex-wrap: wrap;" v-if="sortData.categories">
  201. <view class="sort-brand-item" :key="categoriesIndex" v-for="(categoryId, categoriesIndex) in sortData.categories" @click="handleSort(categoryId, categoriesIndex, 'categoryId')">
  202. <view class="sort-radius" :class="{
  203. 'sort-active': categoryId.__selected,
  204. }">
  205. {{ categoryId.name }}
  206. </view>
  207. </view>
  208. </view>
  209. <!-- <u-empty v-else text="暂无分类" mode="list"></u-empty> -->
  210. </view>
  211. </view>
  212. <view class="sort-list">
  213. <view class="sort-item">
  214. <view class="sort-title"> 价格区间 </view>
  215. <view style="display:flex; margin-top:20rpx; align-items: center;">
  216. <view class="sort-brand-item uinput">
  217. <view class="sort-radius">
  218. <u-input v-model="minPrice" type="number" placeholder="最低价" input-align="center" />
  219. </view>
  220. </view>
  221. <view>-</view>
  222. <view class="sort-brand-item uinput">
  223. <view class="sort-radius">
  224. <u-input v-model="maxPrice" type="number" placeholder="最高价" input-align="center" />
  225. </view>
  226. </view>
  227. </view>
  228. </view>
  229. </view>
  230. <view class="sort-list" v-if="sortData.paramOptions">
  231. <view class="sort-item" :key="paramIndex" v-for="(param, paramIndex) in sortData.paramOptions">
  232. <view class="sort-title"> {{ param.key }} </view>
  233. <view class="flex" style="flex-warp:warp" v-if="param.values">
  234. <view class="sort-brand-item" :key="i" v-for="(value, i) in param.values" @click="handleSort(value, i, 'prop', param)">
  235. <view class="sort-radius" :class="{
  236. 'sort-active': value.__selected,
  237. }">
  238. {{ value.title }}
  239. </view>
  240. </view>
  241. </view>
  242. </view>
  243. </view>
  244. <div class="null-view"></div>
  245. <view class="sort-btn mp-iphonex-bottom">
  246. <view class="sort-btn-repick" @click="repick">重置</view>
  247. <view class="sort-btn-confim" @click="sortConfim">确定</view>
  248. </view>
  249. </view>
  250. </u-popup>
  251. </view>
  252. </template>
  253. <script>
  254. import { getGoodsList, getGoodsRelated } from "@/api/goods.js";
  255. import { getHotKeywords } from "@/api/home.js";
  256. import mSearch from "@/components/m-search-revision/m-search-revision.vue";
  257. export default {
  258. data() {
  259. return {
  260. loadIndex: 10,
  261. oldKeywordIndex: "",
  262. selectedWay: {
  263. brand: [],
  264. categoryId: [],
  265. prop: [],
  266. },
  267. sortPopup: false, //筛选的开关
  268. navObj: {
  269. background: "#fff",
  270. },
  271. typeSortData: {
  272. type: "",
  273. index: "",
  274. },
  275. goodsHeight: "",
  276. defaultKeyword: "",
  277. keyword: "",
  278. oldKeywordList: [],
  279. hotKeywordList: [],
  280. keywordList: [],
  281. goodsList: [],
  282. cateMaskState: 0, //分类面板展开状态
  283. loadingType: "more", //加载更多状态
  284. filterIndex: 0,
  285. cateId: 0, //已选三级分类id
  286. priceOrder: 0, //1 价格从低到高 2价格从高到低
  287. cateList: [],
  288. isShowSeachGoods: false,
  289. isShowKeywordList: false,
  290. sortData: "",
  291. isSWitch: false,
  292. params: {
  293. pageNumber: 0,
  294. pageSize: 10,
  295. // sort: 'grade_asc',
  296. sort: "releaseTime",
  297. order: "desc",
  298. keyword: "",
  299. },
  300. minPrice: "",
  301. maxPrice: "",
  302. sortParams: {
  303. pageNumber: 0,
  304. pageSize: 10,
  305. // price: "", //价格,示例值(10_30)
  306. // prop: "", //属性:参数名_参数值@参数名_参数值,示例值(屏幕类型_LED@屏幕尺寸_15英寸)
  307. // brandId:"", //品牌,可以多选 品牌Id@品牌Id@品牌Id
  308. categoryId: "",
  309. },
  310. routerVal: "",
  311. };
  312. },
  313. onLoad(val) {
  314. // return false
  315. this.init();
  316. this.initSortGoods();
  317. // 接收分类的数据
  318. this.routerVal = val;
  319. // 有值
  320. if (this.routerVal.category) {
  321. this.params.categoryId = this.routerVal.category;
  322. this.isShowSeachGoods = true;
  323. }
  324. if (this.routerVal.keyword) {
  325. this.params.keyword = this.routerVal.keyword;
  326. this.isShowSeachGoods = true;
  327. }
  328. if (this.routerVal.storeId) {
  329. this.params.storeId = this.routerVal.storeId;
  330. this.isShowSeachGoods = true;
  331. }
  332. this.loadData();
  333. },
  334. components: {
  335. //引用mSearch组件,如不需要删除即可
  336. mSearch,
  337. },
  338. onReachBottom() {
  339. this.params.pageNumber++;
  340. this.loadData();
  341. },
  342. mounted() {
  343. const { windowWidth, windowHeight } = uni.getSystemInfoSync();
  344. let topHeight = 0;
  345. let navHeight = 0;
  346. uni.getSystemInfo({
  347. success: function (res) {
  348. // res - 各种参数
  349. let top = uni.createSelectorQuery().select(".u-navbar");
  350. top
  351. .boundingClientRect(function (data) {
  352. if (data && data.height) {
  353. //data - 各种参数
  354. topHeight = data.height; // 获取元素宽度
  355. }
  356. })
  357. .exec();
  358. let nav = uni.createSelectorQuery().select(".navbar");
  359. nav
  360. .boundingClientRect(function (data) {
  361. if (data && data.height) {
  362. //data - 各种参数
  363. navHeight = data.height; // 获取元素宽度
  364. }
  365. })
  366. .exec();
  367. },
  368. });
  369. this.goodsHeight = windowHeight - navHeight - topHeight;
  370. // #ifdef APP-PLUS
  371. this.goodsHeight = this.goodsHeight - 100;
  372. // #endif
  373. this.goodsHeight += "px";
  374. },
  375. methods: {
  376. // 数据去重一下 只显示一次 减免 劵 什么的
  377. getPromotion(item) {
  378. if (item.promotionMap) {
  379. let array = [];
  380. Object.keys(item.promotionMap).forEach((child) => {
  381. if (!array.includes(child.split("-")[0])) {
  382. array.push(child.split("-")[0]);
  383. }
  384. });
  385. return array;
  386. }
  387. },
  388. // 格式化金钱 1999 --> [1999,00]
  389. formatPrice(val) {
  390. if (typeof val == "undefined") {
  391. return val;
  392. }
  393. return val.toFixed(2).split(".");
  394. },
  395. // 展示更多数据
  396. showMore() {
  397. this.loadOldKeyword(this.oldKeywordIndex);
  398. },
  399. // 点击确定进行筛选
  400. sortConfim() {
  401. // 处理品牌(多选
  402. this.params.brandId = [];
  403. this.selectedWay["brand"].forEach((item) => {
  404. if (item.__selected) {
  405. this.params.brandId.push(item.value);
  406. }
  407. });
  408. this.params.brandId = this.params.brandId.join("@");
  409. // 处理分类 (单选)
  410. if (this.selectedWay["categoryId"][0]) {
  411. this.params.categoryId = this.selectedWay["categoryId"][0].value;
  412. }
  413. // 处理属性
  414. this.params.prop = [];
  415. this.selectedWay["prop"].forEach((item) => {
  416. if (item.__selected) {
  417. this.params.prop.push(`${item.parent}_${item.title}`);
  418. }
  419. });
  420. this.params.prop = this.params.prop.join("@");
  421. // 处理价格
  422. if (this.minPrice || this.maxPrice) {
  423. this.params.price = `${this.minPrice}_${this.maxPrice}`;
  424. } else {
  425. this.params.price = 0;
  426. }
  427. this.goodsList = [];
  428. this.loadData();
  429. this.sortPopup = false;
  430. },
  431. // 重置
  432. repick() {
  433. this.initSortGoods();
  434. this.minPrice = "";
  435. this.maxPrice = "";
  436. this.params = {
  437. pageNumber: 0,
  438. pageSize: 10,
  439. };
  440. this.loadData();
  441. },
  442. // 点击筛选的内容
  443. handleSort(val, index, type, parent) {
  444. if (type == "prop") {
  445. val.parent = parent.key;
  446. }
  447. this.selectedWay[type].push(val);
  448. if (type == "categoryId") {
  449. this.sortData.categories.forEach((item) => {
  450. item.__selected = false;
  451. });
  452. val.__selected = true;
  453. } else {
  454. val.__selected ? (val.__selected = false) : (val.__selected = true);
  455. }
  456. },
  457. init() {
  458. this.loadDefaultKeyword();
  459. this.loadOldKeyword(this.loadIndex);
  460. this.loadHotKeyword();
  461. },
  462. blur() {
  463. uni.hideKeyboard();
  464. },
  465. back() {
  466. uni.navigateBack({
  467. delta: 1,
  468. });
  469. },
  470. navigateToDetailPage(item) {
  471. uni.navigateTo({
  472. url: `/pages/product/goods?id=${item.id}&goodsId=${item.goodsId}`,
  473. });
  474. },
  475. loadmore() {
  476. this.params.pageNumber++;
  477. this.loadData();
  478. },
  479. initSortGoods() {
  480. getGoodsRelated(this.sortParams).then((res) => {
  481. if (res.data.success) {
  482. for (let item of Object.keys(res.data.result)) {
  483. res.data.result[item].forEach((child) => {
  484. child.__selected = false;
  485. // 循环出和品牌分类一样的数据格式
  486. if (child.values) {
  487. child.values = child.values.map((item) => ({
  488. title: item,
  489. __selected: false,
  490. }));
  491. }
  492. });
  493. }
  494. this.sortData = res.data.result;
  495. }
  496. });
  497. },
  498. // 筛选商品
  499. sortGoods() {
  500. this.sortPopup = true;
  501. },
  502. tabClick(index, type) {
  503. this.params.pageNumber = 0;
  504. this.params.pageSize = 10;
  505. // this.params.order = "desc";
  506. if (this.params.sort == type) {
  507. this.params.order == "asc"
  508. ? (this.params.order = "desc")
  509. : (this.params.order = "asc");
  510. this.$set(this.params, "sort", type);
  511. } else {
  512. this.params.order = "desc";
  513. this.$set(this.params, "sort", type);
  514. }
  515. if (index == 0) {
  516. this.params.sort = "releaseTime";
  517. this.params.order = "desc";
  518. }
  519. this.filterIndex = index;
  520. uni.pageScrollTo({
  521. duration: 300,
  522. scrollTop: 0,
  523. });
  524. this.loadData("refresh", 1);
  525. uni.showLoading({
  526. title: "正在加载",
  527. });
  528. },
  529. //加载默认搜索关键字
  530. loadDefaultKeyword() {
  531. //定义默认搜索关键字,可以自己实现ajax请求数据再赋值,用户未输入时,以水印方式显示在输入框,直接不输入内容搜索会搜索默认关键字
  532. this.defaultKeyword = "请输入搜索商品";
  533. },
  534. //加载历史搜索,自动读取本地Storage
  535. loadOldKeyword(index) {
  536. this.oldKeywordList = [];
  537. uni.getStorage({
  538. key: "OldKeys",
  539. success: (res) => {
  540. var OldKeys = JSON.parse(res.data);
  541. this.oldKeywordIndex = res.data.length;
  542. for (let i = 0; i < index; i++) {
  543. this.oldKeywordList.push(OldKeys[i]);
  544. }
  545. },
  546. });
  547. },
  548. //加载热门搜索
  549. async loadHotKeyword() {
  550. this.hotKeywordList = [];
  551. let res = await getHotKeywords();
  552. let keywords = res.data.result;
  553. keywords.forEach((item) => {
  554. this.hotKeywordList.push(item);
  555. });
  556. },
  557. //加载商品 ,带下拉刷新和上滑加载
  558. async loadData(type, loading) {
  559. this.loadingType = "loading";
  560. if (type == "refresh") {
  561. this.goodsList = [];
  562. }
  563. //没有更多直接返回
  564. let goodsList = await getGoodsList(this.params);
  565. if (goodsList.data.result.content.length < 10) {
  566. this.loadingType = "noMore";
  567. }
  568. this.goodsList.push(...goodsList.data.result.content);
  569. this.initSortGoods();
  570. uni.hideLoading();
  571. },
  572. //监听输入
  573. inputChange(event) {
  574. //兼容引入组件时传入参数情况
  575. var keyword = event.detail ? event.detail.value : event;
  576. if (!keyword) {
  577. this.keywordList = [];
  578. this.isShowKeywordList = false;
  579. return;
  580. }
  581. this.isShowKeywordList = true;
  582. this.getKeywordNumFun(keyword);
  583. },
  584. //高亮关键字
  585. drawCorrelativeKeyword(keywords, keyword) {
  586. var len = keywords.length,
  587. keywordArr = [];
  588. for (var i = 0; i < len; i++) {
  589. var row = keywords[i];
  590. //定义高亮#9f9f9f
  591. var html = row[0].replace(
  592. keyword,
  593. "<span style='color: #9f9f9f;'>" + keyword + "</span>"
  594. );
  595. html = "<div>" + html + "</div>";
  596. var tmpObj = {
  597. keyword: row[0],
  598. htmlStr: html,
  599. };
  600. keywordArr.push(tmpObj);
  601. }
  602. return keywordArr;
  603. },
  604. //顶置关键字
  605. setKeyword(index) {
  606. this.keyword = this.keywordList[index].keyword;
  607. },
  608. //清除历史搜索
  609. oldDelete() {
  610. uni.showModal({
  611. content: "确定清除历史搜索记录?",
  612. success: (res) => {
  613. if (res.confirm) {
  614. this.oldKeywordList = [];
  615. uni.removeStorage({
  616. key: "OldKeys",
  617. });
  618. }
  619. },
  620. });
  621. },
  622. // 样式修改布局
  623. doSearchSwitch(val) {
  624. this.isSWitch = !this.isSWitch;
  625. this.isShowSeachGoods = true;
  626. },
  627. //执行搜索
  628. doSearch(keyword) {
  629. keyword = keyword === false ? this.keyword : keyword;
  630. this.keyword = keyword;
  631. this.saveKeyword(keyword); //保存为历史
  632. this.isShowSeachGoods = true;
  633. this.$refs.mSearch.isShowSeachGoods = true;
  634. this.params.keyword = keyword;
  635. this.params.pageNumber = 0;
  636. this.$set(this.sortParams, "keyword", keyword);
  637. this.loadData("refresh", 1);
  638. },
  639. //保存关键字到历史记录
  640. saveKeyword(keyword) {
  641. if (!keyword) return false;
  642. uni.getStorage({
  643. key: "OldKeys",
  644. success: (res) => {
  645. var OldKeys = JSON.parse(res.data);
  646. var findIndex = OldKeys.indexOf(keyword);
  647. if (findIndex == -1) {
  648. OldKeys.unshift(keyword);
  649. } else {
  650. OldKeys.splice(findIndex, 1);
  651. OldKeys.unshift(keyword);
  652. }
  653. //最多10个纪录
  654. OldKeys.length > 10 && OldKeys.pop();
  655. uni.setStorage({
  656. key: "OldKeys",
  657. data: JSON.stringify(OldKeys),
  658. });
  659. this.oldKeywordList = OldKeys; //更新历史搜索
  660. },
  661. fail: (e) => {
  662. var OldKeys = [keyword];
  663. uni.setStorage({
  664. key: "OldKeys",
  665. data: JSON.stringify(OldKeys),
  666. });
  667. this.oldKeywordList = OldKeys; //更新历史搜索
  668. },
  669. });
  670. },
  671. // 搜索关键字
  672. getKeywordNumFun(keywords) {
  673. this.params.keyword = keywords;
  674. this.$set(this.sortParams, "keyword", keywords);
  675. },
  676. },
  677. };
  678. </script>
  679. <style lang="scss" scoped>
  680. @import "./search.scss";
  681. </style>