>>>>>>>>>>>>>>>>>>>>> * >>> api-name 接口类型 * >>> api-token 接口认证 * >>>>>>>>>>>>>>>>>>>>>> * --- 手机浏览器访问 wap * --- 电脑浏览器访问 web * --- 微信小程序访问 wxapp * --- 微信服务号访问 wechat * --- 苹果应用接口访问 isoapp * --- 安卓应用接口访问 android * @var string */ protected $type; /** * 当前用户编号 * @var integer */ protected $uuid; /** * 当前用户数据 * @var array */ protected $user; protected $noNeedLogin=[]; /** * 控制器初始化 */ protected function initialize() { // 检查接口类型 $this->type = $this->request->header('api-name'); if (empty($this->type)||!isset(UserAdminService::TYPES[$this->type])) { $this->error("接口类型未定义!header中增加api-name,值为手机浏览器访问wap电脑浏览器访问web微信小程序访问wxapp微信服务号访问wechat苹果应用接口访问iosapp安卓应用接口访问android"); } // 读取用户数据 $this->user = $this->getUser(); $this->uuid = $this->user['id'] ?? ''; } /** * 获取用户数据 * @return array */ protected function getUser() { try { if (empty($this->uuid)) { $token = $this->request->header('api-token',''); // if (empty($token)){ // $this->error('请重新登录,登录认证无效', '{-null-}', 401); // } if($this->needLogin()) { if (empty($token)) $this->error('登录认证不能为空!',null); } [$state, $info, $this->uuid] = UserTokenService::check($this->type, $token); // print_r(UserTokenService::check($this->type, $token)); // exit(); if($this->needLogin()) { if (empty($state)) $this->error($info, null, 401); } } if($this->uuid) { $user = UserAdminService::get($this->uuid, $this->type); if (!$user['status']) $this->error('账户已被冻结!',null,401); if($user['shipyard'])$user['shipyard']['login_url']='https://ship.shipcc.cn/shipyard.php'; if($user['merchants'])$user['merchants']['login_url']='https://ship.shipcc.cn/ship.php'; return $user; } return []; } catch (HttpResponseException $exception) { throw $exception; } catch (\Exception $exception) { trace_file($exception); $this->error($exception->getMessage()); } } /** * 显示用户禁用提示 */ protected function checkUserStatus() { if (empty($this->user['status'])) { $this->error('账户已被冻结!',null,401); } } /** * @return bool */ protected function needLogin(): bool { if($this->noNeedLogin=='*'||$this->noNeedLogin==['*']){ return false; } return !in_array($this->app->request->action(),$this->noNeedLogin,true); } }