# qq.getUserInfo

# qq.getUserInfo(Object object)

调用前需要 用户授权 scope.userInfo。

获取用户信息。

# 参数

# Object object
属性 类型 默认值 必填 说明
withCredentials boolean 否 是否带上登录态信息。当 withCredentials 为 true 时,要求此前有调用过 qq.login 且登录态尚未过期,此时返回的数据会包含 encryptedData, iv 等敏感信息;当 withCredentials 为 false 时,不要求有登录态,返回的数据不包含 encryptedData, iv 等敏感信息。
lang string en 否 显示用户信息的语言
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)

object.lang 的合法值

值 说明
en 英文
zh_CN 简体中文
zh_TW 繁体中文
# object.success 回调函数

参数

Object res

属性 类型 说明
userInfo UserInfo 用户信息对象,不包含 openid 等敏感信息
rawData string 不包括敏感信息的原始数据字符串,用于计算签名
signature string 使用 sha1( rawData + sessionkey ) 得到字符串,用于校验用户信息,详见 用户数据的签名验证和加解密
encryptedData string 包括敏感数据在内的完整用户信息的加密数据,详见 用户数据的签名验证和加解密
iv string 加密算法的初始向量,详见 用户数据的签名验证和加解密

# 接口调整说明

在用户未授权过的情况下调用此接口,将不再出现授权弹窗,会直接进入 fail 回调。在用户已授权的情况下调用此接口,可成功获取用户信息。

# 示例代码

// 必须是在用户已经授权的情况下调用
qq.getUserInfo({
  success(res) {
    const userInfo = res.userInfo
    const nickName = userInfo.nickName
    const avatarUrl = userInfo.avatarUrl
  }
})
1
2
3
4
5
6
7
8

encryptedData 解密后为以下 json 结构,详见加密数据解密算法

{
  "openId": "OPENID",
  "nickName": "NICKNAME",
  "avatarUrl": "AVATARURL",
  "unionId": "UNIONID",
  "watermark": {
    "appid": "APPID",
    "timestamp": TIMESTAMP
  }
}
1
2
3
4
5
6
7
8
9
10

# 示例代码

<!-- 如果只是展示用户头像昵称,可以使用 <open-data /> 组件 -->
<open-data type="userAvatarUrl"></open-data>
<open-data type="userNickName"></open-data>
<!-- 需要使用 button 来授权登录 -->
<button
  qq:if="{{canIUse}}"
  open-type="getUserInfo"
  bindgetuserinfo="bindGetUserInfo"
>
  授权登录
</button>
<view qq:else>请升级QQ版本</view>
1
2
3
4
5
6
7
8
9
10
11
12
Page({
  data: {
    canIUse: qq.canIUse('button.open-type.getUserInfo')
  },
  onLoad() {
    // 查看是否授权
    qq.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称
          qq.getUserInfo({
            success(res) {
              console.log(res.userInfo)
            }
          })
        }
      }
    })
  },
  bindGetUserInfo(e) {
    console.log(e.detail.userInfo)
  }
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# qq.createUserInfoButton

# UserInfoButton qq.createUserInfoButton(Object object)

创建用户信息按钮

# 参数

# Object object
属性 类型 默认值 必填 说明
type string 是 按钮的类型
text string 否 按钮上的文本,仅当 type 为 text 时有效
image string 否 按钮的背景图片,仅当 type 为 image 时有效
style Object 是 按钮的样式
withCredentials boolean 是 是否带上登录态信息。当 withCredentials 为 true 时,要求此前有调用过 qq.login 且登录态尚未过期,此时返回的数据会包含 encryptedData, iv 等敏感信息;当 withCredentials 为 false 时,不要求有登录态,返回的数据不包含 encryptedData, iv 等敏感信息。
lang string en 否 描述用户信息的语言

type 的合法值

值 说明
text 可以设置背景色和文本的按钮
image 只能设置背景贴图的按钮,背景贴图会直接拉伸到按钮的宽高

style 的结构

