# 获取不限制的小程序码

> 接口应在服务器端调用，不可在前端（小程序、网页、APP等）直接调用，具体可参考[接口调用指南](/doc/oplatform/developers/dev/guide.md)。

接口英文名：getUnlimitedQRCode

该接口用于获取小程序码，适用于需要的码数量极多的业务场景。通过该接口生成的小程序码，永久有效，数量暂无限制。 更多用法详见 [获取小程序码](/miniprogram/dev/framework/open-ability/qr-code.md)。

## 1. 调用方式

### HTTPS 调用

```bash
POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
```

> **支持加密请求：** 本接口支持服务通信二次加密和签名，可有效防止数据篡改与泄露。[查看详情](/miniprogram/dev/server/getting_started/api_signature)

### 云调用

- 调用方法：wxacode.getUnlimited

- 出入参和 HTTPS 调用相同，调用方式可查看 [云调用](/doc/oplatform/developers/dev/cloudCall.md) 说明文档。

### 第三方调用

- 本接口支持第三方平台代商家调用。

- 该接口所属的权限集 id 为：17

- 服务商获得其中之一权限集授权后，可通过使用 [authorizer_access_token](/doc/oplatform/developers/dev/AuthorizerAccessToken.md) 代商家进行调用，具体可查看 [第三方调用](/doc/oplatform/Third-party_Platforms/2.0/Before_Develop/call_interface.md) 说明文档。

## 2. 请求参数

### 查询参数 `Query String Parameters`

```json
[
  {
    "参数名": "access_token",
    "类型": "string",
    "必填": "是",
    "说明": "接口调用凭证，可使用 [access_token](/miniprogram/dev/server/API/mp-access-token/api_getaccesstoken)、[authorizer_access_token](/doc/oplatform/openApi/ticket-token/api_getauthorizeraccesstoken.md)"
  }
]
```

### 请求体 `Request Payload`

```json
[
  {
    "参数名": "scene",
    "类型": "string",
    "必填": "是",
    "说明": "最大32个可见字符，只支持数字，大小写英文以及部分特殊字符：!#$&'()*+,/:;=?@-._~，其它字符请自行编码为合法字符（因不支持%，中文无法使用 urlencode 处理，请使用其他编码方式）"
  },
  {
    "参数名": "page",
    "类型": "string",
    "必填": "否",
    "说明": "默认是主页，页面 page，例如 pages/index/index，根路径前不要填加 /，不能携带参数（参数请放在scene字段里），如果不填写这个字段，默认跳主页面。scancode_time为系统保留参数，不允许配置"
  },
  {
    "参数名": "check_path",
    "类型": "boolean",
    "必填": "否",
    "说明": "默认是true，检查page 是否存在，为 true 时 page 必须是已经发布的小程序存在的页面（否则报错）；为 false 时允许小程序未发布或者 page 不存在， 但page 有数量上限（60000个）请勿滥用。"
  },
  {
    "参数名": "env_version",
    "类型": "string",
    "必填": "否",
    "说明": "要打开的小程序版本。正式版为 \"release\"，体验版为 \"trial\"，开发版为 \"develop\"。默认是正式版。"
  },
  {
    "参数名": "width",
    "类型": "number",
    "必填": "否",
    "说明": "默认430，二维码的宽度，单位 px，最小 280px，最大 1280px"
  },
  {
    "参数名": "auto_color",
    "类型": "boolean",
    "必填": "否",
    "说明": "自动配置线条颜色，如果颜色依然是黑色，则说明不建议配置主色调，默认 false"
  },
  {
    "参数名": "line_color",
    "类型": "object",
    "必填": "否",
    "说明": "默认是{\"r\":0,\"g\":0,\"b\":0}\t。auto_color 为 false 时生效，使用 rgb 设置颜色 例如 {\"r\":\"xxx\",\"g\":\"xxx\",\"b\":\"xxx\"} 十进制表示"
  },
  {
    "参数名": "is_hyaline",
    "类型": "boolean",
    "必填": "否",
    "说明": "默认是false，是否需要透明底色，为 true 时，生成透明底色的小程序"
  }
]
```

### Body.line_color `Object Payload`

默认是{"r":0,"g":0,"b":0}	。auto_color 为 false 时生效，使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} 十进制表示

```json
[
  {
    "参数名": "r",
    "类型": "-",
    "必填": "是",
    "说明": "默认值{\"r\":0,\"g\":0,\"b\":0}\t；auto_color 为 false 时生效，使用 rgb 设置颜色 例如 {\"r\":\"xxx\",\"g\":\"xxx\",\"b\":\"xxx\"} 十进制表示"
  },
  {
    "参数名": "g",
    "类型": "-",
    "必填": "是",
    "说明": "默认值{\"r\":0,\"g\":0,\"b\":0}\t；auto_color 为 false 时生效，使用 rgb 设置颜色 例如 {\"r\":\"xxx\",\"g\":\"xxx\",\"b\":\"xxx\"} 十进制表示"
  },
  {
    "参数名": "b",
    "类型": "-",
    "必填": "是",
    "说明": "默认值{\"r\":0,\"g\":0,\"b\":0}\t；auto_color 为 false 时生效，使用 rgb 设置颜色 例如 {\"r\":\"xxx\",\"g\":\"xxx\",\"b\":\"xxx\"} 十进制表示"
  }
]
```

