cache-image.vue 4.7 KB

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