mixin.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { directive, Swiper, SwiperSlide } from 'vue-awesome-swiper'
  2. import {funMixin} from '../../config/mixin'
  3. import 'swiper/css/swiper.css'
  4. import api from '../../config/api'
  5. import { mapGetters } from 'vuex'
  6. export const commonMixin = {
  7. name: 'productList',
  8. mixins: [funMixin],
  9. props: {
  10. terminal: {
  11. type: Number,
  12. default: 4
  13. },
  14. typeId: {
  15. type: Number,
  16. default: 1
  17. },
  18. shopId: {
  19. type: Number,
  20. default: 0
  21. },
  22. componentContent: {
  23. type: Object
  24. }
  25. },
  26. components: {
  27. Swiper,
  28. SwiperSlide
  29. },
  30. directives: {
  31. swiper: directive
  32. },
  33. data () {
  34. return {
  35. productData: []
  36. }
  37. },
  38. mounted() {
  39. this.getData(true)
  40. },
  41. watch: {
  42. 'newProductNum': {
  43. handler(newVal, oldVal) {
  44. this.getData()
  45. },
  46. deep: true
  47. }
  48. },
  49. computed: {
  50. ...mapGetters([
  51. 'newProductNum'
  52. ]),
  53. swiper() {
  54. if(this.$refs.mySwiper){
  55. return this.$refs.mySwiper.$swiper
  56. }
  57. }
  58. },
  59. methods: {
  60. getData(isFirst) {
  61. const _ = this
  62. if (_.componentContent.productData.sourceType === '1') {
  63. if(_.componentContent.productData.productIdList && _.componentContent.productData.productIdList.length>0){
  64. this.beforeGetData()
  65. _.sendReq({
  66. url: `${api.getProducts}?page=1&pageSize=99&ids=${_.componentContent.productData.productIdList}`,
  67. method: 'GET'
  68. }, (proRes) => {
  69. _.afterGetData()
  70. _.productData = proRes.data.list
  71. if(isFirst){
  72. _.componentContent.productData.imgTextData = _.productData
  73. }
  74. _.$forceUpdate() // 刷新轮播图
  75. },(err)=>{
  76. _.afterGetData()
  77. })
  78. } else {
  79. _.productData = []
  80. }
  81. } else if(_.componentContent.productData.sourceType === '2'){
  82. if(_.componentContent.productData.categoryId) {
  83. this.beforeGetData()
  84. _.sendReq({
  85. url: `${api.getProducts}?page=1&pageSize=99&classifyId=${_.componentContent.productData.categoryId}`,
  86. method: 'GET'
  87. }, (proRes) => {
  88. _.afterGetData()
  89. _.productData = proRes.data.list
  90. if(isFirst){
  91. _.componentContent.productData.imgTextData = _.productData
  92. }
  93. _.$forceUpdate() // 刷新轮播图
  94. // _.swiper.update()
  95. },(err)=>{
  96. _.afterGetData()
  97. })
  98. } else {
  99. _.productData = {
  100. products:[]
  101. }
  102. }
  103. }
  104. },
  105. }
  106. }