Files
rxthinkcmf-wiki/RXThinkCMF_ThinkPhp8_AntdVue/zh/appendix/functions.md
T
2026-09-29 10:14:04 +08:00

66 lines
2.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# D. 公共函数速查
:::tip 概述
以下函数定义在 `app/common.php` 中,可在任何地方直接调用。
:::
## 字符串 / 加密
| 函数 | 说明 | 签名 |
|------|------|------|
| `random_string($length)` | 生成随机字符串 | `string random_string(int $length = 16)` |
| `encrypt_password($password, $salt)` | 密码加密(bcrypt) | `array encrypt_password(string $password, ?string $salt = null)` |
| `verify_password($password, $hash, $salt)` | 验证密码 | `bool verify_password(string $password, string $encryptPassword, string $salt)` |
## 文件处理
| 函数 | 说明 | 签名 |
|------|------|------|
| `upload_file($formName, $type)` | 上传文件到临时目录 | `array upload_file(string $formName = 'file', string $type = 'file')` |
| `save_file($fileUrl, $saveDir)` | 临时文件迁移到正式目录 | `string\|false save_file(string $fileUrl, string $saveDir = '')` |
| `save_content(&$content, $saveDir)` | 处理富文本中的媒体文件 | `bool save_content(string &$content, string $saveDir = 'article')` |
| `get_file_url($path)` | 相对路径转完整 URL | `string get_file_url(string $path)` |
| `create_file_path($saveDir, $ext)` | 生成文件存储路径 | `string create_file_path(string $saveDir = '', string $ext = '')` |
| `save_remote_file($url, $saveDir)` | 保存远程文件 | `string\|false save_remote_file(string $url, string $saveDir = '')` |
| `detect_image_ext($content)` | 根据内容检测图片后缀 | `?string detect_image_ext(string $content)` |
## 驼峰 / 下划线转换
| 函数 | 说明 | 签名 |
|------|------|------|
| `snake_to_camel($str)` | 下划线转驼峰 | `string snake_to_camel(string $str)` |
| `camel_to_snake($str)` | 驼峰转下划线 | `string camel_to_snake(string $str)` |
| `array_snake_to_camel($data)` | 数组键名下划线转驼峰(递归) | `array array_snake_to_camel(array $data)` |
| `array_camel_to_snake($data, $except)` | 数组键名驼峰转下划线(递归) | `array array_camel_to_snake(array $data, array $except = [])` |
| `is_camel_convert_enabled()` | 是否开启转换 | `bool is_camel_convert_enabled()` |
## 工具函数
| 函数 | 说明 | 签名 |
|------|------|------|
| `get_client_ip()` | 获取客户端 IP | `string get_client_ip()` |
| `format_page_params($params)` | 格式化分页参数 | `array format_page_params(array $params)` |
## 使用示例
```php
// 生成随机字符串
$str = random_string(32);
// 密码加密
$result = encrypt_password('123456');
// ['password' => '$2y$12$...', 'salt' => 'xK7pMn3qRt']
// 验证密码
$ok = verify_password('123456', $user->password, $user->salt);
// 文件上传
$result = upload_file('avatar', 'image');
if (isset($result['error'])) {
return Result::fail($result['error']);
}
// 驼峰转换
camel_to_snake('createTime'); // 'create_time'
snake_to_camel('create_time'); // 'createTime'
```