Files
orico-official-website/app/index/common.php

225 lines
7.0 KiB
PHP

<?php
// 这是系统自动生成的公共文件
use think\facade\Lang;
if (!function_exists('str_contains')) {
/**
* 检查字符串是否包含子字符串
* @param string $haystack 要搜索的字符串
* @param string $needle 要搜索的子字符串
* @return bool
*/
function str_contains(string $haystack, string $needle): bool
{
return \think\helper\Str::contains($haystack, $needle);
}
}
if (!function_exists('rgb_or_image')) {
/**
* 判断字符串是否为RGB颜色值或图片链接
* @param string $str 要判断的字符串
* @return string IMAGE|RGB|''
*/
function rgb_or_image(string $str): string
{
if (
\think\helper\Str::startsWith($str, ['http', 'https']) ||
\think\helper\Str::endsWith($str, ['.png', '.jpg', '.jpeg', '.gif', '.tif', '.svg', '.webp', '.bmp']) ||
preg_match('/^data:image\/[jpg|jpeg|png|svg\+xml];base64,.+$/', $str)
) {
return 'IMAGE';
}
if (
preg_match('/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/', $str) ||
preg_match('/^rgb\(\d{1,3},\s*\d{1,3},\s*\d{1,3}\)$/', $str) ||
preg_match('/^rgba\(\d{1,3},\s*\d{1,3},\s*\d{1,3},\s*\d*(\.\d+)?\)$/', $str)
) {
return 'RGB';
}
return '';
}
}
if (!function_exists('style')) {
/**
* 输出style行内样式 - 避免编辑器因css校验规则提示错误
* @param array|string $styles 样式数组
* @return string
*/
function style(array|string $styles): string
{
$css = '';
if (is_array($styles)) {
$key = array_keys($styles);
foreach ($key as $v) {
if (empty($styles[$v])){
continue;
}
$css .= $v . ':' . $styles[$v] . ';';
}
} else {
$css = $styles;
}
if ($css == '') {
return '';
}
return 'style="' . $css . '"';
}
}
if (!function_exists('get_path_from_img_tag')) {
/**
* 从img标签中获取图片路径
* @param string $img
* @return string
*/
function get_path_from_img_tag(string $img): string
{
$match = preg_match('/<img[^>]+src=[\'"](.*?)[\'"][^>]+>/i', rawurldecode($img), $matches);
if ($match) {
return $matches[1];
}
return '';
}
}
if (!function_exists('lang_i18n')) {
/**
* 获取国际化语言包
* 根据请求页面path获取分组语言对应值
*
* @param string $name 语言包名称
* @param array $vars 替换变量
* @param string $lang 语言标识
* @return string
*/
function lang_i18n(string $name, array $vars = [], string $lang = ''): string
{
if (is_null($name)) {
return '';
}
$path = strtolower(request()->controller() . '/' . request()->action());
$lang_key = "{$path}.{$name}";
if (!Lang::has($lang_key, $lang)) {
return Lang::get($name, $vars, $lang);
}
return Lang::get($lang_key, $vars, $lang);
}
}
if (!function_exists('date_format_i18n')) {
/**
* 格式化日期时间为指定语言
* @param int|string $datetime 要格式化的日期时间字符串
* @param string $lang 语言标识
* @param bool $keep_time 是否保留时间
* @return string
*/
function date_format_i18n(int|string|null $datetime, string $lang = '', bool $keep_time = false): string
{
// 处理语言标识,若未提供则使用当前语言设置,并转换为小写
$lang = $lang ?: Lang::getLangSet();
$lang = strtolower($lang);
// 处理日期时间输入
if (is_numeric($datetime)) {
// 若输入为数字,直接作为时间戳处理
$timestamp = (int)$datetime;
} elseif (is_string($datetime)) {
// 尝试将字符串转换为时间戳
$timestamp = strtotime($datetime);
if ($timestamp === false) {
return '';
}
} else {
return '';
}
// 根据$datetime传值决定格式
$date_parts = [];
if (is_numeric($datetime)) {
$date_str = date('Y-m-d', $timestamp);
$date_parts = explode('-', $date_str);
} elseif (is_string($datetime)) {
if (preg_match('/^\d{4}$/', $datetime)) {
$date_parts = [$datetime];
} elseif (preg_match('/^\d{4}-\d{1,2}$/', $datetime)) {
$date_parts = explode('-', $datetime);
} elseif (preg_match('/^\d{4}-\d{1,2}-\d{1,2}(.{1,9})?$/', $datetime)) {
$date_parts = explode('-', $datetime);
}
}
// 定义默认格式
$fmt = '';
// 根据语言和日期部分数量设置日期格式
$format = [
'zh-cn' => [0 => '', 1 => 'Y', 2 => 'Y-m', 3 => 'Y-m-d'],
'en-us' => [0 => '', 1 => 'Y', 2 => 'F Y', 3 => 'F j, Y'],
'default' => [0 => '', 1 => 'Y', 2 => 'Y-m', 3 => 'Y-m-d']
];
$fmt = $format[$lang][count($date_parts)] ?? $format['default'][count($date_parts)];
// 若需要保留时间
if ($keep_time) {
if ($lang === 'en-us') {
$fmt .= ', g:i A';
} else {
$fmt .= ' H:i:s';
}
}
return date($fmt, $timestamp);
}
}
if (!function_exists('get_platform')) {
/**
* 获取平台
* @return string
*/
function get_platform(): string
{
$detect = new \Detection\MobileDetect();
$platform = 'pc';
if ($detect->isMobile() || $detect->isTablet()) {
$platform = 'mobile';
} else {
// 在非移动端环境,根据配置规则判断是否要显示移动端
$view_cfg = $view_cfg = [
'rule' => env('VIEW_TPL.RULE', 'query'),
'query' => [
'name' => env('VIEW_TPL.RULE_QUERY_NAME', 'mtpl'),
'value' => env('VIEW_TPL.RULE_QUERY_VALUE', '1'),
],
'domain' => [
'scheme' => env('VIEW_TPL.RULE_DOMAIN_SCHEME', ['http']),
'host' => env('VIEW_TPL.RULE_DOMAIN_HOST'),
],
];
if ($view_cfg['rule'] == 'query') {
$name = $view_cfg['query']['name'];
$value = $view_cfg['query']['value'];
if (request()->get($name) == $value) {
$platform = 'mobile';
}
} elseif ($view_cfg['rule'] == 'domain') {
$scheme = $view_cfg['domain']['scheme'];
$host = $view_cfg['domain']['host'];
if (in_array(request()->scheme(), $scheme) && $host == request()->host()) {
$platform = 'mobile';
}
}
}
return $platform;
}
}