美文网首页
微信小程序二维码展示组件

微信小程序二维码展示组件

作者: Sasoli | 来源:发表于2023-03-20 17:16 被阅读0次
<template>
  <canvas
    v-if="!state.canvasImg"
    id="myQrcode"
    type="2d"
    style="width: 100%;height: 100%;"
  />
  <image
    v-else
    :src="state.canvasImg"
    mode="scaleToFill"
    style="width: 100%;height: 100%;"
  />
</template>
<script setup lang="ts">
import {
  createSelectorQuery,
  canvasToTempFilePath,
  getSetting,
  authorize,
  saveImageToPhotosAlbum,
  showToast,
  openSetting,
  showModal,
} from '@tarojs/taro';
import { ref, watch, reactive } from 'vue';
import drawQrcode from 'weapp-qrcode-canvas-2d';
import { debounce } from '@/utils';

const nodeCanvas = ref(null);
const emit = defineEmits(['update:tempFilePath']);
const state = reactive({
  canvasImg: '',
});

const handleWriteFile = () => {
  // 存入相册
  saveImageToPhotosAlbum({
    filePath: state.canvasImg,
    success: () => {
      showToast({
        title: '保存成功',
        icon: 'none',
      });
    },
  });
};

const saveQrCode = () => {
  getSetting({
    success: ({ authSetting }) => {
      // 没有权限则申请
      if (!authSetting['scope.writePhotosAlbum']) {
        authorize({
          scope: 'scope.writePhotosAlbum',
          success: () => {
            handleWriteFile();
          },
          fail: (err) => {
            showModal({
              content: '无保存权限,请开启',
              success: (res) => {
                if (res.confirm) {
                  openSetting();
                }
              },
            });
            console.log(err);
          },
        });
      } else {
        handleWriteFile();
      }
    },
  });
};

interface Props {
  url: string;
  img?: string;
  options?: {
    status: Number | null;
  };
}
const props = withDefaults(defineProps<Props>(), {
  url: '',
  img: '',
  options: () => ({
    status: null,
  }),
});

function emitTempFilePath(canvas) {
  // 获取临时路径
  canvasToTempFilePath({
    canvas,
    destWidth: 1000,
    destHeight: 1000,
    success(res) {
      state.canvasImg = res.tempFilePath;
      emit('update:tempFilePath', res.tempFilePath);
    },
    fail(res) {
      console.error(res);
    },
  });
}

function makeQrCode() {
  state.canvasImg = '';
  const query = createSelectorQuery();
  query
    .select('#myQrcode')
    .fields({
      node: true,
      size: true,
    })
    .exec((res) => {
      const canvas = res[0].node;
      nodeCanvas.value = canvas;
      if (props.img) {
        const img = canvas.createImage();
        img.src = props.img;

        img.onload = () => {
          const options = {
            canvas,
            canvasId: 'myQrcode',
            correctLevel: 1,
            text: props.url,
            ...props.options,
            image: {
              imageResource: img,
              width: 50, // 建议不要设置过大,以免影响扫码
              height: 50, // 建议不要设置过大,以免影响扫码
              round: false,
            },
          };

          drawQrcode(options);
          emitTempFilePath(canvas);
        };
      } else {
        drawQrcode({
          canvas,
          canvasId: 'myQrcode',
          correctLevel: 1,
          text: props.url,
          ...props.options,
        });
        emitTempFilePath(canvas);
      }
    });
}

// 观察属性
watch(props, debounce(makeQrCode, 400), {
  immediate: true, // 立即执行
  deep: true, // 深度监听
});

defineExpose({
  saveQrCode,
});
</script>

使用

<QrImg
  :url="state.codeInfo.buyerCode"
  :img="codeLogo"
/>

相关文章

  • official-account组件

    official-account是公众号关注组件,用于微信小程序中展示,用户在小程序中可以看到公众号的展示信息,如...

  • 小程序好文集合

    组件篇 微信小程序:组件实践 整体梳理 微信小程序开发深入解读

  • 微信小程序-canvas压缩图片使用入门

    微信小程序canvas组件官方文档 微信小程序canvas组件官方文档canvas API canvas组件介绍 ...

  • 微信小程序日历组件开发

    # 微信小程序日历组件开发 微信小程序基础知识 微信小程序 框架介绍 组件文档 上述是开发小程序的基本知识。 今天...

  • 微信小程序基础

    微信小程序介绍微信小程序开发工具的使用微信小程序框架文件微信小程序逻辑层微信小程序视图层微信小程序组件介绍微信小程...

  • 微信小程序与支付宝小程序API差异

    1、组件差异 (1) image 在微信小程序中,image组件中,可以嵌套组件,用绝对定位可以展示出来,也可以放...

  • 微信小程序组件探究和应用

    把玩微信小程序组件 微信小程序把玩《二》:页面生命周期,模块化,数据绑定,view组件 微信小程序把玩《三》:sc...

  • 天坛祈年殿木作营造

    (微信小程序展示)

  • 微信小程序性能优化实践

    历史回顾: 微信小程序自定义组件 - 表格组件来啦 通过微信小程序来实现 “钉钉打卡” 记一次微信小程序项目分包,...

  • 微信小程序组件化开发

    什么是组件化开发 微信小程序的组件和Vue的组件非常相似。 在微信小程序中有很多内置组件,比如button vie...

网友评论

      本文标题:微信小程序二维码展示组件

      本文链接:https://www.haomeiwen.com/subject/qrxprdtx.html