mixin.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import api from '../../config/api'
  2. import { funMixin } from '../../config/mixin'
  3. export const commonMixin = {
  4. name: 'spikeList',
  5. mixins: [funMixin],
  6. data() {
  7. return {
  8. productData: {
  9. products: [],
  10. },
  11. count: [],
  12. state: 0,
  13. timer: null,
  14. }
  15. },
  16. props: {
  17. terminal: {
  18. type: Number,
  19. default: 4,
  20. },
  21. typeId: {
  22. type: Number,
  23. default: 1,
  24. },
  25. shopId: {
  26. type: Number,
  27. default: 0,
  28. },
  29. componentContent: {
  30. type: Object,
  31. },
  32. },
  33. watch: {
  34. componentContent: {
  35. handler(newVal, oldVal) {
  36. this.getData()
  37. },
  38. deep: true,
  39. },
  40. },
  41. created() {
  42. this.getData()
  43. },
  44. methods: {
  45. getData() {
  46. const _ = this
  47. if (_.componentContent.shopSeckillId) {
  48. if (this.typeId === 1) {
  49. const params = {
  50. method: 'GET',
  51. url: `${api.getPlatformSeckills}?ids=${_.componentContent.shopSeckillId}`,
  52. }
  53. this.sendReq(params, (res) => {
  54. if (res.data.length > 0) {
  55. _.productData = res.data[0]
  56. _.productData.products.map(function (value) {
  57. value.sliderVal = (
  58. (value.stockNumber / value.total) *
  59. 100
  60. ).toFixed(2)
  61. return value
  62. })
  63. // 只有进行中和未开始活动, 用倒计时
  64. this.timer = setInterval(() => {
  65. _.getTime(_.productData)
  66. }, 1000)
  67. }
  68. })
  69. }
  70. if (this.typeId === 3) {
  71. const params = {
  72. method: 'GET',
  73. url: `${api.getSeckills}?shopId=${_.shopId}&ids=${_.componentContent.shopSeckillId}`,
  74. }
  75. this.sendReq(params, (res) => {
  76. if (res.data.length > 0) {
  77. _.productData = res.data[0]
  78. _.productData.products.map(function (value) {
  79. value.sliderVal = (
  80. (value.stockNumber / value.total) *
  81. 100
  82. ).toFixed(2)
  83. return value
  84. })
  85. // 只有进行中和未开始活动, 用倒计时
  86. if (_.productData.state !== 2) {
  87. this.timer = setInterval(() => {
  88. _.getTime(_.productData)
  89. }, 1000)
  90. }
  91. } else {
  92. _.productData = {
  93. products: [],
  94. }
  95. }
  96. })
  97. }
  98. } else {
  99. _.productData = {
  100. products: [],
  101. }
  102. }
  103. },
  104. getTime(info) {
  105. const date = new Date().getTime()
  106. let startTime = ''
  107. let endTime = ''
  108. if (this.typeId === 1) {
  109. startTime = new Date(info.startTime.replace(/-/g, '/')).getTime()
  110. endTime = new Date(info.endTime.replace(/-/g, '/')).getTime()
  111. } else {
  112. startTime = new Date(info.effectiveStart.replace(/-/g, '/')).getTime()
  113. endTime = new Date(info.effectiveEnd.replace(/-/g, '/')).getTime()
  114. }
  115. if (date > endTime) {
  116. this.state = 2
  117. } else if (startTime > date) {
  118. this.state = 0
  119. this.countDown(startTime - date) // 未开始
  120. } else {
  121. this.state = 1
  122. this.countDown(endTime - date) // 进行中
  123. }
  124. },
  125. countDown(time) {
  126. const fn = (v) => (v < 10 ? `0${v}` : v)
  127. const t = parseInt(time / 1000)
  128. const text = this.state == 0 ? '开始' : '结束'
  129. const hour = parseInt(t / 3600)
  130. const min = parseInt((t % 3600) / 60)
  131. const s = t % 60
  132. this.count = [text, fn(hour), fn(min), fn(s)]
  133. },
  134. },
  135. beforeDestroy() {
  136. clearInterval(this.timer)
  137. },
  138. }