企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 工具函数 工具函数列表,注释都写的很清楚了。 ```php <?php /** * Fend Framework Helpers * [Gimoo!] (C)2006-2009 Gimoo Inc. (http://Fend.Gimoo.Net) **/ use \App\Fend\Application; if (! function_exists('isIp')) { /** * Fend Framework * [Gimoo!] (C)2006-2009 Eduu Inc. (http://www.eduu.com) * * 检测一个IP地址是否正常 * 只能进行模糊的检测 * * @Package GimooFend * @Support http://bbs.eduu.com * @Author Gimoo <gimoohr@gmail.com> * @version $Id: fend.isip.php 3 2011-12-29 15:01:09Z gimoo $ **/ function isIp($ip) { return preg_match('/^\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}$/', $ip) ? true : false; } } if (! function_exists('isMail')) { /** * Fend Framework * [Gimoo!] (C)2006-2009 Eduu Inc. (http://www.eduu.com) * * 检测是否为Email * * @param string $str 电子邮件Email * @return bool 成功则是true * @-------------------------------- * @Package GimooFend * @Support http://bbs.eduu.com * @Author Gimoo <gimoohr@gmail.com> * @version $Id: fend.ismail.php 3 2011-12-29 15:01:09Z gimoo $ **/ function isMail($str) { return preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/', $str); } } if (! function_exists('isMobile')) { /** * Fend Framework * [Gimoo!] (C)2006-2009 Eduu Inc. (http://www.eduu.com) * * 检测是否来自移动端 * * @Package GimooFend * @Author Gimoo <gimoohr@gmail.com> * @version $Id$ **/ function isMobile() { $type = 0; //0未知,1IOS,2安卓 $_tmp = $_SERVER['HTTP_USER_AGENT']; if (false !== stripos($_tmp, 'android')) { $type = 2; } elseif (stripos($_tmp, 'iphone') || stripos($_tmp, 'ipod')) { $type = 1; } else { $type = 0; } return $type; } } if (!function_exists('dump')) { /** * @author Nicolas Grekas <p@tchwork.com> */ function dump($var, ...$moreVars) { var_dump($var); foreach ($moreVars as $v) { var_dump($v); } if (1 < func_num_args()) { return func_get_args(); } return $var; } } if (!function_exists('dd')) { function dd(...$vars) { foreach ($vars as $v) { var_dump($v); } die(1); } } if (! function_exists('getHost')) { /** * 获取HOTS */ function getHost() { return empty($_SERVER['HTTP_HOST']) ? 'localhost' : $_SERVER['HTTP_HOST']; } } if (! function_exists('getDomain')) { /** * 获取 ServerName */ function getDomain() { $host = getHost(); $domain = explode('.', $host); if (count($domain) < 3) { return $host; } unset($domain[0]); return implode('.', $domain); } } if (! function_exists('getDomainPrefix')) { /** * 获取 ServerName */ function getDomainPrefix() { $host = getHost(); $domain = explode('.', $host); return count($domain) >= 3 ? $domain[0] : $host; } } if (! function_exists('byteToSize')) { /** * 单位转换 * * @param $byte * @return string */ function byteToSize($byte) { if ($byte > pow(2, 40)) { $size = round($byte / pow(2, 40), 2) . ' TB'; } elseif ($byte > pow(2, 30)) { $size = round($byte / pow(2, 30), 2) . ' GB'; } elseif ($byte > pow(2, 20)) { $size = round($byte / pow(2, 20), 2) . ' MB'; } elseif ($byte > pow(2, 10)) { $size = round($byte / pow(2, 10), 2) . ' KB'; } else { $size = round($byte, 2) . ' B'; } return $size; } } if (! function_exists("getValue")) { /** * 获取对象/数组值 * * @param $arr_or_obj * @param $key_or_prop * @param string $else * @return mixed|string */ function getValue($arr_or_obj, $key_or_prop, $else = '') { $result = $else; if (isset($arr_or_obj)) { if (is_array($arr_or_obj)) { if (isset($arr_or_obj[$key_or_prop])) { $result = $arr_or_obj[$key_or_prop]; } } elseif (is_object($arr_or_obj)) { if (isset($arr_or_obj->$key_or_prop)) { $result = $arr_or_obj->$key_or_prop; } } } return $result; } } if (! function_exists("isJson")) { /** * 检查字符串是否是 json 格式 * * @param $string * @return bool */ function isJson($string) { json_decode($string); return (json_last_error() == JSON_ERROR_NONE); } } if (! function_exists('app')) { /** * Get the available container instance. * * @param string $abstract * @param array $parameters * @return mixed|\Mod\Fend\Application */ function app($abstract = null, array $parameters = []) { if (is_null($abstract)) { return Application::getInstance(); } return Application::getInstance()->make($abstract, $parameters); } } if (! function_exists('event')) { function event($event = null) { app('events')->dispatch($event->getEventName(), $event); } } if (! function_exists('data_set')) { /** * Set an item on an array or object using dot notation. * * @param mixed $target * @param string|array $key * @param mixed $value * @param bool $overwrite * @return mixed */ function data_set(&$target, $key, $value, $overwrite = true) { $segments = is_array($key) ? $key : explode('.', $key); if (($segment = array_shift($segments)) === '*') { if (! is_array($target)) { $target = []; } if ($segments) { foreach ($target as &$inner) { data_set($inner, $segments, $value, $overwrite); } } elseif ($overwrite) { foreach ($target as &$inner) { $inner = $value; } } } elseif (is_array($target)) { if ($segments) { if (! array_key_exists($segment, $target)) { $target[$segment] = []; } data_set($target[$segment], $segments, $value, $overwrite); } elseif ($overwrite || ! array_key_exists($segment, $target)) { $target[$segment] = $value; } } elseif (is_object($target)) { if ($segments) { if (! isset($target->{$segment})) { $target->{$segment} = []; } data_set($target->{$segment}, $segments, $value, $overwrite); } elseif ($overwrite || ! isset($target->{$segment})) { $target->{$segment} = $value; } } else { $target = []; if ($segments) { data_set($target[$segment], $segments, $value, $overwrite); } elseif ($overwrite) { $target[$segment] = $value; } } return $target; } } if (! function_exists('data_get')) { /** * Get an item from an array or object using "dot" notation. * * @param mixed $target * @param string|array|int $key * @param mixed $default * @return mixed */ function data_get($target, $key, $default = null) { if (is_null($key)) { return $target; } $key = is_array($key) ? $key : explode('.', $key); while (! is_null($segment = array_shift($key))) { if ($segment === '*') { if (! is_array($target)) { return value($default); } $result = []; foreach ($target as $item) { $result[] = data_get($item, $key); } return in_array('*', $key) ? collapse($result) : $result; } if (is_array($target) && array_key_exists($segment, $target)) { $target = $target[$segment]; } elseif (is_object($target) && isset($target->{$segment})) { $target = $target->{$segment}; } else { return value($default); } } return $target; } } if (! function_exists('value')) { /** * Return the default value of the given value. * * @param mixed $value * @return mixed */ function value($value) { return $value instanceof Closure ? $value() : $value; } } if (! function_exists('collapse')) { /** * Collapse an array of arrays into a single array. * * @param array $array * @return array */ function collapse($array) { $results = []; foreach ($array as $values) { if (! is_array($values)) { continue; } $results = array_merge($results, $values); } return $results; } } if (! function_exists('rand_str')) { /** * 生成随机字符串 * * @param int $length * @return string */ function rand_str($length = 6) { $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQEST123456789'; $len = strlen($chars); $randStr = ''; for ($i = 0; $i < $length; $i++) { $randStr .= $chars[mt_rand(0, $len - 1)]; } return $randStr; } } if (! function_exists('formatMoney')) { /** * 格式化金额 * * @param $val * @return string */ function formatMoney($val) { return rtrim(rtrim(sprintf('%.2f', $val), '0'), '.'); } } if (! function_exists('getIp')) { /** * 获取IP * * @return array|false|string */ function getIp() { $ip = '0.0.0.0'; if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) { $ip = getenv('HTTP_CLIENT_IP'); } elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); } elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) { $ip = getenv('REMOTE_ADDR'); } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) { $ip = $_SERVER['REMOTE_ADDR']; } return ($ip); } } if (! function_exists('strStudly')) { /** * Convert a value to studly caps case. * * @param string $value * @return string */ function strStudly($value) { $value = ucwords(str_replace(['-', '_'], ' ', $value)); return str_replace(' ', '', $value); } } if (! function_exists('strSnake')) { /** * Convert a string to snake case. * * @param string $value * @param string $delimiter * @return string */ function strSnake($value, $delimiter = '_') { if (! ctype_lower($value)) { $value = preg_replace('/\s+/u', '', ucwords($value)); $value = strLower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value)); } return $value; } } if (! function_exists('strLower')) { /** * Convert the given string to lower-case. * * @param string $value * @return string */ function strLower($value) { return mb_strtolower($value, 'UTF-8'); } } if (! function_exists('class_basename')) { /** * Get the class "basename" of the given object / class. * * @param string|object $class * @return string */ function class_basename($class) { $class = is_object($class) ? get_class($class) : $class; return basename(str_replace('\\', '/', $class)); } } if (! function_exists('getUrl')) { /** * 获取当前URL * * @param int $tx * @return string */ function getUrl($tx = 0) { if ($tx) return empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['HTTP_REFERER']; $url = 'http://' . $_SERVER['HTTP_HOST']; if (isset($_SERVER['REQUEST_URI'])) { $url .= $_SERVER['REQUEST_URI']; } else { $url .= $_SERVER['PHP_SELF'] . (! empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : ''); } return $url; } } if (! function_exists('isRun')) { function isRun($fName, $type) { global $_cfg; $fPath = $_cfg['sys_cache'] . $fName . '.lock'; // 开始 if ($type == 'start') { // 没有找到文件可以继续运行 if (is_file($fPath)) { die("already running.\n"); } //写入一个临时文件 @file_put_contents($fPath, time()); // 检测是否有运行 } elseif ($type == 'isrun') { return is_file($fPath) ? true : false; // 结束 } else { @unlink($fPath); } return false; } } if (! function_exists('incrScores')) { /** * 金币/积分 变更 * * @param $uid * @param int $t * @param string $intro * * @return int */ function incrScores($uid, $t = 0, $intro = '备注') { // 还是做一次校验吧 $uid = (int)$uid; if ($uid <= 0 || $t == 0) return 0; $rs = ['uid' => $uid, 'scores' => $t, 'intro' => $intro]; $res = \Api_Home::Factory()->getRes("user/score/add", $rs); return $res; } } if (! function_exists('incrNameScores')) { /** * 根据扣减金币规则增减金币 * @param $uid 增减用户 * @param $name 增减规则 * @param int $r 执行次数 * @return bool */ function incrNameScores($uid, $name, $r = 1) { $data = [ 'uid' => $uid, 'cmd' => $name, 'r' => $r ]; $res = \Api_Home::factory()->getRes('user/score/addName', $data); return empty($res) ? false : $res; } } if (! function_exists('incrCredits')) { /** * 经验值/小红花 变更 * * @param $uid * @param int $t * @param string $intro * * @return int */ function incrCredits($uid, $t = 0, $intro = '系统赠送') { // 还是做一次校验吧 $uid = (int)$uid; if ($uid <= 0 || $t == 0) return 0; // 去远程更新 $rs = ['uid' => $uid, 'scores' => $t, 'intro' => $intro]; $res = \Api_Home::Factory()->getRes("user/credit/add", $rs); return $res; } } if (! function_exists('userInfo')) { /** * 获取用户信息 * * @param int $uid * @param string $f * @return array */ function userInfo($uid, $f = '') { $uid = (int)$uid; if ($uid <= 0 || empty($f)) { return []; } is_array($f) && $f = implode(',', $f); $res = \Api_Home::Factory()->getRes("user/info/get?uid={$uid}&f={$f}"); return $res; } } ```