123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695 |
- <?php
- use think\facade\Config;
- use think\facade\Db;
- use think\facade\Filesystem;
- use tp5er\thinkCsv\Export;
- function app_show($code=0,$message="",$data=[]){
- $result = ['code'=>$code,"message"=>$message,"data"=>$data];
- return json($result);
- }
- function error_show($code=0,$message=""){
- $result = ['code'=>$code,"message"=>$message];
- return json($result);
- }
- function GetUserInfo($token){
- $host = Config::get("app");
- $url = $host["api_host"]."/verifyToken";
- $data=[
- "token"=>$token
- ];
- $response=curl_request($url,$data);
- return json_decode($response,true);
- }
- function setUserCompany($condition){
- $host = Config::get("app");
- $url = $host["api_host"]."/setcompany";
- $response=curl_request($url,$condition);
- return json_decode($response,true);
- }
- function setCompanyStatus($condition){
- $host = Config::get("app");
- $url = $host["api_host"]."/companystatus";
- $response=curl_request($url,$condition);
- return json_decode($response,true);
- }
- function setStatus($condition){
- $host = Config::get("app");
- $url = $host["api_host"]."/userstatus";
- $response=curl_request($url,$condition);
- return json_decode($response,true);
- }
- function checkMobile($mobile){
- if (!is_numeric($mobile)) {
- return false;
- }
- return preg_match('#^1[3,4,5,6,7,8,9]{1}[\d]{9}$#', $mobile) ? true : false;
- }
- function checkTel($tel){
- if (!$tel) {
- return false;
- }
- return preg_match('/^(0[0-9]{2,3}\-)([0-9]{7,8})+(\-[0-9]{1,4})?$/', $tel) ? true : false;
- }
- function checkEmail($email){
- if (!$email) {
- return false;
- }
- return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $email) ? true : false;
- }
- function makeSalt(){
- $salt = rand(10000000,99999999);
- return $salt;
- }
- function VerifyTokens($token){
- $host = Config::get("app");
- $url = $host["api_host"]."/verifyToken";
- $data=[
- "token"=>$token
- ];
- $response=curl_request($url,$data);
- return json_decode($response,true);
- }
- function GetUserlist($condition){
- $host = Config::get("app");
- $url = $host["api_host"]."/userlistbycompany";
- $response=curl_request($url,$condition);
- return json_decode($response,true);
- }
- function GetList($condition){
- $host = Config::get("app");
- $url = $host["api_host"]."/userlist";
- $response=curl_request($url,$condition);
- return json_decode($response,true);
- }
- function GetInfoById($token,$condition){
- $host = Config::get("app");
- $url = $host["api_host"]."/userinfo";
- $condition['token']=$token;
- $response=curl_request($url,$condition);
- return json_decode($response,true);
- }
- function makeNo($str){
- $date=date("mdHis");
- $year = date("Y")-2000;
- $msec=rand(1000,9999);
- return $str.$year.$date.$msec;
- }
- function makeStr($str){
- $date=date("mdHis");
- $year = date("Y")-2000;
- $msec=randomkeys(4);
- return $str.$msec.$year.$date;
- }
- function randomkeys($length) {
- $returnStr='';
- $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';
- for($i = 0; $i < $length; $i ++) {
- $returnStr .= $pattern[mt_rand ( 0, strlen($pattern)-1 )];
- }
- return $returnStr;
- }
- function resetpwd($condition){
- $host = Config::get("app");
- $url = $host["api_host"]."/setpasswd";
- $response=curl_request($url,$condition);
- return json_decode($response,true);
- }
- function resetpasswd($token,$condition){
- $host = Config::get("app");
- $url = $host["api_host"]."/Api/passsave";
- $condition['token']=$token;
- $response=curl_request($url,$condition);
- return json_decode($response,true);
- }
-
- function checkLogin($condition){
- $host = Config::get("app");
- $url = $host["api_host"]."/login";
- $response=curl_request($url,$condition);
- return json_decode($response,true);
- }
- function resetinfo($condition){
- $host = Config::get("app");
- $url = $host["api_host"]."/usersave";
- $response=curl_request($url,$condition);
- return json_decode($response,true);
- }
- function addacount($condition){
- $host = Config::get("app");
- $url = $host["api_host"]."/useradd";
- $response=curl_request($url,$condition);
- return json_decode($response,true);
- }
- if(function_exists('add_admin_account') == false){
- function add_admin_acount(array $data=[]){
- $host = Config::get("app");
- $url = $host["api_host"]."/add_admin_account";
- $response=curl_request($url,$data);
- return json_decode($response,true);
- }
- }
- if(!function_exists("headerSign")){
- function headerSign($post){
- $config = Config::get("sign");
- $appid=$config['appid'];
- $appkey=$config['appkey'];
- $headerArr=["appid"=>'123',"noce"=>randomkeys(16),"sign"=>'',"timestamp"=>time()];
- $value =array_merge($post,$headerArr);
- $Sign= new \Sign($appid,$appkey);
- $headerArr['sign'] = $Sign->makeSign($value);
- $list=["Content-Type"=>"multipart/json;charset=utf-8"];
- foreach ($headerArr as $key=>$value){
- $list[]=$key.":".$value;
- }
- return $list;
- }
- }
- function curl_request($url,$post='',$header=[]){
- $header = headerSign($post);
- if(is_array($post)) $post=http_build_query($post);
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- if($post) {
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
- }
- curl_setopt($curl, CURLOPT_TIMEOUT, 10);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- $data = curl_exec($curl);
- if (curl_errno($curl)) {
- return curl_error($curl);
- }
- curl_close($curl);
- return $data;
- }
- function checkRole($roleid,$menu){
- $roleinfo = \think\facade\Db::name("role_action")->where([['role_id',"=",$roleid],["status","=",1]])->find();
- if($roleinfo['private_data']!=""){
- $private = explode(",",$roleinfo['private_data']);
- if(in_array($menu,$private)){
- return true;
- }
- }
- return false;
- }
- function makeMenu($row,&$list){
- $list[$row['id']]=$row;
- if($row['pid']==0){
- return $list;
- }
- $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();
- if($parent==false) return $list;
- makeMenu($parent,$list);
- }
- function MenuTree(&$menuArr,$pid=0){
- $meun=[];
- foreach ($menuArr as $value){
- if($value['pid']==$pid){
- if($value['menu_type']==1)$value['child']=MenuTree($menuArr,$value['id']);
- $meun[]=$value ;
- }
- }
- return $meun;
- };
- function upload($files,$extend="xls")
- {
-
- try {
- validate([
- 'file' => [
- 'fileExt' => 'xlsx,xls'
- ]
- ],
- [
- 'file.fileExt' => '不支持的文件',
- ]
- )->check(['file' => $files]);
- if ($extend == 'xlsx') {
- $objReader = PHPExcel_IOFactory::createReader('Excel2007');
- } else {
- $objReader = PHPExcel_IOFactory::createReader('Excel5');
- }
- $savename = Filesystem::disk('public')->putFile('topic/excel', $files);
- $import_path = root_path() . 'public/storage/' . $savename;
- $spreadsheet = $objReader->load($import_path);
- $sheet = $spreadsheet->getActiveSheet();
- $sheetData = $sheet->toArray();
- if (empty($sheetData) || !is_array($sheetData)) {
- return ['code' => 1003, "msg" => '数据不能为空'];
- }
- return ['code' => 0, "msg" => '数据解析成功', 'data' => $sheetData];
- } catch (think\exception\ValidateException $e) {
-
- return ['code' => 1003, "msg" => $e->getMessage()];
- }
- }
- function UploadImg($files)
- {
- $savename = [];
- $files = !is_array($files) ? [$files] : $files;
- try {
-
- validate(['imgFile' => ['fileSize' => 10240000, 'fileExt' => 'jpg,jpeg,png,bmp,gif', 'fileMime' => 'image/jpeg,image/png,image/gif']])->check(['imgFile' => $files]);
- foreach ($files as $file) {
- $url = Filesystem::disk('public')->putFile('img/' . date("Ymd"), $file, function () use ($file) {
- return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . "_" . date('YmdHis'));
- });
- $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
- $temp = ["url" =>"storage/".$url, "name" => $name];
- $savename[] = $temp;
- }
- return $savename;
- } catch (\think\exception\ValidateException $e) {
- return $e->getMessage();
- }
- }
- if(!function_exists("invoiceType")){
-
- function invoiceType($key=0){
- $panda= ['',"004",'007','026','028','000'];
- return $panda[$key]??'';
- }
- }
- if(!function_exists('excelExport')){
-
- function excelExport($fileName = '', $headArr = [], $data = [])
- {
- $objPHPExcel = new PHPExcel();
- $objPHPExcel->getProperties();
- $keyA = 0;
- foreach ($headArr as $v) {
- $colum = PHPExcel_Cell::stringFromColumnIndex($keyA);
- $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
- $keyA += 1;
- }
- $column = 2;
- $objActSheet = $objPHPExcel->getActiveSheet();
- foreach ($data as $key => $rows) {
- $span = 0;
- foreach ($rows as $keyName => $value) {
-
- if(is_array($value)){
- $value = implode("、", $value);
- }
- $objActSheet->setCellValue(PHPExcel_Cell::stringFromColumnIndex($span) . $column, $value);
- $span++;
- }
- $column++;
- }
-
- $fileName .= "_" . date("Y_m_d", time()) . ".xls";
-
-
- $objPHPExcel->setActiveSheetIndex(0);
-
- header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
- header('Content-Disposition: attachment;filename="'.$fileName.'"');
- header('Cache-Control: max-age=0');
- header('Cache-Control: max-age=1');
- header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
- header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
- header ('Cache-Control: cache, must-revalidate');
- header ('Pragma: public');
- header("Content-Type: application/octet-stream");
- header("Content-Transfer-Encoding: binary");
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
- $objWriter->save('php://output');
- exit();
- }
- }
- if(!function_exists("Csv_save")){
- function Csv_save($fileName,$headArr =[],$data=[],$num=10000){
-
- set_time_limit(0);
- $fileName .= date('_Y_m_d');
- header('Content-Encoding:UTF-8');
- header("Content-type:application/vnd.ms-excel;charset=UTF-8");
- header('Content-Disposition:attachment;filename="' . $fileName . '.csv"');
- $fp = fopen('php://output', 'a');
- fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));
- try {
- if (!empty($headArr)) fputcsv($fp, $headArr);
- $max = PHP_INT_MAX;
- $count=0;
-
- for ($i = 1; $i > 0; $i++) {
- $has_data = false;
- foreach ($data as $k => $value) {
- if (empty($headArr)) {
- $headArr = array_keys($value);
- fputcsv($fp, $headArr);
- }
- fputcsv($fp, $value);
- if (($k + 1) % $num == 0) {
- ob_flush();
- flush();
- }
- $count++;
- }
- if ($has_data === false) break;
- if($count>=1000000) throw new \think\Exception('超出文件的最大显示行数,请根据条件分批次导出数据或使用预约导出功能,更多问题请联系开发人员');
- }
- }catch (\think\Exception $exception){
- fputcsv($fp,[$exception->getMessage()]);
- }
- ob_flush();
- flush();
- ob_end_clean();
- fclose($fp);
- exit();
- }
- }
- if(!function_exists('menuAction')){
- function menuAction($row,&$list=[]){
- $temp=[];
- foreach ($row as $key=>$value){
- if($value['pid']==0){
- $list[]=$value;
- }else{
- $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();
- if(empty($menu)) continue;
- if(!isset($temp[$value['pid']]))$temp[$value['pid']]=$menu;
- $temp[$value['pid']]['child'][]=$value;
- }
- }
- if (!empty($temp)){
- menuAction($temp,$list);
- }
- }
- }
- if (!function_exists('excelSaveFile')) {
- function excelSaveFile($datas, string $filename = '')
- {
- $dir = root_path() . 'public/storage/report/' . date("YmdHis") . "/";
- if (!is_dir($dir)) mkdir($dir, 0777, true);
- PHPExcel_Settings::setCacheStorageMethod();
- PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
- $objPHPExcel = new PHPExcel();
- $keyA = 0;
- $column = 2;
- $objActSheet = $objPHPExcel->getActiveSheet();
- foreach ($datas as $key => $rows) {
-
- if($key==0){
- $objPHPExcel->getProperties();
- foreach ($rows as $k=>$v) {
- $colum = PHPExcel_Cell::stringFromColumnIndex($keyA);
- $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $k);
- $keyA += 1;
- }
- }
-
- $span = 0;
- foreach ($rows as $keyName => $value) {
-
- if (is_array($value)) $value = implode("、", $value);
- $objActSheet->setCellValue(PHPExcel_Cell::stringFromColumnIndex($span) . $column, $value);
- $span++;
- }
- $column++;
- }
- $file = $filename . ".xls";
- $objPHPExcel->setActiveSheetIndex(0);
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
- $objWriter->save($dir . $file);
- $url = $dir . $file;
- if (!file_exists($url)) throw new Exception('文件生成失败');
- $saveDir = root_path() . "public/storage/zip/" . date('Ymd') . '/';
- if (!is_dir($saveDir)) mkdir($saveDir, 0777, true);
- $file_dir = $saveDir . $filename . ".zip";
-
- $zip = new \ZipArchive ();
-
- if ($zip->open($file_dir, \ZipArchive::OVERWRITE) !== true && $zip->open($file_dir, \ZipArchive::CREATE) !== true) echo '无法打开文件或者文件创建失败';
-
- @$zip->addFile($url, DIRECTORY_SEPARATOR . basename($url));
-
-
- $zip->close();
- @deldir($dir);
-
- if (!file_exists($file_dir)) throw new Exception('压缩包文件不存在');
- return str_replace(root_path()."public/" , env("host.host"), $file_dir);
- }
- }
- if (!function_exists('read_big_file')){
- function read_big_file(string $file=''){
- $handle = fopen($file, 'rb');
- while (feof($handle) === false) {
- yield fgets($handle);
- }
- fclose($handle);
- }
- }
- function deldir($path){
-
- if(is_dir($path)){
-
- $p = scandir($path);
-
- if(count($p)>2){
- foreach($p as $val){
-
- if($val !="." && $val !=".."){
-
- if(is_dir($path.$val)){
-
- deldir($path.$val.'/');
- }else{
-
- unlink($path.$val);
- }
- }
- }
- }
- }
-
- return rmdir($path);
- }
|