seckill.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. console.log(e,'eeee')
  189. // 初始化翻页页数
  190. e.page = 1;
  191. // 初始每次加载的数据条数
  192. e.limit = 6;
  193. // 创建储存订单数据
  194. e.dataList = [];
  195. // 初始化加载
  196. // e.loadingType = 'more';
  197. let ar = e;
  198. // 判断是否为进行中的活动
  199. if (ar.status === 1) {
  200. // 计算倒计时时间
  201. obj.timeComputed(ar.stop * 1000, ar);
  202. } else {
  203. // 获取距离开始还需要多少时间
  204. let arTime = ar.time.split(':');
  205. let h = arTime[0];
  206. let m = arTime[1];
  207. let time = new Date();
  208. // 设置时间
  209. time.setHours(h, m, 0);
  210. // 计算倒计时时间
  211. obj.timeComputed(time.getTime(), ar);
  212. }
  213. return e;
  214. });
  215. // 获取当前活动中的下标值
  216. obj.classIndex = data.seckillTimeIndex;
  217. // 获取当前显示中对象的数据
  218. obj.$nextTick(() => {
  219. obj.getList(obj.classIndex);
  220. });
  221. if (obj.indexList.length <= 4) {
  222. // 当数量小于等于4的时候自适应宽度
  223. obj.topNavWidth = 100 / obj.indexList.length + '%';
  224. } else {
  225. // 页面渲染完毕后加载scroll-view左侧距离
  226. obj.$nextTick(() => {
  227. obj.numClassWidht();
  228. });
  229. }
  230. })
  231. .catch(e => {
  232. console.log(e);
  233. });
  234. },
  235. // 计算倒计时时间
  236. timeComputed(da, ar) {
  237. let obj = this;
  238. // 计算时间,保存需要多少时间到期
  239. let stopTime = timeComputed(da);
  240. console.log(stopTime);
  241. ar.stopTimeH = stopTime.hours;
  242. ar.stopTimeM = stopTime.minutes;
  243. ar.stopTimeS = stopTime.seconds;
  244. }
  245. }
  246. };
  247. </script>
  248. <style lang="scss">
  249. page,
  250. .seckillBox {
  251. min-height: 100%;
  252. height: 100%;
  253. }
  254. // 头部时间段样式
  255. .class-box {
  256. white-space: nowrap;
  257. height: 60px;
  258. .item {
  259. text-align: center;
  260. display: inline-block;
  261. color: $font-color-base;
  262. padding: 11.5px 5px;
  263. background-color: #ffffff;
  264. line-height: 1;
  265. .time {
  266. font-size: 16px;
  267. padding-bottom: 3px;
  268. &.action {
  269. color: $color-red;
  270. }
  271. }
  272. .status {
  273. font-size: 12px;
  274. margin: 0 auto;
  275. width: 60px;
  276. padding: 3px 0;
  277. &.action {
  278. color: #ffffff;
  279. border-radius: 99px;
  280. background-color: $color-red;
  281. }
  282. }
  283. }
  284. }
  285. // 列表上方标题样式
  286. .title-box {
  287. margin-top: 10px;
  288. padding: 10px;
  289. background-color: #ffffff;
  290. .title-box-left {
  291. font-size: 15px;
  292. color: $font-color-base;
  293. }
  294. .title-box-right {
  295. .tip {
  296. font-size: 15px;
  297. color: #999999;
  298. padding-right: 5px;
  299. }
  300. /deep/ .uni-countdown__number {
  301. border-radius: 3px;
  302. width: 22px;
  303. }
  304. /deep/ .uni-countdown__splitor,
  305. /deep/ .uni-countdown__number {
  306. height: 20px;
  307. line-height: 18px;
  308. }
  309. }
  310. }
  311. // 列表样式
  312. .list-Box {
  313. height: calc(100% - 60px);
  314. .list {
  315. height: calc(100% - 55px);
  316. }
  317. }
  318. // 商品列表
  319. $slider-color: #fe9398; //滑块左侧颜色
  320. .goodsList-item {
  321. background-color: #ffffff;
  322. padding: 30rpx;
  323. border-bottom: 1px solid $border-color-light;
  324. image {
  325. flex-shrink: 0;
  326. border-radius: $border-radius-sm;
  327. height: 180rpx;
  328. width: 180rpx;
  329. }
  330. .slider {
  331. margin-top: 15rpx;
  332. justify-content: flex-start;
  333. .slider-box {
  334. width: 196rpx;
  335. border-radius: 99px;
  336. border: 1px solid $slider-color;
  337. height: 16rpx;
  338. .slider-action {
  339. background-color: $slider-color;
  340. height: 100%;
  341. }
  342. }
  343. .sales-nub {
  344. color: $font-color-light;
  345. font-size: 24rpx;
  346. padding-left: 20rpx;
  347. }
  348. }
  349. .goodsList-content {
  350. margin-left: 20rpx;
  351. flex-grow: 1;
  352. height: 180rpx;
  353. position: relative;
  354. .title {
  355. font-size: $font-base;
  356. color: $font-color-dark;
  357. font-weight: 500;
  358. width: 0;
  359. min-width: 100%;
  360. }
  361. .goods-money {
  362. position: absolute;
  363. left: 0;
  364. bottom: 0;
  365. width: 100%;
  366. .money-box {
  367. .money {
  368. font-size: $font-lg + 10rpx;
  369. color: $color-red;
  370. font-weight: bold;
  371. }
  372. .otMoney-box {
  373. font-size: $font-sm;
  374. .otMoney {
  375. color: $font-color-light;
  376. padding-right: 20rpx;
  377. text-decoration: line-through;
  378. }
  379. .sales {
  380. color: $font-color-light;
  381. }
  382. }
  383. }
  384. .cart {
  385. font-size: $font-base;
  386. border-radius: 99px;
  387. padding: 15rpx 30rpx;
  388. line-height: 1;
  389. color: #ffffff;
  390. background-color: $color-gray;
  391. &.seckill-action {
  392. border: 1px solid $color-red;
  393. background-color: $color-red;
  394. }
  395. }
  396. }
  397. }
  398. }
  399. </style>