小程序使用getUserInfo获取用户信息失败

本文探讨了在新版微信开发工具中,使用getUserInfo获取用户信息失败的问题。文章指出,官方推荐使用wx.getUserProfile代替。然而,如果需要使用getUserInfo,可以通过将project.config.json设置为旧版本来实现。但作者建议遵循官方建议,使用新方法以简化流程。

1.使用getUserInfo

 wx.getUserInfo({
      success: (res) => {
        console.log(res);
        this.setData({
          userInfo: res.userInfo
        })
      },
      fail: (err) => {
        console.log(err);
      }
    })
  },

获取得到的信息:
在这里插入图片描述
也就是不能获取用户的头像和昵称

2. 原因

新版的微信开发工具推荐使用wx.getUserProfile获取用户信息

getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },

3.那要是非得想用getUserInfo呢?

答案:可以

做法,把project.config.json版本换成老版本。老版本就可以使用getUserInfo获取用户信息了。当然官方都推荐用新的了,咱还是用新的吧,省事。

(老版本如下)

{
    "description": "项目配置文件",
    "packOptions": {
        "ignore": []
    },
    "setting": {
        "urlCheck": true,
        "es6": true,
        "enhance": true,
        "postcss": true,
        "preloadBackgroundData": false,
        "minified": true,
        "newFeature": true,
        "coverView": true,
        "nodeModules": false,
        "autoAudits": false,
        "showShadowRootInWxmlPanel": true,
        "scopeDataCheck": false,
        "uglifyFileName": false,
        "checkInvalidKey": true,
        "checkSiteMap": true,
        "uploadWithSourceMap": true,
        "compileHotReLoad": false,
        "lazyloadPlaceholderEnable": false,
        "useMultiFrameRuntime": true,
        "useApiHook": true,
        "useApiHostProcess": true,
        "babelSetting": {
            "ignore": [],
            "disablePlugins": [],
            "outputPath": ""
        },
        "useIsolateContext": true,
        "userConfirmedBundleSwitch": false,
        "packNpmManually": false,
        "packNpmRelationList": [],
        "minifyWXSS": true,
        "disableUseStrict": false,
        "minifyWXML": true,
        "showES6CompileOption": false,
        "useCompilerPlugins": false,
        "ignoreUploadUnusedFiles": true
    },
    "compileType": "miniprogram",
    "libVersion": "2.13.1",
    "appid": "wx7a5dfd35d20f6982",
    "projectname": "wechat_study",
    "debugOptions": {
        "hidedInDevtools": []
    },
    "isGameTourist": false,
    "simulatorType": "wechat",
    "simulatorPluginLibVersion": {},
    "condition": {
        "search": {
            "list": []
        },
        "conversation": {
            "list": []
        },
        "game": {
            "currentL": -1,
            "list": []
        },
        "miniprogram": {
            "list": []
        }
    }
}

老版获取用户信息效果如下:
在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值