13404089107
2026-04-30 9f77dd9245c48498f0ad866ef5a8dac52e3cdd56
fix(微信绑定): 修复APP端微信绑定失败及错误提示问题

- 在APP-PLUS环境下使用原生微信服务进行授权,解决uni.login可能失效的问题
- 添加错误信息归一化处理,将微信授权失败的具体原因展示给用户
- 调整绑定微信列表项的点击事件,避免图标与列表项事件冲突
1个文件已修改
100 ■■■■■ 已修改文件
traditionalApp/pages/settings/settings.vue 100 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
traditionalApp/pages/settings/settings.vue
@@ -44,7 +44,7 @@
                        <image class="edit-icon" src="/static/image/mine/edit.png" mode="aspectFit" @tap="editPassword"></image>
                    </view>
                </view>
                <view class="list-item">
                <view class="list-item" @tap="swapWechat">
                    <view class="list-left">
                        <image class="list-icon" src="/static/image/mine/weixinIcon.png" mode="aspectFit"></image>
                        <text class="list-title">绑定微信</text>
@@ -55,7 +55,7 @@
                            <text class="wechat-name">{{ profile.wechatBinding.nickname || '已绑定微信' }}</text>
                        </view>
                        <text class="wechat-unbound" v-else>去绑定</text>
                        <image class="swap-icon" src="/static/image/mine/refish.png" mode="aspectFit" @tap="swapWechat"></image>
                        <image class="swap-icon" src="/static/image/mine/refish.png" mode="aspectFit" @tap.stop="swapWechat"></image>
                    </view>
                </view>
            </view>
@@ -250,7 +250,7 @@
        })
    } catch (error) {
        uni.showToast({
            title: '微信绑定失败',
            title: resolveWechatBindErrorMessage(error),
            icon: 'none',
        })
    } finally {
@@ -345,6 +345,33 @@
 */
function requestWechatAuthorization() {
    return new Promise<WechatLoginResult>((resolve, reject) => {
        // #ifdef APP-PLUS
        plus.oauth.getServices((services) => {
            const weixinService = services.find((service) => service.id === 'weixin')
            if (!weixinService) {
                reject({
                    code: -3,
                    errMsg: '当前环境不支持微信登录',
                })
                return
            }
            if (!weixinService.nativeClient) {
                reject({
                    code: -8,
                    errMsg: '请先安装微信客户端',
                })
                return
            }
            requestWechatAuthorizeByService(weixinService, resolve, reject)
        }, (error: any) => {
            reject({
                code: error?.code,
                errMsg: error?.message || '获取微信登录服务失败',
            })
        })
        return
        // #endif
        uni.login({
            provider: 'weixin',
            success: (res) => {
@@ -354,6 +381,62 @@
                reject(error)
            },
        })
    })
}
/**
 * 通过微信原生服务执行授权;如存在旧授权态则先退出再重新拉起。
 */
function requestWechatAuthorizeByService(
    weixinService: any,
    resolve: (value: WechatLoginResult) => void,
    reject: (reason?: any) => void
) {
    const executeAuthorize = () => {
        weixinService.authorize((event: any) => {
            const code = normalizeOptionalText(event?.code)
            const authResult = weixinService.authResult || {}
            const accessToken = normalizeOptionalText(authResult.access_token || authResult.accessToken)
            const openid = normalizeOptionalText(authResult.openid || authResult.openId)
            if (!code && !(accessToken && openid)) {
                reject({
                    code: -99,
                    errMsg: '微信授权未返回有效凭证',
                })
                return
            }
            resolve({
                code,
                authResult: {
                    access_token: authResult.access_token,
                    accessToken: authResult.accessToken,
                    openid: authResult.openid,
                    openId: authResult.openId,
                    unionid: authResult.unionid,
                    unionId: authResult.unionId,
                },
            })
        }, (error: any) => {
            reject({
                code: error?.code,
                errMsg: error?.message || '微信授权失败',
            })
        }, {
            scope: 'snsapi_userinfo',
            state: 'bind_wechat',
        })
    }
    const authResult = weixinService?.authResult || {}
    const hasAuthorized = !!normalizeOptionalText(authResult.access_token || authResult.accessToken || authResult.openid || authResult.openId)
    if (!hasAuthorized || typeof weixinService.logout !== 'function') {
        executeAuthorize()
        return
    }
    weixinService.logout(() => {
        executeAuthorize()
    }, () => {
        executeAuthorize()
    })
}
@@ -377,6 +460,17 @@
    })
    return null
}
/**
 * 将微信绑定错误归一化为可直接提示给用户的文案。
 */
function resolveWechatBindErrorMessage(error: any) {
    const errorMessage = normalizeOptionalText(error?.errMsg || error?.message)
    if (errorMessage) {
        return errorMessage
    }
    return '微信绑定失败'
}
</script>
<style lang="scss" scoped>