index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. })
  47. const qrCodeUrl = ref('')
  48. const userType = 1 //默认为1
  49. const userId = uni.getStorageSync('userId')// 读取本地存储
  50. // 初始化二维码版本号
  51. const referrerQrCodeVersion = ref()
  52. // 打印用户信息,方便查看
  53. console.log('=== QRCode组件读取到的用户信息 ===')
  54. console.log('userType:', userType)
  55. console.log('userId:', userId)
  56. const generateQRCode = async () => {
  57. try {
  58. const response = await getUnlimitedQRCode({
  59. page: props.page,
  60. referrerType: userType,
  61. referrerId: userId,
  62. referrerQrCodeVersion: referrerQrCodeVersion.value,
  63. })
  64. // 处理返回的二进制数据
  65. if (response.data instanceof ArrayBuffer) {
  66. // 使用arrayBufferToBase64转换二进制数据为base64
  67. const base64Data = uni.arrayBufferToBase64(response.data)
  68. // 拼接Base64图片前缀
  69. qrCodeUrl.value = 'data:image/png;base64,' + base64Data
  70. } else {
  71. // 如果返回的不是ArrayBuffer,使用原有逻辑
  72. qrCodeUrl.value = 'data:image/jpeg;base64,' + response.data
  73. }
  74. } catch (error) {
  75. console.error('生成二维码失败:', error)
  76. uni.showToast({
  77. title: '生成二维码失败',
  78. icon: 'error',
  79. })
  80. }
  81. }
  82. const handleLongPress = async () => {
  83. try {
  84. await saveQRCodeToLocal(qrCodeUrl.value)
  85. } catch (error) {
  86. console.error('保存二维码失败:', error)
  87. }
  88. }
  89. onMounted(() => {
  90. generateQRCode()
  91. })
  92. </script>
  93. <style lang="scss" scoped>
  94. .qrcode-container {
  95. display: flex;
  96. flex-direction: column;
  97. align-items: center;
  98. justify-content: center;
  99. padding: 20rpx;
  100. .qrcode-box {
  101. display: flex;
  102. flex-direction: column;
  103. align-items: center;
  104. .qrcode-image {
  105. width: 400rpx;
  106. height: 400rpx;
  107. }
  108. .qrcode-tip {
  109. margin-top: 20rpx;
  110. font-size: 24rpx;
  111. color: #999;
  112. }
  113. }
  114. .qrcode-loading {
  115. padding: 40rpx;
  116. }
  117. }
  118. </style>