mixin.js 3.9 KB

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