多租户商城-商户小程序端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.6 KiB

2 years ago
  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 = (value.stockNumber/value.total*100).toFixed(2)
  58. return value;
  59. });
  60. // 只有进行中和未开始活动, 用倒计时
  61. this.timer = setInterval(()=>{
  62. _.getTime(_.productData)
  63. }, 1000)
  64. }
  65. })
  66. }
  67. if(this.typeId === 3){
  68. const params = {
  69. method: 'GET',
  70. url: `${api.getSeckills}?shopId=${_.shopId}&ids=${_.componentContent.shopSeckillId}`,
  71. }
  72. this.sendReq(params, (res) => {
  73. if(res.data.length> 0){
  74. _.productData = res.data[0]
  75. _.productData.products.map(function(value){
  76. value.sliderVal = (value.stockNumber/value.total*100).toFixed(2)
  77. return value;
  78. });
  79. // 只有进行中和未开始活动, 用倒计时
  80. if(_.productData.state !==2) {
  81. this.timer = setInterval(()=>{
  82. _.getTime(_.productData)
  83. }, 1000)
  84. }
  85. } else {
  86. _.productData = {
  87. products:[]
  88. }
  89. }
  90. })
  91. }
  92. } else {
  93. _.productData = {
  94. products:[]
  95. }
  96. }
  97. },
  98. getTime(info) {
  99. const date = new Date().getTime()
  100. let startTime = ''
  101. let endTime = ''
  102. if(this.typeId === 1){
  103. startTime = new Date(info.startTime.replace(/-/g,'/')).getTime()
  104. endTime = new Date(info.endTime.replace(/-/g,'/')).getTime()
  105. } else {
  106. startTime = new Date(info.effectiveStart.replace(/-/g,'/')).getTime()
  107. endTime = new Date(info.effectiveEnd.replace(/-/g,'/')).getTime()
  108. }
  109. if(date > endTime){
  110. this.state = 2
  111. } else if(startTime > date) {
  112. this.state = 0
  113. this.countDown(startTime-date) // 未开始
  114. } else {
  115. this.state = 1
  116. this.countDown(endTime-date) // 进行中
  117. }
  118. },
  119. countDown(time) {
  120. const fn = (v) => v < 10 ? `0${v}` : v
  121. const t = parseInt(time / 1000)
  122. const text = this.state == 0 ? '开始' : '结束'
  123. const hour = parseInt(t / 3600)
  124. const min = parseInt((t % 3600) / 60)
  125. const s = t % 60
  126. // console.log(min, '分',t)
  127. this.count = [text, fn(hour), fn(min), fn(s)]
  128. // console.log(text, fn(hour), fn(min), fn(s))
  129. }
  130. },
  131. beforeDestroy() {
  132. clearInterval(this.timer)
  133. }
  134. }