123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
- namespace app\api\controller;
- use app\api\actions\ZskkCache;
- use app\api\model\institution\InstitutionModel;
- use app\api\model\log\LogModel;
- use app\api\request\ZskkDefaultRequest;
- use app\api\response\ZskkCrosResponse;
- use app\api\response\ZskkDefaultResponse;
- use app\api\utils\UUIDUtils;
- use app\common\base\controller\BaseController;
- use app\common\base\actions\IBaseAction;
- use app\common\base\actions\IBaseLog;
- use app\api\actions\ZskkDefaultAction;
- use app\api\actions\ZskkDefaultLog;
- use app\common\library\uploadToCloud;
- use think\facade\Log;
- use think\facade\Request;
- use OSS\OssClient;
- use OSS\Core\OssException;
- /**
- * 后台控制器基类
- * 接口方法权限 必传参数 接口返回 错误抛出 通用参数处理
- */
- class ZskkDefaultController extends BaseController implements IBaseAction, IBaseLog
- {
- use ZskkDefaultAction;
- use ZskkDefaultLog;
- use ZskkCache;
- private const tokenName = "zskk-token";
- protected $logName = "ZskkDefaultController";
- protected function checkToken()
- {
- $header = $this->getHeader();
- if (!isset($header[self::tokenName]) || empty($header[self::tokenName])) {
- return $this->throwTokenError();
- }
- $token = $header[self::tokenName];
- $doctor = $this->getCache($token);
- $all = InstitutionModel::where('operate_close',1)->cache(3600)->column('id,operate_close_message');
- $ids = array_keys($all);
- if(in_array($doctor['institution_id'] ?? '',$ids))
- {
- if(empty($all[$doctor['institution_id']]))
- {
- return $this->throwError('云影像服务暂停','7777');
- }else{
- return $this->throwError($all[$doctor['institution_id']],'7777');
- }
- }
- return $this->tokenSuccess($header[self::tokenName]);
- }
- protected function mountRequest()
- {
- $this->setRequest(ZskkDefaultRequest::instance());
- }
- protected function beforeSetRequest()
- {
- // todo
- $this->setCors();
- }
- protected function afterSetRequest()
- {
- Log::record('请求开始----------'.date('Y-m-d h:i:s'));
- // todo
- if ($this->needToken) {
- $this->checkToken();
- $token = $this->getHeader()[self::tokenName];
- $doctor = $this->getCache($token);
- $user = $doctor['id'] ?? null;
- }else{
- $user = '';
- }
- $url = Request::instance()->baseUrl();
- // 格式化参数格式
- $param = Request::param(false);
- $data = [
- 'id'=>UUIDUtils::uuid(),
- 'url'=>$url,
- 'uid'=>$user
- ];
- if($url != '/message/unread'){
- LogModel::insert($data);
- }
- if(isset($_FILES['attachment']) && !empty($_FILES['attachment'])){
- //上传附件
- return ;
- }
- if(isset($_FILES['autograph']) && !empty($_FILES['autograph'])){
- //上传签名
- return ;
- }
- if(isset($_FILES['file']) && !empty($_FILES['file'])){
- //上传签名
- return ;
- }
- if(isset($_FILES['apply']) && !empty($_FILES['apply'])){
- //上传签名
- return ;
- }
- if(Request::instance()->baseUrl() == '/cancelExam' || Request::instance()->baseUrl() == '/creatPACSOrder' || Request::instance()->baseUrl()== '/xz/push2' ||
- Request::instance()->baseUrl()== '/xz/push3' || Request::instance()->baseUrl()== '/xz/push4' || Request::instance()->baseUrl()=='/exam/create'){
- return ;
- }
- if(isset($param['params'])){
- $zskk_param = json_decode($param['params'], true);
- $this->setParams($zskk_param);
- // $this->throwParamsError('20000','请传入正确的params参数');
- }
- }
- protected function setCors()
- {
- if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
- ZskkCrosResponse::throwCors();
- }
- }
- protected function getHeader()
- {
- return $this->getRequest()->header();
- }
- protected function tokenSuccess($token)
- {
- $this->setToken($token);
- }
- protected function success($data)
- {
- log::record('请求结束--------------'.date('Y-m-d h:i:s'));
- return ZskkDefaultResponse::getSuccess($data);
- }
- // public function throwParamsError($code, $msg) {
- // ZskkErrorResponse::throwParamsError($code, $msg);
- // }
- // public function throwError($error) {
- // ZskkErrorResponse::throwError($error);
- // }
- // public function throwSuccess($data) {
- // ZskkDefaultResponse::throwSuccess($data);
- // }
- // public function throwTokenError() {
- // ZskkErrorResponse::throwTokenError();
- // }
- // public function log($str, $level = null) {
- // Log::record($str, is_null($level) ? $this->logName : $level);
- // }
- public function aliUpload($url)
- {
- $accessKeyId = "LTAI5tLyAFqdCs9qxxhHRFwG";
- $accessKeySecret = "I4ZZTINQPldFrujikkuJlDk9WS3eDw";
- // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
- $endpoint = "https://oss-cn-beijing.aliyuncs.com/";
- // 填写Bucket名称,例如examplebucket。
- $bucket= "zskk-image";
- // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
- $user = $this->getCache($this->getToken());
- $ins = $user['institution_id'] ?? '';
- if(empty($ins))
- {
- $ins = 'other';
- }
- $format = substr($url,strripos($url,".")+1);
- $object = "$ins/".time().rand(0,9999).".$format";
- //本地上传文件路径
- // $file = '/uploads/cs.webp';
- // $url = ROOT_PATH.'public'.$file;
- $filePath = $url;
- try{
- $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
- $result = $ossClient->uploadFile($bucket, $object, $filePath);
- // \think\facade\Cache::set('result',$result);
- // var_dump($result);
- } catch(OssException $e) {
- // printf(__FUNCTION__ . ": FAILED\n");
- // printf($e->getMessage() . "\n");
- return $e->getMessage();
- }
- return $result['info']['url'] ?? '';
- }
- public function makeFileUrl($file,$type,$fileType='attachment')
- {
- $upload = new uploadToCloud();
- $data = $file;
- if($type == '2')
- {
- if(!empty($file))
- {
- $data = $upload->makeUrl($file,$fileType);
- }
- }elseif ($type == '1')
- {
- if(!empty($file))
- {
- $data = $file;
- }
- }
- return $data;
- }
- }
|