uniapp微信小程序,公众号授权登录踩坑记录

一、小程序

1、微信小程序获取code

1 uni.login({
2    provider: \'weixin\',
3    success: function(res) {
4         let code = res.code
5         //将code发给后台
6    }  
7 })

2、将code发给后台进行获取openid

3、根据后台返回的状态进行逻辑编写

二、公众号

1、公众号申请以及认证(未认证的情况下可以申请测试开发)

2、绑定授权域名

2、引入wx js-sdk

let script = document.createElement(\'script\')
            script.type = \'text/javascript\'
            script.src = \'http://res2.wx.qq.com/open/js/jweixin-1.6.0.js\'
            document.getElementsByTagName(\'head\')[0].appendChild(script)

3、js-sdk 初始化配置(必须)

wx.config({
                                debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                                appId: res.data.appId, // 必填,公众号的唯一标识
                                    // appId: \'wx0c63e1401f1c86dc\',
                                timestamp: res.data.timeStamp, // 必填,生成签名的时间戳
                                nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
                                signature: res.data.signType,// 必填,签名,见附录1
                                jsApiList: [\'chooseWXPay\'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
                            });

4、调用授权api

wx.chooseWXPay