20 lines
12 KiB
JavaScript
20 lines
12 KiB
JavaScript
import{_ as i,o as a,c as t,a2 as e}from"./chunks/framework.B-QBKwc1.js";const c=JSON.parse('{"title":"公共函数速查","description":"以下函数定义在 app/common.php 中,可在任何地方直接调用。","frontmatter":{},"headers":[],"relativePath":"zh/appendix/functions.md","filePath":"zh/appendix/functions.md"}'),n={name:"zh/appendix/functions.md"};function h(d,s,l,r,p,k){return a(),t("div",null,[...s[0]||(s[0]=[e(`<h1 id="D-公共函数速查" tabindex="-1">D. 公共函数速查 <a class="header-anchor" href="#D-公共函数速查" aria-label="Permalink to "D. 公共函数速查""></a></h1><div class="tip custom-block"><p class="custom-block-title">概述</p><p>以下函数定义在 <code>app/common.php</code> 中,可在任何地方直接调用。</p></div><h2 id="字符串-加密" tabindex="-1">字符串 / 加密 <a class="header-anchor" href="#字符串-加密" aria-label="Permalink to "字符串 / 加密""></a></h2><table tabindex="0"><thead><tr><th>函数</th><th>说明</th><th>签名</th></tr></thead><tbody><tr><td><code>random_string($length)</code></td><td>生成随机字符串</td><td><code>string random_string(int $length = 16)</code></td></tr><tr><td><code>encrypt_password($password, $salt)</code></td><td>密码加密(bcrypt)</td><td><code>array encrypt_password(string $password, ?string $salt = null)</code></td></tr><tr><td><code>verify_password($password, $hash, $salt)</code></td><td>验证密码</td><td><code>bool verify_password(string $password, string $encryptPassword, string $salt)</code></td></tr></tbody></table><h2 id="文件处理" tabindex="-1">文件处理 <a class="header-anchor" href="#文件处理" aria-label="Permalink to "文件处理""></a></h2><table tabindex="0"><thead><tr><th>函数</th><th>说明</th><th>签名</th></tr></thead><tbody><tr><td><code>upload_file($formName, $type)</code></td><td>上传文件到临时目录</td><td><code>array upload_file(string $formName = 'file', string $type = 'file')</code></td></tr><tr><td><code>save_file($fileUrl, $saveDir)</code></td><td>临时文件迁移到正式目录</td><td><code>string|false save_file(string $fileUrl, string $saveDir = '')</code></td></tr><tr><td><code>save_content(&$content, $saveDir)</code></td><td>处理富文本中的媒体文件</td><td><code>bool save_content(string &$content, string $saveDir = 'article')</code></td></tr><tr><td><code>get_file_url($path)</code></td><td>相对路径转完整 URL</td><td><code>string get_file_url(string $path)</code></td></tr><tr><td><code>create_file_path($saveDir, $ext)</code></td><td>生成文件存储路径</td><td><code>string create_file_path(string $saveDir = '', string $ext = '')</code></td></tr><tr><td><code>save_remote_file($url, $saveDir)</code></td><td>保存远程文件</td><td><code>string|false save_remote_file(string $url, string $saveDir = '')</code></td></tr><tr><td><code>detect_image_ext($content)</code></td><td>根据内容检测图片后缀</td><td><code>?string detect_image_ext(string $content)</code></td></tr></tbody></table><h2 id="驼峰-下划线转换" tabindex="-1">驼峰 / 下划线转换 <a class="header-anchor" href="#驼峰-下划线转换" aria-label="Permalink to "驼峰 / 下划线转换""></a></h2><table tabindex="0"><thead><tr><th>函数</th><th>说明</th><th>签名</th></tr></thead><tbody><tr><td><code>snake_to_camel($str)</code></td><td>下划线转驼峰</td><td><code>string snake_to_camel(string $str)</code></td></tr><tr><td><code>camel_to_snake($str)</code></td><td>驼峰转下划线</td><td><code>string camel_to_snake(string $str)</code></td></tr><tr><td><code>array_snake_to_camel($data)</code></td><td>数组键名下划线转驼峰(递归)</td><td><code>array array_snake_to_camel(array $data)</code></td></tr><tr><td><code>array_camel_to_snake($data, $except)</code></td><td>数组键名驼峰转下划线(递归)</td><td><code>array array_camel_to_snake(array $data, array $except = [])</code></td></tr><tr><td><code>is_camel_convert_enabled()</code></td><td>是否开启转换</td><td><code>bool is_camel_convert_enabled()</code></td></tr></tbody></table><h2 id="工具函数" tabindex="-1">工具函数 <a class="header-anchor" href="#工具函数" aria-label="Permalink to "工具函数""></a></h2><table tabindex="0"><thead><tr><th>函数</th><th>说明</th><th>签名</th></tr></thead><tbody><tr><td><code>get_client_ip()</code></td><td>获取客户端 IP</td><td><code>string get_client_ip()</code></td></tr><tr><td><code>format_page_params($params)</code></td><td>格式化分页参数</td><td><code>array format_page_params(array $params)</code></td></tr></tbody></table><h2 id="使用示例" tabindex="-1">使用示例 <a class="header-anchor" href="#使用示例" aria-label="Permalink to "使用示例""></a></h2><div class="language-php vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">php</span><pre class="shiki shiki-themes dracula-soft vitesse-light vp-code" tabindex="0"><code><span class="line"><span style="--shiki-dark:#7B7F8B;--shiki-light:#A0ADA0;">// 生成随机字符串</span></span>
|
||
<span class="line"><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">$</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#B07D48;">str</span><span style="--shiki-dark:#F286C4;--shiki-light:#999999;"> =</span><span style="--shiki-dark:#62E884;--shiki-light:#59873A;"> random_string</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">(</span><span style="--shiki-dark:#BF9EEE;--shiki-light:#2F798A;">32</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-dark:#7B7F8B;--shiki-light:#A0ADA0;">// 密码加密</span></span>
|
||
<span class="line"><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">$</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#B07D48;">result</span><span style="--shiki-dark:#F286C4;--shiki-light:#999999;"> =</span><span style="--shiki-dark:#62E884;--shiki-light:#59873A;"> encrypt_password</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">(</span><span style="--shiki-dark:#DEE492;--shiki-light:#B5695977;">'</span><span style="--shiki-dark:#E7EE98;--shiki-light:#B56959;">123456</span><span style="--shiki-dark:#DEE492;--shiki-light:#B5695977;">'</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">);</span></span>
|
||
<span class="line"><span style="--shiki-dark:#7B7F8B;--shiki-light:#A0ADA0;">// ['password' => '$2y$12$...', 'salt' => 'xK7pMn3qRt']</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-dark:#7B7F8B;--shiki-light:#A0ADA0;">// 验证密码</span></span>
|
||
<span class="line"><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">$</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#B07D48;">ok</span><span style="--shiki-dark:#F286C4;--shiki-light:#999999;"> =</span><span style="--shiki-dark:#62E884;--shiki-light:#59873A;"> verify_password</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">(</span><span style="--shiki-dark:#DEE492;--shiki-light:#B5695977;">'</span><span style="--shiki-dark:#E7EE98;--shiki-light:#B56959;">123456</span><span style="--shiki-dark:#DEE492;--shiki-light:#B5695977;">'</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">,</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;"> $</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#B07D48;">user</span><span style="--shiki-dark:#F286C4;--shiki-light:#AB5959;">-></span><span style="--shiki-dark:#F6F6F4;--shiki-light:#B07D48;">password</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">,</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;"> $</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#B07D48;">user</span><span style="--shiki-dark:#F286C4;--shiki-light:#AB5959;">-></span><span style="--shiki-dark:#F6F6F4;--shiki-light:#B07D48;">salt</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-dark:#7B7F8B;--shiki-light:#A0ADA0;">// 文件上传</span></span>
|
||
<span class="line"><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">$</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#B07D48;">result</span><span style="--shiki-dark:#F286C4;--shiki-light:#999999;"> =</span><span style="--shiki-dark:#62E884;--shiki-light:#59873A;"> upload_file</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">(</span><span style="--shiki-dark:#DEE492;--shiki-light:#B5695977;">'</span><span style="--shiki-dark:#E7EE98;--shiki-light:#B56959;">avatar</span><span style="--shiki-dark:#DEE492;--shiki-light:#B5695977;">'</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">,</span><span style="--shiki-dark:#DEE492;--shiki-light:#B5695977;"> '</span><span style="--shiki-dark:#E7EE98;--shiki-light:#B56959;">image</span><span style="--shiki-dark:#DEE492;--shiki-light:#B5695977;">'</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">);</span></span>
|
||
<span class="line"><span style="--shiki-dark:#F286C4;--shiki-light:#1E754F;">if</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;"> (</span><span style="--shiki-dark:#97E1F1;--shiki-light:#998418;">isset</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">($</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#B07D48;">result</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">[</span><span style="--shiki-dark:#DEE492;--shiki-light:#B5695977;">'</span><span style="--shiki-dark:#E7EE98;--shiki-light:#B56959;">error</span><span style="--shiki-dark:#DEE492;--shiki-light:#B5695977;">'</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">]))</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;"> {</span></span>
|
||
<span class="line"><span style="--shiki-dark:#F286C4;--shiki-light:#1E754F;"> return</span><span style="--shiki-dark:#97E1F1;--shiki-dark-font-style:italic;--shiki-light:#998418;--shiki-light-font-style:inherit;"> Result</span><span style="--shiki-dark:#F286C4;--shiki-light:#AB5959;">::</span><span style="--shiki-dark:#62E884;--shiki-light:#59873A;">fail</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">($</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#B07D48;">result</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">[</span><span style="--shiki-dark:#DEE492;--shiki-light:#B5695977;">'</span><span style="--shiki-dark:#E7EE98;--shiki-light:#B56959;">error</span><span style="--shiki-dark:#DEE492;--shiki-light:#B5695977;">'</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">]);</span></span>
|
||
<span class="line"><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">}</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-dark:#7B7F8B;--shiki-light:#A0ADA0;">// 驼峰转换</span></span>
|
||
<span class="line"><span style="--shiki-dark:#62E884;--shiki-light:#59873A;">camel_to_snake</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">(</span><span style="--shiki-dark:#DEE492;--shiki-light:#B5695977;">'</span><span style="--shiki-dark:#E7EE98;--shiki-light:#B56959;">createTime</span><span style="--shiki-dark:#DEE492;--shiki-light:#B5695977;">'</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">);</span><span style="--shiki-dark:#7B7F8B;--shiki-light:#A0ADA0;"> // 'create_time'</span></span>
|
||
<span class="line"><span style="--shiki-dark:#62E884;--shiki-light:#59873A;">snake_to_camel</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">(</span><span style="--shiki-dark:#DEE492;--shiki-light:#B5695977;">'</span><span style="--shiki-dark:#E7EE98;--shiki-light:#B56959;">create_time</span><span style="--shiki-dark:#DEE492;--shiki-light:#B5695977;">'</span><span style="--shiki-dark:#F6F6F4;--shiki-light:#999999;">);</span><span style="--shiki-dark:#7B7F8B;--shiki-light:#A0ADA0;"> // 'createTime'</span></span></code></pre></div>`,12)])])}const g=i(n,[["render",h]]);export{c as __pageData,g as default};
|