= 1024 && $i < 6; $i++) $size /= 1024; return round($size, 2) . $delimiter . $units[$i]; } } if (!function_exists('datetime')) { /** * 将时间戳转换为日期时间 * @param int $time 时间戳 * @param string $format 日期时间格式 * @return string */ function datetime($time, $format = 'Y-m-d H:i:s') { $time = is_numeric($time) ? $time : strtotime($time); return date($format, $time); } } if (!function_exists('human_date')) { /** * 获取语义化时间 * @param int $time 时间 * @param int $local 本地时间 * @return string */ function human_date($time, $local = null) { return \fast\Date::human($time, $local); } } if (!function_exists('cdnurl')) { /** * 获取上传资源的CDN的地址 * @param string $url 资源相对地址 * @return string */ function cdnurl($url) { return preg_match("/^https?:\/\/(.*)/i", $url) ? $url : think\Config::get('upload.cdnurl') . $url; } } if (!function_exists('getCurrentDomain')) { /** * 获取用户当前域名 * @param int $channel_id 渠道ID * @param string $redirect 跳转地址 * @param array|string $params 参数 * @param bool $isUseMenuDomain * @return string */ function getCurrentDomain($channel_id,$redirect = null,$params = [],$isUseMenuDomain = false){ \think\Log::info('GetCurrentDomain: Channel_id:'.$channel_id.' Redirect:'.$redirect.' Params:'.(is_array($params)? var_export($params,true):$params)); if (!$channel_id) { return ''; } try{ $webParams = []; $webSitePath = ''; $scheme = \think\Config::get('site.scheme'); if (empty($scheme)) { //库里拉取 $site = model("Config")->getConfigSiteArr(); $scheme = $site['scheme']; } $adminConfig = \app\main\service\AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channel_id); if($isUseMenuDomain && isset($adminConfig['menuophost']) && !empty($adminConfig['menuophost'])){ $webSite = $scheme.'://'.($adminConfig['appid'] ?? '').'.'.$adminConfig['menuophost']; }else{ $webSite = $scheme.'://'.($adminConfig['appid'] ?? '').'.'.$adminConfig['ophost_host']; } if($redirect){ $pathParams = parse_url($redirect); //添加路径 if(isset($pathParams['path']) && $pathParams['path']){ $webSitePath = $pathParams['path']; } //检查路径是否有参数 if(isset($pathParams['query']) && $pathParams['query']){ $pathQuery = explode('&',$pathParams['query']); if($pathQuery){ foreach($pathQuery as $val){ $temp = explode('=',$val); $webParams[$temp[0]] = $temp[1] ?? ''; } } } } if($params){ if(is_array($params)){ $webParams = array_merge($webParams,$params); } if(is_string($params)){ //检查参数 $pathParams = parse_url($params); if(isset($pathParams['query']) && $pathParams['query']){ $pathQuery = explode('&',$pathParams['query']); if($pathQuery){ foreach($pathQuery as $val){ $temp = explode('=',$val); $webParams[$temp[0]] = $temp[1] ?? ''; } } } } } //拼接路径 if(empty($webSitePath)){ $webSitePath = '/'; } $webSite = $webSite . $webSitePath; //拼接参数 if($webParams){ $webSite = $webSite .'?'.http_build_query($webParams); } \think\Log::info('GetCurrentDomain: URI:'.$webSite); return $webSite; }catch (\Exception $e){ \think\Log::error('GetCurrentDomain Error:'.$e->getMessage()); } return ''; } } if (!function_exists('getCurrentOphost')) { function getCurrentOphost(){ $domain = get_host_no_port(); $hostArr = explode('.', $domain); array_shift($hostArr); return implode('.', $hostArr); } } if (!function_exists('is_really_writable')) { /** * 判断文件或文件夹是否可写 * @param string $file 文件或目录 * @return bool */ function is_really_writable($file) { if (DIRECTORY_SEPARATOR === '/') { return is_writable($file); } if (is_dir($file)) { $file = rtrim($file, '/') . '/' . md5(mt_rand()); if (($fp = @fopen($file, 'ab')) === FALSE) { return FALSE; } fclose($fp); @chmod($file, 0777); @unlink($file); return TRUE; } elseif (!is_file($file) OR ( $fp = @fopen($file, 'ab')) === FALSE) { return FALSE; } fclose($fp); return TRUE; } } if (!function_exists('rmdirs')) { /** * 删除文件夹 * @param string $dirname 目录 * @param bool $withself 是否删除自身 * @return boolean */ function rmdirs($dirname, $withself = true) { if (!is_dir($dirname)) return false; $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST ); foreach ($files as $fileinfo) { $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink'); $todo($fileinfo->getRealPath()); } if ($withself) { @rmdir($dirname); } return true; } } if (!function_exists('copydirs')) { /** * 复制文件夹 * @param string $source 源文件夹 * @param string $dest 目标文件夹 */ function copydirs($source, $dest) { if (!is_dir($dest)) { mkdir($dest, 0755); } foreach ( $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST) as $item ) { if ($item->isDir()) { $sontDir = $dest . DS . $iterator->getSubPathName(); if (!is_dir($sontDir)) { mkdir($sontDir); } } else { copy($item, $dest . DS . $iterator->getSubPathName()); } } } } if (!function_exists('mb_ucfirst')) { function mb_ucfirst($string) { return mb_strtoupper(mb_substr($string, 0, 1)) . mb_strtolower(mb_substr($string, 1)); } } if (!function_exists('addtion')) { /** * 附加关联字段数据 * @param array $items 数据列表 * @param mixed $fields 渲染的来源字段 * @return array */ function addtion($items, $fields) { if (!$items || !$fields) return $items; $fieldsArr = []; if (!is_array($fields)) { $arr = explode(',', $fields); foreach ($arr as $k => $v) { $fieldsArr[$v] = ['field' => $v]; } } else { foreach ($fields as $k => $v) { if (is_array($v)) { $v['field'] = isset($v['field']) ? $v['field'] : $k; } else { $v = ['field' => $v]; } $fieldsArr[$v['field']] = $v; } } foreach ($fieldsArr as $k => &$v) { $v = is_array($v) ? $v : ['field' => $v]; $v['display'] = isset($v['display']) ? $v['display'] : str_replace(['_ids', '_id'], ['_names', '_name'], $v['field']); $v['primary'] = isset($v['primary']) ? $v['primary'] : ''; $v['column'] = isset($v['column']) ? $v['column'] : 'name'; $v['model'] = isset($v['model']) ? $v['model'] : ''; $v['table'] = isset($v['table']) ? $v['table'] : ''; $v['name'] = isset($v['name']) ? $v['name'] : str_replace(['_ids', '_id'], '', $v['field']); } unset($v); $ids = []; $fields = array_keys($fieldsArr); foreach ($items as $k => $v) { foreach ($fields as $m => $n) { if (isset($v[$n])) { $ids[$n] = array_merge(isset($ids[$n]) && is_array($ids[$n]) ? $ids[$n] : [], explode(',', $v[$n])); } } } $result = []; foreach ($fieldsArr as $k => $v) { if ($v['model']) { $model = new $v['model']; } else { $model = $v['name'] ? \think\Db::name($v['name']) : \think\Db::table($v['table']); } $primary = $v['primary'] ? $v['primary'] : $model->getPk(); $result[$v['field']] = $model->where($primary, 'in', $ids[$v['field']])->column("{$primary},{$v['column']}"); } foreach ($items as $k => &$v) { foreach ($fields as $m => $n) { if (isset($v[$n])) { $curr = array_flip(explode(',', $v[$n])); $v[$fieldsArr[$n]['display']] = implode(',', array_intersect_key($result[$n], $curr)); } } } return $items; } } if(!function_exists('formatNumber')) { /** * 格式化书籍字数 */ function formatNumber($string) { if(stristr($string,'万')){ return $string; } $num = intval($string); if($num > 10000){ $num = intval($string)/10000; return round($num,2).'万'; }else{ return $string; } } } if(!function_exists('replaceShortDomain')) { /** * 使用渠道商/代理商绑定的短链接域名替换默认短链接域名 * * @author liues@dianzhong.com * @date 2018-08-16 16:18:23 * @param string $source 默认短链接地址,如: * @param string $short_id 短链接域名 * @return string */ function replaceShortDomain($source, $short_id) { $host = model('ShortDomain')->getHostById($short_id); $source = empty($host) ? $source : sprintf("%s%s", $host, parse_url($source, PHP_URL_PATH)); return $source; } } //下划线转驼峰 if(!function_exists('camelizeArr')){ function camelizeArr(array $arr) { if(is_array($arr)){ $newArr = []; foreach ($arr as $key=>$value){ if ($value instanceof \think\Model) { $value = $value->toArray(); } $newKey = camelize($key); if(is_array($value)){ $newArr[$newKey] = camelizeArr($value); }else{ $newArr[$newKey] = $value; } } return $newArr; } return $arr; } } //下划线转驼峰 if(!function_exists('camelize')){ function camelize($uncamelized_words,$separator='_') { $uncamelized_words = $separator. str_replace($separator, " ", strtolower($uncamelized_words)); return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator ); } }