# 数据预拉取和数据周期性更新

[调试诊断](https://developers.weixin.qq.com/console/devtools/debug?utm_source=api_tools)

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

接口英文名：getFetchdataSetting

- 调用本接口可以获取数据预拉取和数据周期性更新配置、设置预拉取数据、设置周期性拉取数据。
- 使用过程中如遇到问题，可在[开放平台服务商专区](https://developers.weixin.qq.com/community/minihome/mixflow/1355698687267438595)发帖交流。 

## 1. 调用方式

### HTTPS 调用

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

### 云调用

- 本接口不支持云调用。

### 第三方调用

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

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

- 服务商获得其中之一权限集授权后，可通过使用 [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",
    "必填": "是",
    "说明": "接口调用凭证，可使用 [authorizer_access_token](/doc/oplatform/openApi/ticket-token/api_getauthorizeraccesstoken)"
  }
]
```

### 请求体 `Request Payload`

```json
[
  {
    "参数名": "action",
    "类型": "string",
    "必填": "是",
    "说明": "可填\"get\"、“ set_pre_fetch”、\"set_period_fetch\""
  },
  {
    "参数名": "is_pre_fetch_open",
    "类型": "boolean",
    "必填": "否",
    "说明": "数据预拉取是否开启，action=set_pre_fetch时，必填"
  },
  {
    "参数名": "pre_fetch_type",
    "类型": "number",
    "必填": "否",
    "说明": "数据来源，0: 开发者服务器；1：云函数。action=set_pre_fetch时，必填"
  },
  {
    "参数名": "pre_fetch_url",
    "类型": "string",
    "必填": "否",
    "说明": "数据下载地址，当pre_fetch_type=0必填。\n说明，1、需要将域名先添加到第三方平台的业务域名；2、然后将该域名添加到小程序业务域名；才可以成功设置"
  },
  {
    "参数名": "pre_env",
    "类型": "string",
    "必填": "否",
    "说明": "环境ID，当pre_fetch_type=1必填"
  },
  {
    "参数名": "pre_function_name",
    "类型": "string",
    "必填": "否",
    "说明": "函数名，当pre_fetch_type=1必填"
  },
  {
    "参数名": "is_period_fetch_open",
    "类型": "boolean",
    "必填": "否",
    "说明": "周期性拉取数据是否开启，true开启，false关闭。action= set_period_fetch时，必填"
  },
  {
    "参数名": "period_fetch_type",
    "类型": "number",
    "必填": "否",
    "说明": "数据来源，0: 开发者服务器；1：云函数。action= set_period_fetch时，必填"
  },
  {
    "参数名": "period_fetch_url",
    "类型": "string",
    "必填": "否",
    "说明": "数据下载地址，当period_fetch_type=0必填；\n1、需要将域名先添加到第三方平台的业务域名；2、然后将该域名添加到小程序业务域名；才可以成功设置"
  },
  {
    "参数名": "period_env",
    "类型": "string",
    "必填": "否",
    "说明": "环境ID，当period_fetch_type=1必填"
  },
  {
    "参数名": "period_function_name",
    "类型": "string",
    "必填": "否",
    "说明": "函数名，当period_fetch_type=1必填"
  }
]
```

## 3. 返回参数

### 返回体 `Response Payload`

```json
[
  {
    "参数名": "errcode",
    "类型": "number",
    "说明": "返回码"
  },
  {
    "参数名": "errmsg",
    "类型": "string",
    "说明": "错误信息"
  },
  {
    "参数名": "is_pre_fetch_open",
    "类型": "boolean",
    "说明": "数据预拉取是否开启"
  },
  {
    "参数名": "pre_fetch_type",
    "类型": "number",
    "说明": "数据来源，0: 开发者服务器；1：云函数"
  },
  {
    "参数名": "pre_fetch_url",
    "类型": "string",
    "说明": "数据下载地址，当pre_fetch_type=0有效"
  },
  {
    "参数名": "pre_env",
    "类型": "string",
    "说明": "环境ID，当pre_fetch_type=1有效"
  },
  {
    "参数名": "pre_function_name",
    "类型": "string",
    "说明": "函数名，当pre_fetch_type=1有效"
  },
  {
    "参数名": "is_period_fetch_open",
    "类型": "boolean",
    "说明": "数据周期性更新是否开启"
  },
  {
    "参数名": "period_fetch_type",
    "类型": "number",
    "说明": "0: 开发者服务器；1：云函数"
  },
  {
    "参数名": "period_fetch_url",
    "类型": "string",
    "说明": "数据下载地址当period_fetch_type=0有效"
  },
  {
    "参数名": "period_env",
    "类型": "string",
    "说明": "环境ID，当period_fetch_type=1有效"
  },
  {
    "参数名": "period_function_name",
    "类型": "string",
    "说明": "函数名，当period_fetch_type=1有效"
  }
]
```

## 4. 注意事项

- 该接口有三个功能，通过action的值进行区分，请详细按照接口文档调用接口。
- pre_fetch_url或者period_fetch_url的域名需要配置为小程序的业务域名

## 5. 代码示例

### 5.1 获取数据预拉取和数据周期性更新配置

请求示例

```json
{
  "action": "get"
}
```

返回示例

```json
{
  "errcode": 0,
  "errmsg": "ok",
  "is_pre_fetch_open": true,
  "pre_fetch_type": 1,
  "pre_env": "test-pfyol",
  "pre_function_name": "test_func",
  "is_period_fetch_open": true,
  "period_fetch_type": 0,
  "period_fetch_url": "https://111.qq.com"
}
```

### 5.2 设置预拉取数据

请求示例

```json
{
  "action": "set_pre_fetch",
  "is_pre_fetch_open": true,
  "pre_fetch_type": 0,
  "pre_fetch_url": "https://fff.com"
}
```

返回示例

```json
{
  "errcode": 0,
  "errmsg": "ok"
}
```

### 5.3 设置周期性拉取数据

请求示例

```json
{
  "action": "set_period_fetch",
  "is_period_fetch_open": true,
  "period_fetch_type": 1,
  "period_env": "test-pfyol",
  "period_function_name": "login"
}
```

返回示例

```json
{
  "errcode": 0,
  "errmsg": "ok"
}
```

## 6. 错误码

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

```json
[
  {
    "错误码": "-1",
    "错误描述": "system error",
    "解决方案": "系统繁忙，此时请开发者稍候再试"
  },
  {
    "错误码": "40166",
    "错误描述": "invalid weapp appid",
    "解决方案": "非小程序发起调用"
  },
  {
    "错误码": "9410013",
    "错误描述": "无效action",
    "解决方案": "无效action"
  },
  {
    "错误码": "9410016",
    "错误描述": "",
    "解决方案": "存在无效域名。具体原因见errmsg（可能有多种原因）。"
  }
]
```

## 7. 适用范围

本接口支持「第三方平台」账号类型代调用，权限集请参考「调用方式」部分。其他账号类型如无特殊说明，均不可调用。
