ZskkDefaultController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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('请求结束1--------------'.date('Y-m-d h:i:s'));
  127. return ZskkDefaultResponse::getSuccess($data);
  128. }
  129. protected function success2($data,$code=1)
  130. {
  131. log::record('请求结束2--------------'.date('Y-m-d h:i:s'));
  132. return ZskkDefaultResponse::getSuccess($data,'success',$code);
  133. }
  134. // public function throwParamsError($code, $msg) {
  135. // ZskkErrorResponse::throwParamsError($code, $msg);
  136. // }
  137. // public function throwError($error) {
  138. // ZskkErrorResponse::throwError($error);
  139. // }
  140. // public function throwSuccess($data) {
  141. // ZskkDefaultResponse::throwSuccess($data);
  142. // }
  143. // public function throwTokenError() {
  144. // ZskkErrorResponse::throwTokenError();
  145. // }
  146. // public function log($str, $level = null) {
  147. // Log::record($str, is_null($level) ? $this->logName : $level);
  148. // }
  149. public function aliUpload($url)
  150. {
  151. $accessKeyId = "LTAI5tLyAFqdCs9qxxhHRFwG";
  152. $accessKeySecret = "I4ZZTINQPldFrujikkuJlDk9WS3eDw";
  153. // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
  154. $endpoint = "https://oss-cn-beijing.aliyuncs.com/";
  155. // 填写Bucket名称,例如examplebucket。
  156. $bucket= "zskk-image";
  157. // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
  158. $user = $this->getCache($this->getToken());
  159. $ins = $user['institution_id'] ?? '';
  160. if(empty($ins))
  161. {
  162. $ins = 'other';
  163. }
  164. $format = substr($url,strripos($url,".")+1);
  165. $object = "$ins/".time().rand(0,9999).".$format";
  166. //本地上传文件路径
  167. // $file = '/uploads/cs.webp';
  168. // $url = ROOT_PATH.'public'.$file;
  169. $filePath = $url;
  170. try{
  171. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  172. $result = $ossClient->uploadFile($bucket, $object, $filePath);
  173. // \think\facade\Cache::set('result',$result);
  174. // var_dump($result);
  175. } catch(OssException $e) {
  176. // printf(__FUNCTION__ . ": FAILED\n");
  177. // printf($e->getMessage() . "\n");
  178. return $e->getMessage();
  179. }
  180. return $result['info']['url'] ?? '';
  181. }
  182. public function makeFileUrl($file,$type,$fileType='attachment')
  183. {
  184. $upload = new uploadToCloud();
  185. $data = $file;
  186. if($type == '2')
  187. {
  188. if(!empty($file))
  189. {
  190. $data = $upload->makeUrl($file,$fileType);
  191. }
  192. }elseif ($type == '1')
  193. {
  194. if(!empty($file))
  195. {
  196. $data = $file;
  197. }
  198. }
  199. return $data;
  200. }
  201. }