cache-image.vue 3.7 KB

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