search.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <template>
  2. <view class='content'>
  3. <Navbar :title='title'></Navbar>
  4. <!-- 筛选框 -->
  5. <view class="search-styl">
  6. <!-- <SearchBox typeFlag="searchPage" :searchTitle="searchContent" @inputSearch="searchGoods"
  7. @cancelEvent="cancelEvent"></SearchBox> -->
  8. <view class="search-box">
  9. <view class="search-left">
  10. <text class="icon1">&#xe885;</text>
  11. <input type="text" class="input-styl" placeholder-class="placeholder" confirm-type="search"
  12. v-model="searchContent" @confirm="searchGoods" @focus="focus = true" placeholder="搜索商品名称" />
  13. <text class="icon1" v-if="searchContent" @click="deleteContent">&#xe88a;</text>
  14. </view>
  15. <!-- <view class="search-btn" @click="clickCancel" v-if="showHistory">取消</view> -->
  16. <view class="search-btn" @click="clickCancel">取消</view>
  17. <!-- <view v-else class="search-btn">
  18. <IconText :code="`\ue870`" v-if="!sortType || sortType === 1" @click.native="changeType(2)">
  19. </IconText>
  20. <IconText :code="`\ue874`" v-else @click.native="changeType(1)"></IconText>
  21. </view> -->
  22. </view>
  23. </view>
  24. <!-- <view class="sort-box" v-if="!showHistory && !focus">
  25. <view class="sort-item" :class="{active: rank === 0}" @click="changeRank(0)">
  26. 综合
  27. </view>
  28. <view class="sort-item" :class="{active: rank === 1}" @click="changeRank(1)">
  29. 销量
  30. </view>
  31. <view class="sort-item" :class="{active: rank === 2 || rank === 3}" @click="changeRank('price')">
  32. 价格
  33. <view class="sj">
  34. <view class="ssj" :class="{active: rank === 2}"></view>
  35. <view class="xsj" :class="{active: rank === 3}"></view>
  36. </view>
  37. </view>
  38. </view> -->
  39. <view class="search-history" v-if="showHistory || focus">
  40. <view class="search-title">
  41. 搜索历史
  42. <IconText :code="`\ue88f`" size="12" color="#999" @click.native="emptyHistory"></IconText>
  43. </view>
  44. <view class="search-content">
  45. <view class="search-font" v-for="(item,index) in searchHistory" :key="index"
  46. @longpress="pressItem(index)" @click="clickItem(item)">
  47. {{item}}
  48. <view class="delete" @click.stop="deleteHistory(item)" v-if="deleteIndex === index">
  49. <IconText :code="`\ue88a`" color="#999"></IconText>
  50. </view>
  51. </view>
  52. </view>
  53. </view>
  54. <view class="goods-module" :class="{'goods-module2': sortType === 2}" v-else>
  55. <!-- <view class="total">搜到个<text>{{total}}</text>商品</view> -->
  56. <GoodItem v-for="(item, index) in goodsList" :item="item" :sortType="sortType" :key="index"></GoodItem>
  57. <view class="no-data" v-if="!goodsList.length">
  58. <NoData :show="!goodsList.length" topNum="10" title="没有搜到商品!" imgUrl="no-data.png"></NoData>
  59. </view>
  60. </view>
  61. <!-- 空数据组件 -->
  62. <!-- <NoData :show="nodataShow" v-if="!showHistory" imgUrl='search.png'></NoData> -->
  63. <!-- 规格组件 -->
  64. <!-- <SpecPopup ref="pescEle" @addCart="addCart" :product="goodsPopup" typeName="goodsDetail"></SpecPopup> -->
  65. </view>
  66. </template>
  67. <script lang='ts'>
  68. import {
  69. Component,
  70. // Prop,
  71. Vue
  72. } from 'vue-property-decorator';
  73. import {
  74. namespace
  75. } from 'vuex-class';
  76. import {
  77. types
  78. } from '@/common/store';
  79. const baseModule = namespace('base');
  80. @Component({})
  81. export default class Name extends Vue {
  82. @baseModule.Getter('_searchHistory') searchHistory: any;
  83. @baseModule.Mutation(types.SET_SEARCHHISTORY) setSearchHistory: any;
  84. @baseModule.Mutation(types.EMPTY_SEARCHHISTORY) emptySearchHistory: any;
  85. @baseModule.Mutation(types.DELETE_SEARCHHISTORY) deleteSearchHistory: any;
  86. // @baseModule.Mutation(types.ADD_CARTGOODS) addCartGoods: any;
  87. // data------------------------
  88. goodsList: Array < any > = []; //商品列表
  89. searchContent: string = ''; //搜索内容
  90. showHistory: boolean = true; //历史搜索数据展示
  91. nodataShow: boolean = false; //空数据展示
  92. total: number = 0;
  93. deleteIndex: any = null;
  94. title: any = '商品搜索';
  95. partition: any = '';
  96. categoryId: any = null;
  97. finish: boolean = false;
  98. loading: boolean = false;
  99. current: number = 1;
  100. size: number = 20;
  101. name: any = '';
  102. iconShow: boolean = false;
  103. sortType: number = 1;
  104. rank: any = 0;
  105. focus: boolean = false;
  106. goodsPopup = {
  107. skuList: [],
  108. spu_info: {},
  109. } //规格弹窗专用
  110. onLoad() {
  111. // this.searchGoods({
  112. // detail: {
  113. // value: ''
  114. // }
  115. // });
  116. let query = this.$Route.query;
  117. if (query.id) {
  118. this.title = query.name;
  119. if(query.partition) this.title = query.partition;
  120. this.categoryId = +query.id;
  121. this.partition = query.partition;
  122. this.searchGoods({
  123. detail: {
  124. value: ''
  125. }
  126. });
  127. }
  128. }
  129. onReachBottom() {
  130. if (this.finish || this.loading) return;
  131. this.current++;
  132. this.getGoodsList('', 'load');
  133. }
  134. onPullDownRefresh() {
  135. console.log('下拉');
  136. this.finish = false;
  137. this.getGoodsList('', 'pull').then(() => {
  138. uni.stopPullDownRefresh();
  139. }, () => {
  140. uni.stopPullDownRefresh();
  141. });
  142. }
  143. //methods----------------------
  144. changeType(type: any) {
  145. this.sortType = type;
  146. }
  147. clickCancel() {
  148. this.$Router.back(1);
  149. }
  150. deleteContent() {
  151. this.searchContent = '';
  152. // this.showHistory = true;
  153. this.goodsList = [];
  154. this.current = 1;
  155. this.finish = false;
  156. this.deleteIndex = null;
  157. this.getGoodsList('');
  158. }
  159. // 加入购物车
  160. addCart(e: any): void {
  161. console.log('加入购物车的数据', e);
  162. let data = {
  163. spu_id: e.spu_id,
  164. sku_id: e.sku_id,
  165. num: e.num,
  166. }
  167. this.$http.post({
  168. url: this.$api.addCarGoods,
  169. data: data
  170. }).then((res: any) => {
  171. uni.showToast({
  172. title: '加入购物车成功',
  173. icon: 'none'
  174. });
  175. // this.addCartGoods(e);
  176. }).catch((err: any) => {
  177. console.log(err);
  178. })
  179. }
  180. //获取商品列表
  181. getGoodsList(val ? : string, isLoad ? : any) {
  182. console.log(val);
  183. this.loading = true;
  184. if (val) this.name = val;
  185. if (isLoad === 'load' || isLoad == 'pull') val = this.name || '';
  186. // return
  187. let data: object = {
  188. name: val,
  189. status: '3',
  190. secondSortId: this.categoryId || 0,
  191. page: this.current,
  192. size: this.size,
  193. partition: this.partition || '',
  194. rank: this.rank
  195. }
  196. uni.showLoading({
  197. title: '加载中',
  198. });
  199. return this.$http.get({
  200. url: this.$api.getGoodsList,
  201. data: data
  202. }).then((res: any) => {
  203. if (isLoad === 'pull') {
  204. this.goodsList = res.list;
  205. } else {
  206. this.goodsList = [...this.goodsList, ...res.list];
  207. }
  208. this.nodataShow = !this.goodsList.length;
  209. this.total = res.total;
  210. if (res.list.length < this.size) this.finish = true;
  211. this.loading = false;
  212. this.focus = false;
  213. uni.hideLoading();
  214. }).catch((err: any) => {
  215. console.log(err);
  216. this.loading = false;
  217. uni.hideLoading();
  218. })
  219. }
  220. //子->父 点击商品
  221. clickRecGoods(e: any): void {
  222. this.$Router.push({
  223. path: '/pages/front/goods-detail',
  224. query: {
  225. id: e.id,
  226. steady_id: e.steady_id
  227. }
  228. });
  229. }
  230. //子->父 点击商品推荐+号
  231. clickAdd(val: any): void {
  232. this.goodsPopup.skuList = val.skus;
  233. this.goodsPopup.spu_info = val;
  234. (this.$refs.pescEle as any).open();
  235. }
  236. //搜索商品内容
  237. searchGoods(e: any) {
  238. let value = e.detail.value;
  239. this.goodsList = [];
  240. this.current = 1;
  241. this.finish = false;
  242. this.deleteIndex = null;
  243. this.setSearchHistory(value);
  244. this.getGoodsList(value);
  245. this.showHistory = false;
  246. }
  247. emptyHistory() {
  248. this.deleteIndex = null;
  249. this.emptySearchHistory();
  250. }
  251. //点击搜索历史
  252. clickItem(item: string) {
  253. this.searchContent = item;
  254. this.searchGoods({
  255. detail: {
  256. value: this.searchContent
  257. }
  258. });
  259. }
  260. deleteHistory(item: any) {
  261. this.deleteIndex = null;
  262. this.deleteSearchHistory(item);
  263. }
  264. pressItem(index: any) {
  265. if (index === this.deleteIndex) {
  266. this.deleteIndex = null;
  267. } else {
  268. this.deleteIndex = index;
  269. }
  270. }
  271. //子->父 点击取消按钮
  272. cancelEvent() {
  273. this.showHistory = true;
  274. }
  275. changeRank(rank: any) {
  276. if (rank === this.rank) return;
  277. if (rank === 'price') {
  278. if (this.rank === 2) {
  279. this.rank = 3;
  280. } else if (this.rank === 3) {
  281. this.rank = 2;
  282. } else {
  283. this.rank = 2;
  284. }
  285. } else {
  286. this.rank = rank;
  287. }
  288. this.goodsList = [];
  289. this.current = 1;
  290. this.finish = false;
  291. this.getGoodsList('', 'load');
  292. }
  293. }
  294. </script>
  295. <style lang='scss' scoped>
  296. .content {
  297. width: 100%;
  298. min-height: 100vh;
  299. @include flex-y();
  300. background: #F6F6F6;
  301. .search-styl {
  302. width: 100%;
  303. padding-top: vw(6);
  304. padding-bottom: vw(8);
  305. background-color: #ffffff;
  306. .search-box {
  307. @include flex-x();
  308. @include wh(348, 32);
  309. margin: 0 auto;
  310. .search-left {
  311. @include flex-x(flex-start);
  312. @include wh(310, 32);
  313. border-radius: vw(5);
  314. background-color: #F6F6F6;
  315. .icon1 {
  316. margin: 0 vw(10);
  317. @include icon(14);
  318. }
  319. .input-styl {
  320. width: vw(315);
  321. font-size: vw(14);
  322. }
  323. .placeholder {
  324. color: #999;
  325. }
  326. }
  327. .search-btn {
  328. /* padding-left: vw(12);
  329. margin-right: vw(13); */
  330. flex: 1;
  331. text-align: center;
  332. color: $btn-color;
  333. font-size: vw(14);
  334. }
  335. }
  336. }
  337. .sort-box {
  338. height: vw(40);
  339. width: 100%;
  340. @include flex-x();
  341. margin-top: vw(1);
  342. background: #fff;
  343. .sort-item {
  344. flex: 1;
  345. @include flex-x(center);
  346. background: #fff;
  347. height: vw(20);
  348. @include word-vw(14, #999);
  349. border-right: vw(1) solid #F5F5F5;
  350. &:nth-last-of-type(1) {
  351. border-width: 0;
  352. }
  353. .sj {
  354. position: relative;
  355. @include flex-y(center);
  356. margin-left: vw(3);
  357. }
  358. .ssj {
  359. border-top: vw(5) solid transparent;
  360. border-bottom: vw(5) solid #ccc;
  361. border-left: vw(5) solid transparent;
  362. border-right: vw(5) solid transparent;
  363. &.active {
  364. border-bottom: vw(5) solid $btn-color;
  365. }
  366. }
  367. .xsj {
  368. border-top: vw(5) solid #ccc;
  369. border-bottom: vw(5) solid transparent;
  370. border-left: vw(5) solid transparent;
  371. border-right: vw(5) solid transparent;
  372. margin-top: vw(3);
  373. &.active {
  374. border-top: vw(5) solid $btn-color;
  375. }
  376. }
  377. &.active {
  378. color: $btn-color;
  379. }
  380. }
  381. }
  382. .search-history {
  383. width: 100%;
  384. flex: 1;
  385. box-sizing: border-box;
  386. padding: vw(12) vw(23) 0 vw(16);
  387. @include word-vw(12, $gray6);
  388. .search-title {
  389. width: 100%;
  390. @include flex-x();
  391. }
  392. .search-content {
  393. @include flex-x(flex-start);
  394. flex-flow: row wrap;
  395. margin-top: vw(18);
  396. .search-font {
  397. padding: vw(8) vw(14);
  398. margin: 0 vw(10) vw(10) 0;
  399. background-color: #ffffff;
  400. position: relative;
  401. }
  402. .delete {
  403. position: absolute;
  404. right: -6px;
  405. top: -6px;
  406. }
  407. }
  408. }
  409. .goods-module {
  410. width: 100%;
  411. padding: vw(10) vw(10) 0;
  412. box-sizing: border-box;
  413. /* flex: 1;
  414. overflow-y: auto; */
  415. @include flex-x(space-between, flex-start);
  416. flex-wrap: wrap;
  417. &.goods-module2 {
  418. @include flex-y();
  419. padding: vw(10) 0 0;
  420. }
  421. .no-data {
  422. width: 100%;
  423. height: 100%;
  424. padding-top: 20%;
  425. @include flex-y();
  426. }
  427. .total {
  428. padding: 0 vw(10);
  429. margin-top: vw(6);
  430. @include word-vw(14, #999);
  431. height: vw(30);
  432. line-height: vw(30);
  433. text {
  434. color: #333;
  435. padding: 0 vw(3);
  436. }
  437. }
  438. }
  439. }
  440. </style>