index.vue 3.0 KB

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