= 0 ? true : false; } /** * @notes 检查文件是否可写 * @param string $dir * @return bool * @author 段誉 * @date 2021/12/28 18:27 */ function check_dir_write(string $dir = '') : bool { $route = root_path() . '/' . $dir; return is_writable($route); } /** * 多级线性结构排序 * 转换前: * [{"id":1,"pid":0,"name":"a"},{"id":2,"pid":0,"name":"b"},{"id":3,"pid":1,"name":"c"}, * {"id":4,"pid":2,"name":"d"},{"id":5,"pid":4,"name":"e"},{"id":6,"pid":5,"name":"f"}, * {"id":7,"pid":3,"name":"g"}] * 转换后: * [{"id":1,"pid":0,"name":"a","level":1},{"id":3,"pid":1,"name":"c","level":2},{"id":7,"pid":3,"name":"g","level":3}, * {"id":2,"pid":0,"name":"b","level":1},{"id":4,"pid":2,"name":"d","level":2},{"id":5,"pid":4,"name":"e","level":3}, * {"id":6,"pid":5,"name":"f","level":4}] * @param array $data 线性结构数组 * @param string $symbol 名称前面加符号 * @param string $name 名称 * @param string $id_name 数组id名 * @param string $parent_id_name 数组祖先id名 * @param int $level 此值请勿给参数 * @param int $parent_id 此值请勿给参数 * @return array */ function linear_to_tree($data, $sub_key_name = 'sub', $id_name = 'id', $parent_id_name = 'pid', $parent_id = 0) { $tree = []; foreach ($data as $row) { if ($row[$parent_id_name] == $parent_id) { $temp = $row; $child = linear_to_tree($data, $sub_key_name, $id_name, $parent_id_name, $row[$id_name]); if ($child) { $temp[$sub_key_name] = $child; } $tree[] = $temp; } } return $tree; } /** * 根据父级ID获取所有子集的值 * @param $data * @param $pid * @param $idField * @param $pidField * @return array */ function get_tree_ids($data,$pid = 0, $idField = 'id',$pidField = 'pid') { $child = []; foreach($data as $val){ if ($val[$pidField] == $pid) { $children = get_tree_ids($data, $val[$idField],$idField,$pidField,); if ( count($children) > 0) { $child = array_merge($child,$children); } $child[] = $val['id']; } } return $child; } /** * @notes 删除目标目录 * @param $path * @param $delDir * @return bool|void * @author 段誉 * @date 2022/4/8 16:30 */ function del_target_dir($path, $delDir) { //没找到,不处理 if (!file_exists($path)) { return false; } //打开目录句柄 $handle = opendir($path); if ($handle) { while (false !== ($item = readdir($handle))) { if ($item != "." && $item != "..") { if (is_dir("$path/$item")) { del_target_dir("$path/$item", $delDir); } else { unlink("$path/$item"); } } } closedir($handle); if ($delDir) { return rmdir($path); } } else { if (file_exists($path)) { return unlink($path); } return false; } } /** * @notes 下载文件 * @param $url * @param $saveDir * @param $fileName * @return string * @author 段誉 * @date 2022/9/16 9:53 */ function download_file($url, $saveDir, $fileName) { if (!file_exists($saveDir)) { mkdir($saveDir, 0775, true); } $fileSrc = $saveDir . $fileName; file_exists($fileSrc) && unlink($fileSrc); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); $file = curl_exec($ch); curl_close($ch); $resource = fopen($fileSrc, 'a'); fwrite($resource, $file); fclose($resource); if (filesize($fileSrc) == 0) { unlink($fileSrc); return ''; } return $fileSrc; } /** * @notes 去除内容图片域名 * @param $content * @return array|string|string[] * @author 段誉 * @date 2022/9/26 10:43 */ function clear_file_domain($content) { $fileUrl = FileService::getFileUrl(); $pattern = '/]*\bsrc=["\']'.preg_quote($fileUrl, '/').'([^"\']+)["\']/i'; return preg_replace($pattern, 'make($table)->where($field, $sn)->find()) { return generate_sn($table, $field, $prefix, $randSuffixLength, $pool); } return $sn; } /** * @notes 格式化金额 * @param $float * @return int|mixed|string * @author 段誉 * @date 2023/2/24 11:20 */ function format_amount($float) { if ($float == intval($float)) { return intval($float); } elseif ($float == sprintf('%.1f', $float)) { return sprintf('%.1f', $float); } return $float; } /** * curl提交 * @param $str * @return string */ function http_request($url , $data = NULL) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); // 检查是否有错误发生 if(curl_errno($ch)) { echo 'CURL ERROR CODE: '. curl_errno($ch) . ' , reason : ' . curl_error($ch); } curl_close($ch); $jsoninfo = json_decode($output , true); return $jsoninfo; } /** * sql语句打印 * 需要打印sql时将record_sql()方法放到sql语句之前,或 config.database.trigger_sql设置为true */ function record_sql() { if(!config("database.connections.mysql.trigger_sql")){ $config = config('database'); $config['connections']['mysql']['trigger_sql'] = true; app()->config->set($config,'database'); } \think\facade\Db::listen(function ($sql,$time,$connection) { if(strpos($sql,'CONNECT') !== false){ return; } if(strpos($sql,'SHOW FULL') !== false){ return; } \think\facade\Log::debug( '打印sql: '.$sql. ' time:'.$time); }); }