seckill.vue 10.0 KB

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