cache-image.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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="[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. console.log('cache-image', this.url);
  111. this.imgStyle = {
  112. width: this.width,
  113. height: this.height,
  114. borderRadius: this.radius,
  115. ...this.styles
  116. };
  117. // 查找获取图片缓存
  118. this.fnGetImageCache();
  119. },
  120. methods: {
  121. // 查找获取图片缓存
  122. async fnGetImageCache() {
  123. // #ifdef APP-PLUS
  124. var result = await imageCache.getImageCache(this.url, this.fileMd5);
  125. if (result) {
  126. this.src = result;
  127. } else {
  128. this.src = this.url;
  129. }
  130. // #endif
  131. // #ifndef APP-PLUS
  132. this.src = this.url;
  133. // #endif
  134. },
  135. fnOnLoad() {
  136. this.loadStatus = 'success';
  137. },
  138. fnOnError() {
  139. this.loadStatus = 'error';
  140. }
  141. }
  142. };
  143. </script>
  144. <style scoped lang="scss">
  145. .cache-image-wrap {
  146. width: 100%;
  147. height: 100%;
  148. }
  149. .img-loading,
  150. .img-error {
  151. display: flex;
  152. flex-direction: column;
  153. align-items: center;
  154. justify-content: center;
  155. box-sizing: border-box;
  156. background-color: #f2f2f2;
  157. }
  158. .img-load-icon {
  159. font-size: 36rpx;
  160. animation: xhRote 0.8s infinite linear;
  161. }
  162. .img-load-text {
  163. font-size: 28rpx;
  164. margin-top: 8rpx;
  165. color: inherit;
  166. }
  167. .img-error {
  168. font-size: 28rpx;
  169. }
  170. .img-err-icon {
  171. font-size: 36rpx;
  172. }
  173. @keyframes xhRote {
  174. 0% {
  175. transform: rotate(0deg);
  176. }
  177. 100% {
  178. transform: rotate(360deg);
  179. }
  180. }
  181. </style>