common.php 22 KB

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