wk-auth-img.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <image
  3. :src="imgSrc"
  4. :mode="mode"
  5. class="wk-auth-img"
  6. @error="errorCallback"
  7. @load="loadCallback"
  8. @click="showLargeImg" />
  9. </template>
  10. <script>
  11. /**
  12. * 带权限的图片
  13. * @author yxk
  14. * @property {String} src 图片地址
  15. * @property {String} mode image mode属性,默认 scaleToFill
  16. * @property {Boolean} preview 是否可以查看大图,默认 true
  17. */
  18. import request from '@/utils/request.js'
  19. import { BASE_URL_FN } from '@/config.js'
  20. import cookies from 'weapp-cookie'
  21. export default {
  22. name: 'WkAuthImg',
  23. props: {
  24. src: {
  25. type: String,
  26. default: ''
  27. },
  28. mode: {
  29. type: String,
  30. default: 'scaleToFill'
  31. },
  32. preview: {
  33. type: Boolean,
  34. default: true
  35. },
  36. isTemp: {
  37. type: Boolean,
  38. default: false
  39. }
  40. },
  41. data() {
  42. return {
  43. imgSrc: '',
  44. showImage: true
  45. }
  46. },
  47. watch: {
  48. src: {
  49. handler() {
  50. this.initSrc()
  51. },
  52. immediate: true
  53. }
  54. },
  55. methods: {
  56. getUrl() {
  57. if (this.isTemp) return this.src
  58. let url = BASE_URL_FN() + (this.src.startsWith('/') ? this.src.replace('/', '') : this.src)
  59. const token = uni.getStorageSync('token') || ''
  60. const appid = uni.getStorageSync('appid') || ''
  61. const arr = []
  62. if (token) {
  63. arr.push(`c=${token}`)
  64. }
  65. if (appid) {
  66. arr.push(`k=${appid}`)
  67. }
  68. if (arr.length > 0) {
  69. return url + '?' + arr.join('&')
  70. } else {
  71. return url
  72. }
  73. },
  74. initSrc() {
  75. if (this.$isEmpty(this.src)) {
  76. this.imgSrc = ''
  77. return
  78. }
  79. const imgDict = getApp().globalData.imgDict
  80. if (imgDict.hasOwnProperty(this.src)) {
  81. this.imgSrc = imgDict[this.src]
  82. return
  83. }
  84. if (
  85. this.src &&
  86. typeof this.src === 'string' &&
  87. this.src.startsWith('data:')) {
  88. this.imgSrc = this.src
  89. return
  90. }
  91. this.imgSrc = this.getUrl()
  92. this.showImage = false
  93. this.$nextTick(() => {
  94. this.showImage = true
  95. })
  96. },
  97. /**
  98. * 通过请求挂载图片
  99. */
  100. getSrcByRequest() {
  101. const imgDict = getApp().globalData.imgDict
  102. imgDict[this.src] = '' // 防止同一时间多次请求同一张图片浪费资源
  103. const appid = uni.getStorageSync('appid') || ''
  104. const header = {
  105. 'Admin-Token': uni.getStorageSync('token') || ''
  106. }
  107. if (appid) {
  108. header.k = appid
  109. }
  110. uni.request({
  111. url: this.getUrl(),
  112. header: header,
  113. responseType: 'arraybuffer',
  114. success: res => {
  115. console.log('image res: ', res)
  116. const contentType = res.header['Content-Type'] || res.header['content-type']
  117. this.imgSrc = `data:${contentType};base64,${uni.arrayBufferToBase64(res.data)}`
  118. imgDict[this.src] = this.imgSrc
  119. this.showImage = false
  120. this.$nextTick(() => {
  121. this.showImage = true
  122. })
  123. },
  124. fail: () => {
  125. this.imgSrc = ''
  126. delete imgDict[this.src]
  127. }
  128. })
  129. },
  130. errorCallback(evt) {
  131. console.log('error: ', evt)
  132. this.$emit('error', evt)
  133. },
  134. loadCallback(evt) {
  135. this.$emit('load', evt)
  136. },
  137. showLargeImg(evt) {
  138. if (!this.preview) return
  139. uni.previewImage({
  140. urls: [this.getUrl()],
  141. indicator: 'number'
  142. })
  143. // const imgDict = getApp().globalData.imgDict
  144. // uni.previewImage({
  145. // urls: [imgDict[this.src] || this.getUrl()],
  146. // indicator: 'number'
  147. // })
  148. }
  149. }
  150. }
  151. </script>
  152. <style scoped lang="scss">
  153. .wk-auth-img {
  154. width: 100%;
  155. height: 100%;
  156. }
  157. </style>