cache-image.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <view class="">
  3. <view v-if="loadStatus == 'loading'" class="img-loading" :style="[imgStyle, loadStyle]">
  4. <!-- <text class="img-load-icon iconfont icon-loading"></text>
  5. <text class="img-load-text">{{ loadText }}</text> -->
  6. <image :src="loadingImgSrc" :style="[imgStyle]" mode="aspectFit"></image>
  7. </view>
  8. <view v-if="loadStatus == 'error'" class="img-error" :style="[imgStyle, loadErrStyle]">
  9. <text class="img-err-icon iconfont icon-exclamation-circle"></text>
  10. <text class="img-load-text">{{ loadErrText }}</text>
  11. </view>
  12. <image
  13. v-show="loadStatus == 'success'"
  14. :src="src"
  15. @load="fnOnLoad"
  16. @error="fnOnError"
  17. :lazy-load="lazyLoad"
  18. :style="[imgStyle]"
  19. :mode="mode"
  20. @click="$emit('on-click', url)"
  21. ></image>
  22. </view>
  23. </template>
  24. <script>
  25. import imageCache from '@/utils/imageCache.js';
  26. export default {
  27. name: 'cache-image',
  28. props: {
  29. url: {
  30. type: String,
  31. default: ''
  32. },
  33. lazyLoad: {
  34. type: Boolean,
  35. default: true
  36. },
  37. loadStyle: {
  38. type: Object,
  39. default() {
  40. return {
  41. backgroundColor: '#ffffff',
  42. color: '#333'
  43. };
  44. }
  45. },
  46. loadErrStyle: {
  47. type: Object,
  48. default() {
  49. return {
  50. color: 'rgba(244, 67, 54,1)'
  51. // backgroundColor: 'rgba(244, 67, 54,0.2)'
  52. };
  53. }
  54. },
  55. mode: {
  56. type: String,
  57. default: 'aspectFill'
  58. },
  59. loadText: {
  60. type: String,
  61. default: '加载中...'
  62. },
  63. loadErrText: {
  64. type: String,
  65. default: '加载失败'
  66. },
  67. fileMd5: {
  68. type: String,
  69. default: ''
  70. },
  71. styles: {
  72. type: Object,
  73. default() {
  74. return {};
  75. }
  76. },
  77. width: {
  78. type: String,
  79. default: '100%'
  80. },
  81. height: {
  82. type: String,
  83. default: '100%'
  84. },
  85. radius: {
  86. type: String,
  87. default: ''
  88. }
  89. },
  90. data() {
  91. return {
  92. imgStyle: {},
  93. src: '', // 图片地址
  94. loadStatus: 'loading'
  95. };
  96. },
  97. computed: {
  98. loadingImgSrc() {
  99. return getApp().globalData.loadingGifUrl;
  100. }
  101. },
  102. watch: {
  103. // 监听图片md5值的变化
  104. fileMd5(val) {
  105. // 查找获取图片缓存
  106. this.fnGetImageCache();
  107. }
  108. },
  109. created() {
  110. this.imgStyle = {
  111. width: this.width,
  112. height: this.height,
  113. borderRadius: this.radius,
  114. ...this.styles
  115. };
  116. // 查找获取图片缓存
  117. this.fnGetImageCache();
  118. },
  119. methods: {
  120. // 查找获取图片缓存
  121. async fnGetImageCache() {
  122. // #ifdef APP-PLUS
  123. var result = await imageCache.getImageCache(this.url, this.fileMd5);
  124. if (result) {
  125. this.src = result;
  126. } else {
  127. this.src = this.url;
  128. }
  129. // #endif
  130. // #ifndef APP-PLUS
  131. this.src = this.url;
  132. // #endif
  133. },
  134. fnOnLoad() {
  135. this.loadStatus = 'success';
  136. },
  137. fnOnError() {
  138. this.loadStatus = 'error';
  139. }
  140. }
  141. };
  142. </script>
  143. <style scoped lang="scss">
  144. .img-loading,
  145. .img-error {
  146. display: flex;
  147. flex-direction: column;
  148. align-items: center;
  149. justify-content: center;
  150. box-sizing: border-box;
  151. background-color: #f2f2f2;
  152. }
  153. .img-load-icon {
  154. font-size: 36rpx;
  155. animation: xhRote 0.8s infinite linear;
  156. }
  157. .img-load-text {
  158. font-size: 28rpx;
  159. margin-top: 8rpx;
  160. color: inherit;
  161. }
  162. .img-error {
  163. font-size: 28rpx;
  164. }
  165. .img-err-icon {
  166. font-size: 36rpx;
  167. }
  168. @keyframes xhRote {
  169. 0% {
  170. transform: rotate(0deg);
  171. }
  172. 100% {
  173. transform: rotate(360deg);
  174. }
  175. }
  176. </style>