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->getStyle(PHPExcel_Cell::stringFromColumnIndex($span) . $column)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
  396. $objActSheet->setCellValueExplicit(PHPExcel_Cell::stringFromColumnIndex($span) . $column, $value);
  397. $span++;
  398. }
  399. $column++;
  400. }
  401. // var_dump($objActSheet->getActiveCell());
  402. $fileName .= "_" . date("Y_m_d", time()) . ".xls";
  403. //$fileName .= "_" . date("Y_m_d", Request()->instance()->time()) . ".xls";
  404. //$fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表
  405. $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表
  406. // Redirect output to a client’s web browser (Excel2007)
  407. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  408. header('Content-Disposition: attachment;filename="'.$fileName.'"');
  409. header('Cache-Control: max-age=0');
  410. // If you're serving to IE 9, then the following may be needed
  411. header('Cache-Control: max-age=1');
  412. // If you're serving to IE over SSL, then the following may be needed
  413. header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  414. header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
  415. header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  416. header ('Pragma: public'); // HTTP/1.0
  417. // header("Content-Type: application/octet-stream"); # 流文件输出
  418. // header("Content-Transfer-Encoding: binary"); # 告诉浏览器,这是二进制文件
  419. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  420. $objWriter->save('php://output'); // 文件通过浏览器下载
  421. exit();
  422. }
  423. }
  424. if(!function_exists("Csv_save")){
  425. function Csv_save($fileName,$headArr =[],$data=[],$num=10000){
  426. /*
  427. * 导出数据生成csv文件
  428. * @param file_name string 文件名
  429. * @param headArr array 表头
  430. * @param $db \think\db\Query 使用Db方法后生成的对象实例
  431. * @param callback 回调函数,一般用在对查询结果进行二次处理的时候(例如二次计算,将状态等值由数字转变为文本,额外添加字段等)
  432. */
  433. set_time_limit(0);//让程序一直运行
  434. $fileName .= date('_Y_m_d');//文件名拼接日期
  435. header('Content-Encoding:UTF-8');
  436. header("Content-type:application/vnd.ms-excel;charset=UTF-8");
  437. header('Content-Disposition:attachment;filename="' . $fileName . '.csv"');
  438. $fp = fopen('php://output', 'a');//打开php标准输出流
  439. fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));//添加bom头,以UTF-8编码导出的csv文件,如果文件头未添加bom头,打开会出现乱码
  440. try {
  441. if (!empty($headArr)) fputcsv($fp, $headArr);//添加导出标题
  442. $max = PHP_INT_MAX;
  443. $count=0;//阈值
  444. //写入数据
  445. for ($i = 1; $i > 0; $i++) {
  446. $has_data = false;//默认没有查询到数据
  447. foreach ($data as $k => $value) {
  448. // $max = $value[$pk];//维护最大值
  449. // $has_data = true;//存在查询数据
  450. // if ($function) $value = $function($value);//调用回调函数处理
  451. if (empty($headArr)) {
  452. $headArr = array_keys($value);
  453. fputcsv($fp, $headArr);//添加导出标题
  454. }
  455. fputcsv($fp, $value);
  456. if (($k + 1) % $num == 0) {
  457. ob_flush();//清空缓存,释放内存
  458. flush();
  459. }
  460. $count++;//阈值自增
  461. }
  462. if ($has_data === false) break;//结束循环
  463. if($count>=1000000) throw new \think\Exception('超出文件的最大显示行数,请根据条件分批次导出数据或使用预约导出功能,更多问题请联系开发人员');
  464. }
  465. }catch (\think\Exception $exception){
  466. fputcsv($fp,[$exception->getMessage()]);
  467. }
  468. ob_flush();
  469. flush();
  470. ob_end_clean();
  471. fclose($fp);
  472. exit();
  473. }
  474. }
  475. if(!function_exists('menuAction')){
  476. function menuAction($row,&$list=[]){
  477. $temp=[];
  478. foreach ($row as $key=>$value){
  479. if($value['pid']==0){
  480. $list[]=$value;
  481. }else{
  482. $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();
  483. if(empty($menu)) continue;
  484. if(!isset($temp[$value['pid']]))$temp[$value['pid']]=$menu;
  485. $temp[$value['pid']]['child'][]=$value;
  486. }
  487. }
  488. // $list=$temp;
  489. if (!empty($temp)){
  490. menuAction($temp,$list);
  491. }
  492. }
  493. }
  494. /**
  495. * 批量生成多个文件excel,生成压缩包,保存到本地,返回文件链接
  496. * datas 生成器
  497. * header array 头部字段
  498. * filename string 文件名
  499. */
  500. if (!function_exists('excelSaveFile')) {
  501. function excelSaveFile($datas, string $filename = '')
  502. {
  503. // $urls = [];
  504. $dir = root_path() . 'public/storage/report/' . date("YmdHis") . "/";
  505. if (!is_dir($dir)) mkdir($dir, 0777, true);
  506. // foreach ($datas as $item) {
  507. PHPExcel_Settings::setCacheStorageMethod();
  508. PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;//单元格缓存为MemoryGZip
  509. $objPHPExcel = new PHPExcel();
  510. $keyA = 0; // 设置表头
  511. $column = 2;
  512. $objActSheet = $objPHPExcel->getActiveSheet();
  513. foreach ($datas as $key => $rows) { // 行写入
  514. //第一行取key作表头
  515. if($key==0){
  516. $objPHPExcel->getProperties();
  517. foreach ($rows as $k=>$v) {
  518. $colum = PHPExcel_Cell::stringFromColumnIndex($keyA);
  519. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $k);
  520. $keyA += 1;
  521. }
  522. }
  523. //写入列
  524. $span = 0;
  525. foreach ($rows as $keyName => $value) {
  526. //判断数据是否有数组,如果有数组,转换成字符串
  527. if (is_array($value)) $value = implode("、", $value);
  528. $objActSheet->setCellValue(PHPExcel_Cell::stringFromColumnIndex($span) . $column, $value);
  529. $span++;
  530. }
  531. $column++;
  532. }
  533. $file = $filename . ".xls";
  534. $objPHPExcel->setActiveSheetIndex(0);
  535. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  536. $objWriter->save($dir . $file); // 文件通过浏览器下载
  537. $url = $dir . $file;
  538. if (!file_exists($url)) throw new Exception('文件生成失败');
  539. $saveDir = root_path() . "public/storage/zip/" . date('Ymd') . '/';
  540. if (!is_dir($saveDir)) mkdir($saveDir, 0777, true);
  541. $file_dir = $saveDir . $filename . ".zip";
  542. # 5.1 文件打包,提示:使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
  543. $zip = new \ZipArchive ();
  544. # 5.2 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
  545. if ($zip->open($file_dir, \ZipArchive::OVERWRITE) !== true && $zip->open($file_dir, \ZipArchive::CREATE) !== true) echo '无法打开文件或者文件创建失败';
  546. # 5.3 批量写入压缩包
  547. // $zip->addEmptyDir($fileName);//往zip压缩包写入空目录
  548. // foreach ($urls as $fileName) {
  549. @$zip->addFile($url, DIRECTORY_SEPARATOR . basename($url));
  550. // }
  551. // @$zip->addFile($v['file_path'], 'resume'.DIRECTORY_SEPARATOR.basename($headername));
  552. # 5.4 关闭压缩包写入
  553. $zip->close();
  554. @deldir($dir);//删除已生成的文件及目录
  555. //6. 检查文件是否存在,并输出文件
  556. if (!file_exists($file_dir)) throw new Exception('压缩包文件不存在');
  557. return str_replace(root_path()."public/" , env("host.host"), $file_dir);
  558. }
  559. }
  560. //读取大文件
  561. if (!function_exists('read_big_file')){
  562. function read_big_file(string $file=''){
  563. $handle = fopen($file, 'rb');
  564. while (feof($handle) === false) {
  565. yield fgets($handle);
  566. }
  567. fclose($handle);
  568. }
  569. }
  570. function deldir($path){
  571. //如果是目录则继续
  572. if(is_dir($path)){
  573. //扫描一个文件夹内的所有文件夹和文件并返回数组
  574. $p = scandir($path);
  575. //如果 $p 中有两个以上的元素则说明当前 $path 不为空
  576. if(count($p)>2){
  577. foreach($p as $val){
  578. //排除目录中的.和..
  579. if($val !="." && $val !=".."){
  580. //如果是目录则递归子目录,继续操作
  581. if(is_dir($path.$val)){
  582. //子目录中操作删除文件夹和文件
  583. deldir($path.$val.'/');
  584. }else{
  585. //如果是文件直接删除
  586. unlink($path.$val);
  587. }
  588. }
  589. }
  590. }
  591. }
  592. //删除目录
  593. return rmdir($path);
  594. }
  595. //检查供应商是否有开通账号
  596. if (!function_exists('check_has_account_by_supplierNos')) {
  597. function check_has_account_by_supplierNos(array $supplierNo = []): array
  598. {
  599. $host = Config::get("app");
  600. $url = $host["api_host"]."/check_has_account_by_supplierNos";
  601. $response=curl_request($url,['supplierNo' => $supplierNo]);
  602. return json_decode($response,true);
  603. }
  604. }
  605. if(!function_exists("getUidByDepartId")){
  606. /**
  607. * @param $depart_id
  608. * @return array|mixed
  609. */
  610. function getUidByDepartId($depart_id=0):array{
  611. $uidArr=Cache::get("Depart_Uid_Arr_$depart_id");
  612. if($uidArr==false){
  613. $uidArr = Db::connect("mysql_sys")->name("account_item")->where(["itemid"=>$depart_id])->column("account_id");
  614. Cache::set("Depart_Uid_Arr_$depart_id",$uidArr, new \DateTime(date("Y-m-d 23:59:59")));
  615. }
  616. return $uidArr;
  617. }
  618. }
  619. if(!function_exists("getDepartByUid")){
  620. /**
  621. * @param $depart_id
  622. * @return array|mixed
  623. */
  624. function getDepartByUid($uid=0,$get_tops=2):array{
  625. $host = Config::get("app");
  626. $url = $host["api_host"]."/get_company_name_by_uid";
  627. $response=curl_request($url,['uid'=>$uid,'get_tops'=>$get_tops]);
  628. return json_decode($response,true);
  629. }
  630. }
  631. if(!function_exists("getPayRate")){
  632. /**
  633. * @param $payday
  634. * @return array|mixed
  635. */
  636. function getPayRate($payday=0){
  637. $rate=0;
  638. switch ($payday){
  639. case 0:
  640. case 1:
  641. case 2:
  642. case 3:
  643. $rate=1;
  644. break;
  645. case 4:
  646. $rate=0.6;
  647. break;
  648. case 5:
  649. $rate=0.4;
  650. break;
  651. default:
  652. break;
  653. }
  654. return $rate;
  655. }
  656. }
  657. if(!function_exists("getLadderRate")){
  658. function getLadderRate($rate){
  659. if($rate>=0 && $rate<10){
  660. return 0;
  661. }
  662. if($rate>=10 && $rate<=18){
  663. return 0.003;
  664. }
  665. if($rate>18 && $rate<=20){
  666. return 0.006;
  667. }
  668. if($rate>20 && $rate<=25){
  669. return 0.012;
  670. }
  671. if($rate>25 && $rate<=30){
  672. return 0.02;
  673. }
  674. if($rate>30 ){
  675. return 0.035;
  676. }
  677. return 0;
  678. }
  679. }
  680. if(!function_exists("CheckTax")){
  681. // 1.如果YHZCBS为1, 则ZZSTSGL必须填; 如果YHZCBS为0,ZZSTSGL不填。
  682. // 2.如果YHZCBS为1, 且税率为0, 则LSLBS只能根据实际情况选择"1或2"中的一种, 不能选择3, 且ZZSTSGL内容也只能写与1/2对应的"免税/不征税";
  683. // 3.如果税率为0,但并不属于优惠政策(即普通的零税率),则YHZCBS填0,LSLBS填3,ZZSTSGL为空;
  684. // 4.如果税率不为0, 但属于优惠政策,则YHZCBS填1,LSLBS填空或不填,ZZSTSGL根据实际情况填写;
  685. // 5.如果税率不为0, 且不属于优惠政策, 则YHZCBS填0,LSLBS填空或不填,ZZSTSGL不填或空.
  686. // 优惠政策下 税率标识只能选择 0非零说率 1免税 2不征税 增值税管理为空"出口零税/免税/不征税"
  687. // 优惠政策 1 1 0 0
  688. // 税率 0 13 0 13
  689. // 零税率标识 1/2 0 3 0
  690. // 增值税管理 免税/不征税 空值 空值 空值
  691. /**
  692. * @param $is_discount 优惠政策 0/1
  693. * @param $tax 税率
  694. * @param $invTag 零税率标识 0非零说率 1免税 2不征税
  695. * @param $addTax 增值税管理 出口零税/免税/不征税
  696. * @param string $message
  697. * @return false
  698. */
  699. function CheckTax($is_discount,$tax,$invTag,&$addTax,&$message=""){
  700. // 有优惠政策
  701. if($is_discount==1){
  702. if($tax==0){
  703. if($invTag==3){
  704. $message = "税率标识不能选择零税率";
  705. return false;
  706. }
  707. if($addTax==''){
  708. $message = '增值税管理不能为空';
  709. return false;
  710. }
  711. if($addTax!==\app\admin\model\Good::$Tax[$invTag]){
  712. $message = '税率标识与增值税管理内容不符';
  713. return false;
  714. }
  715. }
  716. }else{
  717. // 非优惠政策下 零税率 税率标识只能选择 3 普通零税率 增值税管理为空
  718. // 非优惠政策下 非零税率 税率标识只能选择 0 非零税率 增值税管理为空
  719. if($tax==0 && $invTag!=3){
  720. $message = '税率不为零时税率标识只能选择零税率';
  721. return false;
  722. }
  723. if($tax!=0 && $invTag!=0){
  724. $message = '税率为零时税率标识只能选择非零税率';
  725. return false;
  726. }
  727. $addTax='';
  728. }
  729. return true;
  730. }
  731. }