---
name: "getCustomConfig"
mode: "api"
versionRequirements:
  - { name: "基础库", version: "2.0.0" }
  - { name: "@ray-js/ray", version: "1.7.29" }
async: true
---

## getCustomConfig

> [VERSION] 基础库 >= 2.0.0 | @ray-js/ray >= 1.7.29

> ⚡ **支持 Promise 调用** — 不传 success / fail / complete 回调时，该方法返回 Promise。

### 描述

获取小程序自定义配置信息。此接口用于获取在开发者后台配置的自定义业务配置，支持多种数据类型（链接、布尔值、数字、字符串）。自定义配置支持模板配置和实例配置的分层管理，实例配置优先于模板配置。配置修改后会自动同步到云端，小程序可以通过此接口获取最新的配置信息。

### 参数


#### 重载 1: `getCustomConfig(options: Object)` → `void`

| 参数 | 类型 | 必填 | 描述 |
| --- | --- | --- | --- |
| `success` | `(config: Record<string, CustomConfigValue>) => void` | 否 | 接口调用成功的回调函数，参数为各配置项的键值对 |
| `fail` | `(err: GetCustomConfigError) => void` | 否 | 接口调用失败的回调函数 |
| `complete` | `(res: Record<string, CustomConfigValue> \| GetCustomConfigError) => void` | 否 | 接口调用结束的回调函数（调用成功、失败都会执行） |

#### 重载 2: `getCustomConfig()` → `Promise<Record<string, string \| number \| boolean \| string>>`

无


### 返回值

类型: `void \| Promise<Record<string, string \| number \| boolean \| string>>`

不传回调时返回 Promise，resolve 值为自定义配置键值对对象

### 错误码

| 错误码 | 错误信息 |
| --- | --- |
| `7` | API Internal processing failed |

### 引用对象

##### `type` CustomConfigValue

自定义配置项的值类型，支持字符串、数字、布尔值

```typescript
export type CustomConfigValue =
  /** 字符串 */
  | string
  /** 数字 */
  | number
  /** 布尔值 */
  | boolean
  /** 链接(特殊字符串) */
  | string;
```

##### `interface` GetCustomConfigError

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `errorMsg` | `string` | 错误信息 |
| `errorCode` | `string` | 错误码 |


### 示例代码

#### 回调方式

```tsx
import { View, Button, Text, getCustomConfig } from '@ray-js/ray'

export default function Demo() {
  const handleGetConfig = () => {
    getCustomConfig({
      success(config) {
        console.log('自定义配置:', config);
      },
      fail(err) {
        console.error('获取失败:', err.errorMsg);
      },
    });
  };

  return (
    <View style={{ padding: 20 }}>
      <Button onClick={handleGetConfig}>获取自定义配置(回调)</Button>
    </View>
  );
}
```

#### Promise 方式

```tsx
import { View, Button, Text, getCustomConfig } from '@ray-js/ray'

export default function Demo() {
  const [config, setConfig] = useState('');

  const handleGetConfig = async () => {
    try {
      const result = await getCustomConfig();
      setConfig(JSON.stringify(result));
      console.log('自定义配置:', result);
    } catch (err) {
      console.error('获取失败:', err);
    }
  };

  return (
    <View style={{ padding: 20 }}>
      <Button onClick={handleGetConfig}>获取自定义配置(Promise)</Button>
      {config && <Text style={{ marginTop: 10 }}>{config}</Text>}
    </View>
  );
}
```
