getCustomConfig

版本要求:
基础库≥2.0.0@ray-js/ray≥1.7.29
import { getCustomConfig } from '@ray-js/ray';

// Promise 调用
try {
  const res = await getCustomConfig();
  console.log(res);
} catch (err) {
  console.log(err.errorMsg);
}

描述

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

参数

重载 1getCustomConfig(options: Object)→void
属性类型必填说明
否接口调用成功的回调函数,参数为各配置项的键值对
否接口调用失败的回调函数
否接口调用结束的回调函数(调用成功、失败都会执行)
重载 2getCustomConfig()→Promise<Record<string, string | number | boolean | string>>

无

返回值

类型:void | Promise<Record<string, string | number | boolean | string>>
不传回调时返回 Promise,resolve 值为自定义配置键值对对象

错误码

错误码错误信息
7API Internal processing failed

引用对象

typeCustomConfigValue自定义配置项的值类型,支持字符串、数字、布尔值
export type CustomConfigValue =
  /** 字符串 */
  | string
  /** 数字 */
  | number
  /** 布尔值 */
  | boolean
  /** 链接(特殊字符串) */
  | string;

示例代码

回调方式

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 方式

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>
  );
}