isDir()) { deldir($fileinfo->getRealPath(), true); } else { @unlink($fileinfo->getRealPath()); } } if ($delself) { @rmdir($dirname); } return true; } } if (!function_exists('__')) { /** * 语言翻译 * @param string $name 被翻译字符 * @param array $vars 替换字符数组 * @param string $lang 翻译语言 * @return string */ function __(string $name, array $vars = [], string $lang = '') { if (is_numeric($name) || !$name) { return $name; } return \think\facade\Lang::get($name, $vars, $lang); } } if (!function_exists('get_sys_config')) { /** * 获取站点的系统配置,不传递参数则获取所有配置项 * @param string $name 变量名 * @param string $group 变量分组,传递此参数来获取某个分组的所有配置项 * @param bool $reduct 是否开启简洁模式,简洁模式下,获取多项配置时只返回配置的键值对 * @return string | array */ function get_sys_config($name = '', $group = '', $reduct = true) { $config = false; if ($name) { // 直接使用->value('value')不能使用到模型的类型格式化 $config = \app\admin\model\Config::where('name', $name)->find(); if ($config) $config = $config['value']; } else { if ($group) { $temp = \app\admin\model\Config::where('group', $group)->select()->toArray(); } else { $temp = \app\admin\model\Config::order('weigh desc')->select()->toArray(); } if ($reduct) { $config = []; foreach ($temp as $item) { $config[$item['name']] = $item['value']; } } else { $config = $temp; } } return $config; } } if (!function_exists('get_route_remark')) { /** * 获取当前路由后台菜单规则的备注信息 * @return string */ function get_route_remark() { $controllername = request()->controller(true); $actionname = request()->action(true); $path = str_replace('.', '/', $controllername); $remark = \think\facade\Db::name('menu_rule') ->where('name', $path) ->whereOr('name', $path . '/' . $actionname) ->value('remark'); return __((string)$remark); } } if (!function_exists('full_url')) { /** * 获取资源完整url地址 * @param string $relativeUrl 资源相对地址 不传入则获取域名 * @param boolean $domain 是否携带域名 或者直接传入域名 * @return string */ function full_url($relativeUrl = false, $domain = true, $default = '') { $relativeUrl = $relativeUrl ? $relativeUrl : $default; if (!$relativeUrl) { return $domain === true ? request()->domain() : $domain; } $regex = "/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i"; if (preg_match('/^http(s)?:\/\//', $relativeUrl) || preg_match($regex, $relativeUrl) || $domain === false) { return $relativeUrl; } return $domain === true ? request()->domain() . $relativeUrl : $domain . $relativeUrl; } } if (!function_exists('encrypt_password')) { /** * 加密密码 */ function encrypt_password($password, $salt = '', $encrypt = 'md5') { return $encrypt($encrypt($password) . $salt); } } if (!function_exists('action_in_arr')) { /** * 检测一个方法是否在传递的数组内 * @return bool */ function action_in_arr($arr = []) { $arr = is_array($arr) ? $arr : explode(',', $arr); if (!$arr) { return false; } $arr = array_map('strtolower', $arr); if (in_array(strtolower(request()->action()), $arr) || in_array('*', $arr)) { return true; } return false; } } if (!function_exists('build_suffix_svg')) { /** * 构建文件后缀的svg图片 * @param string $suffix 文件后缀 * @param string $background 背景颜色,如:rgb(255,255,255) * @return string */ function build_suffix_svg($suffix = 'file', $background = null) { $suffix = mb_substr(strtoupper($suffix), 0, 4); $total = unpack('L', hash('adler32', $suffix, true))[1]; $hue = $total % 360; [$r, $g, $b] = hsv2rgb($hue / 360, 0.3, 0.9); $background = $background ? $background : "rgb({$r},{$g},{$b})"; $icon = ' ' . $suffix . ' '; return $icon; } } if (!function_exists('get_area')) { function get_area() { $province = request()->get('province', ''); $city = request()->get('city', ''); $where = ['pid' => 0, 'level' => 1]; $provincelist = null; if ($province !== '') { $where['pid'] = $province; $where['level'] = 2; if ($city !== '') { $where['pid'] = $city; $where['level'] = 3; } } $provincelist = Db::name('area')->where($where)->field('id as value,name as label')->select(); return $provincelist; } } if (!function_exists('hsv2rgb')) { function hsv2rgb($h, $s, $v) { $r = $g = $b = 0; $i = floor($h * 6); $f = $h * 6 - $i; $p = $v * (1 - $s); $q = $v * (1 - $f * $s); $t = $v * (1 - (1 - $f) * $s); switch ($i % 6) { case 0: $r = $v; $g = $t; $b = $p; break; case 1: $r = $q; $g = $v; $b = $p; break; case 2: $r = $p; $g = $v; $b = $t; break; case 3: $r = $p; $g = $q; $b = $v; break; case 4: $r = $t; $g = $p; $b = $v; break; case 5: $r = $v; $g = $p; $b = $q; break; } return [ floor($r * 255), floor($g * 255), floor($b * 255) ]; } }