123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- use think\Db;
- use think\File;
- function makeToken($account){
- $now=time();
- $str = $account['username'].$account['salt'].$now;
- $token = base64_encode($str);
- $has = Db::name("admin_token")->where(["adminid"=>$account['id']])->find();
- if($has){
- Db::name("admin_token")->where(["adminid"=>$account['id']])->update(["token"=>$token,"expiretime"=>date("Y-m-d H:i:s",$now+1800)]);
- }else{
- Db::name("admin_token")->insert(["token"=>$token,"expiretime"=>date("Y-m-d H:i:s",$now+1800),"addtime"=>date("Y-m-d H:i:s"),
- "adminid"=>$account['id']]);
- }
- return $token;
- }
- function verifyToken($token){
- $has = Db::name("admin_token")->where(["token"=>$token])->find();
- if(!$has){
- return ["code"=>101,"msg"=>"token不存在"];
- }
- if(strtotime($has['expiretime'])<=time()){
- return ["code"=>102,"msg"=>"token已失效"];
- }
- $account = Db::name("admin")->where(["id"=>$has['adminid'],"is_del"=>0])->find();
- if(!$account){
- return ["code"=>103,"msg"=>"未找到账户"];
- }
- if($account['status']!=1){
- return ["code"=>104,"msg"=>"账户已禁用"];
- }
- $token_str = base64_decode($token);
- $account_str= substr($token_str,0,-10);
- if($account_str==$account['username'].$account['salt']){
- Db::name("admin_token")->where(["token"=>$token])->update(["expiretime"=>date("Y-m-d H:i:s",time()+1800)]);
- return ["code"=>0,"msg"=>"账户有效"];
- }else{
- return ["code"=>105,"msg"=>"账户token无效"];
- }
- }
- function checkAccount($username){
- $match ='/^(1745)([\d]{6})$/';
- return preg_match($match,$username)?true:false;
- }
- function checkPasswd($pawd){
- $match ='/^([a-zA-z]{2})([\d]{4})$/';
- return preg_match($match,$pawd)?true:false;
- }
- function UploadImg($files){
- $savename = [];
- $files= !is_array($files) ? [$files] : $files;
-
- try{
- foreach($files as $file){
- $info= $file->validate(['size'=>10240000,'ext'=>'jpg,jpeg,png,bmp,gif'])->move(ROOT_PATH .'public' .DS .'upload');
- if($info){
- $temp = ['url'=>'upload/'. $info->getSaveName()];
- $savename[]=$temp;
- }else{
- return "";
- }
- }
- return $savename;
- }catch (\think\exception\ValidateException $e) {
- return $e->getMessage();
- }
- }
- function QueuePush($data,$queue="createOrderJob"){
-
- $jobHandlerClassName = 'app\admin\JobInv';
-
- $jobQueueName = $queue;
- $isPushed = Queue::push($jobHandlerClassName, $data,$jobQueueName);
- if( $isPushed !== false ){
- Log::write("{$jobQueueName} 任务失败:{$data['id']}");
- }
- }
- 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 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++;
- }
-
- $file = $fileName. ".xls";
-
-
- $dir =ROOT_PATH. 'public/storage/report/'.date("YmdHis")."/";
- if(!is_dir($dir)){
- mkdir($dir,0777,true);
- }
- $objPHPExcel->setActiveSheetIndex(0);
- $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";
-
- $zip = new \ZipArchive ();
-
- if ($zip->open($file_dir, \ZipArchive::OVERWRITE) !== true && $zip->open($file_dir, \ZipArchive::CREATE) !==
- true) echo '无法打开文件或者文件创建失败';
-
- $zip->addEmptyDir($fileName);
-
- @$zip->addFile($url,$fileName.DIRECTORY_SEPARATOR.basename($url));
-
- $zip->close();
- @deldir($dir);
-
- 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);
-
- 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);
- }
- 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');
-
-
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
- $objWriter->save('php://output');
- exit();
- }
|