123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- <?php
- if (!function_exists('line_feed')) {
- /**
- * 多行输入框回车换行
- * @param string $str
- * @return string mixed
- */
- function line_feed(string $str): string
- {
- return str_replace("\n", "<br />", $str ?? '');
- }
- }
- if (!function_exists('str_limit')){
- /**
- * 超出字符省略
- * @param $value
- * @param int $limit
- * @param string $end
- * @return mixed|string
- */
- function str_limit($value, int $limit = 100, string $end = '...'): mixed {
- if (mb_strwidth($value, 'UTF-8') <= $limit) {
- return $value;
- }
- return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')) . $end;
- }
- }
- if (!function_exists('str_behind')) {
- /**
- * 获取指定字符之后的数据
- * @param string $str
- * @param string $keyword
- * @return string
- */
- function str_behind(string $str, string $keyword = '')
- {
- $str = explode($keyword, $str);
- if (count($str) < 1) {
- return $str[0];
- }
- $string = '';
- foreach ($str as $key => $val) {
- if ($key === 0) continue;
- $string .= "/{$val}";
- }
- return $string;
- }
- }
- /**
- * 返回输入数组中某个单一列的值
- * @Author PandaEyes
- * @email joeyoung0314@qq.com
- * @PHP_VERSION >=7.3
- * @param array $array 多维数组
- * @param string|null $column_keys 可以是索引数组的列的整数索引,或者是关联数组的列的字符串键值,该参数也可以是 NULL,此时将返回整个数组,配合index_key使用,注意,与array_column不同的是,此处可以返回多列,可用','分割
- * @param string|null $index_key 取出数组中这一列当做返回数组的索引 注意,与array_column不同的是,此处将不会去重,而是将所有符合的数组编排到同一键中
- * @return array
- */
- if (!function_exists('array_columns')) {
- function array_columns(array $array, string $column_keys = null, string $index_key = null): array
- {
- $result = [];
- $keys = isset($column_keys) ? explode(',', $column_keys) : [];
- if ($array) {
- foreach ($array as $item) {
- // 指定返回列
- if ($keys) {
- $tmp = [];
- foreach ($keys as $key) {
- $tmp[$key] = $item[$key];
- }
- } else {
- $tmp = $item;
- }
- // 指定索引列
- if (isset($index_key)) {
- $result[$item[$index_key]][] = $tmp;
- } else {
- $result[] = $tmp;
- }
- }
- }
- return $result;
- }
- }
- /**
- * fastadmin site config
- * @param string $key
- * @return false|mixed|Redis|string
- * @throws Exception
- */
- if (!function_exists('site')) {
- function site(string $key = '')
- {
- $config = \App\Utils\RedisUtil::getInstance(\App\Master\Enum\RedisKeyEnum::FA_SITE_SETUP)->get();
- if (!$config){
- throw new Exception('fastadmin site config not found');
- }
- $config = json_decode($config,true);
- if (!empty($key)){
- return $config[$key] ?? '';
- }
- return $config;
- }
- }
- /**
- * 关键词判断是否存在
- * @param string $keyword
- * @return bool
- */
- if (!function_exists('keyword_exits')) {
- function keyword_exits(string $keyword): bool
- {
- $keyword = strtolower($keyword);
- $keyword_filter = explode('|',trim(site('keyword_filter')));
- // 检查是否包含敏感词
- $is_true = false;
- foreach ($keyword_filter as $word) {
- if (strpos($keyword, $word) !== false) {
- $is_true = true;
- break;
- }
- }
- return $is_true;
- }
- }
- /**
- * 关键词过滤
- * @param string $keyword
- * @return string mixed
- */
- if (!function_exists('keyword_filter')) {
- function keyword_filter(string $keyword): string
- {
- $keyword = strtolower($keyword);
- $keyword_filter = explode('|',trim(site('keyword_filter')));
- $keyword_filter = array_unique($keyword_filter) ?? [];
- //根据敏感词字数替换相同数量的'*'
- $kong = [];
- foreach ($keyword_filter as $k => $v) {
- $x = '';
- for ($i = 0; $i < mb_strlen($v, 'utf-8'); $i++) {
- $x .= '*';
- }
- $kong[$k] = $x;
- unset($x);
- }
- $replace = array_combine($keyword_filter, $kong);//将敏感词作为下标,将对应的'*'作为值
- return strtr($keyword, $replace);//将敏感词替换为'*'
- }
- }
- /**
- * 自动拼im id前缀
- * @param $user_id
- * @return mixed
- */
- if (!function_exists('im_prefix')) {
- function im_prefix($user_id)
- {
- return \Hyperf\Config\config('tencent.im.chat_prefix').$user_id;
- }
- }
- /**
- * 将秒时间转换具体时间:秒转天时分秒
- * @return bool|string
- */
- if (!function_exists('time_ext')) {
- function time_ext(int $seconds,int $type = 0,int $min_m = 0): bool|string
- {
- $d = floor($seconds / (3600*24));
- $h = floor(($seconds % (3600*24)) / 3600);
- $m = floor((($seconds % (3600*24)) % 3600) / 60);
- if ($type === 1){
- if($d>'0'){
- $time = "{$d}天{$h}小时{$m}分钟";
- }else{
- if($h!='0'){
- $time = "{$h}小时{$m}分钟";
- }else{
- $m = ($min_m === 1 && $m < 1) ? 1 : $m;
- $time = "{$m}分钟";
- }
- }
- } else {
- if($d>'0'){
- $time = "{$d}d{$h}h{$m}m";
- }else{
- if($h!='0'){
- $time = "{$h}h{$m}m";
- }else{
- $m = ($min_m === 1 && $m < 1) ? 1 : $m;
- $time = "{$m}m";
- }
- }
- }
- return $time;
- }
- }
- /**
- * 将时间戳转换小时时间
- * @return bool|string
- */
- if (!function_exists('time_hour')) {
- function time_hour(int $time): bool|string
- {
- $year = date('Y', $time);
- $month = date('m', $time);
- $day = date('d', $time);
- if ($day == date('d') && $month == date('m') && $year == date('Y')) {
- $times = date('H:i', $time);
- } else {
- $times = date('m-d H:i', $time);
- }
- return $times;
- }
- }
- if (!function_exists('unix_time')) {
- /**
- * 格式化
- * @param $time
- * @return string
- */
- function unix_time($time): string
- {
- //获取今天凌晨的时间戳
- $day = strtotime(date('Y-m-d', time()));
- //获取昨天凌晨的时间戳
- $pday = strtotime(date('Y-m-d', strtotime('-1 day')));
- //获取现在的时间戳
- $nowtime = time();
- $t = $nowtime - $time;
- if ($time < $pday) {
- $str = date('m-d', $time);
- } elseif ($time < $day && $time > $pday) {
- $str = "昨天";
- } elseif ($t > 60 * 60) {
- $str = floor($t / (60 * 60)) . "小时前";
- } elseif ($t > 60) {
- $str = floor($t / 60) . "分钟前";
- } else {
- $str = "刚刚";
- }
- return $str;
- }
- }
- /**
- * 毫秒时间戳
- * @return int
- */
- if (!function_exists('ms_time')) {
- function ms_time(): int
- {
- list($ms, $sec) = explode(' ', microtime());
- return intval((floatval($ms) + floatval($sec)) * 1000);
- }
- }
- /**
- * 获取上传资源的CDN的地址
- * @param string $url 资源相对地址
- * @param bool $ssl
- * @return string
- */
- if (!function_exists('cdn_url')) {
- function cdn_url(string $url,bool $ssl = true)
- {
- if (empty($url)) return '';
- $regex = "/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i";
- $cdn_url = \Hyperf\Config\config('cdn_url');
- if (strrpos($url, 'http') !== false || preg_match($regex, $url)) {
- $url = $url;
- } elseif(empty($cdn_url)) {
- $domain = $_SERVER['HTTP_HOST'] ?? '127.0.0.1';
- $http = 'http://';
- $ssl && $http = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
- $url = $http.$domain.$url;
- }else{
- $url = $cdn_url.$url;
- }
- return $url;
- }
- }
- /**
- * 自动拼im id前缀
- * @param $user_id
- * @return string
- */
- if (!function_exists('im_prefix')) {
- function im_prefix($user_id): string
- {
- return \Hyperf\Config\config('tencent.im.chat_prefix').$user_id;
- }
- }
- /**
- * 自动拼im id前缀
- * @param $user_id
- * @return string
- */
- if (!function_exists('im_un_prefix')) {
- function im_un_prefix($user_id): int
- {
- return (int)str_replace(\Hyperf\Config\config('tencent.im.chat_prefix'),'',$user_id);
- }
- }
- if (!function_exists('dd')) {
- function dd(...$vars)
- {
- foreach ($vars as $v) {
- var_dump($v);
- }
- }
- }
|