zw-swiper.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <swiper class="swiper-box" :style="{'height': height + 'rpx'}" :interval="2000"
  3. :disable-touch="!list || list.length <= 1" circular :indicator-dots="list && list.length > 1"
  4. indicator-active-color="#fff" @change="changeSwoper">
  5. <swiper-item class="swiper-item" v-for="(item , index) in list">
  6. <image @click.stop="$openPage(item)" :src="item.image" mode="scaleToFill"
  7. :style="{'border-radius':borderRadius + 'rpx'}"></image>
  8. </swiper-item>
  9. </swiper>
  10. </template>
  11. <script>
  12. import {
  13. getBannerList
  14. } from "@/api/government.js"
  15. export default {
  16. name: "zw-swiper",
  17. props: {
  18. borderRadius: {
  19. type: Number,
  20. default: 0
  21. },
  22. autoLoad: {
  23. type: Boolean,
  24. default: true
  25. },
  26. height: {
  27. type: Number,
  28. default: 200
  29. },
  30. StorageKey: {
  31. type: String,
  32. default: ""
  33. },
  34. keyword: {
  35. type: String,
  36. default: ""
  37. },
  38. },
  39. data() {
  40. return {
  41. list: []
  42. };
  43. },
  44. created() {
  45. },
  46. methods: {
  47. init() {
  48. this.getBanner(this.keyword)
  49. },
  50. // 获取 banner
  51. getBanner(keyword) {
  52. if (!keyword) return
  53. getBannerList(keyword).then(res => {
  54. const data = res.data || []
  55. this.list = data.map(el => {
  56. el.image = this.$getImgPath(el.image)
  57. return el
  58. });
  59. console.log()
  60. this.$emit('getBanner', data)
  61. if (this.StorageKey) {
  62. uni.setStorage({
  63. key: this.StorageKey,
  64. data: this.list,
  65. success: function() {
  66. console.log('success');
  67. }
  68. });
  69. }
  70. });
  71. },
  72. // 点击轮播图
  73. openSwiper(e) {
  74. this.$openPage(this.list[e])
  75. },
  76. changeSwoper(e) {
  77. if (e.detail.source == "touch") {
  78. this.$emit("disabledScoll")
  79. }
  80. }
  81. },
  82. watch: {
  83. keyword: {
  84. handler: function(newKwd, oldKod) {
  85. if (oldKod === undefined && this.StorageKey) {
  86. uni.getStorage({
  87. key: this.StorageKey,
  88. success: res => {
  89. this.list = res.data || []
  90. }
  91. });
  92. }
  93. if (this.autoLoad && newKwd && newKwd !== oldKod) this.getBanner(newKwd)
  94. },
  95. immediate: true,
  96. deep: true
  97. }
  98. }
  99. }
  100. </script>
  101. <style lang="scss" scoped>
  102. .swiper-box {
  103. width: 100%;
  104. .swiper-item {
  105. width: 100%;
  106. height: 100%;
  107. image {
  108. width: 100%;
  109. height: 100%;
  110. }
  111. }
  112. }
  113. </style>