seckill.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <template>
  2. <view class="seckillBox">
  3. <view class="top"></view>
  4. <view class="seckill-bg">
  5. <image src="../../static/img/seckill.png" mode=""></image>
  6. </view>
  7. <scroll-view scroll-x="true" class="class-box flex b-t" :scroll-left="scrollLeft" :scroll-with-animation="true">
  8. <view @click="tabClass(idx)" class="item" :style="{ width: topNavWidth }" v-for="(item, idx) in indexList" :key="idx">
  9. <view class="time" :class="{ action: idx == classIndex }">{{ item.time }}</view>
  10. <view class="status" :class="{ action: idx == classIndex }">{{ item.state }}</view>
  11. </view>
  12. </scroll-view>
  13. <swiper class="list-Box" :current="classIndex" @change="swiperChange" duration="500">
  14. <swiper-item class="list-item" v-for="(ls, idx) in indexList">
  15. <view class=" b-b position-relative">
  16. <view class="title-box flex ">
  17. <view class="title-box-left">
  18. <text v-if="ls.status == 1">抢购中先下先得哦!</text>
  19. <text v-if="ls.status == 2">精品好货即将开抢!</text>
  20. <text v-if="ls.status == 0">本场已结束,下次早点来哦!</text>
  21. </view>
  22. <view class="title-box-right flex">
  23. <text class="tip" v-if="ls.status == 1">距离结束</text>
  24. <text class="tip" v-if="ls.status == 2">距离开始</text>
  25. <uni-countdown
  26. color="#F9F9F8"
  27. background-color="#666666"
  28. v-if="ls.status == 1 || ls.status == 2"
  29. :show-day="false"
  30. :hour="ls.stopTimeH"
  31. :minute="ls.stopTimeM"
  32. :second="ls.stopTimeS"
  33. ></uni-countdown>
  34. </view>
  35. </view>
  36. </view>
  37. <scroll-view scroll-y="true" class="list" @scrolltolower="getList(classIndex)">
  38. <view class="goodsList-item flex" :key="ind" v-for="(lss, ind) in ls.dataList">
  39. <image :src="lss.image" lazy-load mode="scaleToFill"></image>
  40. <view class="goodsList-content">
  41. <view class="title clamp">
  42. <text>{{ lss.title }}</text>
  43. </view>
  44. <view class="slider flex">
  45. <view class="slider-box"><view class="slider-action" :style="{ width: lss.percent + '%' }"></view></view>
  46. <view class="sales-nub">已抢{{ lss.percent + '%' }}</view>
  47. </view>
  48. <view class="goods-money flex">
  49. <view class="money-box">
  50. <view class="money">
  51. <text class="font-size-sm">¥</text>
  52. {{ lss.price }}
  53. </view>
  54. <view class="otMoney-box">
  55. <text class="otMoney">¥{{ lss.ot_price }}</text>
  56. </view>
  57. </view>
  58. <view @click="navProduct(ls, ind)" class="cart" :class="{ 'seckill-action': ls.status == 1 }">
  59. {{ ls.status == 1 ? '去抢购' : ls.status == 2 ? '未开始' : '已结束' }}
  60. </view>
  61. </view>
  62. </view>
  63. </view>
  64. <uni-load-more :status="ls.loadingType"></uni-load-more>
  65. </scroll-view>
  66. </swiper-item>
  67. </swiper>
  68. </view>
  69. </template>
  70. <script>
  71. import { getSeckillList, getSeckillClass } from '@/api/product.js';
  72. import uniCountdown from '@/components/uni-countdown/uni-countdown.vue';
  73. import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
  74. import { timeComputed } from '@/utils/rocessor.js';
  75. export default {
  76. components: {
  77. uniCountdown,
  78. uniLoadMore
  79. },
  80. data() {
  81. return {
  82. indexList: [], //分类列表
  83. showTime: '', //显示的时间
  84. showTImeId: '', //显示时间id用于查询数据
  85. // 判断是否所有活动已经结束
  86. stop: false, //活动是否已经结束
  87. show: false, //是否显示活动
  88. status: 0, //获取状态值1为有活动开始中 2为活动未开始 0为活动已经结束
  89. topNavWidth: '25%', //设置导航默认宽度
  90. classIndex: 0, //当前进行中的活动
  91. itemWidht: 0 //顶部分类宽度
  92. };
  93. },
  94. computed: {
  95. // 计算左侧距离
  96. scrollLeft() {
  97. if (this.classIndex > 1) {
  98. return this.itemWidht * (this.classIndex - 1.5);
  99. } else {
  100. return 0;
  101. }
  102. }
  103. },
  104. onLoad: function(e) {
  105. // 载入分类
  106. this.getClass();
  107. },
  108. methods: {
  109. //抢购时间切换时触发效果
  110. swiperChange(e) {
  111. this.classIndex = e.target.current;
  112. this.getList(this.classIndex, 'tabChange');
  113. },
  114. // 获取项目宽度
  115. numClassWidht() {
  116. uni.createSelectorQuery()
  117. .select('.seckillBox')
  118. .fields(
  119. {
  120. size: true
  121. },
  122. data => {
  123. // 设置项目宽度
  124. this.itemWidht = Math.floor((data.width / 750) * 187.5);
  125. }
  126. )
  127. .exec();
  128. },
  129. // 切换当前选中的秒杀活动
  130. tabClass(ind) {
  131. // 保存当前选中的对象
  132. this.classIndex = ind;
  133. },
  134. // 跳转到商品详情
  135. navProduct(ls, ind) {
  136. if (ls.status == 1) {
  137. let data = ls.dataList[ind];
  138. uni.navigateTo({
  139. url: '/pages/product/product?id=' + data.id + '&type=1'
  140. });
  141. }
  142. },
  143. // 获取商品
  144. getList(ind, source) {
  145. // 获取数据对象
  146. let date = this.indexList[ind];
  147. console.log(date, source);
  148. if (source === 'tabChange' && date.loaded === true) {
  149. //tab切换只有第一次需要加载数据
  150. return;
  151. }
  152. if (date.loadingType === 'noMore') {
  153. //防止重复加载
  154. return;
  155. }
  156. if (date.loadingType === 'loading') {
  157. //防止重复加载
  158. return;
  159. }
  160. // 修改当前对象状态为加载中
  161. date.loadingType = 'loading';
  162. getSeckillList(
  163. {
  164. page: date.page,
  165. limit: date.limit
  166. },
  167. date.id
  168. )
  169. .then(e => {
  170. date.dataList.push(...e.data);
  171. // 查询翻页增加
  172. date.page++;
  173. // 判断是否可以继续加载
  174. if (date.limit == e.data.length) {
  175. date.loadingType = 'more';
  176. } else {
  177. date.loadingType = 'noMore';
  178. }
  179. // 设置当前数据已加载完毕
  180. this.$set(date, 'loaded', true);
  181. })
  182. .catch(e => {
  183. console.log(e);
  184. });
  185. },
  186. // 获取秒杀时间段
  187. getClass() {
  188. let obj = this;
  189. getSeckillClass({})
  190. .then(({ data }) => {
  191. obj.indexList = data.seckillTime.map((e, ind) => {
  192. console.log(e, 'eeee');
  193. // 初始化翻页页数
  194. e.page = 1;
  195. // 初始每次加载的数据条数
  196. e.limit = 6;
  197. // 创建储存订单数据
  198. e.dataList = [];
  199. // 初始化加载
  200. // e.loadingType = 'more';
  201. let ar = e;
  202. // 判断是否为进行中的活动
  203. if (ar.status === 1) {
  204. // 计算倒计时时间
  205. obj.timeComputed(ar.stop * 1000, ar);
  206. } else {
  207. // 获取距离开始还需要多少时间
  208. let arTime = ar.time.split(':');
  209. let h = arTime[0];
  210. let m = arTime[1];
  211. let time = new Date();
  212. // 设置时间
  213. time.setHours(h, m, 0);
  214. // 计算倒计时时间
  215. obj.timeComputed(time.getTime(), ar);
  216. }
  217. return e;
  218. });
  219. // 获取当前活动中的下标值
  220. obj.classIndex = data.seckillTimeIndex;
  221. // 获取当前显示中对象的数据
  222. obj.$nextTick(() => {
  223. obj.getList(obj.classIndex);
  224. });
  225. if (obj.indexList.length <= 4) {
  226. // 当数量小于等于4的时候自适应宽度
  227. obj.topNavWidth = 100 / obj.indexList.length + '%';
  228. } else {
  229. // 页面渲染完毕后加载scroll-view左侧距离
  230. obj.$nextTick(() => {
  231. obj.numClassWidht();
  232. });
  233. }
  234. })
  235. .catch(e => {
  236. console.log(e);
  237. });
  238. },
  239. // 计算倒计时时间
  240. timeComputed(da, ar) {
  241. let obj = this;
  242. // 计算时间,保存需要多少时间到期
  243. let stopTime = timeComputed(da);
  244. console.log(stopTime);
  245. ar.stopTimeH = stopTime.hours;
  246. ar.stopTimeM = stopTime.minutes;
  247. ar.stopTimeS = stopTime.seconds;
  248. }
  249. }
  250. };
  251. </script>
  252. <style lang="scss">
  253. page,
  254. .seckillBox {
  255. min-height: 100%;
  256. height: 100%;
  257. }
  258. .top {
  259. width: 750rpx;
  260. height: 300rpx;
  261. background: #e83323;
  262. border-bottom-left-radius: 150rpx;
  263. border-bottom-right-radius: 150rpx;
  264. }
  265. .seckill-bg {
  266. width: 700rpx;
  267. height: 300rpx;
  268. margin: -260rpx auto 0;
  269. image {
  270. width: 100%;
  271. height: 100%;
  272. }
  273. }
  274. // 头部时间段样式
  275. .class-box {
  276. white-space: nowrap;
  277. height: 60px;
  278. .item {
  279. text-align: center;
  280. display: inline-block;
  281. color: $font-color-base;
  282. padding: 11.5px 5px;
  283. background-color: #ffffff;
  284. line-height: 1;
  285. .time {
  286. font-size: 16px;
  287. padding-bottom: 3px;
  288. &.action {
  289. color: $color-red;
  290. }
  291. }
  292. .status {
  293. font-size: 12px;
  294. margin: 0 auto;
  295. width: 60px;
  296. padding: 3px 0;
  297. &.action {
  298. color: #ffffff;
  299. border-radius: 99px;
  300. background-color: $color-red;
  301. }
  302. }
  303. }
  304. }
  305. // 列表上方标题样式
  306. .title-box {
  307. margin-top: 10px;
  308. padding: 10px;
  309. background-color: #ffffff;
  310. .title-box-left {
  311. font-size: 15px;
  312. color: $font-color-base;
  313. }
  314. .title-box-right {
  315. .tip {
  316. font-size: 15px;
  317. color: #999999;
  318. padding-right: 5px;
  319. }
  320. /deep/ .uni-countdown__number {
  321. border-radius: 3px;
  322. width: 22px;
  323. }
  324. /deep/ .uni-countdown__splitor,
  325. /deep/ .uni-countdown__number {
  326. height: 20px;
  327. line-height: 18px;
  328. }
  329. }
  330. }
  331. // 列表样式
  332. .list-Box {
  333. height: calc(100% - 60px);
  334. .list {
  335. height: calc(100% - 55px);
  336. }
  337. }
  338. // 商品列表
  339. $slider-color: #fe9398; //滑块左侧颜色
  340. .goodsList-item {
  341. background-color: #ffffff;
  342. padding: 30rpx;
  343. border-bottom: 1px solid $border-color-light;
  344. image {
  345. flex-shrink: 0;
  346. border-radius: $border-radius-sm;
  347. height: 180rpx;
  348. width: 180rpx;
  349. }
  350. .slider {
  351. margin-top: 15rpx;
  352. justify-content: flex-start;
  353. .slider-box {
  354. width: 196rpx;
  355. border-radius: 99px;
  356. border: 1px solid $slider-color;
  357. height: 16rpx;
  358. .slider-action {
  359. background-color: $slider-color;
  360. height: 100%;
  361. }
  362. }
  363. .sales-nub {
  364. color: $font-color-light;
  365. font-size: 24rpx;
  366. padding-left: 20rpx;
  367. }
  368. }
  369. .goodsList-content {
  370. margin-left: 20rpx;
  371. flex-grow: 1;
  372. height: 180rpx;
  373. position: relative;
  374. .title {
  375. font-size: $font-base;
  376. color: $font-color-dark;
  377. font-weight: 500;
  378. width: 0;
  379. min-width: 100%;
  380. }
  381. .goods-money {
  382. position: absolute;
  383. left: 0;
  384. bottom: 0;
  385. width: 100%;
  386. .money-box {
  387. .money {
  388. font-size: $font-lg + 10rpx;
  389. color: $color-red;
  390. font-weight: bold;
  391. }
  392. .otMoney-box {
  393. font-size: $font-sm;
  394. .otMoney {
  395. color: $font-color-light;
  396. padding-right: 20rpx;
  397. text-decoration: line-through;
  398. }
  399. .sales {
  400. color: $font-color-light;
  401. }
  402. }
  403. }
  404. .cart {
  405. font-size: $font-base;
  406. border-radius: 99px;
  407. padding: 15rpx 30rpx;
  408. line-height: 1;
  409. color: #ffffff;
  410. background-color: $color-gray;
  411. &.seckill-action {
  412. border: 1px solid $color-red;
  413. background-color: $color-red;
  414. }
  415. }
  416. }
  417. }
  418. }
  419. </style>