mixin.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. import { mapGetters } from 'vuex'
  6. export const commonMixin = {
  7. name: 'discountList',
  8. mixins: [funMixin],
  9. data () {
  10. return {
  11. value: 100,
  12. productData: {},
  13. count: [],
  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. components: {
  35. Swiper,
  36. SwiperSlide
  37. },
  38. directives: {
  39. swiper: directive
  40. },
  41. computed: {
  42. ...mapGetters([
  43. 'discountNum'
  44. ]),
  45. },
  46. watch: {
  47. 'discountNum': {
  48. handler(newVal, oldVal) {
  49. this.getData()
  50. },
  51. deep: true
  52. }
  53. },
  54. mounted() {
  55. this.getData()
  56. },
  57. methods: {
  58. getData() {
  59. const _ = this
  60. if(_.componentContent.discountId){
  61. this.beforeGetData()
  62. var _url= ''
  63. if(this.typeId === 1){
  64. _url= `${api.getMinDiscount}?ids=${_.componentContent.discountId}`
  65. }
  66. if(this.typeId === 3){
  67. _url= `${api.getDiscounts}?shopId=${_.shopId}&ids=${_.componentContent.discountId}`
  68. }
  69. const params = {
  70. method: 'GET',
  71. url: _url,
  72. }
  73. this.sendReq(params, (res) => {
  74. _.afterGetData()
  75. if(res.data.length> 0){
  76. _.productData = res.data[0]
  77. // 只有进行中和未开始活动, 用倒计时
  78. if(_.productData.state !==2) {
  79. this.timer = setInterval(()=>{
  80. _.getTime(_.productData)
  81. }, 1000)
  82. }
  83. }
  84. },(err)=>{
  85. _.afterGetData()
  86. })
  87. } else {
  88. _.productData = {
  89. products:[]
  90. }
  91. }
  92. },
  93. getTime(info) {
  94. const date = new Date().getTime()
  95. const startTime = new Date(info.startTime.replace(/-/g,'/')).getTime()
  96. const endTime = new Date(info.endTime.replace(/-/g,'/')).getTime()
  97. if(startTime > date) {
  98. this.countDown(startTime-date,true) // 未开始
  99. } else {
  100. this.countDown(endTime-date) // 进行中
  101. }
  102. },
  103. countDown(time, isStart) {
  104. const fn = (v) => v < 10 ? `0${v}` : v
  105. const t = parseInt(time / 1000)
  106. const text = isStart ? '开始' : '结束'
  107. const hour = parseInt(t / 3600)
  108. const min = parseInt((t % 3600) / 60)
  109. const s = t % 60
  110. this.count = [text, fn(hour), fn(min), fn(s)]
  111. }
  112. },
  113. beforeDestroy() {
  114. clearInterval(this.timer)
  115. }
  116. }