123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- namespace App\Traits;
- use App\Services\Login\LoginTokenService;
- use App\Services\LoginService;
- use Illuminate\Http\JsonResponse;
- use Txj\Elastic\Facades\ES;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Request;
- trait CommonTrait
- {
- public ?array $siteInfo = [];
- public string $siteAliasName = '';
- public $tokenInfo;
- public ?array $userInfo;
- public ?int $userId = 0;
- /**
- * 获取当前的站点配置
- * @param string $namespace
- */
- public function setWebsite(string $namespace)
- {
- $key = 'website:' . md5($namespace);
- if ($this->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;
- }
- }
|