index.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <page title="限时秒杀" class="title" ref="pageRef" nav-color="transparent">
  3. <view class="spike_timer flex">
  4. <image class="img" :src="resource.spike_timer" />
  5. <view v-if="timerFlag" class="text">
  6. {{ skipeType === 1 ? '秒杀开始' : '秒杀结束'}}
  7. </view>
  8. <view v-else class="text">
  9. {{ skipeType === 1 ? '距离开始' : '距离结束'}}
  10. </view>
  11. <timer :targetTime="targetTime" :timerFlag="timerFlag" @timeDone="SpikeTime(1)" />
  12. </view>
  13. <view class="marginT32 marginB18">
  14. <tabbar @change="onChange"/>
  15. </view>
  16. <empty v-if="isEmpty" :top="100" style="width: 100%" />
  17. <view class="visible-hiddle-caculate" style="width: 50%;">
  18. <view id="caculate-demo">
  19. <goods-cell v-if="demo" :data="demo"/>
  20. </view>
  21. </view>
  22. <view class="flex paddingY4 marginX18">
  23. <view class="flex1">
  24. <view id="left">
  25. <goods-cell v-for="item in list" :key="item.id" :data="item" :skipeType="skipeType" :startTime="startTime" />
  26. </view>
  27. </view>
  28. <view class="flex1 marginL8">
  29. <view id="right">
  30. <goods-cell v-for="item in list2" :key="item.id" :data="item" :skipeType="skipeType" :startTime="startTime" />
  31. </view>
  32. </view>
  33. </view>
  34. <view class="refresh" @click.stop="onRefresh">
  35. <image class="width100 height100" :src="resource.spike_refresh" />
  36. </view>
  37. </page>
  38. </template>
  39. <script>
  40. import tabbar from "@/components/spike-tabbar";
  41. import timer from "@/components/spike-timer";
  42. import empty from '@/components/empty'
  43. import goodsCell from './cell'
  44. import resource from '@/utils/resource'
  45. export default {
  46. components: { goodsCell, tabbar, empty, timer },
  47. data() {
  48. return{
  49. resource,
  50. isEmpty: false,
  51. list: [],
  52. list2: [],
  53. demo: null,
  54. subjectId: 0,
  55. targetTime: null,
  56. timerFlag: false,
  57. current: 1,
  58. size: 20,
  59. tab: 99,
  60. leftHeight: 0,
  61. rightHeight: 0,
  62. skipeType: '',
  63. startTime: ''
  64. }
  65. },
  66. onLoad(options) {
  67. this.subjectId = options.subjectId
  68. this.SpikeTime(options.subjectId)
  69. },
  70. mounted(){
  71. this.getSpikeList()
  72. },
  73. methods: {
  74. async SpikeTime(subjectId) {
  75. this.$service.mall.spikeTimer(subjectId).then(res => {
  76. this.targetTime = res.timeStatus === 1 ? res.startTime : res.endTime
  77. this.timerFlag = this.isTimeGreaterThanOneDay(this.targetTime),
  78. this.skipeType = res.timeStatus
  79. this.startTime = res.startTime
  80. })
  81. },
  82. getSpikeList(loading = false) {
  83. this.$service.mall.spikeList({
  84. current: this.current,
  85. size: this.size,
  86. subjectId: 1,
  87. sellOut: this.tab === 99 ? null : this.tab
  88. }, loading).then(async res => {
  89. const { records } = res
  90. if(this.current === 1) {
  91. this.demo = null
  92. this.list = []
  93. this.list2 = []
  94. this.leftHeight = 0
  95. this.rightHeight = 0
  96. }
  97. let array1 = [...this.list]
  98. let array2 = [...this.list2]
  99. for (let i = 0; i < records.length; i++) {
  100. let item = records[i]
  101. let height = await this.getHeight(item)
  102. if (this.leftHeight <= this.rightHeight) {
  103. array1.push(item)
  104. this.leftHeight += height
  105. } else {
  106. array2.push(item)
  107. this.rightHeight += height
  108. }
  109. }
  110. this.list = array1
  111. this.list2 = array2
  112. uni.stopPullDownRefresh()
  113. this.requesing = false
  114. this.isEmpty = this.list.length === 0
  115. })
  116. },
  117. getHeight(item) {
  118. this.demo = item
  119. const query = wx.createSelectorQuery()
  120. return new Promise((resolve, reject) => {
  121. this.$nextTick(() => {
  122. setTimeout(() => {
  123. query.select('#caculate-demo').boundingClientRect()
  124. query.exec((res) => {
  125. if (res[0]) {
  126. resolve(res[0].height)
  127. }
  128. resolve(100)
  129. })
  130. }, 5)
  131. })
  132. })
  133. },
  134. isTimeGreaterThanOneDay(time) {
  135. const now = new Date();
  136. const target = new Date(time);
  137. const diffMs = target.getTime() - now.getTime();
  138. const diffDays = diffMs / (1000 * 60 * 60 * 24);
  139. return diffDays > 1;
  140. },
  141. onChange(tab) {
  142. this.current = 1
  143. this.tab = tab
  144. this.getSpikeList()
  145. },
  146. onRefresh() {
  147. this.current = 1
  148. this.getSpikeList()
  149. }
  150. },
  151. onReachBottom() {
  152. let allData = [...this.list, ...this.list2]
  153. if (allData.length > 0 && allData.length % this.size === 0) {
  154. this.current += 1
  155. this.getSpikeList(true)
  156. }
  157. }
  158. }
  159. </script>
  160. <style>
  161. .title {
  162. color: #fff;
  163. }
  164. </style>
  165. <style lang="scss" scoped>
  166. .title {
  167. text-shadow: -2px 0 black, 0 2px black,
  168. 2px 0 black, 0 -2px black;
  169. }
  170. .spike_timer {
  171. position: relative;
  172. height: 144rpx;
  173. margin: 32rpx;
  174. padding-top: 22rpx;
  175. .img {
  176. position: absolute;
  177. top: 0;
  178. left: 0;
  179. width: 100%;
  180. height: 100%;
  181. z-index: -1;
  182. }
  183. .text {
  184. width: 80rpx;
  185. height: 100rpx;
  186. font-size: 36rpx;
  187. text-shadow: none;
  188. margin: 0 32rpx 22rpx 56rpx;
  189. }
  190. }
  191. .visible-hiddle-caculate {
  192. position: fixed;
  193. left: 0;
  194. right: 0;
  195. bottom: 0;
  196. z-index: -1;
  197. visibility: hidden
  198. }
  199. .refresh {
  200. position: fixed;
  201. bottom: 200rpx;
  202. right: 20rpx;
  203. width: 80rpx;
  204. height: 80rpx;
  205. z-index: 99;
  206. }
  207. </style>