vue3在微信登录时使用嵌入的方式显示二维码-微信开放平台

先看文档

https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html
官方文档有两种方法

原因

  1. 由于前后端分离,使用后端返回url虽然简单,但可能会遇到跨域问题
  2. 那就使用前端嵌入二维码
  • 好处是又方便又快又好管理
  • 坏处是文档中的WxLogin正常引入js会没有,以下主要解决这个问题
<template>
        <div class='inp' v-show='login_type === 1'>
            <div>
                <div id='login_container'></div>
            </div>
        </div>
    </div>
</template>

<script >
    //#region 微信登录
    // 登录二维码地址
    watch(login_type, (newValue) => {
        // 监听,如果登录方式切换为微信,执行微信登录
        if (newValue === 1) {
            loginWechat();
        }
    });
    onMounted(async () => {
        // 以此种方式加载js,后面便可使用WxLogin.如果WxLogin在IDE显示红色,可忽视
        const s = document.createElement('script');
        s.src = 'https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js';
        s.async = true;
        nextTick(() => {
            document.body.appendChild(s);
        });
    });
    const loginWechat = async () => {
        // 微信登录, 获取并显示二维码
        const qrcode = new window.WxLogin({
            self_redirect:true,
            id:'login_container',
            appid: '你的id',
            scope: 'snsapi_login',
            redirect_uri: 'http://你的回调地址',
            state: 'abb',
            style: '',
            stylelite: 1,   // 自己测试一下0和1,看想要哪种显示方式
            fast_login: 1
        });
    };
    //#endregion
</script>

完成以上便可以vue3中正确嵌入并渲染微信登录二维码

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中展示微信支付的二维码,通常会用到`vant-wechat`这个第三方库,它提供了方便快捷的微信支付接口和组件。以下是基本步骤: 1. 安装依赖: 首先,在项目中安装`vant-wechat`库,可以通过npm或yarn进行安装: ```bash npm install vant-wechat # 或者 yarn add vant-wechat ``` 2. 引入并配置微信支付: 在你的Vue组件中,引入`VantWechat`对象并配置微信支付参数,如AppID、商户号等: ```javascript import VantWechat from 'vant-wechat'; Vue.use(VantWechat, { appId: 'your_app_id', // 填写微信支付对应的AppID timestamp: (new Date()).getTime(), // 间戳,用于生成签名 nonceStr: generateNonceStr(), // 随机字符串,用于生成签名 signature: '', // 待填,生成签名后赋值 paySign: '', // 待填,生成支付签名后赋值 success: handleWechatPaySuccess, // 支付成功回调 fail: handleWechatPayFail, // 支付失败回调 }); // 生成随机字符串函数 function generateNonceStr() { return Math.random().toString(36).substring(2); } ``` 3. 创建二维码展示组件: 使用`vant-wechat`提供的`van-cell-action`组件展示二维码: ```html <van-cell title="微信支付" action :actions="[ { text: '微信支付', value: 'wxPay', icon: 'at' }, ]" ></van-cell> <van-cell-action v-if="isGenerating || isProcessing" :loading="isGenerating || isProcessing" ></van-cell-action> <van-cell-action v-else :title="isProcessing ? '正在请求...' : '二维码'" @click="generatePayQRCode" > <van-icon name="qrcode" /> </van-cell-action> ``` 4. 生成二维码的方法: ```javascript methods: { async generatePayQRCode() { if (!this.signature) { // 在这里调用微信支付API生成签名 // 示例代码不包括生成签名部分,实际开发中需填充 // this.signature = this.generatePaySignature(); } this.isGenerating = true; const result = await this.$vantwechat.pay({ // 填充支付参数,如totalAmount, productDescription等 // 示例代码不包括支付参数,实际开发中需填充 // 这里使用预设好的二维码图片地址 path: 'https://your-qr-code-url', }); this.isProcessing = result.status === 'success'; }, // 其他处理支付结果的函数 }, ``` 5. 处理支付结果: 在`success`和`fail`回调中处理支付结果,并可能需要更新用户界面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值