vue实现pc端扫码登录

vue pc端微信扫码登录

效果图

在这里插入图片描述


方式一:npm

安装并引入插件

wxlogin

    // 安装
    npm install vue-wxlogin -S

    // 局部引入
    import wxlogin from 'vue-wxlogin';

    // 注册组件
    components: {
        wxlogin
    }

参数说明

在这里插入图片描述


使用
<wxlogin
	class="login"
    appid="wxappid"
    :scope="'snsapi_login'"  
    :theme="'black'"  
    :redirect_uri='encodeURIComponent(`https://www.test.com/project/#${fullPath}`)'
    :href="'data:text/css;base64,LmltcG93ZXJCb3ggLnRpdGxlIHsKICBkaXNwbGF5OiBub25lOwp9Ci5pbXBvd2VyQm94IC5xcmNvZGUgewogIHdpZHRoOiAyMjVweDsKICBoZWlnaHQ6IDIyNXB4Owp9'"
	:state="`${Math.random()}`"
 >
</wxlogin>

自定义样式
  • 把自定义的样式,转换为base64格式
  • 使用text/css模式
// 根据需求自定义二维码样式
.impowerBox .title {
  	display: none;
}
.impowerBox .status.status_browser {
  	display: none;
}
.impowerBox .qrcode {
  	border: none;
  	width: 225px;
  	height: 225px;
}
.impowerBox .status{
  	display: none
}

方式二:通过js

引用js文件
<script src="https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>

vue
<div id="container">
	<div id="wxLogin"></div>
</div>

使用
<script>
    export default {
        data() {
            return {
                QRCodeStyle: `data:text/css;base64,QGNoYXJzZXQgIlVURi04IjsNCi5pbXBvd2VyQm94IC50aXRsZSB7ZGlzcGxheTogbm9uZTt9DQouaW1wb3dlckJveCAuaW5mbyB7ZGlzcGxheTogbm9uZTt9DQouc3RhdHVzX2ljb24ge2Rpc3BsYXk6IG5vbmV9DQouaW1wb3dlckJveCAuc3RhdHVzIHt0ZXh0LWFsaWduOiBjZW50ZXI7fSANCg==`
            }
        },
        mounted() {
        	// 微信登录
            getWxLogin();
        },
        methods: {
        	getWxLogin() {
        		let url = 'https://www.xxxxx/#/shop';
	            var obj = new WxLogin({
	                id: "wxLogin",
	                appid: "wxappid",
	                scope: "snsapi_login",
	                // 扫码成功后 跳转的地址
	                redirect_uri: encodeURIComponent(url), // 授权成功后回调的url
	                state: Math.ceil(Math.random() * 1000), // 可设置为简单的随机数加session用来校验
	                style: 'black',
	                href: this.QRCodeStyle
	            });
			}
        }
    }
</script>

注意
  • redirect_uri: 回调地址,进行编码【encodeURIComponent】
  • redirect_uri: 确定登录后会重定向url上携带codestate参数
  • 首先,在开放平台申请账号,认证,创建网站应用
https://www.test.com/project/#/login?code=xxx&state=xxx
  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Vue PC端页面实现微信扫码支付,你需要使用微信支付的 Native 支付模式。具体步骤如下: 1. 向服务器获取支付二维码链接:你需要向服务器发送请求,获取支付二维码的链接。 2. 生成支付二维码:你可以使用第三方库 `qrcode` 来生成支付二维码,并将生成的二维码显示在页面上。 ```javascript import QRCode from 'qrcode' export default { data() { return { qrCodeUrl: '' // 存储二维码链接 } }, mounted() { this.getPayQrCode() }, methods: { getPayQrCode() { // 向服务器获取支付二维码链接 axios.get('/api/getPayQrCode') .then(res => { const qrCodeUrl = res.data.qrCodeUrl // 生成二维码 QRCode.toDataURL(qrCodeUrl, { errorCorrectionLevel: 'H', margin: 1 }) .then(dataUrl => { this.qrCodeUrl = dataUrl }) .catch(err => { console.error(err) }) }) .catch(err => { console.error(err) }) } } } ``` 以上代码中,`getPayQrCode` 方法向服务器获取支付二维码链接,然后使用 `qrcode` 库生成二维码,并将二维码链接存储在 `qrCodeUrl` 变量中,你可以将该变量绑定到一个 `img` 标签上来显示二维码。 3. 轮询支付状态:使用轮询的方式来查询支付状态,如果支付成功,则弹出支付成功的提示框。 ```javascript checkPayStatus() { setInterval(() => { axios.get('/api/checkPayStatus') .then(res => { const status = res.data.status if (status === 'success') { // 支付成功 this.showPaySuccess() } }) .catch(err => { console.error(err) }) }, 3000) }, showPaySuccess() { // 弹出支付成功的提示框 alert('支付成功!') } ``` 以上代码中,`checkPayStatus` 方法使用轮询的方式查询支付状态,每隔 3 秒发送一次请求,如果支付成功,则调用 `showPaySuccess` 方法弹出支付成功的提示框。 4. 调起微信扫码支付界面:在支付成功后,你需要调用微信扫码支付界面,在微信扫描支付二维码后完成支付。 ```javascript startNativePay() { const qrCodeUrl = '支付二维码的链接' const userAgent = window.navigator.userAgent.toLowerCase() if (userAgent.indexOf('micromessenger') === -1) { // 不在微信中,跳转到支付链接 window.location.href = qrCodeUrl } else { // 在微信中,调起微信扫码支付界面 window.location.href = 'weixin://wxpay/bizpayurl?url=' + encodeURIComponent(qrCodeUrl) this.checkPayStatus() } } ``` 以上代码中,`startNativePay` 方法判断当前是否在微信中,如果在微信中,则使用 `weixin://wxpay/bizpayurl?url=` 协议调起微信扫码支付界面,并将支付二维码的链接作为参数传递给微信支付界面。支付成功后,使用轮询的方式查询支付状态,如果支付成功,则弹出支付成功的提示框。 希望这能帮助到你。如果你有其他问题,请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值