index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <template>
  2. <view>
  3. <view class='productList'>
  4. <view class='search bg-color acea-row row-between-wrapper'>
  5. <!-- #ifdef H5 -->
  6. <view class="iconfont icon-xiangzuo" @click="goback()"></view>
  7. <!-- #endif -->
  8. <view class='input acea-row row-between-wrapper'><text class='iconfont icon-sousuo'></text>
  9. <input placeholder='搜索商品名称' placeholder-class='placeholder' confirm-type='search' name="search"
  10. :value='where.keyword' @confirm="searchSubmit" />
  11. </view>
  12. <view class='iconfont' :class='is_switch ? "icon-pailie":"icon-tupianpailie"' @click='Changswitch'>
  13. </view>
  14. </view>
  15. <view class='nav acea-row row-middle'>
  16. <view class='item' :class='title ? "font-color":""' @click='set_where(1)'>{{title ? title:'默认'}}</view>
  17. <view class='item' @click='set_where(2)'>
  18. 价格
  19. <image v-if="price === 1" src='../../static/images/up.png'></image>
  20. <image v-else-if="price === 2" src='../../static/images/down.png'></image>
  21. <image v-else src='../../static/images/horn.png'></image>
  22. </view>
  23. <view class='item' @click='set_where(3)'>
  24. 销量
  25. <image v-if="stock === 1" src='../../static/images/up.png'></image>
  26. <image v-else-if="stock === 2" src='../../static/images/down.png'></image>
  27. <image v-else src='../../static/images/horn.png'></image>
  28. </view>
  29. <!-- down -->
  30. <view class='item' :class='nows ? "font-color":""' @click='set_where(4)'>新品</view>
  31. </view>
  32. <view :class='is_switch?"":"listBox"' v-if="productList.length > 0">
  33. <view class='list acea-row row-between-wrapper' :class='is_switch?"":"on"'>
  34. <view class='item' :class='is_switch?"":"on"' hover-class='none'
  35. v-for="(item,index) in productList" :key="index" @click="godDetail(item)">
  36. <view class='pictrue' :class='is_switch?"":"on"'>
  37. <view class="no-sale-num" v-if="item.stock === 0">已售罄</view>
  38. <image :src='item.picUrl' :class='is_switch?"":"on"'></image>
  39. <span class="pictrue_log_class"
  40. :class="is_switch ? 'pictrue_log_big' : 'pictrue_log'"
  41. v-if="item.activityList && item.activityList[0] && item.activityList[0].type === 1">秒杀</span>
  42. <span class="pictrue_log_class"
  43. :class="is_switch ? 'pictrue_log_big' : 'pictrue_log'"
  44. v-if="item.activityList && item.activityList[0] && item.activityList[0].type === 2">砍价</span>
  45. <span class="pictrue_log_class"
  46. :class="is_switch ? 'pictrue_log_big' : 'pictrue_log'"
  47. v-if="item.activityList && item.activityList[0] && item.activityList[0].type === 3">拼团</span>
  48. </view>
  49. <view class='text' :class='is_switch?"":"on"'>
  50. <view class='name line1'>{{item.name}}</view>
  51. <view class='money font-color' :class='is_switch?"":"on"'>¥<text
  52. class='num'>{{ fen2yuan(item.price) }}</text></view>
  53. <view class='vip acea-row row-between-wrapper' :class='is_switch?"":"on"'>
  54. <view class='vip-money' v-if="item.vipPrice > 0">¥{{ fen2yuan(item.price - item.vipPrice) }}
  55. <image src='../../static/images/vip.png'></image>
  56. </view>
  57. <view>已售 {{ item.salesCount || 0}} {{item.unitName}}</view>
  58. </view>
  59. </view>
  60. </view>
  61. </view>
  62. <view class='loadingicon acea-row row-center-wrapper' v-if='productList.length > 0'>
  63. <text class='loading iconfont icon-jiazai' :hidden='!loading'/> {{loadTitle}}
  64. </view>
  65. </view>
  66. </view>
  67. <view class='noCommodity' v-if="productList.length === 0 && where.pageNo > 1">
  68. <view class='pictrue'>
  69. <image src='../../static/images/noShopper.png'></image>
  70. </view>
  71. <recommend :hostProduct="hostProduct" />
  72. </view>
  73. </view>
  74. </template>
  75. <script>
  76. import recommend from '@/components/recommend';
  77. import { mapGetters } from "vuex";
  78. import { goShopDetail } from '@/libs/order.js'
  79. import * as ProductSpuApi from '@/api/product/spu.js';
  80. import * as PromotionActivityApi from '@/api/promotion/activity.js';
  81. import * as ProductUtil from '@/utils/product.js';
  82. import * as Util from '@/utils/util.js';
  83. export default {
  84. computed: mapGetters(['uid']),
  85. components: {
  86. recommend
  87. },
  88. data() {
  89. return {
  90. productList: [],
  91. is_switch: true,
  92. where: { // 筛选条件
  93. keyword: '',
  94. sortField: '',
  95. sortAsc: '',
  96. recommendType: '',
  97. pageNo: 1,
  98. pageSize: 20,
  99. categoryId: 0,
  100. },
  101. price: 0,
  102. stock: 0,
  103. nows: false,
  104. loadend: false,
  105. loading: false,
  106. loadTitle: '加载更多',
  107. title: '',
  108. // ========== 热门商品 ==========
  109. hostProduct: [],
  110. hotPage: 1,
  111. hotLimit: 10,
  112. hotScroll: false
  113. };
  114. },
  115. onLoad: function(options) {
  116. this.$set(this.where, 'categoryId', options.cid || 0);
  117. this.title = options.title || '';
  118. this.$set(this.where, 'keyword', options.searchValue || '');
  119. this.get_product_list();
  120. },
  121. methods: {
  122. goback() {
  123. // #ifdef H5
  124. return history.back();
  125. // #endif
  126. // #ifndef H5
  127. return uni.navigateBack({
  128. delta: 1,
  129. })
  130. // #endif
  131. },
  132. // 去详情页
  133. godDetail(item) {
  134. goShopDetail(item, this.uid).then(res => {
  135. uni.navigateTo({
  136. url: `/pages/goods_details/index?id=${item.id}`
  137. })
  138. })
  139. },
  140. Changswitch: function() {
  141. this.is_switch = !this.is_switch
  142. },
  143. searchSubmit: function(e) {
  144. this.$set(this.where, 'keyword', e.detail.value);
  145. this.loadend = false;
  146. this.$set(this.where, 'pageNo', 1)
  147. this.get_product_list(true);
  148. },
  149. /**
  150. * 点击事件处理
  151. */
  152. set_where: function(e) {
  153. switch (e) {
  154. case 1:
  155. return;
  156. break;
  157. case 2:
  158. if (this.price === 0) this.price = 1;
  159. else if (this.price === 1) this.price = 2;
  160. else if (this.price === 2) this.price = 0;
  161. this.stock = 0;
  162. break;
  163. case 3:
  164. if (this.stock === 0) this.stock = 1;
  165. else if (this.stock === 1) this.stock = 2;
  166. else if (this.stock === 2) this.stock = 0;
  167. this.price = 0
  168. break;
  169. case 4:
  170. this.nows = !this.nows;
  171. break;
  172. }
  173. this.loadend = false;
  174. this.$set(this.where, 'pageNo', 1);
  175. this.get_product_list(true);
  176. },
  177. /**
  178. * 设置where条件
  179. */
  180. setWhere: function() {
  181. if (this.price === 0 && this.stock === 0) {
  182. this.where.sortField = undefined;
  183. this.where.sortAsc = undefined;
  184. } else if (this.price === 1) {
  185. this.where.sortField = 'price';
  186. this.where.sortAsc = true;
  187. } else if (this.price === 2) {
  188. this.where.sortField = 'price';
  189. this.where.sortAsc = false;
  190. } else if (this.stock === 1) {
  191. this.where.sortField = 'salesCount';
  192. this.where.sortAsc = true;
  193. } else if (this.stock === 2) {
  194. this.where.sortField = 'salesCount';
  195. this.where.sortAsc = false;
  196. }
  197. this.where.recommendType = this.nows ? 'new' : undefined;
  198. },
  199. /**
  200. * 查找产品
  201. */
  202. get_product_list: function(isPage) {
  203. this.setWhere();
  204. if (this.loadend || this.loading) {
  205. return;
  206. }
  207. if (isPage === true) {
  208. this.$set(this, 'productList', []);
  209. }
  210. this.loading = true;
  211. this.loadTitle = '';
  212. ProductSpuApi.getSpuPage(this.where).then(res => {
  213. const good_list = res.data.list;
  214. const loadend = good_list.length < this.where.limit;
  215. this.loadend = loadend;
  216. this.loading = false;
  217. this.loadTitle = loadend ? '已全部加载' : '加载更多';
  218. this.$set(this.where, 'pageNo', this.where.pageNo + 1);
  219. // 设置营销活动
  220. const spuIds = good_list.map(item => item.id);
  221. if (spuIds.length > 0) {
  222. PromotionActivityApi.getActivityListBySpuIds(spuIds).then(res => {
  223. ProductUtil.setActivityList(good_list, res.data);
  224. const productList = this.$util.SplitArray(good_list, this.productList)
  225. this.$set(this, 'productList', productList); // 放在此处,避免 Vue 监控不到数组里的元素变化
  226. });
  227. }
  228. // 加载不到商品的情况下,加载热门商品
  229. if (good_list.length === 0 && this.productList.length === 0) {
  230. this.get_host_product();
  231. }
  232. }).catch(err => {
  233. this.loading = false;
  234. this.loadTitle = '加载更多';
  235. });
  236. },
  237. /**
  238. * 获取我的推荐
  239. */
  240. get_host_product: function() {
  241. if (this.hotScroll) {
  242. return
  243. }
  244. ProductSpuApi.getSpuPage({
  245. recommendType: 'hot',
  246. pageNo: this.hotPage,
  247. pageSize: this.hotLimit
  248. }).then(res => {
  249. const good_list = res.data.list;
  250. this.hotPage++
  251. this.hotScroll = good_list.length < this.hotLimit
  252. // 设置营销活动
  253. const spuIds = good_list.map(item => item.id);
  254. if (spuIds.length > 0) {
  255. PromotionActivityApi.getActivityListBySpuIds(spuIds).then(res => {
  256. ProductUtil.setActivityList(good_list, res.data);
  257. this.hostProduct = this.hostProduct.concat(good_list) // 放在此处,避免 Vue 监控不到数组里的元素变化
  258. });
  259. }
  260. });
  261. },
  262. fen2yuan(price) {
  263. return Util.fen2yuan(price)
  264. }
  265. },
  266. onReachBottom() {
  267. if (this.productList.length > 0) {
  268. this.get_product_list();
  269. } else {
  270. this.get_host_product();
  271. }
  272. }
  273. }
  274. </script>
  275. <style scoped lang="scss">
  276. .iconfont {
  277. color: #fff;
  278. }
  279. .listBox{
  280. padding: 20px 15px;
  281. margin-top: 154rpx;
  282. }
  283. .productList .search {
  284. width: 100%;
  285. height: 86rpx;
  286. padding-left: 23rpx;
  287. box-sizing: border-box;
  288. position: fixed;
  289. left: 0;
  290. top: 0;
  291. z-index: 9;
  292. }
  293. .productList .search .input {
  294. // width: 640rpx;
  295. height: 60rpx;
  296. background-color: #fff;
  297. border-radius: 50rpx;
  298. padding: 0 20rpx;
  299. box-sizing: border-box;
  300. }
  301. .productList .search .input input {
  302. /* #ifdef H5 */
  303. width: 528rpx;
  304. /* #endif */
  305. /* #ifndef H5 */
  306. width: 548rpx;
  307. /* #endif */
  308. height: 100%;
  309. font-size: 26rpx;
  310. }
  311. .productList .search .input .placeholder {
  312. color: #999;
  313. }
  314. .productList .search .input .iconfont {
  315. font-size: 35rpx;
  316. color: #555;
  317. }
  318. .productList .search .icon-pailie,
  319. .productList .search .icon-tupianpailie {
  320. color: #fff;
  321. width: 62rpx;
  322. font-size: 40rpx;
  323. height: 86rpx;
  324. line-height: 86rpx;
  325. }
  326. .productList .nav {
  327. height: 86rpx;
  328. color: #454545;
  329. position: fixed;
  330. left: 0;
  331. width: 100%;
  332. font-size: 28rpx;
  333. background-color: #fff;
  334. margin-top: 86rpx;
  335. top: 0;
  336. z-index: 9;
  337. }
  338. .productList .nav .item {
  339. width: 25%;
  340. text-align: center;
  341. }
  342. .productList .nav .item.font-color {
  343. font-weight: bold;
  344. }
  345. .productList .nav .item image {
  346. width: 15rpx;
  347. height: 19rpx;
  348. margin-left: 10rpx;
  349. }
  350. .productList .list {
  351. padding: 0 30rpx;
  352. margin-top: 192rpx;
  353. }
  354. .productList .list.on {
  355. border-radius: 14rpx;
  356. margin-top: 0 !important;
  357. background-color: #fff;
  358. padding: 40rpx 0 0 0;
  359. // margin: 20rpx 0;
  360. // background-color: #fff;
  361. }
  362. .productList .list .item {
  363. width: 335rpx;
  364. background-color: #fff;
  365. border-radius: 14rpx;
  366. margin-bottom: 20rpx;
  367. }
  368. .productList .list .item.on {
  369. width: 100%;
  370. display: flex;
  371. padding: 0 24rpx 50rpx 24rpx;
  372. margin: 0;
  373. border-radius: 14rpx;
  374. }
  375. .productList .list .item .pictrue {
  376. position: relative;
  377. width: 100%;
  378. height: 335rpx;
  379. }
  380. .productList .list .item .pictrue.on {
  381. width: 180rpx;
  382. height: 180rpx;
  383. }
  384. .productList .list .item .pictrue image {
  385. width: 100%;
  386. height: 100%;
  387. border-radius: 20rpx 20rpx 0 0;
  388. }
  389. .productList .list .item .pictrue image.on {
  390. border-radius: 6rpx;
  391. }
  392. .productList .list .item .text {
  393. padding: 18rpx 20rpx;
  394. font-size: 30rpx;
  395. color: #222;
  396. }
  397. .productList .list .item .text.on {
  398. width: 456rpx;
  399. padding: 0 0 0 20rpx;
  400. }
  401. .productList .list .item .text .money {
  402. font-size: 26rpx;
  403. font-weight: bold;
  404. margin-top: 8rpx;
  405. }
  406. .productList .list .item .text .money.on {
  407. margin-top: 50rpx;
  408. }
  409. .productList .list .item .text .money .num {
  410. font-size: 34rpx;
  411. }
  412. .productList .list .item .text .vip {
  413. font-size: 22rpx;
  414. color: #aaa;
  415. margin-top: 7rpx;
  416. }
  417. .productList .list .item .text .vip.on {
  418. margin-top: 12rpx;
  419. }
  420. .productList .list .item .text .vip .vip-money {
  421. font-size: 24rpx;
  422. color: #282828;
  423. font-weight: bold;
  424. }
  425. .productList .list .item .text .vip .vip-money image {
  426. width: 46rpx;
  427. height: 21rpx;
  428. margin-left: 4rpx;
  429. }
  430. .noCommodity {
  431. background-color: #fff;
  432. padding-bottom: 30rpx;
  433. margin-top: 172rpx;
  434. }
  435. .no-sale-num {
  436. width: 100%;
  437. height: 100%;
  438. display: flex;
  439. position: absolute;
  440. justify-content: center;
  441. align-items: center;
  442. z-index:100;
  443. background: rgba(0, 0, 0, 0.36);
  444. border-radius: 20rpx 20rpx 0 0;
  445. color: #fff;
  446. font-weight: 600;
  447. font-size: $bg-end;
  448. }
  449. </style>