common.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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('get_company_list') == false){
  213. function get_company_list(array $data=[]){
  214. $host = Config::get("app");
  215. $url = $host["api_host"]."/get_company_list";
  216. $response=curl_request($url,$data);
  217. echo $response;exit;
  218. // return json_decode($response,true);
  219. }
  220. }
  221. if(!function_exists("UserHandle")){
  222. function UserHandle($uri='',$param=[]){
  223. $host = env("user.hosturl");
  224. $url = $host.$uri;
  225. $response=curl_request($url,$param);
  226. return json_decode($response,true);
  227. }
  228. }
  229. if(!function_exists("headerSign")){
  230. function headerSign($post){
  231. $config = Config::get("sign");
  232. $appid=$config['appid'];
  233. $appkey=$config['appkey'];
  234. $headerArr=["appid"=>'123',"noce"=>randomkeys(16),"sign"=>'',"timestamp"=>time()];
  235. $value =array_merge($post,$headerArr);
  236. $Sign= new \Sign($appid,$appkey);
  237. $headerArr['sign'] = $Sign->makeSign($value);
  238. $list=["Content-Type"=>"multipart/json;charset=utf-8"];
  239. foreach ($headerArr as $key=>$value){
  240. $list[]=$key.":".$value;
  241. }
  242. return $list;
  243. }
  244. }
  245. //参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies
  246. function curl_request($url,$post='',$header=[]){
  247. // $header = empty($header) ? '' : $header;
  248. $header = headerSign($post);
  249. if(is_array($post)) $post=http_build_query($post);
  250. $curl = curl_init();
  251. curl_setopt($curl, CURLOPT_URL, $url);
  252. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  253. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  254. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  255. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  256. if($post) {
  257. curl_setopt($curl, CURLOPT_POST, 1);
  258. curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  259. }
  260. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  261. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  262. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  263. $data = curl_exec($curl);
  264. if (curl_errno($curl)) {
  265. return curl_error($curl);
  266. }
  267. curl_close($curl);
  268. return $data;
  269. }
  270. /**
  271. * @param $roleid
  272. * @param $menu
  273. * @return bool
  274. * @throws \think\db\exception\DataNotFoundException
  275. * @throws \think\db\exception\DbException
  276. * @throws \think\db\exception\ModelNotFoundException
  277. */
  278. function checkRole($roleid,$menu){
  279. $roleinfo = \think\facade\Db::name("role_action")->where([['role_id',"=",$roleid],["status","=",1]])->find();
  280. if($roleinfo['private_data']!=""){
  281. $private = explode(",",$roleinfo['private_data']);
  282. if(in_array($menu,$private)){
  283. return true;
  284. }
  285. }
  286. return false;
  287. }
  288. /**
  289. * @param $row
  290. * @param $list
  291. */
  292. function makeMenu($row,&$list){
  293. $list[$row['id']]=$row;
  294. if($row['pid']==0){
  295. return $list;
  296. }
  297. $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();
  298. if($parent==false) return $list;
  299. makeMenu($parent,$list);
  300. }
  301. /**
  302. * 遍历集合处理
  303. * @param $menuArr
  304. */
  305. function MenuTree(&$menuArr,$pid=0){
  306. $meun=[];
  307. foreach ($menuArr as $value){
  308. if($value['pid']==$pid){
  309. if($value['menu_type']==1)$value['child']=MenuTree($menuArr,$value['id']);
  310. $meun[]=$value ;
  311. }
  312. }
  313. return $meun;
  314. };
  315. function upload($files,$extend="xls")
  316. {
  317. // 获取表单上传文件
  318. try {
  319. validate([
  320. 'file' => [
  321. 'fileExt' => 'xlsx,xls'
  322. ]
  323. ],
  324. [
  325. 'file.fileExt' => '不支持的文件',
  326. ]
  327. )->check(['file' => $files]);
  328. if ($extend == 'xlsx') {
  329. $objReader = PHPExcel_IOFactory::createReader('Excel2007');
  330. } else {
  331. $objReader = PHPExcel_IOFactory::createReader('Excel5');
  332. }
  333. $savename = Filesystem::disk('public')->putFile('topic/excel', $files);
  334. $import_path = root_path() . 'public/storage/' . $savename;
  335. $spreadsheet = $objReader->load($import_path);
  336. $sheet = $spreadsheet->getActiveSheet();
  337. $sheetData = $sheet->toArray();
  338. if (empty($sheetData) || !is_array($sheetData)) {
  339. return ['code' => 1003, "msg" => '数据不能为空'];
  340. }
  341. return ['code' => 0, "msg" => '数据解析成功', 'data' => $sheetData];
  342. } catch (think\exception\ValidateException $e) {
  343. // echo $e->getMessage();
  344. return ['code' => 1003, "msg" => $e->getMessage()];
  345. }
  346. }
  347. /**
  348. * @param $files
  349. * @return array
  350. */
  351. function UploadImg($files)
  352. {
  353. $savename = [];
  354. $files = !is_array($files) ? [$files] : $files;
  355. try {
  356. //验证
  357. validate(['imgFile' => ['fileSize' => 10240000, 'fileExt' => 'jpg,jpeg,png,bmp,gif', 'fileMime' => 'image/jpeg,image/png,image/gif']])->check(['imgFile' => $files]);
  358. foreach ($files as $file) {
  359. $url = Filesystem::disk('public')->putFile('img/' . date("Ymd"), $file, function () use ($file) {
  360. return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . "_" . date('YmdHis'));
  361. });
  362. $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
  363. $temp = ["url" =>"storage/".$url, "name" => $name];
  364. $savename[] = $temp;
  365. }
  366. return $savename;
  367. } catch (\think\exception\ValidateException $e) {
  368. return $e->getMessage();
  369. }
  370. }
  371. if(!function_exists("invoiceType")){
  372. //发票类型 1 专用2普通3电子普通4 电子专用 5 q全电子发票
  373. function invoiceType($key=0){
  374. $panda= ['',"004",'007','026','028','000'];
  375. return $panda[$key]??'';
  376. }
  377. }
  378. if(!function_exists('excelExport')){
  379. /**
  380. * @param string $fileName
  381. * @param array $headArr
  382. * @param array $data
  383. * @throws \PHPExcel_Exception
  384. * @throws \PHPExcel_Reader_Exception
  385. * @throws \PHPExcel_Writer_Exception
  386. */
  387. function excelExport($fileName = '', $headArr = [], $data = [])
  388. {
  389. $objPHPExcel = new PHPExcel();
  390. $objPHPExcel->getProperties();
  391. $keyA = 0; // 设置表头
  392. foreach ($headArr as $v) {
  393. $colum = PHPExcel_Cell::stringFromColumnIndex($keyA);
  394. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
  395. $keyA += 1;
  396. }
  397. $column = 2;
  398. $objActSheet = $objPHPExcel->getActiveSheet();
  399. foreach ($data as $key => $rows) { // 行写入
  400. $span = 0;
  401. foreach ($rows as $keyName => $value) { // 列写入
  402. //判断数据是否有数组,如果有数组,转换成字符串
  403. if(is_array($value)){
  404. $value = implode("、", $value);
  405. }
  406. $objActSheet->setCellValue(PHPExcel_Cell::stringFromColumnIndex($span) . $column, $value);
  407. $span++;
  408. }
  409. $column++;
  410. }
  411. // var_dump($objActSheet->getActiveCell());
  412. $fileName .= "_" . date("Y_m_d", time()) . ".xls";
  413. //$fileName .= "_" . date("Y_m_d", Request()->instance()->time()) . ".xls";
  414. //$fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表
  415. $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表
  416. // Redirect output to a client’s web browser (Excel2007)
  417. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  418. header('Content-Disposition: attachment;filename="'.$fileName.'"');
  419. header('Cache-Control: max-age=0');
  420. // If you're serving to IE 9, then the following may be needed
  421. header('Cache-Control: max-age=1');
  422. // If you're serving to IE over SSL, then the following may be needed
  423. header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  424. header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
  425. header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  426. header ('Pragma: public'); // HTTP/1.0
  427. // header("Content-Type: application/octet-stream"); # 流文件输出
  428. // header("Content-Transfer-Encoding: binary"); # 告诉浏览器,这是二进制文件
  429. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  430. $objWriter->save('php://output'); // 文件通过浏览器下载
  431. exit();
  432. }
  433. }
  434. if(!function_exists("Csv_save")){
  435. function Csv_save($fileName,$headArr =[],$data=[],$num=10000){
  436. /*
  437. * 导出数据生成csv文件
  438. * @param file_name string 文件名
  439. * @param headArr array 表头
  440. * @param $db \think\db\Query 使用Db方法后生成的对象实例
  441. * @param callback 回调函数,一般用在对查询结果进行二次处理的时候(例如二次计算,将状态等值由数字转变为文本,额外添加字段等)
  442. */
  443. set_time_limit(0);//让程序一直运行
  444. $fileName .= date('_Y_m_d');//文件名拼接日期
  445. header('Content-Encoding:UTF-8');
  446. header("Content-type:application/vnd.ms-excel;charset=UTF-8");
  447. header('Content-Disposition:attachment;filename="' . $fileName . '.csv"');
  448. $fp = fopen('php://output', 'a');//打开php标准输出流
  449. fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));//添加bom头,以UTF-8编码导出的csv文件,如果文件头未添加bom头,打开会出现乱码
  450. try {
  451. if (!empty($headArr)) fputcsv($fp, $headArr);//添加导出标题
  452. $max = PHP_INT_MAX;
  453. $count=0;//阈值
  454. //写入数据
  455. for ($i = 1; $i > 0; $i++) {
  456. $has_data = false;//默认没有查询到数据
  457. foreach ($data as $k => $value) {
  458. // $max = $value[$pk];//维护最大值
  459. // $has_data = true;//存在查询数据
  460. // if ($function) $value = $function($value);//调用回调函数处理
  461. if (empty($headArr)) {
  462. $headArr = array_keys($value);
  463. fputcsv($fp, $headArr);//添加导出标题
  464. }
  465. fputcsv($fp, $value);
  466. if (($k + 1) % $num == 0) {
  467. ob_flush();//清空缓存,释放内存
  468. flush();
  469. }
  470. $count++;//阈值自增
  471. }
  472. if ($has_data === false) break;//结束循环
  473. if($count>=1000000) throw new \think\Exception('超出文件的最大显示行数,请根据条件分批次导出数据或使用预约导出功能,更多问题请联系开发人员');
  474. }
  475. }catch (\think\Exception $exception){
  476. fputcsv($fp,[$exception->getMessage()]);
  477. }
  478. ob_flush();
  479. flush();
  480. ob_end_clean();
  481. fclose($fp);
  482. exit();
  483. }
  484. }
  485. if(!function_exists('menuAction')){
  486. function menuAction($row,&$list=[]){
  487. $temp=[];
  488. foreach ($row as $key=>$value){
  489. if($value['pid']==0){
  490. $list[]=$value;
  491. }else{
  492. $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();
  493. if(empty($menu)) continue;
  494. if(!isset($temp[$value['pid']]))$temp[$value['pid']]=$menu;
  495. $temp[$value['pid']]['child'][]=$value;
  496. }
  497. }
  498. // $list=$temp;
  499. if (!empty($temp)){
  500. menuAction($temp,$list);
  501. }
  502. }
  503. }
  504. /**
  505. * 批量生成多个文件excel,生成压缩包,保存到本地,返回文件链接
  506. * datas 生成器
  507. * header array 头部字段
  508. * filename string 文件名
  509. */
  510. if (!function_exists('excelSaveFile')) {
  511. function excelSaveFile($datas, string $filename = '')
  512. {
  513. // $urls = [];
  514. $dir = root_path() . 'public/storage/report/' . date("YmdHis") . "/";
  515. if (!is_dir($dir)) mkdir($dir, 0777, true);
  516. // foreach ($datas as $item) {
  517. PHPExcel_Settings::setCacheStorageMethod();
  518. PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;//单元格缓存为MemoryGZip
  519. $objPHPExcel = new PHPExcel();
  520. $keyA = 0; // 设置表头
  521. $column = 2;
  522. $objActSheet = $objPHPExcel->getActiveSheet();
  523. foreach ($datas as $key => $rows) { // 行写入
  524. //第一行取key作表头
  525. if($key==0){
  526. $objPHPExcel->getProperties();
  527. foreach ($rows as $k=>$v) {
  528. $colum = PHPExcel_Cell::stringFromColumnIndex($keyA);
  529. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $k);
  530. $keyA += 1;
  531. }
  532. }
  533. //写入列
  534. $span = 0;
  535. foreach ($rows as $keyName => $value) {
  536. //判断数据是否有数组,如果有数组,转换成字符串
  537. if (is_array($value)) $value = implode("、", $value);
  538. $objActSheet->setCellValue(PHPExcel_Cell::stringFromColumnIndex($span) . $column, $value);
  539. $span++;
  540. }
  541. $column++;
  542. }
  543. $file = $filename . ".xls";
  544. $objPHPExcel->setActiveSheetIndex(0);
  545. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  546. $objWriter->save($dir . $file); // 文件通过浏览器下载
  547. $url = $dir . $file;
  548. if (!file_exists($url)) throw new Exception('文件生成失败');
  549. $saveDir = root_path() . "public/storage/zip/" . date('Ymd') . '/';
  550. if (!is_dir($saveDir)) mkdir($saveDir, 0777, true);
  551. $file_dir = $saveDir . $filename . ".zip";
  552. # 5.1 文件打包,提示:使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
  553. $zip = new \ZipArchive ();
  554. # 5.2 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
  555. if ($zip->open($file_dir, \ZipArchive::OVERWRITE) !== true && $zip->open($file_dir, \ZipArchive::CREATE) !== true) echo '无法打开文件或者文件创建失败';
  556. # 5.3 批量写入压缩包
  557. // $zip->addEmptyDir($fileName);//往zip压缩包写入空目录
  558. // foreach ($urls as $fileName) {
  559. @$zip->addFile($url, DIRECTORY_SEPARATOR . basename($url));
  560. // }
  561. // @$zip->addFile($v['file_path'], 'resume'.DIRECTORY_SEPARATOR.basename($headername));
  562. # 5.4 关闭压缩包写入
  563. $zip->close();
  564. @deldir($dir);//删除已生成的文件及目录
  565. //6. 检查文件是否存在,并输出文件
  566. if (!file_exists($file_dir)) throw new Exception('压缩包文件不存在');
  567. return str_replace(root_path()."public/" , env("host.host"), $file_dir);
  568. }
  569. }
  570. //读取大文件
  571. if (!function_exists('read_big_file')){
  572. function read_big_file(string $file=''){
  573. $handle = fopen($file, 'rb');
  574. while (feof($handle) === false) {
  575. yield fgets($handle);
  576. }
  577. fclose($handle);
  578. }
  579. }
  580. function deldir($path){
  581. //如果是目录则继续
  582. if(is_dir($path)){
  583. //扫描一个文件夹内的所有文件夹和文件并返回数组
  584. $p = scandir($path);
  585. //如果 $p 中有两个以上的元素则说明当前 $path 不为空
  586. if(count($p)>2){
  587. foreach($p as $val){
  588. //排除目录中的.和..
  589. if($val !="." && $val !=".."){
  590. //如果是目录则递归子目录,继续操作
  591. if(is_dir($path.$val)){
  592. //子目录中操作删除文件夹和文件
  593. deldir($path.$val.'/');
  594. }else{
  595. //如果是文件直接删除
  596. unlink($path.$val);
  597. }
  598. }
  599. }
  600. }
  601. }
  602. //删除目录
  603. return rmdir($path);
  604. }
  605. //检查供应商是否有开通账号
  606. if (!function_exists('check_has_account_by_supplierNos')) {
  607. function check_has_account_by_supplierNos(array $supplierNo = []): array
  608. {
  609. $host = Config::get("app");
  610. $url = $host["api_host"]."/check_has_account_by_supplierNos";
  611. $response=curl_request($url,['supplierNo' => $supplierNo]);
  612. return json_decode($response,true);
  613. }
  614. }