收藏
回答

wx.createUserInfoButton() 这个创建的button为什么多次点击才能销毁?

const windowInfo = wx.getWindowInfo();
// 否则,先通过 wx.createUserInfoButton 接口发起授权
let button = wx.createUserInfoButton({
    type: 'text',
    text: '', // 文本设为空
    style: {
        left: 0, // 左边距为0
        top: 0, // 上边距为0
        width: windowInfo.windowWidth, // 使用 windowInfo 的窗口宽度
        height: windowInfo.windowHeight, // 使用 windowInfo 的窗口高度
        lineHeight: 0,
        backgroundColor: 'transparent', // 背景色透明
        color: 'transparent', // 文字颜色透明
        textAlign: 'center',
        fontSize: 0, // 字体大小为0
        borderRadius: 0,
        borderWidth: 0, // 边框宽度为0
        borderColor: 'transparent' // 边框颜色透明
    }
})
button.onTap(() => {
    // 用户点击后,尝试获取用户信息
    wx.getUserInfo({
        success: (res) => {
            console.log('授权成功,用户信息:', res.userInfo);
            GlobalData.userInfo = res.userInfo;

            button.destroy();    // ?  控制台已经输出了用户信息,但是button没有销毁
        },
        fail: (err) => {
            console.log('用户拒绝授权或获取失败:', err);
            // ❌ 授权失败:保留按钮,让用户可重试
            wx.showToast({title: '请授权才能继续', icon: 'none'});
        }
    });
});


最后一次编辑于  2025-11-07
回答关注问题邀请回答
收藏

1 个回答

  • 智能回答 智能回答 本次回答由AI生成
    2025-11-07
    有用
登录 后发表内容