seckill.vue 10 KB

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