common.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. * fastadmin site config
  51. * @param string $key
  52. * @return false|mixed|Redis|string
  53. * @throws Exception
  54. */
  55. if (!function_exists('site')) {
  56. function site(string $key = '')
  57. {
  58. $config = \App\Utils\RedisUtil::getInstance(\App\Master\Enum\RedisKeyEnum::FA_SITE_SETUP)->get();
  59. if (!$config){
  60. throw new Exception('fastadmin site config not found');
  61. }
  62. $config = json_decode($config,true);
  63. if (!empty($key)){
  64. return $config[$key] ?? '';
  65. }
  66. return $config;
  67. }
  68. }
  69. /**
  70. * 将秒时间转换具体时间:秒转天时分秒
  71. * @return bool|string
  72. */
  73. if (!function_exists('time_ext')) {
  74. function time_ext(int $seconds,int $type = 0,int $min_m = 0): bool|string
  75. {
  76. $d = floor($seconds / (3600*24));
  77. $h = floor(($seconds % (3600*24)) / 3600);
  78. $m = floor((($seconds % (3600*24)) % 3600) / 60);
  79. if ($type === 1){
  80. if($d>'0'){
  81. $time = "{$d}天{$h}小时{$m}分钟";
  82. }else{
  83. if($h!='0'){
  84. $time = "{$h}小时{$m}分钟";
  85. }else{
  86. $m = ($min_m === 1 && $m < 1) ? 1 : $m;
  87. $time = "{$m}分钟";
  88. }
  89. }
  90. } else {
  91. if($d>'0'){
  92. $time = "{$d}d{$h}h{$m}m";
  93. }else{
  94. if($h!='0'){
  95. $time = "{$h}h{$m}m";
  96. }else{
  97. $m = ($min_m === 1 && $m < 1) ? 1 : $m;
  98. $time = "{$m}m";
  99. }
  100. }
  101. }
  102. return $time;
  103. }
  104. }
  105. /**
  106. * 将时间戳转换小时时间
  107. * @return bool|string
  108. */
  109. if (!function_exists('time_hour')) {
  110. function time_hour(int $time): bool|string
  111. {
  112. $year = date('Y', $time);
  113. $month = date('m', $time);
  114. $day = date('d', $time);
  115. if ($day == date('d') && $month == date('m') && $year == date('Y')) {
  116. $times = date('H:i', $time);
  117. } else {
  118. $times = date('m-d H:i', $time);
  119. }
  120. return $times;
  121. }
  122. }
  123. if (!function_exists('unix_time')) {
  124. /**
  125. * 格式化
  126. * @param $time
  127. * @return string
  128. */
  129. function unix_time($time): string
  130. {
  131. //获取今天凌晨的时间戳
  132. $day = strtotime(date('Y-m-d', time()));
  133. //获取昨天凌晨的时间戳
  134. $pday = strtotime(date('Y-m-d', strtotime('-1 day')));
  135. //获取现在的时间戳
  136. $nowtime = time();
  137. $t = $nowtime - $time;
  138. if ($time < $pday) {
  139. $str = date('m-d', $time);
  140. } elseif ($time < $day && $time > $pday) {
  141. $str = "昨天";
  142. } elseif ($t > 60 * 60) {
  143. $str = floor($t / (60 * 60)) . "小时前";
  144. } elseif ($t > 60) {
  145. $str = floor($t / 60) . "分钟前";
  146. } else {
  147. $str = "刚刚";
  148. }
  149. return $str;
  150. }
  151. }
  152. /**
  153. * 毫秒时间戳
  154. * @return int
  155. */
  156. if (!function_exists('ms_time')) {
  157. function ms_time(): int
  158. {
  159. list($ms, $sec) = explode(' ', microtime());
  160. return intval((floatval($ms) + floatval($sec)) * 1000);
  161. }
  162. }
  163. /**
  164. * 获取上传资源的CDN的地址
  165. * @param string $url 资源相对地址
  166. * @param bool $ssl
  167. * @return string
  168. */
  169. if (!function_exists('cdn_url')) {
  170. function cdn_url(string $url,bool $ssl = true)
  171. {
  172. $regex = "/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i";
  173. $cdn_url = \Hyperf\Config\config('cdn_url');
  174. if (strrpos($url, 'http') !== false || preg_match($regex, $url)) {
  175. $url = $url;
  176. } elseif(empty($cdn_url)) {
  177. $domain = $_SERVER['HTTP_HOST'] ?? '127.0.0.1';
  178. $http = 'http://';
  179. $ssl && $http = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
  180. $url = $http.$domain.$url;
  181. }else{
  182. $url = $cdn_url.$url;
  183. }
  184. return $url;
  185. }
  186. }
  187. if (!function_exists('dd')) {
  188. function dd(...$vars)
  189. {
  190. foreach ($vars as $v) {
  191. var_dump($v);
  192. }
  193. }
  194. }