common.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. if (!function_exists('line_feed')) {
  3. /**
  4. * 多行输入框回车换行
  5. * @param string $str
  6. * @return string mixed
  7. */
  8. function line_feed(string $str): string
  9. {
  10. return str_replace("\n", "<br />", $str ?? '');
  11. }
  12. }
  13. if (!function_exists('str_limit')){
  14. /**
  15. * 超出字符省略
  16. * @param $value
  17. * @param int $limit
  18. * @param string $end
  19. * @return mixed|string
  20. */
  21. function str_limit($value, int $limit = 100, string $end = '...'): mixed {
  22. if (mb_strwidth($value, 'UTF-8') <= $limit) {
  23. return $value;
  24. }
  25. return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')) . $end;
  26. }
  27. }
  28. if (!function_exists('str_behind')) {
  29. /**
  30. * 获取指定字符之后的数据
  31. * @param string $str
  32. * @param string $keyword
  33. * @return string
  34. */
  35. function str_behind(string $str, string $keyword = '')
  36. {
  37. $str = explode($keyword, $str);
  38. if (count($str) < 1) {
  39. return $str[0];
  40. }
  41. $string = '';
  42. foreach ($str as $key => $val) {
  43. if ($key === 0) continue;
  44. $string .= "/{$val}";
  45. }
  46. return $string;
  47. }
  48. }
  49. /**
  50. * 返回输入数组中某个单一列的值
  51. * @Author PandaEyes
  52. * @email joeyoung0314@qq.com
  53. * @PHP_VERSION >=7.3
  54. * @param array $array 多维数组
  55. * @param string|null $column_keys 可以是索引数组的列的整数索引,或者是关联数组的列的字符串键值,该参数也可以是 NULL,此时将返回整个数组,配合index_key使用,注意,与array_column不同的是,此处可以返回多列,可用','分割
  56. * @param string|null $index_key 取出数组中这一列当做返回数组的索引 注意,与array_column不同的是,此处将不会去重,而是将所有符合的数组编排到同一键中
  57. * @return array
  58. */
  59. if (!function_exists('array_columns')) {
  60. function array_columns(array $array, string $column_keys = null, string $index_key = null): array
  61. {
  62. $result = [];
  63. $keys = isset($column_keys) ? explode(',', $column_keys) : [];
  64. if ($array) {
  65. foreach ($array as $item) {
  66. // 指定返回列
  67. if ($keys) {
  68. $tmp = [];
  69. foreach ($keys as $key) {
  70. $tmp[$key] = $item[$key];
  71. }
  72. } else {
  73. $tmp = $item;
  74. }
  75. // 指定索引列
  76. if (isset($index_key)) {
  77. $result[$item[$index_key]][] = $tmp;
  78. } else {
  79. $result[] = $tmp;
  80. }
  81. }
  82. }
  83. return $result;
  84. }
  85. }
  86. /**
  87. * fastadmin site config
  88. * @param string $key
  89. * @return false|mixed|Redis|string
  90. * @throws Exception
  91. */
  92. if (!function_exists('site')) {
  93. function site(string $key = '')
  94. {
  95. $config = \App\Utils\RedisUtil::getInstance(\App\Master\Enum\RedisKeyEnum::FA_SITE_SETUP)->get();
  96. if (!$config){
  97. throw new Exception('fastadmin site config not found');
  98. }
  99. $config = json_decode($config,true);
  100. if (!empty($key)){
  101. return $config[$key] ?? '';
  102. }
  103. return $config;
  104. }
  105. }
  106. /**
  107. * 关键词判断是否存在
  108. * @param string $keyword
  109. * @return bool
  110. */
  111. if (!function_exists('keyword_exits')) {
  112. function keyword_exits(string $keyword): bool
  113. {
  114. $keyword = strtolower($keyword);
  115. $keyword_filter = explode('|',trim(site('keyword_filter')));
  116. // 检查是否包含敏感词
  117. $is_true = false;
  118. foreach ($keyword_filter as $word) {
  119. if (strpos($keyword, $word) !== false) {
  120. $is_true = true;
  121. break;
  122. }
  123. }
  124. return $is_true;
  125. }
  126. }
  127. /**
  128. * 关键词过滤
  129. * @param string $keyword
  130. * @return string mixed
  131. */
  132. if (!function_exists('keyword_filter')) {
  133. function keyword_filter(string $keyword): string
  134. {
  135. $keyword = strtolower($keyword);
  136. $keyword_filter = explode('|',trim(site('keyword_filter')));
  137. $keyword_filter = array_unique($keyword_filter) ?? [];
  138. //根据敏感词字数替换相同数量的'*'
  139. $kong = [];
  140. foreach ($keyword_filter as $k => $v) {
  141. $x = '';
  142. for ($i = 0; $i < mb_strlen($v, 'utf-8'); $i++) {
  143. $x .= '*';
  144. }
  145. $kong[$k] = $x;
  146. unset($x);
  147. }
  148. $replace = array_combine($keyword_filter, $kong);//将敏感词作为下标,将对应的'*'作为值
  149. return strtr($keyword, $replace);//将敏感词替换为'*'
  150. }
  151. }
  152. /**
  153. * 自动拼im id前缀
  154. * @param $user_id
  155. * @return mixed
  156. */
  157. if (!function_exists('im_prefix')) {
  158. function im_prefix($user_id)
  159. {
  160. return \Hyperf\Config\config('tencent.im.chat_prefix').$user_id;
  161. }
  162. }
  163. /**
  164. * 将秒时间转换具体时间:秒转天时分秒
  165. * @return bool|string
  166. */
  167. if (!function_exists('time_ext')) {
  168. function time_ext(int $seconds,int $type = 0,int $min_m = 0): bool|string
  169. {
  170. $d = floor($seconds / (3600*24));
  171. $h = floor(($seconds % (3600*24)) / 3600);
  172. $m = floor((($seconds % (3600*24)) % 3600) / 60);
  173. if ($type === 1){
  174. if($d>'0'){
  175. $time = "{$d}天{$h}小时{$m}分钟";
  176. }else{
  177. if($h!='0'){
  178. $time = "{$h}小时{$m}分钟";
  179. }else{
  180. $m = ($min_m === 1 && $m < 1) ? 1 : $m;
  181. $time = "{$m}分钟";
  182. }
  183. }
  184. } else {
  185. if($d>'0'){
  186. $time = "{$d}d{$h}h{$m}m";
  187. }else{
  188. if($h!='0'){
  189. $time = "{$h}h{$m}m";
  190. }else{
  191. $m = ($min_m === 1 && $m < 1) ? 1 : $m;
  192. $time = "{$m}m";
  193. }
  194. }
  195. }
  196. return $time;
  197. }
  198. }
  199. /**
  200. * 将时间戳转换小时时间
  201. * @return bool|string
  202. */
  203. if (!function_exists('time_hour')) {
  204. function time_hour(int $time): bool|string
  205. {
  206. $year = date('Y', $time);
  207. $month = date('m', $time);
  208. $day = date('d', $time);
  209. if ($day == date('d') && $month == date('m') && $year == date('Y')) {
  210. $times = date('H:i', $time);
  211. } else {
  212. $times = date('m-d H:i', $time);
  213. }
  214. return $times;
  215. }
  216. }
  217. if (!function_exists('unix_time')) {
  218. /**
  219. * 格式化
  220. * @param $time
  221. * @return string
  222. */
  223. function unix_time($time): string
  224. {
  225. //获取今天凌晨的时间戳
  226. $day = strtotime(date('Y-m-d', time()));
  227. //获取昨天凌晨的时间戳
  228. $pday = strtotime(date('Y-m-d', strtotime('-1 day')));
  229. //获取现在的时间戳
  230. $nowtime = time();
  231. $t = $nowtime - $time;
  232. if ($time < $pday) {
  233. $str = date('m-d', $time);
  234. } elseif ($time < $day && $time > $pday) {
  235. $str = "昨天";
  236. } elseif ($t > 60 * 60) {
  237. $str = floor($t / (60 * 60)) . "小时前";
  238. } elseif ($t > 60) {
  239. $str = floor($t / 60) . "分钟前";
  240. } else {
  241. $str = "刚刚";
  242. }
  243. return $str;
  244. }
  245. }
  246. /**
  247. * 毫秒时间戳
  248. * @return int
  249. */
  250. if (!function_exists('ms_time')) {
  251. function ms_time(): int
  252. {
  253. list($ms, $sec) = explode(' ', microtime());
  254. return intval((floatval($ms) + floatval($sec)) * 1000);
  255. }
  256. }
  257. /**
  258. * 获取上传资源的CDN的地址
  259. * @param string $url 资源相对地址
  260. * @param bool $ssl
  261. * @return string
  262. */
  263. if (!function_exists('cdn_url')) {
  264. function cdn_url(string $url,bool $ssl = true)
  265. {
  266. if (empty($url)) return '';
  267. $regex = "/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i";
  268. $cdn_url = \Hyperf\Config\config('cdn_url');
  269. if (strrpos($url, 'http') !== false || preg_match($regex, $url)) {
  270. $url = $url;
  271. } elseif(empty($cdn_url)) {
  272. $domain = $_SERVER['HTTP_HOST'] ?? '127.0.0.1';
  273. $http = 'http://';
  274. $ssl && $http = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
  275. $url = $http.$domain.$url;
  276. }else{
  277. $url = $cdn_url.$url;
  278. }
  279. return $url;
  280. }
  281. }
  282. /**
  283. * 自动拼im id前缀
  284. * @param $user_id
  285. * @return string
  286. */
  287. if (!function_exists('im_prefix')) {
  288. function im_prefix($user_id): string
  289. {
  290. return \Hyperf\Config\config('tencent.im.chat_prefix').$user_id;
  291. }
  292. }
  293. /**
  294. * 自动拼im id前缀
  295. * @param $user_id
  296. * @return string
  297. */
  298. if (!function_exists('im_un_prefix')) {
  299. function im_un_prefix($user_id): int
  300. {
  301. return (int)str_replace(\Hyperf\Config\config('tencent.im.chat_prefix'),'',$user_id);
  302. }
  303. }
  304. if (!function_exists('dd')) {
  305. function dd(...$vars)
  306. {
  307. foreach ($vars as $v) {
  308. var_dump($v);
  309. }
  310. }
  311. }