common.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. <?php
  2. // 应用公共文件
  3. use think\facade\Config;
  4. use think\facade\Db;
  5. use think\facade\Filesystem;
  6. use tp5er\thinkCsv\Export;
  7. // 应用公共文件
  8. function app_show($code=0,$message="",$data=[]){
  9. $result = ['code'=>$code,"message"=>$message,"data"=>$data];
  10. return json($result);
  11. }
  12. // 应用公共文件
  13. function error_show($code=0,$message=""){
  14. $result = ['code'=>$code,"message"=>$message];
  15. return json($result);
  16. }
  17. function GetUserInfo($token){
  18. $host = Config::get("app");
  19. $url = $host["api_host"]."/verifyToken";
  20. $data=[
  21. "token"=>$token
  22. ];
  23. $response=curl_request($url,$data);
  24. return json_decode($response,true);
  25. }
  26. function setUserCompany($condition){
  27. $host = Config::get("app");
  28. $url = $host["api_host"]."/setcompany";
  29. $response=curl_request($url,$condition);
  30. return json_decode($response,true);
  31. }
  32. function setCompanyStatus($condition){
  33. $host = Config::get("app");
  34. $url = $host["api_host"]."/companystatus";
  35. $response=curl_request($url,$condition);
  36. return json_decode($response,true);
  37. }
  38. function setStatus($condition){
  39. $host = Config::get("app");
  40. $url = $host["api_host"]."/userstatus";
  41. $response=curl_request($url,$condition);
  42. return json_decode($response,true);
  43. }
  44. /**手机号验证
  45. * @param $mobile
  46. * @return bool
  47. */
  48. function checkMobile($mobile){
  49. if (!is_numeric($mobile)) {
  50. return false;
  51. }
  52. return preg_match('#^1[3,4,5,6,7,8,9]{1}[\d]{9}$#', $mobile) ? true : false;
  53. }
  54. function checkTel($tel){
  55. if (!$tel) {
  56. return false;
  57. }
  58. return preg_match('/^(0[0-9]{2,3}\-)([0-9]{7,8})+(\-[0-9]{1,4})?$/', $tel) ? true : false;
  59. }
  60. /**邮箱验证
  61. * @param $email
  62. * @return bool
  63. */
  64. function checkEmail($email){
  65. if (!$email) {
  66. return false;
  67. }
  68. return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $email) ? true : false;
  69. }
  70. /**
  71. * @param
  72. * @return int
  73. */
  74. function makeSalt(){
  75. $salt = rand(10000000,99999999);
  76. return $salt;
  77. }
  78. /**
  79. * @param $token
  80. * @return array
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. * @throws \think\exception\DbException
  85. */
  86. function VerifyTokens($token){
  87. $host = Config::get("app");
  88. $url = $host["api_host"]."/verifyToken";
  89. $data=[
  90. "token"=>$token
  91. ];
  92. $response=curl_request($url,$data);
  93. return json_decode($response,true);
  94. }
  95. /**
  96. * @param $token
  97. * @param $condition
  98. * @return mixed
  99. */
  100. function GetUserlist($condition){
  101. $host = Config::get("app");
  102. $url = $host["api_host"]."/userlistbycompany";
  103. $response=curl_request($url,$condition);
  104. return json_decode($response,true);
  105. }
  106. ///**
  107. // * @param $token
  108. // * @param $condition
  109. // * @return mixed
  110. // */
  111. //function GetAccountall($token, $condition){
  112. // $host = Config::get("app");
  113. //
  114. // $url = $host["api_host"]."/Api/userall";
  115. // $condition['token']=$token;
  116. // $response=curl_request($url,$condition);
  117. //
  118. // return json_decode($response,true);
  119. //}
  120. function GetList($condition){
  121. $host = Config::get("app");
  122. $url = $host["api_host"]."/userlist";
  123. $response=curl_request($url,$condition);
  124. return json_decode($response,true);
  125. }
  126. /**
  127. * @param $token
  128. * @param $condition ['id'=>1]
  129. * @return mixed
  130. */
  131. function GetInfoById($token,$condition){
  132. $host = Config::get("app");
  133. $url = $host["api_host"]."/userinfo";
  134. $condition['token']=$token;
  135. $response=curl_request($url,$condition);
  136. return json_decode($response,true);
  137. }
  138. /**
  139. * @param $str
  140. * @return string
  141. */
  142. function makeNo($str){
  143. $date=date("mdHis");
  144. $year = date("Y")-2000;
  145. $msec=rand(1000,9999);
  146. return $str.$year.$date.$msec;
  147. }
  148. function makeStr($str){
  149. $date=date("mdHis");
  150. $year = date("Y")-2000;
  151. $msec=randomkeys(4);
  152. return $str.$msec.$year.$date;
  153. }
  154. function randomkeys($length) {
  155. $returnStr='';
  156. $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';//ABCDEFGHIJKLOMNOPQRSTUVWXYZ
  157. for($i = 0; $i < $length; $i ++) {
  158. $returnStr .= $pattern[mt_rand ( 0, strlen($pattern)-1 )]; //生成php随机数
  159. }
  160. return $returnStr;
  161. }
  162. /**
  163. * @param $token
  164. * @param $condition
  165. * @return mixed
  166. */
  167. function resetpwd($condition){
  168. $host = Config::get("app");
  169. $url = $host["api_host"]."/setpasswd";
  170. $response=curl_request($url,$condition);
  171. return json_decode($response,true);
  172. }
  173. function resetpasswd($token,$condition){
  174. $host = Config::get("app");
  175. $url = $host["api_host"]."/Api/passsave";
  176. $condition['token']=$token;
  177. $response=curl_request($url,$condition);
  178. return json_decode($response,true);
  179. }
  180. /**
  181. * @param $condition
  182. * @return array|bool|float|int|mixed|\stdClass|string|null
  183. */
  184. function checkLogin($condition){
  185. $host = Config::get("app");
  186. $url = $host["api_host"]."/login";
  187. $response=curl_request($url,$condition);
  188. return json_decode($response,true);
  189. }
  190. /**
  191. * @param $token
  192. * @param $condition
  193. * @return mixed
  194. */
  195. function resetinfo($condition){
  196. $host = Config::get("app");
  197. $url = $host["api_host"]."/usersave";
  198. $response=curl_request($url,$condition);
  199. return json_decode($response,true);
  200. }
  201. /**
  202. * @param $condition
  203. * @return array|bool|float|int|mixed|\stdClass|string|null
  204. */
  205. function addacount($condition){
  206. $host = Config::get("app");
  207. $url = $host["api_host"]."/useradd";
  208. $response=curl_request($url,$condition);
  209. return json_decode($response,true);
  210. }
  211. //添加超管账号
  212. if(function_exists('add_admin_account') == false){
  213. function add_admin_acount(array $data=[]){
  214. $host = Config::get("app");
  215. $url = $host["api_host"]."/add_admin_account";
  216. $response=curl_request($url,$data);
  217. return json_decode($response,true);
  218. }
  219. }
  220. if(!function_exists("headerSign")){
  221. function headerSign($post){
  222. $config = Config::get("sign");
  223. $appid=$config['appid'];
  224. $appkey=$config['appkey'];
  225. $headerArr=["appid"=>'123',"noce"=>randomkeys(16),"sign"=>'',"timestamp"=>time()];
  226. $value =array_merge($post,$headerArr);
  227. $Sign= new \Sign($appid,$appkey);
  228. $headerArr['sign'] = $Sign->makeSign($value);
  229. $list=["Content-Type"=>"multipart/json;charset=utf-8"];
  230. foreach ($headerArr as $key=>$value){
  231. $list[]=$key.":".$value;
  232. }
  233. return $list;
  234. }
  235. }
  236. //参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies
  237. function curl_request($url,$post='',$header=[]){
  238. // $header = empty($header) ? '' : $header;
  239. $header = headerSign($post);
  240. if(is_array($post)) $post=http_build_query($post);
  241. $curl = curl_init();
  242. curl_setopt($curl, CURLOPT_URL, $url);
  243. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  244. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  245. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  246. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  247. if($post) {
  248. curl_setopt($curl, CURLOPT_POST, 1);
  249. curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  250. }
  251. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  252. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  253. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  254. $data = curl_exec($curl);
  255. if (curl_errno($curl)) {
  256. return curl_error($curl);
  257. }
  258. curl_close($curl);
  259. return $data;
  260. }
  261. /**
  262. * @param $roleid
  263. * @param $menu
  264. * @return bool
  265. * @throws \think\db\exception\DataNotFoundException
  266. * @throws \think\db\exception\DbException
  267. * @throws \think\db\exception\ModelNotFoundException
  268. */
  269. function checkRole($roleid,$menu){
  270. $roleinfo = \think\facade\Db::name("role_action")->where([['role_id',"=",$roleid],["status","=",1]])->find();
  271. if($roleinfo['private_data']!=""){
  272. $private = explode(",",$roleinfo['private_data']);
  273. if(in_array($menu,$private)){
  274. return true;
  275. }
  276. }
  277. return false;
  278. }
  279. /**
  280. * @param $row
  281. * @param $list
  282. */
  283. function makeMenu($row,&$list){
  284. $list[$row['id']]=$row;
  285. if($row['pid']==0){
  286. return $list;
  287. }
  288. $parent =Db::name("admin_menu")->where(["id"=>$row['pid'],"status"=>1,"is_del"=>0])->field("id,menu_name,menu_img,menu_url,menu_route,pid,is_show,is_private,menu_type,weight")->find();
  289. if($parent==false) return $list;
  290. makeMenu($parent,$list);
  291. }
  292. /**
  293. * 遍历集合处理
  294. * @param $menuArr
  295. */
  296. function MenuTree(&$menuArr,$pid=0){
  297. $meun=[];
  298. foreach ($menuArr as $value){
  299. if($value['pid']==$pid){
  300. if($value['menu_type']==1)$value['child']=MenuTree($menuArr,$value['id']);
  301. $meun[]=$value ;
  302. }
  303. }
  304. return $meun;
  305. };
  306. function upload($files,$extend="xls")
  307. {
  308. // 获取表单上传文件
  309. try {
  310. validate([
  311. 'file' => [
  312. 'fileExt' => 'xlsx,xls'
  313. ]
  314. ],
  315. [
  316. 'file.fileExt' => '不支持的文件',
  317. ]
  318. )->check(['file' => $files]);
  319. if ($extend == 'xlsx') {
  320. $objReader = PHPExcel_IOFactory::createReader('Excel2007');
  321. } else {
  322. $objReader = PHPExcel_IOFactory::createReader('Excel5');
  323. }
  324. $savename = Filesystem::disk('public')->putFile('topic/excel', $files);
  325. $import_path = root_path() . 'public/storage/' . $savename;
  326. $spreadsheet = $objReader->load($import_path);
  327. $sheet = $spreadsheet->getActiveSheet();
  328. $sheetData = $sheet->toArray();
  329. if (empty($sheetData) || !is_array($sheetData)) {
  330. return ['code' => 1003, "msg" => '数据不能为空'];
  331. }
  332. return ['code' => 0, "msg" => '数据解析成功', 'data' => $sheetData];
  333. } catch (think\exception\ValidateException $e) {
  334. // echo $e->getMessage();
  335. return ['code' => 1003, "msg" => $e->getMessage()];
  336. }
  337. }
  338. /**
  339. * @param $files
  340. * @return array
  341. */
  342. function UploadImg($files)
  343. {
  344. $savename = [];
  345. $files = !is_array($files) ? [$files] : $files;
  346. try {
  347. //验证
  348. validate(['imgFile' => ['fileSize' => 10240000, 'fileExt' => 'jpg,jpeg,png,bmp,gif', 'fileMime' => 'image/jpeg,image/png,image/gif']])->check(['imgFile' => $files]);
  349. foreach ($files as $file) {
  350. $url = Filesystem::disk('public')->putFile('img/' . date("Ymd"), $file, function () use ($file) {
  351. return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . "_" . date('YmdHis'));
  352. });
  353. $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
  354. $temp = ["url" =>"storage/".$url, "name" => $name];
  355. $savename[] = $temp;
  356. }
  357. return $savename;
  358. } catch (\think\exception\ValidateException $e) {
  359. return $e->getMessage();
  360. }
  361. }
  362. if(!function_exists("invoiceType")){
  363. //发票类型 1 专用2普通3电子普通4 电子专用 5 q全电子发票
  364. function invoiceType($key=0){
  365. $panda= ['',"004",'007','026','028','000'];
  366. return $panda[$key]??'';
  367. }
  368. }
  369. if(!function_exists('excelExport')){
  370. /**
  371. * @param string $fileName
  372. * @param array $headArr
  373. * @param array $data
  374. * @throws \PHPExcel_Exception
  375. * @throws \PHPExcel_Reader_Exception
  376. * @throws \PHPExcel_Writer_Exception
  377. */
  378. function excelExport($fileName = '', $headArr = [], $data = [])
  379. {
  380. $objPHPExcel = new PHPExcel();
  381. $objPHPExcel->getProperties();
  382. $keyA = 0; // 设置表头
  383. foreach ($headArr as $v) {
  384. $colum = PHPExcel_Cell::stringFromColumnIndex($keyA);
  385. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
  386. $keyA += 1;
  387. }
  388. $column = 2;
  389. $objActSheet = $objPHPExcel->getActiveSheet();
  390. foreach ($data as $key => $rows) { // 行写入
  391. $span = 0;
  392. foreach ($rows as $keyName => $value) { // 列写入
  393. //判断数据是否有数组,如果有数组,转换成字符串
  394. if(is_array($value)){
  395. $value = implode("、", $value);
  396. }
  397. $objActSheet->setCellValue(PHPExcel_Cell::stringFromColumnIndex($span) . $column, $value);
  398. $span++;
  399. }
  400. $column++;
  401. }
  402. // var_dump($objActSheet->getActiveCell());
  403. $fileName .= "_" . date("Y_m_d", time()) . ".xls";
  404. //$fileName .= "_" . date("Y_m_d", Request()->instance()->time()) . ".xls";
  405. //$fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表
  406. $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表
  407. // Redirect output to a client’s web browser (Excel2007)
  408. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  409. header('Content-Disposition: attachment;filename="'.$fileName.'"');
  410. header('Cache-Control: max-age=0');
  411. // If you're serving to IE 9, then the following may be needed
  412. header('Cache-Control: max-age=1');
  413. // If you're serving to IE over SSL, then the following may be needed
  414. header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  415. header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
  416. header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  417. header ('Pragma: public'); // HTTP/1.0
  418. header("Content-Type: application/octet-stream"); # 流文件输出
  419. header("Content-Transfer-Encoding: binary"); # 告诉浏览器,这是二进制文件
  420. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  421. $objWriter->save('php://output'); // 文件通过浏览器下载
  422. exit();
  423. }
  424. }
  425. if(!function_exists("Csv_save")){
  426. function Csv_save($fileName,$headArr =[],$data=[],$num=10000){
  427. /*
  428. * 导出数据生成csv文件
  429. * @param file_name string 文件名
  430. * @param headArr array 表头
  431. * @param $db \think\db\Query 使用Db方法后生成的对象实例
  432. * @param callback 回调函数,一般用在对查询结果进行二次处理的时候(例如二次计算,将状态等值由数字转变为文本,额外添加字段等)
  433. */
  434. set_time_limit(0);//让程序一直运行
  435. $fileName .= date('_Y_m_d');//文件名拼接日期
  436. header('Content-Encoding:UTF-8');
  437. header("Content-type:application/vnd.ms-excel;charset=UTF-8");
  438. header('Content-Disposition:attachment;filename="' . $fileName . '.csv"');
  439. $fp = fopen('php://output', 'a');//打开php标准输出流
  440. fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));//添加bom头,以UTF-8编码导出的csv文件,如果文件头未添加bom头,打开会出现乱码
  441. try {
  442. if (!empty($headArr)) fputcsv($fp, $headArr);//添加导出标题
  443. $max = PHP_INT_MAX;
  444. $count=0;//阈值
  445. //写入数据
  446. for ($i = 1; $i > 0; $i++) {
  447. $has_data = false;//默认没有查询到数据
  448. foreach ($data as $k => $value) {
  449. // $max = $value[$pk];//维护最大值
  450. // $has_data = true;//存在查询数据
  451. // if ($function) $value = $function($value);//调用回调函数处理
  452. if (empty($headArr)) {
  453. $headArr = array_keys($value);
  454. fputcsv($fp, $headArr);//添加导出标题
  455. }
  456. fputcsv($fp, $value);
  457. if (($k + 1) % $num == 0) {
  458. ob_flush();//清空缓存,释放内存
  459. flush();
  460. }
  461. $count++;//阈值自增
  462. }
  463. if ($has_data === false) break;//结束循环
  464. if($count>=1000000) throw new \think\Exception('超出文件的最大显示行数,请根据条件分批次导出数据或使用预约导出功能,更多问题请联系开发人员');
  465. }
  466. }catch (\think\Exception $exception){
  467. fputcsv($fp,[$exception->getMessage()]);
  468. }
  469. ob_flush();
  470. flush();
  471. ob_end_clean();
  472. fclose($fp);
  473. exit();
  474. }
  475. }
  476. if(!function_exists('menuAction')){
  477. function menuAction($row,&$list=[]){
  478. $temp=[];
  479. foreach ($row as $key=>$value){
  480. if($value['pid']==0){
  481. $list[]=$value;
  482. }else{
  483. $menu=Db::name("admin_menu")->where(["id"=>$value['pid'],"is_del"=>0,"status"=>1])->field("id,menu_name,menu_img,menu_route,menu_url,pid,level,is_show,is_private,menu_type,status")->findOrEmpty();
  484. if(empty($menu)) continue;
  485. if(!isset($temp[$value['pid']]))$temp[$value['pid']]=$menu;
  486. $temp[$value['pid']]['child'][]=$value;
  487. }
  488. }
  489. // $list=$temp;
  490. if (!empty($temp)){
  491. menuAction($temp,$list);
  492. }
  493. }
  494. }
  495. /**
  496. * 批量生成多个文件excel,生成压缩包,保存到本地,返回文件链接
  497. * datas 生成器
  498. * header array 头部字段
  499. * filename string 文件名
  500. */
  501. if (!function_exists('excelSaveFile')) {
  502. function excelSaveFile($datas, string $filename = '')
  503. {
  504. // $urls = [];
  505. $dir = root_path() . 'public/storage/report/' . date("YmdHis") . "/";
  506. if (!is_dir($dir)) mkdir($dir, 0777, true);
  507. // foreach ($datas as $item) {
  508. PHPExcel_Settings::setCacheStorageMethod();
  509. PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;//单元格缓存为MemoryGZip
  510. $objPHPExcel = new PHPExcel();
  511. $keyA = 0; // 设置表头
  512. $column = 2;
  513. $objActSheet = $objPHPExcel->getActiveSheet();
  514. foreach ($datas as $key => $rows) { // 行写入
  515. //第一行取key作表头
  516. if($key==0){
  517. $objPHPExcel->getProperties();
  518. foreach ($rows as $k=>$v) {
  519. $colum = PHPExcel_Cell::stringFromColumnIndex($keyA);
  520. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $k);
  521. $keyA += 1;
  522. }
  523. }
  524. //写入列
  525. $span = 0;
  526. foreach ($rows as $keyName => $value) {
  527. //判断数据是否有数组,如果有数组,转换成字符串
  528. if (is_array($value)) $value = implode("、", $value);
  529. $objActSheet->setCellValue(PHPExcel_Cell::stringFromColumnIndex($span) . $column, $value);
  530. $span++;
  531. }
  532. $column++;
  533. }
  534. $file = $filename . ".xls";
  535. $objPHPExcel->setActiveSheetIndex(0);
  536. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  537. $objWriter->save($dir . $file); // 文件通过浏览器下载
  538. $url = $dir . $file;
  539. if (!file_exists($url)) throw new Exception('文件生成失败');
  540. $saveDir = root_path() . "public/storage/zip/" . date('Ymd') . '/';
  541. if (!is_dir($saveDir)) mkdir($saveDir, 0777, true);
  542. $file_dir = $saveDir . $filename . ".zip";
  543. # 5.1 文件打包,提示:使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
  544. $zip = new \ZipArchive ();
  545. # 5.2 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
  546. if ($zip->open($file_dir, \ZipArchive::OVERWRITE) !== true && $zip->open($file_dir, \ZipArchive::CREATE) !== true) echo '无法打开文件或者文件创建失败';
  547. # 5.3 批量写入压缩包
  548. // $zip->addEmptyDir($fileName);//往zip压缩包写入空目录
  549. // foreach ($urls as $fileName) {
  550. @$zip->addFile($url, DIRECTORY_SEPARATOR . basename($url));
  551. // }
  552. // @$zip->addFile($v['file_path'], 'resume'.DIRECTORY_SEPARATOR.basename($headername));
  553. # 5.4 关闭压缩包写入
  554. $zip->close();
  555. @deldir($dir);//删除已生成的文件及目录
  556. //6. 检查文件是否存在,并输出文件
  557. if (!file_exists($file_dir)) throw new Exception('压缩包文件不存在');
  558. return str_replace(root_path()."public/" , env("host.host"), $file_dir);
  559. }
  560. }
  561. //读取大文件
  562. if (!function_exists('read_big_file')){
  563. function read_big_file(string $file=''){
  564. $handle = fopen($file, 'rb');
  565. while (feof($handle) === false) {
  566. yield fgets($handle);
  567. }
  568. fclose($handle);
  569. }
  570. }
  571. function deldir($path){
  572. //如果是目录则继续
  573. if(is_dir($path)){
  574. //扫描一个文件夹内的所有文件夹和文件并返回数组
  575. $p = scandir($path);
  576. //如果 $p 中有两个以上的元素则说明当前 $path 不为空
  577. if(count($p)>2){
  578. foreach($p as $val){
  579. //排除目录中的.和..
  580. if($val !="." && $val !=".."){
  581. //如果是目录则递归子目录,继续操作
  582. if(is_dir($path.$val)){
  583. //子目录中操作删除文件夹和文件
  584. deldir($path.$val.'/');
  585. }else{
  586. //如果是文件直接删除
  587. unlink($path.$val);
  588. }
  589. }
  590. }
  591. }
  592. }
  593. //删除目录
  594. return rmdir($path);
  595. }