## 3. 返回参数

### 返回体 `Response Payload`

```json
[
  {
    "参数名": "buffer",
    "类型": "buffer",
    "说明": "图片 Buffer"
  },
  {
    "参数名": "errcode",
    "类型": "number",
    "说明": "错误码"
  },
  {
    "参数名": "errmsg",
    "类型": "string",
    "说明": "错误信息"
  }
]
```

## 4. 注意事项

- 如果调用成功，会直接返回图片二进制内容，如果请求失败，会返回 JSON 格式的数据。
- POST 参数需要转成 JSON 字符串，不支持 form 表单提交。
- 调用分钟频率受限（5000次/分钟），如需大量小程序码，建议预生成

#### 获取 scene 值

- scene 字段的值会作为 query 参数传递给小程序/小游戏。用户扫描该码进入小程序/小游戏后，开发者可以获取到二维码中的 scene 值，再做处理逻辑。
- 调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟，开发工具模拟时的 scene 的参数值需要进行 encodeURIComponent

##### 小程序
```json
Page({
  onLoad (query) {
    // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
    const scene = decodeURIComponent(query.scene)
  }
})
```

##### 小游戏

```json
// 在首次启动时通过 wx.getLaunchOptionsSync 接口获取
const {query} = wx.getLaunchOptionsSync()
const scene = decodeURIComponent(query.scene)

// 或者在 wx.onShow 事件中获取
wx.onShow(function ({query}) {
  // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
  const scene = decodeURIComponent(query.scene)
})
```

## 5. 代码示例

### 5.1 HTTP调用示例（成功）

请求示例

```json
{
  "page": "pages/index/index",
  "scene": "a=1",
  "check_path": true,
  "env_version": "release"
}
```

返回示例

```
图片 Buffer
```

### 5.2 HTTP调用示例（失败）

请求示例

```json
{
  "page": "pages/index/index",
  "scene": "a=1",
  "check_path": true,
  "env_version": "release_error"
}
```

返回示例

```json
{
  "errcode": 40097,
  "errmsg": "invalid args"
}
```

### 5.3 云调用示例（成功）

请求示例

```json
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
})
exports.main = async (event, context) => {
  try {
    const result = await cloud.openapi.wxacode.getUnlimited({
        "page": 'pages/index/index',
        "scene": 'a=1',
        "checkPath": true,
        "envVersion": 'release'
      })
    return result
  } catch (err) {
    return err
  }
}
```

返回示例

```
图片 Buffer
```

## 6. 错误码

以下是本接口的错误码列表，其他错误码可参考 [通用错误码](/doc/oplatform/developers/errCode/README.md)；调用接口遇到报错，可使用官方提供的 [API 诊断工具](https://developers.weixin.qq.com/console/devtools/debug?utm_source=api_errcode) 辅助定位和分析问题。

```json
[
  {
    "错误码": "-1",
    "错误描述": "system error",
    "解决方案": "系统繁忙，此时请开发者稍候再试"
  },
  {
    "错误码": "40001",
    "错误描述": "invalid credential  access_token isinvalid or not latest",
    "解决方案": "获取 access_token 时 AppSecret 错误，或者 access_token 无效。请开发者认真比对 AppSecret 的正确性，或查看是否正在为恰当的公众号调用接口"
  },
  {
    "错误码": "40097",
    "错误描述": "invalid args",
    "解决方案": "参数错误"
  },
  {
    "错误码": "40129",
    "错误描述": "invalid scene",
    "解决方案": "最大32个可见字符，只支持数字，大小写英文以及部分特殊字符：!#$&'()*+,/:;=?@-._~，其它字符请自行编码为合法字符（因不支持%，中文无法使用 urlencode 处理，请使用其他编码方式）"
  },
  {
    "错误码": "40169",
    "错误描述": "invalid length for scene  or thedata is not json string",
    "解决方案": "scene 不合法"
  },
  {
    "错误码": "41030",
    "错误描述": "invalid page",
    "解决方案": "page路径不正确：根路径前不要填加 /，不能携带参数（参数请放在scene字段里），需要保证在现网版本小程序中存在，与app.json保持一致。\n设置check_path=false可不检查page参数。"
  },
  {
    "错误码": "85096",
    "错误描述": "not allow include scancode_time field",
    "解决方案": "scancode_time为系统保留参数，不允许配置"
  }
]
```

## 7. 适用范围

本接口在不同账号类型下的可调用情况：

```json
[
  {
    "小程序": "✔",
    "小游戏": "✔"
  }
]
```

- ✔：该账号可调用此接口。
- 其他未明确声明的账号类型，如无特殊说明，均不可调用此接口。
