$code,"message"=>$message,"data"=>$data]; echo json_encode($result,JSON_UNESCAPED_UNICODE); die(); } // 应用公共文件 function error_show($code=0,$message=""){ $result = ['code'=>$code,"message"=>$message]; echo json_encode($result,JSON_UNESCAPED_UNICODE); die(); } function GetUserInfo($token){ $host = Config::get("app"); $url = $host["api_host"]."/Api/userinfo"; $data=[ "token"=>$token ]; $response=curl_request($url,$data); return json_decode($response,true); } //参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies function curl_request($url,$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, http_build_query($post)); } curl_setopt($curl, CURLOPT_TIMEOUT, 10); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($curl); if (curl_errno($curl)) { return curl_error($curl); } curl_close($curl); return $data; } /**手机号验证 * @param $mobile * @return bool */ 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; } /**邮箱验证 * @param $email * @return bool */ function checkEmail($email){ if (!$email) { return false; } return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $email) ? true : false; } /** * @param * @return int */ function makeSalt(){ $salt = rand(10000000,99999999); return $salt; } /** * @param $token * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ function VerifyTokens($token){ $host = Config::get("app"); $url = $host["api_host"]."/Api/verify_token"; $data=[ "token"=>$token ]; $response=curl_request($url,$data); return json_decode($response,true); } /** * @param $token * @param $condition * @return mixed */ function GetUserlist($token,$condition){ $host = Config::get("app"); $url = $host["api_host"]."/Api/getuserlist"; $condition['token']=$token; $response=curl_request($url,$condition); return json_decode($response,true); } /** * @param $token * @param $condition * @return mixed */ function GetAccountall($token, $condition){ $host = Config::get("app"); $url = $host["api_host"]."/Api/userall"; $condition['token']=$token; $response=curl_request($url,$condition); return json_decode($response,true); } function GetList($token,$condition){ $host = Config::get("app"); $url = $host["api_host"]."/Api/userlist"; $condition['token']=$token; $response=curl_request($url,$condition); return json_decode($response,true); } /** * @param $token * @param $condition ['id'=>1] * @return mixed */ function GetInfoById($token,$condition){ $host = Config::get("app"); $url = $host["api_host"]."/Api/userinfobyid"; $condition['token']=$token; $response=curl_request($url,$condition); return json_decode($response,true); } function makeNo($str){ $date=date("mdHis"); $year = date("Y")-2000; $msec=randomkeys(4); return $str.$msec.$year.$date; } function randomkeys($length) { $returnStr=''; $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';//ABCDEFGHIJKLOMNOPQRSTUVWXYZ for($i = 0; $i < $length; $i ++) { $returnStr .= $pattern[mt_rand ( 0, strlen($pattern)-1 )]; //生成php随机数 } return $returnStr; } function tree(){ } /** * @param $files * @return array */ 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('topic/' . date("Ymd"), $file, function () use ($file) { return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . "_" . date('YmdHis')); }); $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName()); $temp = ["url" => $url, "name" => $name]; $savename[] = $temp; } return $savename; } catch (\think\exception\ValidateException $e) { return $e->getMessage(); } } /** * @param $files * @return array */ function UploadFile($files) { $savename = []; $files = !is_array($files) ? [$files] : $files; try { //验证 validate(['imgFile' => ['fileSize' => 10240000,'fileExt' => 'xlsx,xls,pdf']])->check(['imgFile' => $files]); foreach ($files as $file) { $url = Filesystem::disk('public')->putFile('files/' . date("Ymd"), $file, function () use ($file) { return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . "_" . date('YmdHis')); }); $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName()); $temp = ["url" => $url, "name" => $name]; $savename[] = $temp; } return $savename; } catch (\think\exception\ValidateException $e) { return $e->getMessage(); } } /** * @param $token * @param $condition * @return mixed */ function resetpwd($token,$condition){ $host = Config::get("app"); $url = $host["api_host"]."/Api/passset"; $condition['token']=$token; $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); } /** * @param $token * @param $condition * @return mixed */ function resetinfo($token,$condition){ $host = Config::get("app"); $url = $host["api_host"]."/Api/usersave"; $condition['token']=$token; $response=curl_request($url,$condition); return json_decode($response,true); } /** * @param $token * @param $condition * @return mixed */ function resetstatus($token,$condition){ $host = Config::get("app"); $url = $host["api_host"]."/Api/userstatus"; $condition['token']=$token; $response=curl_request($url,$condition); return json_decode($response,true); } /** * @param $data * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ function crea($data,$vio=0) { $db = Db::name("company_item")->where(['pid'=>$data['id'],'is_del'=>0])->select()->toArray(); if($vio==1){ $d = Db::name("depart_user")->where(['itemid'=>$data['id'],'is_del'=>0])->select()->toArray(); if(empty($d)){ $data['item']=[]; }else{ $data['item']=$d; } } if(empty($db)){ $data['child']=[]; return $data; } //var_dump($db); foreach ($db as $p){ $data['child'][]=crea($p,$vio); } return $data; } function stro($data){ $db=Db::name('cat')->where(['pid'=>$data['id']])->select()->toArray(); if(empty($db)){ $data['child']=[]; return $data; } foreach ($db as $item) { $data['child'][]=stro($item); } return $data; } function made($var,$data=[]){ $str = Db::name('cat')->where(['id'=>$var])->find(); $vmn =[]; $vmn['id'] =$str['id']; $vmn['name'] =$str['cat_name']; array_unshift($data,$vmn); // $var['id']=made(); if($str['pid']==0){ // krsort($data); return $data; }else{ return made($str['pid'],$data); } } function manger($list=[],$level=1){ $var = Db::name('cat')->where(['pid'=>$list,'level'=>$level+1])->column("id"); if(empty($var)){ return $list; } $a=array_merge($list,$var); return manger($a,$level+1); } /** * @param $files * @param string $extend * @return array * @throws PHPExcel_Exception * @throws PHPExcel_Reader_Exception */ function upload_excel($files,$extend="xls") { // 获取表单上传文件 try { validate([ 'file' => [ // 限制文件大小(单位b),这里限制为4M //fileSize' => 4 * 1024 * 1024, 'fileExt' => 'xlsx,xls' ] ], [ //'file.fileSize' => '文件太大', 'file.fileExt' => '不支持的文件', ] )->check(['file' => $files]); // $name = $files->getOriginalExtension(); 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" => '数据不能为空']; } $list = []; foreach ($sheetData as $key => $value) { $list[] = $value; } return ['code' => 0, "msg" => '数据解析成功', 'data' => $list]; } catch (think\exception\ValidateException $e) { // echo $e->getMessage(); return ['code' => 1003, "msg" => $e->getMessage()]; } } /** * @param string $fileName * @param array $headArr * @param array $data */ function excelSave($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++; } // var_dump($objActSheet->getActiveCell()); $file = $fileName. ".xls"; //$fileName .= "_" . date("Y_m_d", Request()->instance()->time()) . ".xls"; //$fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表 $dir =root_path() . 'public/storage/report/'.date("YmdHis")."/"; if(!is_dir($dir)){ mkdir($dir,0777,true); } $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表 $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save($dir . $file); // 文件通过浏览器下载 $url = $dir . $file; if(!file_exists($url)){ echo "文件生成失败"; } $saveDir = root_path()."public/storage/zip/"; if(!is_dir( $saveDir)){ mkdir($saveDir,0777,true); } $datetime = date("Y-m-d H:i:s"); $file_dir = $saveDir.$datetime.".zip"; # 5.1 文件打包,提示:使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释 $zip = new \ZipArchive (); # 5.2 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip if ($zip->open($file_dir, \ZipArchive::OVERWRITE) !== true && $zip->open($file_dir, \ZipArchive::CREATE) !== true) echo '无法打开文件或者文件创建失败'; # 5.3 批量写入压缩包 $zip->addEmptyDir($fileName); // @$zip->addFile($v['file_path'], 'resume'.DIRECTORY_SEPARATOR.basename($headername)); @$zip->addFile($url,$fileName.DIRECTORY_SEPARATOR.basename($url)); # 5.4 关闭压缩包写入 $zip->close(); @deldir($dir); # 6. 检查文件是否存在,并输出文件 if (! file_exists ( $file_dir )) echo '简历文件不存在'; ob_clean(); flush(); header("Cache-Control: max-age=0"); header("Content-Description: File Transfer"); header('Content-disposition: attachment; filename=' . basename($file_dir)); # 处理文件名 header("Content-Type: application/octet-stream"); # 流文件输出 header("Content-Transfer-Encoding: binary"); # 告诉浏览器,这是二进制文件 header('Content-Length: ' . filesize($file_dir)); # 告诉浏览器,文件大小 readfile($file_dir); # 输出文件 @ unlink($file_dir); exit(); } function deldir($path){ //如果是目录则继续 if(is_dir($path)){ //扫描一个文件夹内的所有文件夹和文件并返回数组 $p = scandir($path); //如果 $p 中有两个以上的元素则说明当前 $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); } /** * @param string $AddrJson */ function GetAddr($AddrJson=""){ if($AddrJson==""){ return ''; } $adr = json_decode($AddrJson,true); if(!is_array($adr)){ return ''; } $addr=''; if(isset($adr['provice_code'])&&$adr['provice_code']!=''){ $provice = Db::name("province")->where(["province_code"=>$adr['provice_code']])->find(); $addr.=isset($provice['name']) ? $provice['name']:""; } if(isset($adr['city_code'])&&$adr['city_code']!=''){ $city = Db::name("city")->where(["city_code"=>$adr['city_code']])->find(); $addr.=isset($city['name']) ? $city['name']:""; } if(isset($adr['area_code'])&&$adr['area_code']!=''){ $area = Db::name("area")->where(["area_code"=>$adr['area_code']])->find(); $addr.=isset($area['name']) ? $area['name']:""; } return $addr; }