优化代码写入规范

This commit is contained in:
yumo 2023-11-14 20:08:29 +08:00
parent 2b524af313
commit a96251cab4
3 changed files with 38 additions and 46 deletions

View File

@ -1,8 +1,5 @@
<?php <?php
use think\facade\Cache;
if (!function_exists('un_camelize')) { if (!function_exists('un_camelize')) {
/** /**
* 驼峰法转下划线 * 驼峰法转下划线
@ -10,7 +7,7 @@ if (!function_exists('un_camelize')) {
* @param string $separator * @param string $separator
* @return string * @return string
*/ */
function un_camelize($camelCaps, $separator = '_') function un_camelize($camelCaps, $separator = '_'): string
{ {
return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps)); return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
} }
@ -19,18 +16,19 @@ if (!function_exists('un_camelize')) {
if (!function_exists('auth_is_exit')) { if (!function_exists('auth_is_exit')) {
/** /**
* 判断授权信息是否存在 * 判断授权信息是否存在
* @param int $adminId
* @return bool * @return bool
* @throws \Psr\SimpleCache\InvalidArgumentException
*/ */
function auth_is_exit(int $adminId): bool function auth_is_exit(int $adminId): bool
{ {
return Cache::store('redis')->has('store_' . $adminId); return cache('store_' . $adminId);
} }
} }
if (!function_exists('remove_cache')) { if (!function_exists('remove_cache')) {
/** /**
* 删除缓存 * 删除缓存
* @param string $path
* @return bool * @return bool
* @throws \Psr\SimpleCache\InvalidArgumentException * @throws \Psr\SimpleCache\InvalidArgumentException
*/ */
@ -53,9 +51,11 @@ if (!function_exists('remove_cache')) {
} }
} }
if (!function_exists('to_int_array')) { if (!function_exists('to_int_array')) {
/** /**
* 字符串数组转int数组 * 字符串数组转int数组
* @param array $str
* @return array * @return array
*/ */
function to_int_array(array $str): array function to_int_array(array $str): array
@ -79,9 +79,10 @@ if (!function_exists('tag_options')) {
} }
} }
if (!function_exists('type_options')) { if (!function_exists('type_options')) {
/** /**
* 获取form标签 * 获取form type类型
* @return array * @return array
*/ */
function type_options(): array function type_options(): array
@ -100,6 +101,7 @@ if (!function_exists('type_options')) {
} }
} }
if (!function_exists('get_dir')) { if (!function_exists('get_dir')) {
/** /**
* 获取文件目录列表,该方法返回数组 * 获取文件目录列表,该方法返回数组
@ -107,7 +109,7 @@ if (!function_exists('get_dir')) {
* @return mixed * @return mixed
* @date 2021-02-17 21:27 * @date 2021-02-17 21:27
*/ */
function get_dir($dir) function get_dir($dir): mixed
{ {
$dirArray[] = NULL; $dirArray[] = NULL;
if (false != ($handle = opendir($dir))) { if (false != ($handle = opendir($dir))) {
@ -127,8 +129,15 @@ if (!function_exists('get_dir')) {
} }
if (!function_exists('get_tree_list')) { if (!function_exists('get_tree_list')) {
/*无限分类*/ /**
function get_tree_list(&$list, $pid = 0, $level = 0, $html = '|—') * 无限分类
* @param $list
* @param $pid
* @param $level
* @param $html
* @return array
*/
function get_tree_list(&$list, $pid = 0, $level = 0, $html = '|—'): array
{ {
static $tree = array(); static $tree = array();
foreach ($list as $v) { foreach ($list as $v) {
@ -175,25 +184,6 @@ if (!function_exists('html2mb_str')) {
} }
} }
/**
* 截取中文指定字节
* @param string $str
* @param int $utf8len
* @param string $charset
* @param string $file
* @return string
*/
if (!function_exists('substr_utf8')) {
function substr_utf8($str, int $utf8len = 100, string $charset = 'UTF-8', string $file = '....'): string
{
if (mb_strlen($str, $charset) > $utf8len) {
$str = mb_substr($str, 0, $utf8len, $charset) . $file;
}
return $str;
}
}
/** /**
* 获取本季度 time * 获取本季度 time
* @param int|string $time * @param int|string $time

View File

@ -42,7 +42,7 @@ class AdminNotify extends BaseModel
* @param array $data * @param array $data
* @return int|string * @return int|string
*/ */
public static function addLog(array $data) public static function addLog(array $data): int|string
{ {
return self::create($data); return self::create($data);
} }
@ -55,7 +55,7 @@ class AdminNotify extends BaseModel
* @throws DbException * @throws DbException
* @throws ModelNotFoundException * @throws ModelNotFoundException
*/ */
public static function pageList(int $num) public static function pageList(int $num): array
{ {
$model = new self; $model = new self;
$model = $model->where("is_read", 0); $model = $model->where("is_read", 0);

View File

@ -116,20 +116,22 @@ if (!function_exists('unicode_decode')) {
* *
* @return string * @return string
*/ */
function server_url() if (!function_exists('file_cdn')) {
{ function server_url(): string
if (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) { {
$http = 'https://'; if (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) {
} elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'])) { $http = 'https://';
$http = 'https://'; } elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'])) {
} else { $http = 'https://';
$http = 'http://'; } else {
$http = 'http://';
}
$host = $_SERVER['HTTP_HOST'];
$res = $http . $host;
return $res;
} }
$host = $_SERVER['HTTP_HOST'];
$res = $http . $host;
return $res;
} }
if (!function_exists('file_cdn')) { if (!function_exists('file_cdn')) {
@ -140,7 +142,7 @@ if (!function_exists('file_cdn')) {
* @author 木子的忧伤 * @author 木子的忧伤
* @date 2021-02-17 23:32 * @date 2021-02-17 23:32
*/ */
function file_cdn($path) function file_cdn($path): string
{ {
if (empty($path)) { if (empty($path)) {
return ''; return '';
@ -173,7 +175,7 @@ if (!function_exists('get_rand_str')) {
* @author 木子的忧伤 * @author 木子的忧伤
* @date 2022-04-11 18:26 * @date 2022-04-11 18:26
*/ */
function get_rand_str($length) function get_rand_str($length): string
{ {
//字符组合 //字符组合
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'; $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';