uniapp + JSAPIv3

该文描述了一个JavaScript实现的微信授权获取openId的过程,以及后续的微信支付功能。当用户信息不存在或者不完整时,通过重定向URL获取授权code,然后用code换取openId。成功获取openId后,进行微信支付调用,利用WeixinJSBridge发起支付请求。同时,文章提到了JAVA后端的相关接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获取 openId

wxReload() {
        let _this = this;

        let wxInfo = this.getUrlCode();
        let userInfo = _this.$store.state.userInfo;

        //截取code
        let code = wxInfo.code;
        //判断是否在缓存中存储了用户信息
        if (!_this.$tool.isObjNull(userInfo) && userInfo.userId != null && userInfo.userId != "") {
            return;
        }
        _this.overlayShow = true;
        //如果没有code,则去请求
        if (code == undefined || code == '' || code == null) {
            let url = "https://open.weixin.qq.com/connect/oauth2/authorize";
            url += "?appid=" + uni.$wxAppid;
            //回调地址就是前端地址
            url += "&redirect_uri=" + uni.$frontUrl;
            url += "&response_type=code";
            url += "&scope=snsapi_userinfo";
            url += "&state=1";
            url += "#wechat_redirect";
            window.location.href = url;
        } 
        //通过code去获取openId
        else {
            _this.overlayShow = false;
            _this.updateWxInfo(wxInfo);
            _this.$request("", {
                "code": code
            }, "wx/getAppId").then(res => {
                let wxBody = res.response.body;
                let wxHead = res.response.head;
                let ackCode = wxHead.ackCode;
                if (ackCode == "100.1") {
                    
                } else {
                    uni.showToast({
                        title: wxHead.ackMessage,
                        icon: 'none',
                        duration: 1500
                    });
                }
            });

        }
    },

支付代码

pay() {
    let _this = this;
    if (_this.$tool.isWechat()) {
        _this.$request("", {
            "openId": _this.$store.state.userInfo.userId
        }, "wx/payment").then(res => {
            let response = res.response;
            let ackCode = response.head.ackCode;
            let msg = response.head.ackMessage;
            if (ackCode == "100.1") {
                let data = response.body;
                WeixinJSBridge.invoke('getBrandWCPayRequest', data,
                    function(res) {
                        if (res.err_msg == "get_brand_wcpay_request:ok") {
                            alter("支付成功")
                            // 使用以上方式判断前端返回,微信团队郑重提示:
                            //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
                        }
                    });
            } else {
                uni.showModal({
                    title: msg,
                    icon: 'none',
                    showCancel: false,
                    success: function(res) {}
                });
            }
        });
    }
},

JAVA 后端代码链接:JAVA + JSAPIv3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值