seckill.vue 11 KB

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