wk-signature-view.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <view class="wk-sigature-view" @click="handleToPreview">
  3. <image
  4. :style="{
  5. width: imgStyle.width + 'px',
  6. height: imgStyle.height + 'px'
  7. }"
  8. :mode="mode"
  9. :src="fileTempUrl"
  10. class="img" />
  11. </view>
  12. </template>
  13. <script>
  14. import { FileQueryOneByBatch } from 'API/file.js'
  15. import { BASE_URL_FN } from '@/config.js'
  16. export default {
  17. name: 'WkSignatureView',
  18. props: {
  19. batchId: {
  20. type: String,
  21. required: true
  22. },
  23. mode: {
  24. type: String,
  25. default: 'scaleToFill'
  26. },
  27. preview: {
  28. type: Boolean,
  29. default: true
  30. }
  31. },
  32. data() {
  33. return {
  34. fileData: null,
  35. fileTempUrl: '',
  36. imgStyle: {
  37. width: 0,
  38. height: 0
  39. }
  40. }
  41. },
  42. watch: {
  43. batchId: {
  44. handler() {
  45. if (this.batchId) {
  46. this.$nextTick(() => {
  47. this.getStyle()
  48. })
  49. }
  50. },
  51. deep: true,
  52. immediate: true
  53. }
  54. },
  55. methods: {
  56. getStyle() {
  57. const that = this
  58. uni.createSelectorQuery()
  59. .in(this)
  60. .select('.wk-sigature-view')
  61. .boundingClientRect(data => {
  62. console.log('wk-sigature-view', data)
  63. that.imgStyle = {
  64. width: data.width,
  65. height: data.width / 2.6
  66. }
  67. that.getImage()
  68. })
  69. .exec()
  70. },
  71. getUrl(path) {
  72. let url = BASE_URL_FN() + (path.startsWith('/') ? path.replace('/', '') : path)
  73. const token = uni.getStorageSync('token') || ''
  74. const appid = uni.getStorageSync('appid') || ''
  75. const arr = []
  76. if (token) {
  77. arr.push(`c=${token}`)
  78. }
  79. if (appid) {
  80. arr.push(`k=${appid}`)
  81. }
  82. arr.push(`t=${new Date().getTime()}`)
  83. if (arr.length > 0) {
  84. return url + '?' + arr.join('&')
  85. } else {
  86. return url
  87. }
  88. },
  89. /**
  90. * 通过请求挂载图片
  91. */
  92. getImage() {
  93. const that = this
  94. FileQueryOneByBatch({
  95. batchId: this.batchId
  96. }).then(res => {
  97. if (res) {
  98. this.fileData = res
  99. this.fileTempUrl = this.getUrl(res.url)
  100. }
  101. }).catch(() => {
  102. this.fileData = null
  103. })
  104. },
  105. /**
  106. * 预览签名
  107. */
  108. handleToPreview() {
  109. if (!this.preview) return
  110. console.log('preview: ', this.fileTempUrl)
  111. uni.previewImage({
  112. urls: [this.fileTempUrl]
  113. })
  114. }
  115. }
  116. }
  117. </script>
  118. <style scoped lang="scss">
  119. .wk-sigature-view {
  120. width: 100%;
  121. height: 100%;
  122. .img {
  123. vertical-align: middle;
  124. }
  125. }
  126. </style>