属性 类型 默认值 必填 说明
left number 是 左上角横坐标
top number 是 左上角纵坐标
width number 是 宽度
height number 是 高度
backgroundColor string 是 背景颜色。格式为 6位/8位 16进制数。
borderColor string 是 边框颜色。格式为 6位/8位 16进制数。
borderWidth number 是 边框宽度
borderRadius number 是 边框圆角
color string 是 文本的颜色。格式为 6位 16进制数。
textAlign string 是 文本的水平居中方式
fontSize number 是 字号
lineHeight number 是 文本的行高

style.textAlign 的合法值

值 说明
left 居左
center 居中
right 居右

object.lang 的合法值

值 说明
en 英文
zh_CN 简体中文
zh_TW 繁体中文

# 返回值

# UserInfoButton

# 示例代码

const button = qq.createUserInfoButton({
  type: 'text',
  text: '获取用户信息',
  style: {
    left: 10,
    top: 76,
    width: 200,
    height: 40,
    lineHeight: 40,
    backgroundColor: '#ff0000',
    color: '#ffffff',
    textAlign: 'center',
    fontSize: 16,
    borderRadius: 4
  }
})
button.onTap((res) => {
  console.log(res)
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# UserInfoButton

用户信息按钮

# 属性

# string type

按钮的类型

type 的合法值

值 说明
text 可以设置背景色和文本的按钮
image 只能设置背景贴图的按钮,背景贴图会直接拉伸到按钮的宽高
# string text

按钮上的文本,仅当 type 为 text 时有效

# string image

按钮的背景图片,仅当 type 为 image 时有效

# Object style

按钮的样式

属性 类型 说明
left number 左上角横坐标
top number 左上角纵坐标
width number 宽度
height number 高度
backgroundColor string 背景颜色。格式为 6位/8位 16进制数。
borderColor string 边框颜色。格式为 6位/8位 16进制数。
borderWidth number 边框宽度
borderRadius number 边框圆角
color string 文本的颜色。格式为 6位 16进制数。
textAlign string 文本的水平居中方式
fontSize number 字号
lineHeight number 文本的行高

style.textAlign 的合法值

值 说明
left 居左
center 居中
right 居右

# 方法

# UserInfoButton.show()

显示用户信息按钮

# UserInfoButton.hide()

隐藏用户信息按钮。

# UserInfoButton.destroy()

销毁用户信息按钮

# UserInfoButton.onTap(function callback)

监听用户信息按钮的点击事件

# UserInfoButton.offTap(function callback)

取消监听用户信息按钮的点击事件

# .destroy

# UserInfoButton.destroy()

销毁用户信息按钮

# .hide

# UserInfoButton.hide()

隐藏用户信息按钮。

# .offTap

# UserInfoButton.offTap(function callback)

取消监听用户信息按钮的点击事件

# 参数

# function callback

用户信息按钮的点击事件的回调函数

# .onTap

# UserInfoButton.onTap(function callback)

监听用户信息按钮的点击事件

# 参数

# function callback

用户信息按钮的点击事件的回调函数

# 参数

Object res

属性 类型 说明
userInfo UserInfo 用户信息对象,不包含 openid 等敏感信息
rawData string 不包括敏感信息的原始数据字符串,用于计算签名
signature string 使用 sha1( rawData + sessionkey ) 得到字符串,用于校验用户信息,参考文档signature
encryptedData string 包括敏感数据在内的完整用户信息的加密数据,详细见加密数据解密算法
iv string 加密算法的初始向量,详细见加密数据解密算法

# .show

# UserInfoButton.show()

显示用户信息按钮

# UserInfo

用户信息

# 属性

# string nickName

用户昵称

# string avatarUrl

用户头像图片的 URL。URL 最后一个数值代表正方形头像大小(有 0、46、64、96、132 数值可选,0 代表 640x640 的正方形头像,46 表示 46x46 的正方形头像,剩余数值以此类推。默认132),用户没有头像时该项为空。若用户更换头像,原有头像 URL 将失效。