index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <view class="qrcode-container">
  3. <view class="qrcode-box" v-if="qrCodeUrl">
  4. <image
  5. :src="qrCodeUrl"
  6. mode="aspectFit"
  7. class="qrcode-image"
  8. @longpress="handleLongPress"
  9. ></image>
  10. <text class="qrcode-tip">长按保存二维码</text>
  11. </view>
  12. <view class="qrcode-loading" v-else>
  13. <up-loading-icon vertical text="生成中..."></up-loading-icon>
  14. </view>
  15. </view>
  16. </template>
  17. <script setup>
  18. import { ref, onMounted, watch } from 'vue'
  19. import { getUnlimitedQRCode } from '@/api/qrcode'
  20. import { arrayBufferToBase64, saveQRCodeToLocal } from '@/utils/qrcode'
  21. const props = defineProps({
  22. scene: {
  23. type: String,
  24. required: true,
  25. },
  26. page: {
  27. type: String,
  28. default: 'pages/login',
  29. },
  30. width: {
  31. type: Number,
  32. default: 430,
  33. },
  34. autoColor: {
  35. type: Boolean,
  36. default: false,
  37. },
  38. lineColor: {
  39. type: Object,
  40. default: () => ({ r: 0, g: 0, b: 0 }),
  41. },
  42. isHyaline: {
  43. type: Boolean,
  44. default: false,
  45. },
  46. version: {
  47. type: String,
  48. default: '',
  49. },
  50. })
  51. const qrCodeUrl = ref('')
  52. const userType = uni.getStorageSync('userType') || 1 // 读取本地存储,默认为1
  53. const userId = uni.getStorageSync('userId')// 读取本地存储
  54. // 打印用户信息,方便查看
  55. console.log('=== QRCode组件读取到的用户信息 ===')
  56. console.log('userType:', userType)
  57. console.log('userId:', userId)
  58. const generateQRCode = async () => {
  59. try {
  60. const response = await getUnlimitedQRCode({
  61. page: props.page,
  62. referrerType: userType,
  63. referrerId: userId,
  64. version: props.version, // 使用传入的版本号
  65. })
  66. // 处理返回的二进制数据
  67. if (response.data instanceof ArrayBuffer) {
  68. // 使用arrayBufferToBase64转换二进制数据为base64
  69. const base64Data = uni.arrayBufferToBase64(response.data)
  70. // 拼接Base64图片前缀
  71. qrCodeUrl.value = 'data:image/png;base64,' + base64Data
  72. } else {
  73. // 如果返回的不是ArrayBuffer,使用原有逻辑
  74. qrCodeUrl.value = 'data:image/jpeg;base64,' + response.data
  75. }
  76. } catch (error) {
  77. console.error('生成二维码失败:', error)
  78. uni.showToast({
  79. title: '生成二维码失败',
  80. icon: 'error',
  81. })
  82. }
  83. }
  84. const handleLongPress = async () => {
  85. try {
  86. await saveQRCodeToLocal(qrCodeUrl.value)
  87. } catch (error) {
  88. console.error('保存二维码失败:', error)
  89. }
  90. }
  91. onMounted(() => {
  92. generateQRCode()
  93. })
  94. </script>
  95. <style lang="scss" scoped>
  96. .qrcode-container {
  97. display: flex;
  98. flex-direction: column;
  99. align-items: center;
  100. justify-content: center;
  101. padding: 20rpx;
  102. .qrcode-box {
  103. display: flex;
  104. flex-direction: column;
  105. align-items: center;
  106. .qrcode-image {
  107. width: 400rpx;
  108. height: 400rpx;
  109. }
  110. .qrcode-tip {
  111. margin-top: 20rpx;
  112. font-size: 24rpx;
  113. color: #999;
  114. }
  115. }
  116. .qrcode-loading {
  117. padding: 40rpx;
  118. }
  119. }
  120. </style>