fr-svg.uvue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <view class="__flower-svg" :style="{width:`${width}rpx`,height:`${height}rpx`}">
  3. <!-- #ifdef APP -->
  4. <fr-wvjs type="svg" :isCache="isCache" :resourceId="uuid" :resource="src" />
  5. <image @click="onClick" @longpress="onLongpress" :src="getStoreState(uuid,isCache)"
  6. :style="{width:`${width}rpx`,height:`${height}rpx`}" :mode="mode" :fade-show="fadeShow"
  7. :lazy-load="lazyLoad" :show-menu-by-longpress="showMenuByLongpress" :draggable="draggable"
  8. @error="changeError" @load="changeLoad" />
  9. <!-- #endif -->
  10. <!-- #ifndef APP -->
  11. <image @click="onClick" @longpress="onLongpress"
  12. :src="`data:image/svg+xml;charset=utf-8,${encodeURIComponent(src)}`"
  13. :style="{width:`${width}rpx`,height:`${height}rpx`}" :mode="mode" :fade-show="fadeShow"
  14. :lazy-load="lazyLoad" :show-menu-by-longpress="showMenuByLongpress" :draggable="draggable"
  15. @error="changeError" @load="changeLoad" />
  16. <!-- #endif -->
  17. </view>
  18. </template>
  19. <script>
  20. import { mixinStore } from "@/uni_modules/flower-api/uni-app-x";
  21. /**
  22. * flower-svg svg组件
  23. * @description 一款适用于 uni-app / uni-app-x 的 SVG 组件。全端全版本适配。
  24. * @tutorial
  25. * @property {String} uuid 唯一标识uuid,必填
  26. * @property {String} src svg资源参数
  27. * @property {Number} width 宽度
  28. * @property {Number} height 高度
  29. * @property {String} mode 图片裁剪、缩放的模式,默认值:scaleToFill
  30. * @property {Boolean} fadeShow 图片显示动画效果,默认值:false,App-nvue 2.3.4+ Android有效、uni-app-x
  31. * @property {Boolean} lazyLoad 图片懒加载。只针对page与scroll-view下的image有效,默认值:false,微信小程序、百度小程序、抖音小程序、飞书小程序
  32. * @property {Boolean} showMenuByLongpress 开启长按图片显示识别小程序码菜单,默认值:false,微信小程序2.7.0
  33. * @property {Boolean} draggable 是否能拖动图片,默认值:false,H5 3.1.1+、App(iOS15+)
  34. * @property {Boolean} isCache 是否图片缓存,默认值:false,nvue-app、uvue-app
  35. * @event {Function} click 点击事件
  36. * @event {Function} longpress 长按事件
  37. * @event {Function} error 图片加载错误时触发,event.detail = { errMsg }
  38. * @event {Function} load 图片加载完成时触发,event.detail = { width: '图片宽度px', height: '图片高度px' }
  39. */
  40. export default {
  41. mixins: [mixinStore],
  42. emits: ['click', 'longpress', 'error', 'load'],
  43. props: {
  44. uuid: {
  45. type: String,
  46. default: ""
  47. },
  48. src: {
  49. type: String,
  50. default: ""
  51. },
  52. width: {
  53. type: Number,
  54. default: 48
  55. },
  56. height: {
  57. type: Number,
  58. default: 48
  59. },
  60. mode: {
  61. type: String,
  62. default: "scaleToFill"
  63. },
  64. fadeShow: {
  65. type: Boolean,
  66. default: false
  67. },
  68. lazyLoad: {
  69. type: Boolean,
  70. default: false
  71. },
  72. showMenuByLongpress: {
  73. type: Boolean,
  74. default: false
  75. },
  76. draggable: {
  77. type: Boolean,
  78. default: false
  79. },
  80. isCache: {
  81. type: Boolean,
  82. default: false
  83. }
  84. },
  85. methods: {
  86. onClick() {
  87. this.$emit("click");
  88. },
  89. onLongpress() {
  90. this.$emit("longpress");
  91. },
  92. changeError(event : UniImageErrorEvent) {
  93. this.$emit("error", event)
  94. },
  95. changeLoad(event : UniImageLoadEvent) {
  96. this.$emit("load", event)
  97. }
  98. }
  99. }
  100. </script>
  101. <style scoped>
  102. .__flower-svg {
  103. display: flex;
  104. }
  105. </style>