siteInfo = Cache::get($key)) { $this->siteAliasName = $this->siteInfo['en_alias']; } else { $info = DB::table('dep_website')->where('namespace', $namespace)->first(); if (empty($info)) { abort(508, '管理后台配置错误,站点不存在!'); } else { $this->siteInfo = get_object_vars($info); $this->siteAliasName = $this->siteInfo['en_alias']; Cache::forever($key, $this->siteInfo); } } } /** * 判断该用户是否已经登录 */ public function isLoginJson() { $loginToken = new LoginTokenService($this->siteInfo['token_table']); $tokenInfo = $loginToken->checkLogin(); if ($tokenInfo) { $this->tokenInfo = get_object_vars($tokenInfo); $this->userId = $tokenInfo->user_id; } else { abort(401, '你还没有登录,请登录!'); } } /** * @return bool */ public function isLogin(): bool { $loginToken = new LoginTokenService($this->siteInfo['token_table']); $tokenInfo = $loginToken->checkLogin(); if ($tokenInfo) { $this->userId = $tokenInfo->user_id; return true; } return false; } /** * 获取当前登录的用户信息 */ public function userInfo($table) { if ($this->isLogin()) { return DB::table($table)->find($this->userId); } else { return false; } } /** * 判断是否微信内置浏览器访问 * * @return bool */ public function isWeixinClient(): bool { return str_contains($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger'); } /** * 判断是否支付宝内置浏览器访问 * @return bool */ function isAlipayClient(): bool { return str_contains($_SERVER['HTTP_USER_AGENT'], 'Alipay'); } /** * 随机获取密钥对 */ protected function getRandomCsr() { $keyArr = $this->getOpensslTotalCacheKey(); $total = Cache::tags($keyArr['tags'])->get($keyArr['key']); if (empty($total)) abort(508, '密钥对不存在,请执行命令创建:php artisan tty:openssl create 100'); $index = mt_rand(1, $total); $keyArr = $this->getOpensslCacheKey($index); $result = Cache::tags($keyArr['tags'])->get($keyArr['key']); if (empty($result)) abort(508, '密钥对异常'); return $result; } /** * @return mixed|string[] */ public function getMyRSAData() { $keyArr = $this->getUserCsrCacheKey(); $expired = config('session.lifetime') * 60; if ($rsaData = Cache::tags($keyArr['tags'])->get($keyArr['key'])) { } else { $rsaData = $this->getRandomCsr(); Cache::tags($keyArr['tags'])->put($keyArr['key'], $rsaData, $expired); } return $rsaData; } /** * @return string */ public function getPrivateKey(): string { $rsaData = $this->getMyRSAData(); return $rsaData['private_key']; } /** * @return string */ public function getPublicKey(): string { $rsaData = $this->getMyRSAData(); return $rsaData['public_key']; } /** * @return array */ public function getYzmCacheKey() { if (empty($this->getUniqueIdentifier())) { abort(508, '唯一标识符不存在!请重试!'); } return [ 'tags' => ['yzm'], 'key' => 'SYS:yzm:' . $this->siteAliasName . ':' . $this->getUniqueIdentifier() ]; } /** * @param $encrypt * @return array */ public function getUserIdCacheKey($encrypt) { return [ 'tags' => ['user'], 'key' => 'SYS:user_id:' . $this->siteAliasName . ':' . $encrypt ]; } /** * @param $encrypt * @return array */ public function getUserInfoCacheKey($encrypt) { return [ 'tags' => ['user'], 'key' => 'SYS:user:' . $this->siteAliasName . ':' . $encrypt ]; } public function getUserCsrCacheKey() { if (empty($this->getUniqueIdentifier())) { abort(508, '唯一标识符不存在!请重试!'); } return [ 'tags' => ['user'], 'key' => 'SYS:user_csr:' . $this->getUniqueIdentifier() ]; } public function getOpensslCacheKey($index) { return [ 'tags' => ['openssl'], 'key' => 'SYS:openssl:' . $index ]; } public function getOpensslTotalCacheKey() { return [ 'tags' => ['openssl'], 'key' => 'SYS:openssl_total' ]; } /** * 获取前端的唯一标识符session * * @return string|null */ protected function getUniqueIdentifier(): string|null { return Request::header('x-session'); } protected function getToken(): string|null { $token = Request::header('Authorization'); if (empty($token)) { // 兼容老版本 2022-7-28 $token = Request::header('x-token'); } return $token; } }