index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 } 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/index/index',
  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 generateQRCode = async () => {
  49. try {
  50. const response = await getUnlimitedQRCode({
  51. // scene: props.scene,
  52. // page: props.page,
  53. // width: props.width,
  54. // auto_color: props.autoColor,
  55. // line_color: props.lineColor,
  56. // is_hyaline: props.isHyaline
  57. referrerType: 1,
  58. referrerId: 130,
  59. page: 'pages/index/index',
  60. })
  61. // 拼接Base64图片前缀
  62. qrCodeUrl.value = 'data:image/jpeg;base64,' + response.data
  63. } catch (error) {
  64. console.error('生成二维码失败:', error)
  65. uni.showToast({
  66. title: '生成二维码失败',
  67. icon: 'error',
  68. })
  69. }
  70. }
  71. const handleLongPress = async () => {
  72. try {
  73. await saveQRCodeToLocal(qrCodeUrl.value)
  74. } catch (error) {
  75. console.error('保存二维码失败:', error)
  76. }
  77. }
  78. onMounted(() => {
  79. generateQRCode()
  80. })
  81. </script>
  82. <style lang="scss" scoped>
  83. .qrcode-container {
  84. display: flex;
  85. flex-direction: column;
  86. align-items: center;
  87. justify-content: center;
  88. padding: 20rpx;
  89. .qrcode-box {
  90. display: flex;
  91. flex-direction: column;
  92. align-items: center;
  93. .qrcode-image {
  94. width: 400rpx;
  95. height: 400rpx;
  96. }
  97. .qrcode-tip {
  98. margin-top: 20rpx;
  99. font-size: 24rpx;
  100. color: #999;
  101. }
  102. }
  103. .qrcode-loading {
  104. padding: 40rpx;
  105. }
  106. }
  107. </style>