123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- use think\Db;
- use think\File;
- /**
- * @param $account
- * @return string
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- 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;
- }
- /**
- * @param $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无效"];
- }
- }
- /**
- * @param $username
- * @return bool 账户正则匹配
- */
- function checkAccount($username){
- $match ='/^(1745)([\d]{6})$/';
- return preg_match($match,$username)?true:false;
- }
- /**
- * @param $pawd
- * @return bool 账户正则匹配
- */
- 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;
- // var_dump($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++;
- }
- // 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);
- }
- 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++;
- }
- // var_dump($objActSheet->getActiveCell());
- $fileName .= "_" . date("Y_m_d", time()) . ".xls";
- //$fileName .= "_" . date("Y_m_d", Request()->instance()->time()) . ".xls";
- //$fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表
- $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表
- // Redirect output to a client’s web browser (Excel2007)
- header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
- header('Content-Disposition: attachment;filename="'.$fileName.'"');
- header('Cache-Control: max-age=0');
- // If you're serving to IE 9, then the following may be needed
- header('Cache-Control: max-age=1');
- // If you're serving to IE over SSL, then the following may be needed
- header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
- header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
- header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
- header ('Pragma: public'); // HTTP/1.0
- // header("Content-Type: application/octet-stream"); # 流文件输出
- // header("Content-Transfer-Encoding: binary"); # 告诉浏览器,这是二进制文件
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
- $objWriter->save('php://output'); // 文件通过浏览器下载
- exit();
- }
|