微信小程序新版用户授权

微信小程序在2021年3月30号对小程序的调整发布了一则公告:小程序登录、用户信息相关接口调整说明

总的来说就是原来的wx.getUserInfo接口接近于废弃,希望我们改用新接口wx.getUserProfile

为什么说接近于废弃呢,因为返回的数据结构没有任何变化,但是里面的数据已经没有任何意义了

wx.login({
      success: (res) => {
        console.log(res)
        wx.getUserInfo({
          success: (res) => {
            console.log(JSON.stringify(res))
          },
        })
      },
    })

  

打印结果

{
	errMsg: "login:ok",
	code: "061vm11w3k6L9W273N0w3ty6aq0vm11N"
}

{
	"errMsg": "getUserInfo:ok",
	"rawData": "{\"nickName\":\"微信用户\",\"gender\":0,\"language\":\"\",\"city\":\"\",\"province\":\"\",\"country\":\"\",\"avatarUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132\"}",
	"userInfo": {
		"nickName": "微信用户",
		"gender": 0,
		"language": "",
		"city": "",
		"province": "",
		"country": "",
		"avatarUrl": "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132"
	},
	"signature": "4d3f449f152df32d8013ca167f6a9d9bac3f5669",
	"encryptedData": "VFmQZFcrNTpaOxsV46htOLZlLxp+8d8qO8uXp9jjjHhLkhrrlyjUIILGrDJywZh1fXNL3E2vUTp1UTHs2HbYf2CDPpzI6cynj6C+YzYkLYqTMYCmOewVEoz3E3h3hehfwXmraIg0G+dvyAPoxz6jBTPsyr49imgwWMHc8EgrUqkc3d2q90rlsz/QOhQSpMVNhDJ4hONEqiqHHNDuFbClb2S1pSIA1MpDi9YQQhfOSBldK0I/B7hdEFHuDJdtKutOddlAdhcuf/JGqNG35K8O9yTdVkdXTNS/L4HS8jAZHQVhlEaWChp/mg/7Sl+w+0YOJnGIHQff6U84vw1lFxs1qITO7bUbaNFz0w0L7C3iJwvPQjINF4sYm8p95+HHzeHE8QyHcOhR4XZhj7/GwwqqBSuq5+YUPJoVau8o4t5xrYTRFUAFjgRt/eofKesX3TYpgNHC8+z2+XAxSg6A3hRSb0MEbvxbgRMA2igsZre2EYYayYtPR588imkolfgvdO97lEh78F9jpNwM2oula995pA==",
	"iv": "gx1Jotzr+azhcIgQ+3yFqA=="
} 

可以看到返回的信息是不包含用户昵称等信息的;头像是一个面向所有用户的默认头像

我们对encryptedData进行解密得到结果如下

//通过jscode2session接口用code换取session_key

Array
(
    [session_key] => DoVkwxtLSaNI+dUxtBJ35g==
    [openid] => o-UKg4vLVl4Cpa5TZAEFeUp_j-bg
    [unionid] => oduLh1PZ1D94qW5LywAXfRpY1OMw
)
//通过
session_key,iv对加密字符串encryptedData解密
Array
(
    [openId] => o-UKg4vLVl4Cpa5TZAEFeUp_j-bg
    [nickName] => 微信用户
    [gender] => 0
    [language] => 
    [city] => 
    [province] => 
    [country] => 
    [avatarUrl] => https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132
    [unionId] => oduLh1PZ1D94qW5LywAXfRpY1OMw
    [watermark] => Array
        (
            [timestamp] => 1617767507
            [appid] => wxb9d02
        )

)

  解密效果只是多了包含用户的openidunionId,用户的其他信息 我们依然获取不到

下面改用新街口调试

 <button wx:if="{{canIUseGetUserProfile}}" bindtap="getlogin"> 获取头像昵称 </button>  

getlogin(){
    wx.getUserProfile({
      desc: ‘展示用户信息‘, // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 
      success: (res) => {
        console.log(res)
        wx.login({//必须先获取信息后再调用login,再login里调用信息接口调用不了
          complete: (res) => {
            console.log(res)
          },
        })
      }
    })
  },

 打印结果如下

{
	"errMsg": "getUserProfile:ok",
	"rawData": "{\"nickName\":\"AAA\",\"gender\":1,\"language\":\"zh_CN\",\"city\":\"Xuzhou\",\"province\":\"Jiangsu\",\"country\":\"China\",\"avatarUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j2222xyQamU3pbDq0epMoiaj83F7LoTT40CMAcT5OUAeQdKb2lAJ4hqudggWJ9Wd05plQbw/132\"}",
	"userInfo": {
		"nickName": "AAA",
		"gender": 1,
		"language": "zh_CN",
		"city": "Xuzhou",
		"province": "Jiangsu",
		"country": "China",
		"avatarUrl": "https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLNiclWk0jG0jxyQamU3pbDq0epMoiaj83F7LoTT40CMAcTWd05plQbw/132"
	},
	"signature": "4f729de60e07afec005af561697046a7fb0ad6df",
	"encryptedData": "gpAbYEOQZCtlbeOioHmG7ZIKMe5GmIwfsZIY17u9jYlc46SBc+xXJkeiuYF5KGfEaCPxEfYWB/wDUHSK89dL4svzEOAGZqGomrFZWbmPvVxYBY7lVBnRlgXJZNDqoYSODHV4zHZHSIVBNzG4w2+eyIn3eE4cLYHAk+/nlrWezRAaoYPUUFLgBYGDoyWoJ50ys3K6k9Kzx9H4WkTnvXWx1lBf6gPrIG4MfU54WB9EdnkxTzPw3e4/nfnsdU6Ro2/D/Z/wG4fKOyWUO6j54x7Yfmqn3wVnYeccBqThwxvCtwS6WablYn2JowEN9U7zlugbjVHN3x9B3lfUFwgr3fYJZG20cC0xST5wWH505YZhqZEZ/VCkUYVvzFYAyE2bn/bo0T0sOW7qbByG49hNRVmZach71s9+2yz481hyvjEoji5q8qQvdj1gAiNG/RWMFjIJ",
	"iv": "PIPIgoaGVXBCaUPnmcUr4A=="
}

{
	errMsg: "login:ok",
	code: "091BP00000q0uL1Ky50008IsGk3BP00l"
}

  对数据进行解密:

Array
(
    [session_key] => YaDB4+kxgCOPjQEd0kmuYg==
    [openid] => o-UKg4vL2p_j-bg
    [unionid] => oduLh2pY1OMw
)
Array
(
    [nickName] =>  AAA
    [gender] => 1
    [language] => zh_CN
    [city] => Xuzhou
    [province] => Jiangsu
    [country] => China
    [avatarUrl] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLNi2Qbw/132
    [watermark] => Array
        (
            [timestamp] => 1617769042
            [appid] => wxb92
        )

)

  就这样吧;前端需要进行更改接口调用;因为新接口每次都会有弹窗提示,所以这块逻辑也要修改一下

  

 

微信小程序新版用户授权

上一篇:ubuntu 18.04 Install libpcap


下一篇:小程序授权登录设计