seckill.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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">
  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: $font-color-base;
  261. padding: 11.5px 5px;
  262. background-color: #ffffff;
  263. line-height: 1;
  264. .time {
  265. font-size: 16px;
  266. padding-bottom: 3px;
  267. &.action {
  268. color: $color-red;
  269. }
  270. }
  271. .status {
  272. font-size: 12px;
  273. margin: 0 auto;
  274. width: 60px;
  275. padding: 3px 0;
  276. &.action {
  277. color: #ffffff;
  278. border-radius: 99px;
  279. background-color: $color-red;
  280. }
  281. }
  282. }
  283. }
  284. // 列表上方标题样式
  285. .title-box {
  286. margin-top: 10px;
  287. padding: 10px;
  288. background-color: #ffffff;
  289. .title-box-left {
  290. font-size: 15px;
  291. color: $font-color-base;
  292. }
  293. .title-box-right {
  294. .tip {
  295. font-size: 15px;
  296. color: #999999;
  297. padding-right: 5px;
  298. }
  299. /deep/ .uni-countdown__number {
  300. border-radius: 3px;
  301. width: 22px;
  302. }
  303. /deep/ .uni-countdown__splitor,
  304. /deep/ .uni-countdown__number {
  305. height: 20px;
  306. line-height: 18px;
  307. }
  308. }
  309. }
  310. // 列表样式
  311. .list-Box {
  312. height: calc(100% - 60px);
  313. .list {
  314. height: calc(100% - 55px);
  315. }
  316. }
  317. // 商品列表
  318. $slider-color: #fe9398; //滑块左侧颜色
  319. .goodsList-item {
  320. background-color: #ffffff;
  321. padding: 30rpx;
  322. border-bottom: 1px solid $border-color-light;
  323. image {
  324. flex-shrink: 0;
  325. border-radius: $border-radius-sm;
  326. height: 180rpx;
  327. width: 180rpx;
  328. }
  329. .slider {
  330. margin-top: 15rpx;
  331. justify-content: flex-start;
  332. .slider-box {
  333. width: 196rpx;
  334. border-radius: 99px;
  335. border: 1px solid $slider-color;
  336. height: 16rpx;
  337. .slider-action {
  338. background-color: $slider-color;
  339. height: 100%;
  340. }
  341. }
  342. .sales-nub {
  343. color: $font-color-light;
  344. font-size: 24rpx;
  345. padding-left: 20rpx;
  346. }
  347. }
  348. .goodsList-content {
  349. margin-left: 20rpx;
  350. flex-grow: 1;
  351. height: 180rpx;
  352. position: relative;
  353. .title {
  354. font-size: $font-base;
  355. color: $font-color-dark;
  356. font-weight: 500;
  357. width: 0;
  358. min-width: 100%;
  359. }
  360. .goods-money {
  361. position: absolute;
  362. left: 0;
  363. bottom: 0;
  364. width: 100%;
  365. .money-box {
  366. .money {
  367. font-size: $font-lg + 10rpx;
  368. color: $color-red;
  369. font-weight: bold;
  370. }
  371. .otMoney-box {
  372. font-size: $font-sm;
  373. .otMoney {
  374. color: $font-color-light;
  375. padding-right: 20rpx;
  376. text-decoration: line-through;
  377. }
  378. .sales {
  379. color: $font-color-light;
  380. }
  381. }
  382. }
  383. .cart {
  384. font-size: $font-base;
  385. border-radius: 99px;
  386. padding: 15rpx 30rpx;
  387. line-height: 1;
  388. color: #ffffff;
  389. background-color: $color-gray;
  390. &.seckill-action {
  391. border: 1px solid $color-red;
  392. background-color: $color-red;
  393. }
  394. }
  395. }
  396. }
  397. }
  398. </style>