mixin.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // import { directive, Swiper, SwiperSlide } from 'vue-awesome-swiper'
  2. // import 'swiper/css/swiper.css'
  3. import api from '../../config/api'
  4. import {funMixin} from '../../config/mixin'
  5. export const commonMixin = {
  6. name: 'discountList',
  7. mixins: [funMixin],
  8. data () {
  9. return {
  10. value: 100,
  11. productData: {
  12. products: []
  13. },
  14. count: [],
  15. timer: null
  16. }
  17. },
  18. props: {
  19. terminal: {
  20. type: Number,
  21. default: 4
  22. },
  23. typeId: {
  24. type: Number,
  25. default: 1
  26. },
  27. shopId: {
  28. type: Number,
  29. default: 0
  30. },
  31. componentContent: {
  32. type: Object
  33. }
  34. },
  35. // components: {
  36. // Swiper,
  37. // SwiperSlide
  38. // },
  39. // directives: {
  40. // swiper: directive
  41. // },
  42. watch: {
  43. 'componentContent': {
  44. handler(newVal, oldVal) {
  45. this.getData()
  46. },
  47. deep: true
  48. }
  49. },
  50. created() {
  51. this.getData()
  52. },
  53. methods: {
  54. getData() {
  55. const _ = this
  56. if(_.componentContent.discountId){
  57. var _url= ''
  58. if(this.typeId === 1){
  59. _url= `${api.getMinDiscount}?ids=${_.componentContent.discountId}`
  60. }
  61. if(this.typeId === 3){
  62. _url= `${api.getDiscounts}?shopId=${_.shopId}&ids=${_.componentContent.discountId}`
  63. }
  64. const params = {
  65. method: 'GET',
  66. url: _url,
  67. }
  68. this.sendReq(params, (res) => {
  69. if(res.data.length> 0){
  70. _.productData = res.data[0]
  71. // 只有进行中和未开始活动, 用倒计时
  72. if(_.productData.state !==2) {
  73. this.timer = setInterval(()=>{
  74. _.getTime(_.productData)
  75. }, 1000)
  76. }
  77. }
  78. })
  79. } else {
  80. _.productData = {
  81. products:[]
  82. }
  83. }
  84. },
  85. getTime(info) {
  86. const date = new Date().getTime()
  87. const startTime = new Date(info.startTime.replace(/-/g,'/')).getTime()
  88. const endTime = new Date(info.endTime.replace(/-/g,'/')).getTime()
  89. if(startTime > date) {
  90. this.countDown(startTime-date,true) // 未开始
  91. } else {
  92. this.countDown(endTime-date) // 进行中
  93. }
  94. },
  95. countDown(time, isStart) {
  96. const fn = (v) => v < 10 ? `0${v}` : v
  97. const t = parseInt(time / 1000)
  98. const text = isStart ? '开始' : '结束'
  99. const hour = parseInt(t / 3600)
  100. const min = parseInt((t % 3600) / 60)
  101. const s = t % 60
  102. this.count = [text, fn(hour), fn(min), fn(s)]
  103. }
  104. },
  105. beforeDestroy() {
  106. clearInterval(this.timer)
  107. }
  108. }