common.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. <?php
  2. // 公共助手函数
  3. use Symfony\Component\VarExporter\VarExporter;
  4. if (!function_exists('__')) {
  5. /**
  6. * 获取语言变量值
  7. * @param string $name 语言变量名
  8. * @param array $vars 动态变量值
  9. * @param string $lang 语言
  10. * @return mixed
  11. */
  12. function __($name, $vars = [], $lang = '')
  13. {
  14. if (is_numeric($name) || !$name) {
  15. return $name;
  16. }
  17. if (!is_array($vars)) {
  18. $vars = func_get_args();
  19. array_shift($vars);
  20. $lang = '';
  21. }
  22. return \think\Lang::get($name, $vars, $lang);
  23. }
  24. }
  25. if (!function_exists('format_bytes')) {
  26. /**
  27. * 将字节转换为可读文本
  28. * @param int $size 大小
  29. * @param string $delimiter 分隔符
  30. * @param int $precision 小数位数
  31. * @return string
  32. */
  33. function format_bytes($size, $delimiter = '', $precision = 2)
  34. {
  35. $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
  36. for ($i = 0; $size >= 1024 && $i < 6; $i++) {
  37. $size /= 1024;
  38. }
  39. return round($size, $precision) . $delimiter . $units[$i];
  40. }
  41. }
  42. if (!function_exists('datetime')) {
  43. /**
  44. * 将时间戳转换为日期时间
  45. * @param int $time 时间戳
  46. * @param string $format 日期时间格式
  47. * @return string
  48. */
  49. function datetime($time, $format = 'Y-m-d H:i:s')
  50. {
  51. $time = is_numeric($time) ? $time : strtotime($time);
  52. return date($format, $time);
  53. }
  54. }
  55. if (!function_exists('human_date')) {
  56. /**
  57. * 获取语义化时间
  58. * @param int $time 时间
  59. * @param int $local 本地时间
  60. * @return string
  61. */
  62. function human_date($time, $local = null)
  63. {
  64. return \fast\Date::human($time, $local);
  65. }
  66. }
  67. if (!function_exists('cdnurl')) {
  68. /**
  69. * 获取上传资源的CDN的地址
  70. * @param string $url 资源相对地址
  71. * @param boolean $domain 是否显示域名 或者直接传入域名
  72. * @return string
  73. */
  74. function cdnurl($url, $domain = false)
  75. {
  76. $regex = "/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i";
  77. $cdnurl = \think\Config::get('upload.cdnurl');
  78. $url = preg_match($regex, $url) || ($cdnurl && stripos($url, $cdnurl) === 0) ? $url : $cdnurl . $url;
  79. if ($domain && !preg_match($regex, $url)) {
  80. $domain = is_bool($domain) ? request()->domain() : $domain;
  81. $url = $domain . $url;
  82. }
  83. return $url;
  84. }
  85. }
  86. if (!function_exists('is_really_writable')) {
  87. /**
  88. * 判断文件或文件夹是否可写
  89. * @param string $file 文件或目录
  90. * @return bool
  91. */
  92. function is_really_writable($file)
  93. {
  94. if (DIRECTORY_SEPARATOR === '/') {
  95. return is_writable($file);
  96. }
  97. if (is_dir($file)) {
  98. $file = rtrim($file, '/') . '/' . md5(mt_rand());
  99. if (($fp = @fopen($file, 'ab')) === false) {
  100. return false;
  101. }
  102. fclose($fp);
  103. @chmod($file, 0777);
  104. @unlink($file);
  105. return true;
  106. } elseif (!is_file($file) or ($fp = @fopen($file, 'ab')) === false) {
  107. return false;
  108. }
  109. fclose($fp);
  110. return true;
  111. }
  112. }
  113. if (!function_exists('rmdirs')) {
  114. /**
  115. * 删除文件夹
  116. * @param string $dirname 目录
  117. * @param bool $withself 是否删除自身
  118. * @return boolean
  119. */
  120. function rmdirs($dirname, $withself = true)
  121. {
  122. if (!is_dir($dirname)) {
  123. return false;
  124. }
  125. $files = new RecursiveIteratorIterator(
  126. new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS),
  127. RecursiveIteratorIterator::CHILD_FIRST
  128. );
  129. foreach ($files as $fileinfo) {
  130. $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
  131. $todo($fileinfo->getRealPath());
  132. }
  133. if ($withself) {
  134. @rmdir($dirname);
  135. }
  136. return true;
  137. }
  138. }
  139. if (!function_exists('copydirs')) {
  140. /**
  141. * 复制文件夹
  142. * @param string $source 源文件夹
  143. * @param string $dest 目标文件夹
  144. */
  145. function copydirs($source, $dest)
  146. {
  147. if (!is_dir($dest)) {
  148. mkdir($dest, 0755, true);
  149. }
  150. foreach (
  151. $iterator = new RecursiveIteratorIterator(
  152. new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
  153. RecursiveIteratorIterator::SELF_FIRST
  154. ) as $item
  155. ) {
  156. if ($item->isDir()) {
  157. $sontDir = $dest . DS . $iterator->getSubPathName();
  158. if (!is_dir($sontDir)) {
  159. mkdir($sontDir, 0755, true);
  160. }
  161. } else {
  162. copy($item, $dest . DS . $iterator->getSubPathName());
  163. }
  164. }
  165. }
  166. }
  167. if (!function_exists('mb_ucfirst')) {
  168. function mb_ucfirst($string)
  169. {
  170. return mb_strtoupper(mb_substr($string, 0, 1)) . mb_strtolower(mb_substr($string, 1));
  171. }
  172. }
  173. if (!function_exists('addtion')) {
  174. /**
  175. * 附加关联字段数据
  176. * @param array $items 数据列表
  177. * @param mixed $fields 渲染的来源字段
  178. * @return array
  179. */
  180. function addtion($items, $fields)
  181. {
  182. if (!$items || !$fields) {
  183. return $items;
  184. }
  185. $fieldsArr = [];
  186. if (!is_array($fields)) {
  187. $arr = explode(',', $fields);
  188. foreach ($arr as $k => $v) {
  189. $fieldsArr[$v] = ['field' => $v];
  190. }
  191. } else {
  192. foreach ($fields as $k => $v) {
  193. if (is_array($v)) {
  194. $v['field'] = isset($v['field']) ? $v['field'] : $k;
  195. } else {
  196. $v = ['field' => $v];
  197. }
  198. $fieldsArr[$v['field']] = $v;
  199. }
  200. }
  201. foreach ($fieldsArr as $k => &$v) {
  202. $v = is_array($v) ? $v : ['field' => $v];
  203. $v['display'] = isset($v['display']) ? $v['display'] : str_replace(['_ids', '_id'], ['_names', '_name'], $v['field']);
  204. $v['primary'] = isset($v['primary']) ? $v['primary'] : '';
  205. $v['column'] = isset($v['column']) ? $v['column'] : 'name';
  206. $v['model'] = isset($v['model']) ? $v['model'] : '';
  207. $v['table'] = isset($v['table']) ? $v['table'] : '';
  208. $v['name'] = isset($v['name']) ? $v['name'] : str_replace(['_ids', '_id'], '', $v['field']);
  209. }
  210. unset($v);
  211. $ids = [];
  212. $fields = array_keys($fieldsArr);
  213. foreach ($items as $k => $v) {
  214. foreach ($fields as $m => $n) {
  215. if (isset($v[$n])) {
  216. $ids[$n] = array_merge(isset($ids[$n]) && is_array($ids[$n]) ? $ids[$n] : [], explode(',', $v[$n]));
  217. }
  218. }
  219. }
  220. $result = [];
  221. foreach ($fieldsArr as $k => $v) {
  222. if ($v['model']) {
  223. $model = new $v['model'];
  224. } else {
  225. $model = $v['name'] ? \think\Db::name($v['name']) : \think\Db::table($v['table']);
  226. }
  227. $primary = $v['primary'] ? $v['primary'] : $model->getPk();
  228. $result[$v['field']] = isset($ids[$v['field']]) ? $model->where($primary, 'in', $ids[$v['field']])->column("{$primary},{$v['column']}") : [];
  229. }
  230. foreach ($items as $k => &$v) {
  231. foreach ($fields as $m => $n) {
  232. if (isset($v[$n])) {
  233. $curr = array_flip(explode(',', $v[$n]));
  234. $v[$fieldsArr[$n]['display']] = implode(',', array_intersect_key($result[$n], $curr));
  235. }
  236. }
  237. }
  238. return $items;
  239. }
  240. }
  241. if (!function_exists('var_export_short')) {
  242. /**
  243. * 使用短标签打印或返回数组结构
  244. * @param mixed $data
  245. * @param boolean $return 是否返回数据
  246. * @return string
  247. */
  248. function var_export_short($data, $return = true)
  249. {
  250. return var_export($data, $return);
  251. $replaced = [];
  252. $count = 0;
  253. //判断是否是对象
  254. if (is_resource($data) || is_object($data)) {
  255. return var_export($data, $return);
  256. }
  257. //判断是否有特殊的键名
  258. $specialKey = false;
  259. array_walk_recursive($data, function (&$value, &$key) use (&$specialKey) {
  260. if (is_string($key) && (stripos($key, "\n") !== false || stripos($key, "array (") !== false)) {
  261. $specialKey = true;
  262. }
  263. });
  264. if ($specialKey) {
  265. return var_export($data, $return);
  266. }
  267. array_walk_recursive($data, function (&$value, &$key) use (&$replaced, &$count, &$stringcheck) {
  268. if (is_object($value) || is_resource($value)) {
  269. $replaced[$count] = var_export($value, true);
  270. $value = "##<{$count}>##";
  271. } else {
  272. if (is_string($value) && (stripos($value, "\n") !== false || stripos($value, "array (") !== false)) {
  273. $index = array_search($value, $replaced);
  274. if ($index === false) {
  275. $replaced[$count] = var_export($value, true);
  276. $value = "##<{$count}>##";
  277. } else {
  278. $value = "##<{$index}>##";
  279. }
  280. }
  281. }
  282. $count++;
  283. });
  284. $dump = var_export($data, true);
  285. $dump = preg_replace('#(?:\A|\n)([ ]*)array \(#i', '[', $dump); // Starts
  286. $dump = preg_replace('#\n([ ]*)\),#', "\n$1],", $dump); // Ends
  287. $dump = preg_replace('#=> \[\n\s+\],\n#', "=> [],\n", $dump); // Empties
  288. $dump = preg_replace('#\)$#', "]", $dump); //End
  289. if ($replaced) {
  290. $dump = preg_replace_callback("/'##<(\d+)>##'/", function ($matches) use ($replaced) {
  291. return isset($replaced[$matches[1]]) ? $replaced[$matches[1]] : "''";
  292. }, $dump);
  293. }
  294. if ($return === true) {
  295. return $dump;
  296. } else {
  297. echo $dump;
  298. }
  299. }
  300. }
  301. if (!function_exists('letter_avatar')) {
  302. /**
  303. * 首字母头像
  304. * @param $text
  305. * @return string
  306. */
  307. function letter_avatar($text)
  308. {
  309. $total = unpack('L', hash('adler32', $text, true))[1];
  310. $hue = $total % 360;
  311. list($r, $g, $b) = hsv2rgb($hue / 360, 0.3, 0.9);
  312. $bg = "rgb({$r},{$g},{$b})";
  313. $color = "#ffffff";
  314. $first = mb_strtoupper(mb_substr($text, 0, 1));
  315. $src = base64_encode('<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="100" width="100"><rect fill="' . $bg . '" x="0" y="0" width="100" height="100"></rect><text x="50" y="50" font-size="50" text-copy="fast" fill="' . $color . '" text-anchor="middle" text-rights="admin" dominant-baseline="central">' . $first . '</text></svg>');
  316. $value = 'data:image/svg+xml;base64,' . $src;
  317. return $value;
  318. }
  319. }
  320. if (!function_exists('hsv2rgb')) {
  321. function hsv2rgb($h, $s, $v)
  322. {
  323. $r = $g = $b = 0;
  324. $i = floor($h * 6);
  325. $f = $h * 6 - $i;
  326. $p = $v * (1 - $s);
  327. $q = $v * (1 - $f * $s);
  328. $t = $v * (1 - (1 - $f) * $s);
  329. switch ($i % 6) {
  330. case 0:
  331. $r = $v;
  332. $g = $t;
  333. $b = $p;
  334. break;
  335. case 1:
  336. $r = $q;
  337. $g = $v;
  338. $b = $p;
  339. break;
  340. case 2:
  341. $r = $p;
  342. $g = $v;
  343. $b = $t;
  344. break;
  345. case 3:
  346. $r = $p;
  347. $g = $q;
  348. $b = $v;
  349. break;
  350. case 4:
  351. $r = $t;
  352. $g = $p;
  353. $b = $v;
  354. break;
  355. case 5:
  356. $r = $v;
  357. $g = $p;
  358. $b = $q;
  359. break;
  360. }
  361. return [
  362. floor($r * 255),
  363. floor($g * 255),
  364. floor($b * 255)
  365. ];
  366. }
  367. }
  368. if (!function_exists('check_nav_active')) {
  369. /**
  370. * 检测会员中心导航是否高亮
  371. */
  372. function check_nav_active($url, $classname = 'active')
  373. {
  374. $auth = \app\common\library\Auth::instance();
  375. $requestUrl = $auth->getRequestUri();
  376. $url = ltrim($url, '/');
  377. return $requestUrl === str_replace(".", "/", $url) ? $classname : '';
  378. }
  379. }
  380. if (!function_exists('check_cors_request')) {
  381. /**
  382. * 跨域检测
  383. */
  384. function check_cors_request()
  385. {
  386. if (isset($_SERVER['HTTP_ORIGIN']) && $_SERVER['HTTP_ORIGIN']) {
  387. $info = parse_url($_SERVER['HTTP_ORIGIN']);
  388. $domainArr = explode(',', config('fastadmin.cors_request_domain'));
  389. $domainArr[] = request()->host(true);
  390. if (in_array("*", $domainArr) || in_array($_SERVER['HTTP_ORIGIN'], $domainArr) || (isset($info['host']) && in_array($info['host'], $domainArr))) {
  391. header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']);
  392. } else {
  393. header('HTTP/1.1 403 Forbidden');
  394. exit;
  395. }
  396. header('Access-Control-Allow-Credentials: true');
  397. header('Access-Control-Max-Age: 86400');
  398. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  399. if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {
  400. header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
  401. }
  402. if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
  403. header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
  404. }
  405. exit;
  406. }
  407. }
  408. }
  409. }
  410. if (!function_exists('xss_clean')) {
  411. /**
  412. * 清理XSS
  413. */
  414. function xss_clean($content, $is_image = false)
  415. {
  416. return \app\common\library\Security::instance()->xss_clean($content, $is_image);
  417. }
  418. }
  419. if (!function_exists('check_ip_allowed')) {
  420. /**
  421. * 检测IP是否允许
  422. * @param string $ip IP地址
  423. */
  424. function check_ip_allowed($ip = null)
  425. {
  426. $ip = is_null($ip) ? request()->ip() : $ip;
  427. $forbiddenipArr = config('site.forbiddenip');
  428. $forbiddenipArr = !$forbiddenipArr ? [] : $forbiddenipArr;
  429. $forbiddenipArr = is_array($forbiddenipArr) ? $forbiddenipArr : array_filter(explode("\n", str_replace("\r\n", "\n", $forbiddenipArr)));
  430. if ($forbiddenipArr && \Symfony\Component\HttpFoundation\IpUtils::checkIp($ip, $forbiddenipArr)) {
  431. header('HTTP/1.1 403 Forbidden');
  432. exit;
  433. }
  434. }
  435. }
  436. if (!function_exists('de')) {
  437. /**
  438. * 调试打印函数,执行后后续代码程序将终止
  439. * @param mixed $data 需要打印的数据
  440. * @author fuyelk <fuyelk@fuyelk.com>
  441. */
  442. function de($data = [])
  443. {
  444. echo "<pre>";
  445. if (empty($data) || is_bool($data)) {
  446. var_dump($data);
  447. exit();
  448. }
  449. if (empty($data)) exit;
  450. print_r($data);
  451. exit;
  452. }
  453. }
  454. if (!function_exists('dp')) {
  455. /**
  456. * 调试打印函数,执行后后续代码程序将终止
  457. * @param mixed $data 需要打印的数据
  458. * @author fuyelk <fuyelk@fuyelk.com>
  459. */
  460. function dp($data = [])
  461. {
  462. echo "<pre>";
  463. if (empty($data) || is_bool($data)) {
  464. var_dump($data);
  465. } else {
  466. print_r($data);
  467. }
  468. }
  469. }
  470. if (!function_exists('dt')) {
  471. /**
  472. * 日志
  473. * @param array $data 需要打印的数据
  474. * @param string $name 数据说明
  475. * @return string 日志唯一标识
  476. * @author fuyelk <fuyelk@fuyelk.com>
  477. */
  478. function dt($data = [], $name = '')
  479. {
  480. $log_id = uniqid();
  481. trace("########### [$log_id] 日志开始 ############");
  482. trace($name);
  483. trace($data);
  484. trace("########### [$log_id] 日志结束 ###########");
  485. return $log_id;
  486. }
  487. }
  488. if (!function_exists('dta')) {
  489. /**
  490. * 实时追加型日志
  491. * @param mixed $log 日志内容
  492. * @param string $name 数据说明
  493. * @return string 日志唯一标识
  494. * @author fuyelk <fuyelk@fuyelk.com>
  495. */
  496. function dta($log, $name = '')
  497. {
  498. if (!think\Env::get('app.dta', true)) {
  499. return '';
  500. }
  501. $debug = debug_backtrace();
  502. $log = is_object($log) ? (array)$log : $log;
  503. $log_id = uniqid();
  504. $trace_root = dirname(__DIR__) . '/runtime/log/' . date('Ym');
  505. $file = $trace_root . '/' . date('d') . '_a.log';
  506. if (!is_dir(dirname($file))) mkdir(dirname($file), 0755, true);
  507. $msg = "============ [$log_id] 日志开始 ============" . PHP_EOL;;
  508. $msg .= '[ file ] ' . $debug[0]['file'] . ':' . $debug[0]['line'] . PHP_EOL;
  509. $msg .= '[ time ] ' . date('Y-m-d H:i:s') . PHP_EOL;
  510. $msg .= '[ name ] ' . $name . PHP_EOL;
  511. $msg .= '[ data ] ' . (is_array($log) ? var_export($log, true) : $log) . PHP_EOL;
  512. $fp = @fopen($file, 'a');
  513. fwrite($fp, $msg);
  514. fclose($fp);
  515. return date('Ymd') . $log_id;
  516. }
  517. }
  518. if (!function_exists('get_log')) {
  519. /**
  520. * 获取日志
  521. * @param string $log 日志ID
  522. * @return bool|string
  523. * @author fuyelk <fuyelk@fuyelk.com>
  524. * @date 2021/4/21 13:18
  525. */
  526. function get_log($log)
  527. {
  528. if (empty($log)) return false;
  529. $logFile = dirname(__DIR__) . '/runtime/log/' . mb_substr($log, 0, 6) . '/' . mb_substr($log, 6, 2) . '_a.log';
  530. if (!is_file($logFile)) return false;
  531. $content = file_get_contents($logFile);
  532. $logName = mb_substr($log, 8);
  533. $tag = "============ [$logName] 日志开始 ============";
  534. $startIndex = mb_strpos($content, $tag);
  535. $content = mb_substr($content, $startIndex);
  536. $endIndex = mb_strpos($content, '============ [', 10);
  537. if (false === $endIndex) return $content;
  538. return mb_substr($content, 0, $endIndex);
  539. }
  540. }
  541. if (!function_exists('add_submenu')) {
  542. /**
  543. * Fastadmin 菜单节点添加增删改查
  544. * @param string $menu 节点名
  545. * @param int $pid 父节点ID
  546. * @return int|string
  547. * @author fuyelk <fuyelk@fuyelk.com>
  548. * @date 2021/06/28 14:32
  549. */
  550. function add_submenu(string $menu, int $pid)
  551. {
  552. $time = time();
  553. return db('auth_rule')->insertAll([
  554. [
  555. 'pid' => $pid,
  556. 'name' => $menu . '/index',
  557. 'title' => 'View',
  558. 'icon' => 'fa fa-circle-o',
  559. 'status' => 'normal',
  560. 'createtime' => $time,
  561. 'updatetime' => $time,
  562. ],
  563. [
  564. 'pid' => $pid,
  565. 'name' => $menu . '/add',
  566. 'title' => 'Add',
  567. 'icon' => 'fa fa-circle-o',
  568. 'status' => 'normal',
  569. 'createtime' => $time,
  570. 'updatetime' => $time,
  571. ],
  572. [
  573. 'pid' => $pid,
  574. 'name' => $menu . '/del',
  575. 'title' => 'Del',
  576. 'icon' => 'fa fa-circle-o',
  577. 'status' => 'normal',
  578. 'createtime' => $time,
  579. 'updatetime' => $time,
  580. ],
  581. [
  582. 'pid' => $pid,
  583. 'name' => $menu . '/edit',
  584. 'title' => 'Edit',
  585. 'icon' => 'fa fa-circle-o',
  586. 'status' => 'normal',
  587. 'createtime' => $time,
  588. 'updatetime' => $time,
  589. ],
  590. [
  591. 'pid' => $pid,
  592. 'name' => $menu . '/multi',
  593. 'title' => 'Multi',
  594. 'icon' => 'fa fa-circle-o',
  595. 'status' => 'normal',
  596. 'createtime' => $time,
  597. 'updatetime' => $time,
  598. ],
  599. ]);
  600. }
  601. }
  602. if (!function_exists('byte_conversion')) {
  603. /**
  604. * 格式化字节单位
  605. * @param int $size 字节大小
  606. * @param int $decimals 小数点位数
  607. * @param string $delimiter 分隔符
  608. * @return string
  609. */
  610. function byte_conversion($size = 0, $decimals = 3, $delimiter = ' ')
  611. {
  612. if ($size > pow(1024, 6)) {
  613. $size /= pow(1024, 5);
  614. return number_format($size, $decimals) . $delimiter . 'PB';
  615. }
  616. $units = array('Bytes', 'KB', 'MB', 'GB', 'TB', 'PB');
  617. for ($i = 0; $size >= 1024 && $i < 6; $i++) {
  618. $size /= 1024;
  619. }
  620. return number_format($size, $decimals) . $delimiter . $units[$i];
  621. }
  622. }
  623. if (!function_exists('create_tips_icon')) {
  624. /**
  625. * 创建角标
  626. * @description 应用在a标签中:<li><a href="">{:create_tips_icon(1)}</a></li>
  627. * @param string|number $tips 角标
  628. * @param string $color 颜色
  629. * @return string
  630. * @author fuyelk <fuyelk@fuyelk.com>
  631. * @date 2021/07/14 16:39
  632. */
  633. function create_tips_icon($tips, $color = 'yellow')
  634. {
  635. if (empty($tips)) {
  636. return '';
  637. }
  638. return "<span class=\"pull-right-container\"> <small class=\"label pull-right bg-{$color}\">{$tips}</small></span>";
  639. }
  640. }