common.php 25 KB

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