// +---------------------------------------------------------------------- namespace app\admin\controller; use app\common\controller\Common; use app\admin\model\AuthRule; use OSS\OssClient; use OSS\Core\OssException; use think\Request; use think\Db; use think\Image; class Base extends Common { public function _initialize() { parent::_initialize(); if(!$this->check_admin_login()) $this->redirect('admin/Login/login');//未登录 $auth=new AuthRule; $id_curr=$auth->get_url_id(); //echo "
".$id_curr."
"; if(!$auth->check_auth($id_curr)){ $this->error('没有权限',url('admin/Index/index')); } //获取有权限的菜单tree $menus=$auth->get_admin_menus(); $this->assign('menus',$menus); //当前方法倒推到顶级菜单ids数组 $menus_curr=$auth->get_admin_parents($id_curr); $this->assign('menus_curr',$menus_curr); //取当前操作菜单父节点下菜单 当前菜单id(仅显示状态) $menus_child=$auth->get_admin_parent_menus($id_curr); $this->assign('menus_child',$menus_child); $this->assign('id_curr',$id_curr); $this->assign('admin_avatar',session('admin_auth.admin_avatar')); } public function uploadAliYun($filename='',$filepath=''){ $bucket = \aliyunoss\Common::getBucketName(); $ossClient = \aliyunoss\Common::getOssClient(); try { $result = $ossClient->uploadFile($bucket,$filename,$filepath); $path=$result['info']['url']; return ['action'=>true,'path'=>$path,'filename'=>$filename]; }catch (OssException $e){ return ['action'=>false,'path'=>'','filename'=>'','msg'=>$e->getErrorMessage()]; } } protected function gmt_iso8601($time) { $dtStr = date("c", $time); $mydatetime = new \DateTime($dtStr); $expiration = $mydatetime->format(\DateTime::ISO8601); $pos = strpos($expiration, '+'); $expiration = substr($expiration, 0, $pos); return $expiration."Z"; } function aliinfo(Request $request){ $id=\aliyunoss\Common::accessKeyId; // 请填写您的AccessKeyId。 $key=\aliyunoss\Common::accessKeySecret; // 请填写您的AccessKeySecret。 // $host的格式为 bucketname.endpoint,请替换为您的真实信息。 $host =\aliyunoss\Common::bucketHost; $endpoint=\aliyunoss\Common::endpoint; // $callbackUrl为上传回调服务器的URL,请将下面的IP和Port配置为您自己的真实URL信息。 $callbackUrl = 'http://www.szcaee.cn/extend/aliyunoss/callback.php'; $dir =$request->param('dir'); // 用户上传文件时指定的前缀。 $dir.='/'; $callback_param = array('callbackUrl'=>$callbackUrl, 'callbackBody'=>'filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}&endpoint='.$host, 'callbackBodyType'=>"application/x-www-form-urlencoded"); $callback_string = json_encode($callback_param); $base64_callback_body = base64_encode($callback_string); $now = time(); $expire = 30; //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问。 $end = $now + $expire; $expiration = $this->gmt_iso8601($end); //最大文件大小.用户可以自己设置 $condition = array(0=>'content-length-range', 1=>0, 2=>1048576000); $conditions[] = $condition; // 表示用户上传的数据,必须是以$dir开始,不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录。 $start = array(0=>'starts-with', 1=>'$key', 2=>$dir); $conditions[] = $start; $arr = array('expiration'=>$expiration,'conditions'=>$conditions); $policy = json_encode($arr); $base64_policy = base64_encode($policy); $string_to_sign = $base64_policy; $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $key, true)); $response = array(); $response['accessid'] = $id; $response['host'] = $host; $response['policy'] = $base64_policy; $response['signature'] = $signature; $response['expire'] = $end; $response['callback'] = $base64_callback_body; $response['dir'] = $dir; // 这个参数是设置用户上传文件时指定的前缀。 echo json_encode($response);exit; } /** * 上传方法 */ public function upload(Request $request){ // 获取表单上传文件 $key=$request->param('key'); if(!$key)return $this->error('參數錯誤'); $file = request()->file($key); // 移动到框架应用根目录/uploads/ 目录下 $uploadpath=config('upload_path'); $path=ROOT_PATH . config('upload_path'); $validate = config('upload_validate'); $info = $file->validate($validate)->move($path); if($info){ $data=array(); $data['ext']=$info->getExtension(); $data['path']=$uploadpath.DS.$info->getSaveName(); $data['filename']=$info->getFilename(); return $this->success('上傳成功','',$data); }else{ return $this->error($file->getError()); } } /** * 处理上传图片 * @param string $file * @param string $files * @return NULL[]|string[] */ protected function dealFile($file='',$files=''){ $validate = config('upload_validate'); $img_one=''; $picall_url=''; $upload_path=config('upload_path'). DS . date('Y-m-d'); //单图 if (!empty($file)) { $info = $file[0]->validate($validate)->rule('uniqid')->move(ROOT_PATH .$upload_path); if ($info) { $img_url = $upload_path.'/'. $info->getFilename(); $ext=strtolower(pathinfo($img_url,PATHINFO_EXTENSION)); $imgfile='.'.$img_url; //等比例宿放 if(file_exists($imgfile)&&in_array($ext,['jpg','gif','png','jpeg'])){ $image=Image::open($imgfile); $width=$image->width(); $height=$image->height(); if($width>1000){ $nhei=(1000/$width)*$height; $image->thumb(1000,$nhei); $image->save($imgfile); } } //写入数据库 $data['uptime'] = time(); $data['filesize'] = $info->getSize(); $data['path'] = $img_url; Db::name('plug_files')->insert($data); $img_one = $img_url; } else { return ['error'=>$file[0]->getError()]; } } //多图 if (!empty($files)) { foreach ($files as $file) { $info = $file->validate($validate)->rule('uniqid')->move(ROOT_PATH .$upload_path); if ($info) { $img_url = $upload_path.DS. $info->getFilename(); $ext=strtolower(pathinfo($img_url,PATHINFO_EXTENSION)); $imgfile='.'.$img_url; //等比例宿放 if(file_exists($imgfile)&&in_array($ext,['jpg','gif','png','jpeg'])){ $image=Image::open($imgfile); $width=$image->width(); $height=$image->height(); if($width>1000){ $nhei=(1000/$width)*$height; $image->thumb(1000,$nhei); $image->save($imgfile); } } //写入数据库 $data['uptime'] = time(); $data['filesize'] = $info->getSize(); $data['path'] = $img_url; Db::name('plug_files')->insert($data); $picall_url = $img_url . ',' . $picall_url; } else { return ['error'=>$file[0]->getError()]; } } } return ['img_one'=>$img_one,'picall_url'=>$picall_url]; } }