index.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <view class="qrcode-container">
  3. <view class="qrcode-box" v-if="qrCodeUrl">
  4. <image :src="qrCodeUrl" mode="aspectFit" class="qrcode-image" @longpress="handleLongPress"></image>
  5. <text class="qrcode-tip">长按保存二维码</text>
  6. </view>
  7. <view class="qrcode-loading" v-else>
  8. <up-loading-icon vertical text="生成中..."></up-loading-icon>
  9. </view>
  10. </view>
  11. </template>
  12. <script setup>
  13. import { ref, onMounted } from 'vue'
  14. import { getUnlimitedQRCode } from '@/api/qrcode'
  15. import { arrayBufferToBase64, saveQRCodeToLocal } from '@/utils/qrcode'
  16. const props = defineProps({
  17. scene: {
  18. type: String,
  19. required: true
  20. },
  21. page: {
  22. type: String,
  23. default: 'pages/index/index'
  24. },
  25. width: {
  26. type: Number,
  27. default: 430
  28. },
  29. autoColor: {
  30. type: Boolean,
  31. default: false
  32. },
  33. lineColor: {
  34. type: Object,
  35. default: () => ({ r: 0, g: 0, b: 0 })
  36. },
  37. isHyaline: {
  38. type: Boolean,
  39. default: false
  40. }
  41. })
  42. const qrCodeUrl = ref('')
  43. const generateQRCode = async () => {
  44. try {
  45. const response = await getUnlimitedQRCode({
  46. scene: props.scene,
  47. page: props.page,
  48. width: props.width,
  49. auto_color: props.autoColor,
  50. line_color: props.lineColor,
  51. is_hyaline: props.isHyaline
  52. })
  53. // 将ArrayBuffer转换为Base64图片
  54. qrCodeUrl.value = arrayBufferToBase64(response)
  55. } catch (error) {
  56. console.error('生成二维码失败:', error)
  57. uni.showToast({
  58. title: '生成二维码失败',
  59. icon: 'error'
  60. })
  61. }
  62. }
  63. const handleLongPress = async () => {
  64. try {
  65. await saveQRCodeToLocal(qrCodeUrl.value)
  66. } catch (error) {
  67. console.error('保存二维码失败:', error)
  68. }
  69. }
  70. onMounted(() => {
  71. generateQRCode()
  72. })
  73. </script>
  74. <style lang="scss" scoped>
  75. .qrcode-container {
  76. display: flex;
  77. flex-direction: column;
  78. align-items: center;
  79. justify-content: center;
  80. padding: 20rpx;
  81. .qrcode-box {
  82. display: flex;
  83. flex-direction: column;
  84. align-items: center;
  85. .qrcode-image {
  86. width: 400rpx;
  87. height: 400rpx;
  88. }
  89. .qrcode-tip {
  90. margin-top: 20rpx;
  91. font-size: 24rpx;
  92. color: #999;
  93. }
  94. }
  95. .qrcode-loading {
  96. padding: 40rpx;
  97. }
  98. }
  99. </style>