index.vue 3.5 KB

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