index.vue 13 KB

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