common.php 25 KB

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