多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# wx.getUserInfo ### wx.getUserInfo(Object object) > 调用前需要 [用户授权](https://developers.weixin.qq.com/minigame/dev/tutorial/open-ability/authorize.html) scope.userInfo。 获取用户信息。 #### 参数 ##### Object object 属性类型默认值是否必填说明支持版本withCredentialsboolean否是否带上登录态信息。当 withCredentials 为 true 时,要求此前有调用过 wx.login 且登录态尚未过期,此时返回的数据会包含 encryptedData, iv 等敏感信息;当 withCredentials 为 false 时,不要求有登录态,返回的数据不包含 encryptedData, iv 等敏感信息。langstringen否显示用户信息的语言successfunction否接口调用成功的回调函数failfunction否接口调用失败的回调函数completefunction否接口调用结束的回调函数(调用成功、失败都会执行)**object.lang 的合法值** 值说明en英文zh\_CN简体中文zh\_TW繁体中文###### object.success 回调函数 **参数** **Object res** 属性类型说明支持版本userInfo[UserInfo](/read/wechat-game-api/254.md)用户信息对象,不包含 openid 等敏感信息rawDatastring不包括敏感信息的原始数据字符串,用于计算签名signaturestring使用 sha1( rawData + sessionkey ) 得到字符串,用于校验用户信息,详见 [用户数据的签名验证和加解密](https://developers.weixin.qq.com/minigame/dev/tutorial/open-ability/signature.html)encryptedDatastring包括敏感数据在内的完整用户信息的加密数据,详见 [用户数据的签名验证和加解密](https://developers.weixin.qq.com/minigame/dev/tutorial/open-ability/signature.html#%E5%8A%A0%E5%AF%86%E6%95%B0%E6%8D%AE%E8%A7%A3%E5%AF%86%E7%AE%97%E6%B3%95)ivstring加密算法的初始向量,详见 [用户数据的签名验证和加解密](https://developers.weixin.qq.com/minigame/dev/tutorial/open-ability/signature.html#%E5%8A%A0%E5%AF%86%E6%95%B0%E6%8D%AE%E8%A7%A3%E5%AF%86%E7%AE%97%E6%B3%95)#### 示例代码 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="com">// 必须是在用户已经授权的情况下调用</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">wx</span><span class="pun">.</span><span class="pln">getUserInfo</span><span class="pun">({</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> success</span><span class="pun">:</span><span class="pln"> </span><span class="hljs-params"><span class="kwd">function</span></span><span class="pun">(</span><span class="hljs-params"><span class="pln">res</span></span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="kwd">var</span><span class="pln"> userInfo </span><span class="pun">=</span><span class="pln"> res</span><span class="pun">.</span><span class="pln">userInfo</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="kwd">var</span><span class="pln"> nickName </span><span class="pun">=</span><span class="pln"> userInfo</span><span class="pun">.</span><span class="pln">nickName</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="kwd">var</span><span class="pln"> avatarUrl </span><span class="pun">=</span><span class="pln"> userInfo</span><span class="pun">.</span><span class="pln">avatarUrl</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="kwd">var</span><span class="pln"> gender </span><span class="pun">=</span><span class="pln"> userInfo</span><span class="pun">.</span><span class="pln">gender </span><span class="com">//性别 0:未知、1:男、2:女</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="kwd">var</span><span class="pln"> province </span><span class="pun">=</span><span class="pln"> userInfo</span><span class="pun">.</span><span class="pln">province</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="kwd">var</span><span class="pln"> city </span><span class="pun">=</span><span class="pln"> userInfo</span><span class="pun">.</span><span class="pln">city</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="kwd">var</span><span class="pln"> country </span><span class="pun">=</span><span class="pln"> userInfo</span><span class="pun">.</span><span class="pln">country</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">}</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> </ol> ``` encryptedData 解密后为以下 json 结构,详见[加密数据解密算法](https://developers.weixin.qq.com/minigame/dev/tutorial/open-ability/signature.html#%E5%8A%A0%E5%AF%86%E6%95%B0%E6%8D%AE%E8%A7%A3%E5%AF%86%E7%AE%97%E6%B3%95) ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">{</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="str">"openId"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"OPENID"</span><span class="pun">,</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="str">"nickName"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"NICKNAME"</span><span class="pun">,</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="str">"gender"</span><span class="pun">:</span><span class="pln"> GENDER</span><span class="pun">,</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="str">"city"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"CITY"</span><span class="pun">,</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="str">"province"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"PROVINCE"</span><span class="pun">,</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="str">"country"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"COUNTRY"</span><span class="pun">,</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="str">"avatarUrl"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"AVATARURL"</span><span class="pun">,</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="str">"unionId"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"UNIONID"</span><span class="pun">,</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="str">"watermark"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="str">"appid"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"APPID"</span><span class="pun">,</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="str">"timestamp"</span><span class="pun">:</span><span class="pln"> TIMESTAMP</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">}</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">}</span></code></li> </ol> ``` #### 接口调整说明 在用户未授权过的情况下调用此接口,将不再出现授权弹窗,会直接进入 fail 回调(详见[《公告》](https://developers.weixin.qq.com/community/develop/doc/0000a26e1aca6012e896a517556c01))。在用户已授权的情况下调用此接口,可成功获取用户信息。 #### 示例代码 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="com"><!-- 如果只是展示用户头像昵称,可以使用 <open-data /> 组件 --></span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="kwd"><</span><span class="hljs-params"><span class="kwd">open-data</span></span><span class="pln"> </span><span class="hljs-params"><span class="typ">type</span></span><span class="pun">=</span><span class="hljs-params"><span class="str">"userAvatarUrl"</span></span><span class="kwd">></</span><span class="hljs-params"><span class="kwd">open-data</span></span><span class="kwd">></span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="kwd"><</span><span class="hljs-params"><span class="kwd">open-data</span></span><span class="pln"> </span><span class="hljs-params"><span class="typ">type</span></span><span class="pun">=</span><span class="hljs-params"><span class="str">"userNickName"</span></span><span class="kwd">></</span><span class="hljs-params"><span class="kwd">open-data</span></span><span class="kwd">></span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="com"><!-- 需要使用 button 来授权登录 --></span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="kwd"><</span><span class="hljs-params"><span class="kwd">button</span></span><span class="pln"> </span><span class="hljs-params"><span class="typ">wx:if</span></span><span class="pun">=</span><span class="hljs-params"><span class="str">"{{canIUse}}"</span></span><span class="pln"> </span><span class="hljs-params"><span class="typ">open-type</span></span><span class="pun">=</span><span class="hljs-params"><span class="str">"getUserInfo"</span></span><span class="pln"> </span><span class="hljs-params"><span class="typ">bindgetuserinfo</span></span><span class="pun">=</span><span class="hljs-params"><span class="str">"bindGetUserInfo"</span></span><span class="kwd">></span><span class="pln">授权登录</span><span class="kwd"></</span><span class="hljs-params"><span class="kwd">button</span></span><span class="kwd">></span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="kwd"><</span><span class="hljs-params"><span class="kwd">view</span></span><span class="pln"> </span><span class="hljs-params"><span class="typ">wx:else</span></span><span class="kwd">></span><span class="pln">请升级微信版本</span><span class="kwd"></</span><span class="hljs-params"><span class="kwd">view</span></span><span class="kwd">></span></code></li> </ol> ``` ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="typ">Page</span><span class="pun">({</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> data</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> canIUse</span><span class="pun">:</span><span class="pln"> wx</span><span class="pun">.</span><span class="pln">canIUse</span><span class="pun">(</span><span class="str">'button.open-type.getUserInfo'</span><span class="pun">)</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">},</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> onLoad</span><span class="pun">:</span><span class="pln"> </span><span class="hljs-params"><span class="kwd">function</span></span><span class="pun">(</span><span class="hljs-params"></span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="com">// 查看是否授权</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> wx</span><span class="pun">.</span><span class="pln">getSetting</span><span class="pun">({</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> success </span><span class="pun">(</span><span class="pln">res</span><span class="pun">){</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">res</span><span class="pun">.</span><span class="pln">authSetting</span><span class="pun">[</span><span class="str">'scope.userInfo'</span><span class="pun">])</span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="com">// 已经授权,可以直接调用 getUserInfo 获取头像昵称</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> wx</span><span class="pun">.</span><span class="pln">getUserInfo</span><span class="pun">({</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> success</span><span class="pun">:</span><span class="pln"> </span><span class="hljs-params"><span class="kwd">function</span></span><span class="pun">(</span><span class="hljs-params"><span class="pln">res</span></span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="pln">res</span><span class="pun">.</span><span class="pln">userInfo</span><span class="pun">)</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">}</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">})</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">}</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">}</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">})</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">},</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> bindGetUserInfo </span><span class="pun">(</span><span class="pln">e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="pln">e</span><span class="pun">.</span><span class="pln">detail</span><span class="pun">.</span><span class="pln">userInfo</span><span class="pun">)</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">}</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> </ol> ``` > 原文: <https://developers.weixin.qq.com/minigame/dev/api/open-api/user-info/wx.getUserInfo.html>