2022-02-20 22:42:48 +00:00
|
|
|
|
<?php
|
|
|
|
|
// 应用公共文件
|
|
|
|
|
|
2023-07-12 12:26:27 +00:00
|
|
|
|
use think\App;
|
2023-06-25 20:01:31 +00:00
|
|
|
|
use ba\Filesystem;
|
2023-06-24 18:35:30 +00:00
|
|
|
|
use think\Response;
|
2022-04-23 09:57:29 +00:00
|
|
|
|
use think\facade\Db;
|
2022-08-23 16:27:21 +00:00
|
|
|
|
use think\facade\Lang;
|
2023-07-12 12:26:27 +00:00
|
|
|
|
use think\facade\Event;
|
2022-07-21 02:29:16 +00:00
|
|
|
|
use think\facade\Config;
|
2023-08-02 07:42:46 +00:00
|
|
|
|
use voku\helper\AntiXSS;
|
2022-07-21 03:00:46 +00:00
|
|
|
|
use app\admin\model\Config as configModel;
|
2022-07-21 02:29:16 +00:00
|
|
|
|
use think\exception\HttpResponseException;
|
2022-08-23 16:27:21 +00:00
|
|
|
|
use Symfony\Component\HttpFoundation\IpUtils;
|
2022-08-15 04:02:19 +00:00
|
|
|
|
|
2022-02-20 22:42:48 +00:00
|
|
|
|
if (!function_exists('__')) {
|
2023-06-24 18:35:30 +00:00
|
|
|
|
|
2022-03-13 13:23:43 +00:00
|
|
|
|
/**
|
|
|
|
|
* 语言翻译
|
|
|
|
|
* @param string $name 被翻译字符
|
|
|
|
|
* @param array $vars 替换字符数组
|
|
|
|
|
* @param string $lang 翻译语言
|
2023-06-24 18:35:30 +00:00
|
|
|
|
* @return mixed
|
2022-03-13 13:23:43 +00:00
|
|
|
|
*/
|
2023-06-24 18:35:30 +00:00
|
|
|
|
function __(string $name, array $vars = [], string $lang = ''): mixed
|
2022-02-20 22:42:48 +00:00
|
|
|
|
{
|
|
|
|
|
if (is_numeric($name) || !$name) {
|
|
|
|
|
return $name;
|
|
|
|
|
}
|
2022-08-23 16:27:21 +00:00
|
|
|
|
return Lang::get($name, $vars, $lang);
|
2022-02-20 22:42:48 +00:00
|
|
|
|
}
|
2022-03-01 16:24:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-01 13:40:48 +00:00
|
|
|
|
if (!function_exists('filter')) {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 输入过滤
|
2023-08-02 07:42:46 +00:00
|
|
|
|
* 富文本反XSS请使用 clean_xss,也就不需要及不能再 filter 了
|
2023-08-01 13:40:48 +00:00
|
|
|
|
* @param string $string 要过滤的字符串
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function filter(string $string): string
|
|
|
|
|
{
|
|
|
|
|
// 去除字符串两端空格(对防代码注入有一定作用)
|
|
|
|
|
$string = trim($string);
|
|
|
|
|
|
|
|
|
|
// 过滤html和php标签
|
|
|
|
|
$string = strip_tags($string);
|
|
|
|
|
|
|
|
|
|
// 特殊字符转实体
|
|
|
|
|
return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-02 07:42:46 +00:00
|
|
|
|
if (!function_exists('clean_xss')) {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 清理XSS
|
|
|
|
|
* 通常只用于富文本,比 filter 慢
|
|
|
|
|
* @param string $string
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2023-08-25 09:10:11 +00:00
|
|
|
|
function clean_xss(string $string): string
|
2023-08-02 07:42:46 +00:00
|
|
|
|
{
|
2023-08-25 09:10:11 +00:00
|
|
|
|
return (new AntiXSS())->xss_clean($string);
|
2023-08-02 07:42:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 16:42:15 +00:00
|
|
|
|
if (!function_exists('htmlspecialchars_decode_improve')) {
|
|
|
|
|
/**
|
|
|
|
|
* html解码增强
|
2024-01-31 09:58:56 +00:00
|
|
|
|
* 被 filter函数 内的 htmlspecialchars 编码的字符串,需要用此函数才能完全解码
|
2023-08-15 16:42:15 +00:00
|
|
|
|
* @param string $string
|
|
|
|
|
* @param int $flags
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function htmlspecialchars_decode_improve(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401): string
|
|
|
|
|
{
|
|
|
|
|
return htmlspecialchars_decode($string, $flags);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-30 16:37:59 +00:00
|
|
|
|
if (!function_exists('get_sys_config')) {
|
2023-06-24 18:35:30 +00:00
|
|
|
|
|
2022-06-25 03:31:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* 获取站点的系统配置,不传递参数则获取所有配置项
|
2023-06-24 18:35:30 +00:00
|
|
|
|
* @param string $name 变量名
|
|
|
|
|
* @param string $group 变量分组,传递此参数来获取某个分组的所有配置项
|
|
|
|
|
* @param bool $concise 是否开启简洁模式,简洁模式下,获取多项配置时只返回配置的键值对
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws Throwable
|
2022-06-25 03:31:02 +00:00
|
|
|
|
*/
|
2023-06-24 18:35:30 +00:00
|
|
|
|
function get_sys_config(string $name = '', string $group = '', bool $concise = true): mixed
|
2022-03-30 16:37:59 +00:00
|
|
|
|
{
|
|
|
|
|
if ($name) {
|
2022-06-25 03:31:02 +00:00
|
|
|
|
// 直接使用->value('value')不能使用到模型的类型格式化
|
2022-07-21 03:00:46 +00:00
|
|
|
|
$config = configModel::cache($name, null, configModel::$cacheTag)->where('name', $name)->find();
|
2022-06-25 03:31:02 +00:00
|
|
|
|
if ($config) $config = $config['value'];
|
2022-03-30 16:37:59 +00:00
|
|
|
|
} else {
|
2022-06-25 03:31:02 +00:00
|
|
|
|
if ($group) {
|
2022-07-21 03:00:46 +00:00
|
|
|
|
$temp = configModel::cache('group' . $group, null, configModel::$cacheTag)->where('group', $group)->select()->toArray();
|
2022-06-25 03:31:02 +00:00
|
|
|
|
} else {
|
2022-07-21 03:00:46 +00:00
|
|
|
|
$temp = configModel::cache('sys_config_all', null, configModel::$cacheTag)->order('weigh desc')->select()->toArray();
|
2022-06-25 03:31:02 +00:00
|
|
|
|
}
|
2023-06-24 18:35:30 +00:00
|
|
|
|
if ($concise) {
|
2022-06-25 03:31:02 +00:00
|
|
|
|
$config = [];
|
|
|
|
|
foreach ($temp as $item) {
|
|
|
|
|
$config[$item['name']] = $item['value'];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$config = $temp;
|
|
|
|
|
}
|
2022-03-30 16:37:59 +00:00
|
|
|
|
}
|
2022-06-25 03:31:02 +00:00
|
|
|
|
return $config;
|
2022-03-30 16:37:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 16:24:27 +00:00
|
|
|
|
if (!function_exists('get_route_remark')) {
|
2023-06-24 18:35:30 +00:00
|
|
|
|
|
2022-03-13 13:23:43 +00:00
|
|
|
|
/**
|
|
|
|
|
* 获取当前路由后台菜单规则的备注信息
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2023-06-24 18:35:30 +00:00
|
|
|
|
function get_route_remark(): string
|
2022-03-01 16:24:27 +00:00
|
|
|
|
{
|
2023-06-24 18:35:30 +00:00
|
|
|
|
$controllerName = request()->controller(true);
|
|
|
|
|
$actionName = request()->action(true);
|
|
|
|
|
$path = str_replace('.', '/', $controllerName);
|
2022-03-17 16:09:51 +00:00
|
|
|
|
|
2023-06-24 23:04:22 +00:00
|
|
|
|
$remark = Db::name('admin_rule')
|
2022-03-17 16:09:51 +00:00
|
|
|
|
->where('name', $path)
|
2023-06-24 18:35:30 +00:00
|
|
|
|
->whereOr('name', $path . '/' . $actionName)
|
2022-03-17 16:09:51 +00:00
|
|
|
|
->value('remark');
|
2022-05-07 14:37:58 +00:00
|
|
|
|
return __((string)$remark);
|
2022-03-01 16:24:27 +00:00
|
|
|
|
}
|
2022-03-11 17:29:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!function_exists('full_url')) {
|
2023-06-24 18:35:30 +00:00
|
|
|
|
|
2022-03-11 17:29:06 +00:00
|
|
|
|
/**
|
2023-06-24 18:35:30 +00:00
|
|
|
|
* 获取资源完整url地址;若安装了云存储或 config/buildadmin.php 配置了CdnUrl,则自动使用对应的CdnUrl
|
2024-07-02 12:15:26 +00:00
|
|
|
|
* @param string $relativeUrl 资源相对地址 不传入则获取域名
|
|
|
|
|
* @param string|bool $domain 是否携带域名 或者直接传入域名
|
|
|
|
|
* @param string $default 默认值
|
2022-03-11 17:29:06 +00:00
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2024-07-02 12:15:26 +00:00
|
|
|
|
function full_url(string $relativeUrl = '', string|bool $domain = true, string $default = ''): string
|
2022-03-11 17:29:06 +00:00
|
|
|
|
{
|
2023-07-12 12:26:27 +00:00
|
|
|
|
// 存储/上传资料配置
|
|
|
|
|
Event::trigger('uploadConfigInit', App::getInstance());
|
|
|
|
|
|
2022-08-03 03:34:42 +00:00
|
|
|
|
$cdnUrl = Config::get('buildadmin.cdn_url');
|
2024-03-09 07:56:15 +00:00
|
|
|
|
if (!$cdnUrl) $cdnUrl = request()->upload['cdn'] ?? '//' . request()->host();
|
2022-08-03 03:34:42 +00:00
|
|
|
|
if ($domain === true) {
|
2022-09-18 07:19:18 +00:00
|
|
|
|
$domain = $cdnUrl;
|
2022-08-03 03:34:42 +00:00
|
|
|
|
} elseif ($domain === false) {
|
|
|
|
|
$domain = '';
|
2022-03-11 17:29:06 +00:00
|
|
|
|
}
|
2022-08-03 03:34:42 +00:00
|
|
|
|
|
2022-08-23 16:27:21 +00:00
|
|
|
|
$relativeUrl = $relativeUrl ?: $default;
|
2022-08-03 03:34:42 +00:00
|
|
|
|
if (!$relativeUrl) return $domain;
|
|
|
|
|
|
2022-03-11 17:29:06 +00:00
|
|
|
|
$regex = "/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i";
|
|
|
|
|
if (preg_match('/^http(s)?:\/\//', $relativeUrl) || preg_match($regex, $relativeUrl) || $domain === false) {
|
|
|
|
|
return $relativeUrl;
|
|
|
|
|
}
|
2022-08-03 03:34:42 +00:00
|
|
|
|
return $domain . $relativeUrl;
|
2022-03-11 17:29:06 +00:00
|
|
|
|
}
|
2022-03-13 13:23:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-19 16:07:34 +00:00
|
|
|
|
if (!function_exists('encrypt_password')) {
|
2023-06-24 18:35:30 +00:00
|
|
|
|
|
2022-06-19 16:07:34 +00:00
|
|
|
|
/**
|
|
|
|
|
* 加密密码
|
|
|
|
|
*/
|
|
|
|
|
function encrypt_password($password, $salt = '', $encrypt = 'md5')
|
|
|
|
|
{
|
|
|
|
|
return $encrypt($encrypt($password) . $salt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 15:44:27 +00:00
|
|
|
|
if (!function_exists('str_attr_to_array')) {
|
2023-06-24 18:35:30 +00:00
|
|
|
|
|
2023-03-21 15:44:27 +00:00
|
|
|
|
/**
|
|
|
|
|
* 将字符串属性列表转为数组
|
|
|
|
|
* @param string $attr 属性,一行一个,无需引号,比如:class=input-class
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
function str_attr_to_array(string $attr): array
|
|
|
|
|
{
|
|
|
|
|
if (!$attr) return [];
|
|
|
|
|
$attr = explode("\n", trim(str_replace("\r\n", "\n", $attr)));
|
|
|
|
|
$attrTemp = [];
|
|
|
|
|
foreach ($attr as $item) {
|
|
|
|
|
$item = explode('=', $item);
|
2023-06-13 12:22:04 +00:00
|
|
|
|
if (isset($item[0]) && isset($item[1])) {
|
2023-03-24 16:53:02 +00:00
|
|
|
|
$attrVal = $item[1];
|
|
|
|
|
if ($item[1] === 'false' || $item[1] === 'true') {
|
|
|
|
|
$attrVal = !($item[1] === 'false');
|
|
|
|
|
} elseif (is_numeric($item[1])) {
|
|
|
|
|
$attrVal = (float)$item[1];
|
|
|
|
|
}
|
2023-03-21 15:44:27 +00:00
|
|
|
|
if (strpos($item[0], '.')) {
|
|
|
|
|
$attrKey = explode('.', $item[0]);
|
2023-06-13 12:22:04 +00:00
|
|
|
|
if (isset($attrKey[0]) && isset($attrKey[1])) {
|
2023-03-24 16:53:02 +00:00
|
|
|
|
$attrTemp[$attrKey[0]][$attrKey[1]] = $attrVal;
|
2023-03-21 15:44:27 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-24 16:53:02 +00:00
|
|
|
|
$attrTemp[$item[0]] = $attrVal;
|
2023-03-21 15:44:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $attrTemp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-19 16:07:34 +00:00
|
|
|
|
if (!function_exists('action_in_arr')) {
|
2023-06-24 18:35:30 +00:00
|
|
|
|
|
2022-06-19 16:07:34 +00:00
|
|
|
|
/**
|
|
|
|
|
* 检测一个方法是否在传递的数组内
|
2022-10-29 17:49:43 +00:00
|
|
|
|
* @param array $arr
|
2022-06-19 16:07:34 +00:00
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2022-10-29 17:49:43 +00:00
|
|
|
|
function action_in_arr(array $arr = []): bool
|
2022-06-19 16:07:34 +00:00
|
|
|
|
{
|
|
|
|
|
$arr = is_array($arr) ? $arr : explode(',', $arr);
|
|
|
|
|
if (!$arr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$arr = array_map('strtolower', $arr);
|
|
|
|
|
if (in_array(strtolower(request()->action()), $arr) || in_array('*', $arr)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-13 13:23:43 +00:00
|
|
|
|
if (!function_exists('build_suffix_svg')) {
|
2023-06-24 18:35:30 +00:00
|
|
|
|
|
2022-03-13 13:23:43 +00:00
|
|
|
|
/**
|
|
|
|
|
* 构建文件后缀的svg图片
|
2023-06-24 18:35:30 +00:00
|
|
|
|
* @param string $suffix 文件后缀
|
|
|
|
|
* @param ?string $background 背景颜色,如:rgb(255,255,255)
|
2022-03-13 13:23:43 +00:00
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2022-10-29 17:49:43 +00:00
|
|
|
|
function build_suffix_svg(string $suffix = 'file', string $background = null): string
|
2022-03-13 13:23:43 +00:00
|
|
|
|
{
|
|
|
|
|
$suffix = mb_substr(strtoupper($suffix), 0, 4);
|
|
|
|
|
$total = unpack('L', hash('adler32', $suffix, true))[1];
|
|
|
|
|
$hue = $total % 360;
|
|
|
|
|
[$r, $g, $b] = hsv2rgb($hue / 360, 0.3, 0.9);
|
|
|
|
|
|
2022-10-29 17:49:43 +00:00
|
|
|
|
$background = $background ?: "rgb($r,$g,$b)";
|
2022-03-13 13:23:43 +00:00
|
|
|
|
|
2022-08-23 16:27:21 +00:00
|
|
|
|
return '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
|
2022-03-13 13:23:43 +00:00
|
|
|
|
<path style="fill:#E2E5E7;" d="M128,0c-17.6,0-32,14.4-32,32v448c0,17.6,14.4,32,32,32h320c17.6,0,32-14.4,32-32V128L352,0H128z"/>
|
|
|
|
|
<path style="fill:#B0B7BD;" d="M384,128h96L352,0v96C352,113.6,366.4,128,384,128z"/>
|
|
|
|
|
<polygon style="fill:#CAD1D8;" points="480,224 384,128 480,128 "/>
|
|
|
|
|
<path style="fill:' . $background . ';" d="M416,416c0,8.8-7.2,16-16,16H48c-8.8,0-16-7.2-16-16V256c0-8.8,7.2-16,16-16h352c8.8,0,16,7.2,16,16 V416z"/>
|
|
|
|
|
<path style="fill:#CAD1D8;" d="M400,432H96v16h304c8.8,0,16-7.2,16-16v-16C416,424.8,408.8,432,400,432z"/>
|
|
|
|
|
<g><text><tspan x="220" y="380" font-size="124" font-family="Verdana, Helvetica, Arial, sans-serif" fill="white" text-anchor="middle">' . $suffix . '</tspan></text></g>
|
|
|
|
|
</svg>';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-23 09:57:29 +00:00
|
|
|
|
if (!function_exists('get_area')) {
|
2023-06-24 18:35:30 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取省份地区数据
|
|
|
|
|
* @throws Throwable
|
|
|
|
|
*/
|
|
|
|
|
function get_area(): array
|
2022-04-23 09:57:29 +00:00
|
|
|
|
{
|
2022-08-23 16:27:21 +00:00
|
|
|
|
$province = request()->get('province', '');
|
|
|
|
|
$city = request()->get('city', '');
|
|
|
|
|
$where = ['pid' => 0, 'level' => 1];
|
2022-04-23 09:57:29 +00:00
|
|
|
|
if ($province !== '') {
|
|
|
|
|
$where['pid'] = $province;
|
|
|
|
|
$where['level'] = 2;
|
|
|
|
|
if ($city !== '') {
|
|
|
|
|
$where['pid'] = $city;
|
|
|
|
|
$where['level'] = 3;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-24 18:35:30 +00:00
|
|
|
|
return Db::name('area')
|
|
|
|
|
->where($where)
|
|
|
|
|
->field('id as value,name as label')
|
|
|
|
|
->select()
|
|
|
|
|
->toArray();
|
2022-04-23 09:57:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-13 13:23:43 +00:00
|
|
|
|
if (!function_exists('hsv2rgb')) {
|
2022-10-29 17:49:43 +00:00
|
|
|
|
function hsv2rgb($h, $s, $v): array
|
2022-03-13 13:23:43 +00:00
|
|
|
|
{
|
|
|
|
|
$r = $g = $b = 0;
|
|
|
|
|
|
|
|
|
|
$i = floor($h * 6);
|
|
|
|
|
$f = $h * 6 - $i;
|
|
|
|
|
$p = $v * (1 - $s);
|
|
|
|
|
$q = $v * (1 - $f * $s);
|
|
|
|
|
$t = $v * (1 - (1 - $f) * $s);
|
|
|
|
|
|
|
|
|
|
switch ($i % 6) {
|
|
|
|
|
case 0:
|
|
|
|
|
$r = $v;
|
|
|
|
|
$g = $t;
|
|
|
|
|
$b = $p;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
$r = $q;
|
|
|
|
|
$g = $v;
|
|
|
|
|
$b = $p;
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
$r = $p;
|
|
|
|
|
$g = $v;
|
|
|
|
|
$b = $t;
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
$r = $p;
|
|
|
|
|
$g = $q;
|
|
|
|
|
$b = $v;
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
$r = $t;
|
|
|
|
|
$g = $p;
|
|
|
|
|
$b = $v;
|
|
|
|
|
break;
|
|
|
|
|
case 5:
|
|
|
|
|
$r = $v;
|
|
|
|
|
$g = $p;
|
|
|
|
|
$b = $q;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
floor($r * 255),
|
|
|
|
|
floor($g * 255),
|
|
|
|
|
floor($b * 255)
|
|
|
|
|
];
|
|
|
|
|
}
|
2022-07-21 02:29:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!function_exists('ip_check')) {
|
2023-06-24 18:35:30 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* IP检查
|
|
|
|
|
* @throws Throwable
|
|
|
|
|
*/
|
|
|
|
|
function ip_check($ip = null): void
|
2022-07-21 02:29:16 +00:00
|
|
|
|
{
|
|
|
|
|
$ip = is_null($ip) ? request()->ip() : $ip;
|
|
|
|
|
$noAccess = get_sys_config('no_access_ip');
|
|
|
|
|
$noAccess = !$noAccess ? [] : array_filter(explode("\n", str_replace("\r\n", "\n", $noAccess)));
|
2022-08-23 16:27:21 +00:00
|
|
|
|
if ($noAccess && IpUtils::checkIp($ip, $noAccess)) {
|
2022-07-21 02:29:16 +00:00
|
|
|
|
$response = Response::create(['msg' => 'No permission request'], 'json', 403);
|
|
|
|
|
throw new HttpResponseException($response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!function_exists('set_timezone')) {
|
2023-06-24 18:35:30 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置时区
|
|
|
|
|
* @throws Throwable
|
|
|
|
|
*/
|
|
|
|
|
function set_timezone($timezone = null): void
|
2022-07-21 02:29:16 +00:00
|
|
|
|
{
|
|
|
|
|
$defaultTimezone = Config::get('app.default_timezone');
|
|
|
|
|
$timezone = is_null($timezone) ? get_sys_config('time_zone') : $timezone;
|
|
|
|
|
if ($timezone && $defaultTimezone != $timezone) {
|
|
|
|
|
Config::set([
|
|
|
|
|
'app.default_timezone' => $timezone
|
|
|
|
|
]);
|
|
|
|
|
date_default_timezone_set($timezone);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-02 11:54:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-17 15:30:21 +00:00
|
|
|
|
if (!function_exists('get_upload_config')) {
|
2023-06-24 18:35:30 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取上传配置
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
function get_upload_config(): array
|
2022-09-17 15:30:21 +00:00
|
|
|
|
{
|
2023-07-12 12:26:27 +00:00
|
|
|
|
// 存储/上传资料配置
|
|
|
|
|
Event::trigger('uploadConfigInit', App::getInstance());
|
|
|
|
|
|
2024-08-03 13:39:05 +00:00
|
|
|
|
$uploadConfig = Config::get('upload');
|
|
|
|
|
$uploadConfig['max_size'] = Filesystem::fileUnitToByte($uploadConfig['max_size']);
|
2022-09-17 17:05:00 +00:00
|
|
|
|
|
|
|
|
|
$upload = request()->upload;
|
2022-09-17 15:30:21 +00:00
|
|
|
|
if (!$upload) {
|
|
|
|
|
$uploadConfig['mode'] = 'local';
|
|
|
|
|
return $uploadConfig;
|
|
|
|
|
}
|
2022-09-18 07:19:18 +00:00
|
|
|
|
unset($upload['cdn']);
|
2022-09-17 15:30:21 +00:00
|
|
|
|
return array_merge($upload, $uploadConfig);
|
|
|
|
|
}
|
2022-09-17 16:46:48 +00:00
|
|
|
|
}
|
2024-03-11 16:37:09 +00:00
|
|
|
|
|
|
|
|
|
if (!function_exists('get_auth_token')) {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取鉴权 token
|
|
|
|
|
* @param array $names
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function get_auth_token(array $names = ['ba', 'token']): string
|
|
|
|
|
{
|
|
|
|
|
$separators = [
|
|
|
|
|
'header' => ['', '-'], // batoken、ba-token【ba_token 不在 header 的接受列表内因为兼容性不高,改用 http_ba_token】
|
|
|
|
|
'param' => ['', '-', '_'], // batoken、ba-token、ba_token
|
|
|
|
|
'server' => ['_'], // http_ba_token
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$tokens = [];
|
|
|
|
|
$request = request();
|
|
|
|
|
foreach ($separators as $fun => $sps) {
|
|
|
|
|
foreach ($sps as $sp) {
|
|
|
|
|
$tokens[] = $request->$fun(($fun == 'server' ? 'http_' : '') . implode($sp, $names));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$tokens = array_filter($tokens);
|
|
|
|
|
return array_values($tokens)[0] ?? '';
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-03 13:39:05 +00:00
|
|
|
|
|
|
|
|
|
if (!function_exists('keys_to_camel_case')) {
|
|
|
|
|
|
|
|
|
|
/**
|
2024-08-03 22:14:21 +00:00
|
|
|
|
* 将数组 key 的命名方式转换为小写驼峰
|
|
|
|
|
* @param array $array 被转换的数组
|
|
|
|
|
* @param array $keys 要转换的 key,默认所有
|
2024-08-03 13:39:05 +00:00
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2024-08-03 22:14:21 +00:00
|
|
|
|
function keys_to_camel_case(array $array, array $keys = []): array
|
2024-08-03 13:39:05 +00:00
|
|
|
|
{
|
|
|
|
|
$result = [];
|
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
|
// 将键名转换为驼峰命名
|
2024-08-03 22:14:21 +00:00
|
|
|
|
$camelCaseKey = $keys && in_array($key, $keys) ? parse_name($key, 1, false) : $key;
|
|
|
|
|
|
2024-08-03 13:39:05 +00:00
|
|
|
|
if (is_array($value)) {
|
|
|
|
|
// 如果值是数组,递归转换
|
|
|
|
|
$result[$camelCaseKey] = keys_to_camel_case($value);
|
|
|
|
|
} else {
|
|
|
|
|
$result[$camelCaseKey] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
}
|