uniapp实现公众号H5、小程序和App微信授权登录功能

本文详细介绍了如何在uniapp中实现微信授权登录功能,涵盖H5、小程序和App三个平台。首先,需要在微信开放平台注册并绑定相关应用。接着,分别阐述了H5的网页授权过程,包括授权按钮、js代码及授权注意事项;小程序的授权步骤,强调了授权按钮的配置和用户信息获取;最后,讨论了App授权的两种方式及其安全性考虑。完成这些步骤后,即可实现跨平台的微信授权登录功能。
摘要由CSDN通过智能技术生成

本文介绍在使用uniapp开发的H5、小程序和App中使用微信授权登录的方法。
由于微信的公众号、小程序和App是相对独立的体系,同一个用户在这些不同的端中授权所返回的openid是不一样的,这个时候就必须在微信开放平台注册账号,并把对应的公众号、小程序和移动应用绑定上去,在授权的时候就能返回一个unionid了,这样就可以在后台识别到是同一个用户了。
前期要准备的工作:
1、申请公众号、小程序和微信开放平台,并拿到对应平台的appid和secret;
2、H5网页授权还要在公众号后台设置网页授权域名;
3、小程序的接口域名必须启用https,且要设置request、download合法域名等;
4、App必须在微信开放平台申请应用并绑定。
上述工作准备好后,就可以开干了!
一、H5网页授权
1、授权按钮

// official.vue
<u-button class="button" type="success" @click="getWeChatCode">立即授权</u-button>


2、js代码

// official.vue
onLoad(options) {
    if (options.scope) {
        this.scope = options.scope
    }
    if (this.$wechat && this.$wechat.isWechat()) {
        uni.setStorageSync('scope', this.scope)
    let code = this.getUrlCode('code')
        if (code) {
        this.checkWeChatCode(code)
    } else {
        if (this.scope == 'snsapi_base') {
        this.getWeChatCode()
        }
        }
    }
},
methods: {
    getUrlCode(name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null
    },
    getWeChatCode() {
    if (this.$wechat && this.$wechat.isWechat()) {
        let appid = '公众号appid'
        let local = encodeURIComponent(window.location.href)
        let scope = uni.getStorageSync('scope')
        window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${local}&response_type=code&scope=${scope}&state=1#wechat_redirect`
    } else {
        uni.showModal({
        content: '请在微信中打开',
        showCancel: false
        })
    }
    },
    checkWeChatCode(code) {
    if (code) {
        let scope = uni.getStorageSync('scope')
        this.$http.post('/wechat/login', {
        code,
        scope,
        type: 1
    }).then(res => {
        if (res.code == 1) {
        if (scope == 'snsapi_userinfo') {
            this.login(res.data.userinfo)
            uni.navigateBack()
            }
        } else {
        uni.showModal({
            content: res.msg,
            showCancel: false
        })
        }
        }).catch(err => {
        uni.showModal({
        content: err.msg,
        showCancel: false
        })
        })
    }
}


注意,
1、公众号授权scope有两种方式:snsapi_ba

uniapp微信公众号支付需要通过网页授权获取微信的code来实现。首先,需要在配置中设置回调域名。然后,通过后端接口获取支付配置信息。对于微信支付,可以使用以下代码获取配置信息: ``` await this.$http({ apiName: "wxPayConfig", type: "POST", data: { orderId: this.orderId } }).then(res => { // 处理返回的支付配置信息 }).catch(_ => { // 处理异常情况 }); ``` 对于支付宝支付,需要单独从接口获取配置信息,得到的是一长串字符串: ``` await this.$http({ apiName: "aliPay", type: "POST", data: { orderNo: this.orderId } }).then(res => { // 处理返回的支付配置信息 }).catch(_ => { // 处理异常情况 }); ``` 通过以上步骤,可以在uniapp实现微信公众号支付。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [uniapp微信H5公众号授权与支付](https://blog.csdn.net/qq_63358859/article/details/127021504)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [uniappAPP/微信小程序/公众号支付](https://blog.csdn.net/weixin_36185028/article/details/106328412)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值