ZskkDefaultController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\actions\ZskkCache;
  4. use app\api\model\institution\InstitutionModel;
  5. use app\api\model\log\LogModel;
  6. use app\api\request\ZskkDefaultRequest;
  7. use app\api\response\ZskkCrosResponse;
  8. use app\api\response\ZskkDefaultResponse;
  9. use app\api\utils\UUIDUtils;
  10. use app\common\base\controller\BaseController;
  11. use app\common\base\actions\IBaseAction;
  12. use app\common\base\actions\IBaseLog;
  13. use app\api\actions\ZskkDefaultAction;
  14. use app\api\actions\ZskkDefaultLog;
  15. use app\common\library\uploadToCloud;
  16. use think\facade\Log;
  17. use think\facade\Request;
  18. use OSS\OssClient;
  19. use OSS\Core\OssException;
  20. /**
  21. * 后台控制器基类
  22. * 接口方法权限 必传参数 接口返回 错误抛出 通用参数处理
  23. */
  24. class ZskkDefaultController extends BaseController implements IBaseAction, IBaseLog
  25. {
  26. use ZskkDefaultAction;
  27. use ZskkDefaultLog;
  28. use ZskkCache;
  29. private const tokenName = "zskk-token";
  30. protected $logName = "ZskkDefaultController";
  31. protected function checkToken()
  32. {
  33. $header = $this->getHeader();
  34. if (!isset($header[self::tokenName]) || empty($header[self::tokenName])) {
  35. return $this->throwTokenError();
  36. }
  37. $token = $header[self::tokenName];
  38. $doctor = $this->getCache($token);
  39. $all = InstitutionModel::where('operate_close',1)->cache(3600)->column('id,operate_close_message');
  40. $ids = array_keys($all);
  41. if(in_array($doctor['institution_id'] ?? '',$ids))
  42. {
  43. if(empty($all[$doctor['institution_id']]))
  44. {
  45. return $this->throwError('云影像服务暂停','7777');
  46. }else{
  47. return $this->throwError($all[$doctor['institution_id']],'7777');
  48. }
  49. }
  50. return $this->tokenSuccess($header[self::tokenName]);
  51. }
  52. protected function mountRequest()
  53. {
  54. $this->setRequest(ZskkDefaultRequest::instance());
  55. }
  56. protected function beforeSetRequest()
  57. {
  58. // todo
  59. $this->setCors();
  60. }
  61. protected function afterSetRequest()
  62. {
  63. Log::record('请求开始----------'.date('Y-m-d h:i:s'));
  64. // todo
  65. if ($this->needToken) {
  66. $this->checkToken();
  67. $token = $this->getHeader()[self::tokenName];
  68. $doctor = $this->getCache($token);
  69. $user = $doctor['id'] ?? null;
  70. }else{
  71. $user = '';
  72. }
  73. $url = Request::instance()->baseUrl();
  74. // 格式化参数格式
  75. $param = Request::param(false);
  76. $data = [
  77. 'id'=>UUIDUtils::uuid(),
  78. 'url'=>$url,
  79. 'uid'=>$user
  80. ];
  81. if($url != '/message/unread'){
  82. LogModel::insert($data);
  83. }
  84. if(isset($_FILES['attachment']) && !empty($_FILES['attachment'])){
  85. //上传附件
  86. return ;
  87. }
  88. if(isset($_FILES['autograph']) && !empty($_FILES['autograph'])){
  89. //上传签名
  90. return ;
  91. }
  92. if(isset($_FILES['file']) && !empty($_FILES['file'])){
  93. //上传签名
  94. return ;
  95. }
  96. if(isset($_FILES['apply']) && !empty($_FILES['apply'])){
  97. //上传签名
  98. return ;
  99. }
  100. if(Request::instance()->baseUrl() == '/cancelExam' || Request::instance()->baseUrl() == '/creatPACSOrder' || Request::instance()->baseUrl()== '/xz/push2' ||
  101. Request::instance()->baseUrl()== '/xz/push3' || Request::instance()->baseUrl()== '/xz/push4' || Request::instance()->baseUrl()=='/exam/create'){
  102. return ;
  103. }
  104. if(isset($param['params'])){
  105. $zskk_param = json_decode($param['params'], true);
  106. $this->setParams($zskk_param);
  107. // $this->throwParamsError('20000','请传入正确的params参数');
  108. }
  109. }
  110. protected function setCors()
  111. {
  112. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  113. ZskkCrosResponse::throwCors();
  114. }
  115. }
  116. protected function getHeader()
  117. {
  118. return $this->getRequest()->header();
  119. }
  120. protected function tokenSuccess($token)
  121. {
  122. $this->setToken($token);
  123. }
  124. protected function success($data)
  125. {
  126. log::record('请求结束--------------'.date('Y-m-d h:i:s'));
  127. return ZskkDefaultResponse::getSuccess($data);
  128. }
  129. // public function throwParamsError($code, $msg) {
  130. // ZskkErrorResponse::throwParamsError($code, $msg);
  131. // }
  132. // public function throwError($error) {
  133. // ZskkErrorResponse::throwError($error);
  134. // }
  135. // public function throwSuccess($data) {
  136. // ZskkDefaultResponse::throwSuccess($data);
  137. // }
  138. // public function throwTokenError() {
  139. // ZskkErrorResponse::throwTokenError();
  140. // }
  141. // public function log($str, $level = null) {
  142. // Log::record($str, is_null($level) ? $this->logName : $level);
  143. // }
  144. public function aliUpload($url)
  145. {
  146. $accessKeyId = "LTAI5tLyAFqdCs9qxxhHRFwG";
  147. $accessKeySecret = "I4ZZTINQPldFrujikkuJlDk9WS3eDw";
  148. // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
  149. $endpoint = "https://oss-cn-beijing.aliyuncs.com/";
  150. // 填写Bucket名称,例如examplebucket。
  151. $bucket= "zskk-image";
  152. // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
  153. $user = $this->getCache($this->getToken());
  154. $ins = $user['institution_id'] ?? '';
  155. if(empty($ins))
  156. {
  157. $ins = 'other';
  158. }
  159. $format = substr($url,strripos($url,".")+1);
  160. $object = "$ins/".time().rand(0,9999).".$format";
  161. //本地上传文件路径
  162. // $file = '/uploads/cs.webp';
  163. // $url = ROOT_PATH.'public'.$file;
  164. $filePath = $url;
  165. try{
  166. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  167. $result = $ossClient->uploadFile($bucket, $object, $filePath);
  168. // \think\facade\Cache::set('result',$result);
  169. // var_dump($result);
  170. } catch(OssException $e) {
  171. // printf(__FUNCTION__ . ": FAILED\n");
  172. // printf($e->getMessage() . "\n");
  173. return $e->getMessage();
  174. }
  175. return $result['info']['url'] ?? '';
  176. }
  177. public function makeFileUrl($file,$type,$fileType='attachment')
  178. {
  179. $upload = new uploadToCloud();
  180. $data = $file;
  181. if($type == '2')
  182. {
  183. if(!empty($file))
  184. {
  185. $data = $upload->makeUrl($file,$fileType);
  186. }
  187. }elseif ($type == '1')
  188. {
  189. if(!empty($file))
  190. {
  191. $data = $file;
  192. }
  193. }
  194. return $data;
  195. }
  196. }