CommonTrait.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace App\Traits;
  3. use App\Services\Login\LoginTokenService;
  4. use App\Services\LoginService;
  5. use Illuminate\Http\JsonResponse;
  6. use Txj\Elastic\Facades\ES;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Request;
  10. trait CommonTrait
  11. {
  12. public ?array $siteInfo = [];
  13. public string $siteAliasName = '';
  14. public $tokenInfo;
  15. public ?array $userInfo;
  16. public ?int $userId = 0;
  17. /**
  18. * 获取当前的站点配置
  19. * @param string $namespace
  20. */
  21. public function setWebsite(string $namespace)
  22. {
  23. $key = 'website:' . md5($namespace);
  24. if ($this->siteInfo = Cache::get($key)) {
  25. $this->siteAliasName = $this->siteInfo['en_alias'];
  26. } else {
  27. $info = DB::table('dep_website')->where('namespace', $namespace)->first();
  28. if (empty($info)) {
  29. abort(508, '管理后台配置错误,站点不存在!');
  30. } else {
  31. $this->siteInfo = get_object_vars($info);
  32. $this->siteAliasName = $this->siteInfo['en_alias'];
  33. Cache::forever($key, $this->siteInfo);
  34. }
  35. }
  36. }
  37. /**
  38. * 判断该用户是否已经登录
  39. */
  40. public function isLoginJson()
  41. {
  42. $loginToken = new LoginTokenService($this->siteInfo['token_table']);
  43. $tokenInfo = $loginToken->checkLogin();
  44. if ($tokenInfo) {
  45. $this->tokenInfo = get_object_vars($tokenInfo);
  46. $this->userId = $tokenInfo->user_id;
  47. } else {
  48. abort(401, '你还没有登录,请登录!');
  49. }
  50. }
  51. /**
  52. * @return bool
  53. */
  54. public function isLogin(): bool
  55. {
  56. $loginToken = new LoginTokenService($this->siteInfo['token_table']);
  57. $tokenInfo = $loginToken->checkLogin();
  58. if ($tokenInfo) {
  59. $this->userId = $tokenInfo->user_id;
  60. return true;
  61. }
  62. return false;
  63. }
  64. /**
  65. * 获取当前登录的用户信息
  66. */
  67. public function userInfo($table)
  68. {
  69. if ($this->isLogin()) {
  70. return DB::table($table)->find($this->userId);
  71. } else {
  72. return false;
  73. }
  74. }
  75. /**
  76. * 判断是否微信内置浏览器访问
  77. *
  78. * @return bool
  79. */
  80. public function isWeixinClient(): bool
  81. {
  82. return str_contains($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger');
  83. }
  84. /**
  85. * 判断是否支付宝内置浏览器访问
  86. * @return bool
  87. */
  88. function isAlipayClient(): bool
  89. {
  90. return str_contains($_SERVER['HTTP_USER_AGENT'], 'Alipay');
  91. }
  92. /**
  93. * 随机获取密钥对
  94. */
  95. protected function getRandomCsr()
  96. {
  97. $keyArr = $this->getOpensslTotalCacheKey();
  98. $total = Cache::tags($keyArr['tags'])->get($keyArr['key']);
  99. if (empty($total)) abort(508, '密钥对不存在,请执行命令创建:php artisan tty:openssl create 100');
  100. $index = mt_rand(1, $total);
  101. $keyArr = $this->getOpensslCacheKey($index);
  102. $result = Cache::tags($keyArr['tags'])->get($keyArr['key']);
  103. if (empty($result)) abort(508, '密钥对异常');
  104. return $result;
  105. }
  106. /**
  107. * @return mixed|string[]
  108. */
  109. public function getMyRSAData()
  110. {
  111. $keyArr = $this->getUserCsrCacheKey();
  112. $expired = config('session.lifetime') * 60;
  113. if ($rsaData = Cache::tags($keyArr['tags'])->get($keyArr['key'])) {
  114. } else {
  115. $rsaData = $this->getRandomCsr();
  116. Cache::tags($keyArr['tags'])->put($keyArr['key'], $rsaData, $expired);
  117. }
  118. return $rsaData;
  119. }
  120. /**
  121. * @return string
  122. */
  123. public function getPrivateKey(): string
  124. {
  125. $rsaData = $this->getMyRSAData();
  126. return $rsaData['private_key'];
  127. }
  128. /**
  129. * @return string
  130. */
  131. public function getPublicKey(): string
  132. {
  133. $rsaData = $this->getMyRSAData();
  134. return $rsaData['public_key'];
  135. }
  136. /**
  137. * @return array
  138. */
  139. public function getYzmCacheKey()
  140. {
  141. if (empty($this->getUniqueIdentifier())) {
  142. abort(508, '唯一标识符不存在!请重试!');
  143. }
  144. return [
  145. 'tags' => ['yzm'],
  146. 'key' => 'SYS:yzm:' . $this->siteAliasName . ':' . $this->getUniqueIdentifier()
  147. ];
  148. }
  149. /**
  150. * @param $encrypt
  151. * @return array
  152. */
  153. public function getUserIdCacheKey($encrypt)
  154. {
  155. return [
  156. 'tags' => ['user'],
  157. 'key' => 'SYS:user_id:' . $this->siteAliasName . ':' . $encrypt
  158. ];
  159. }
  160. /**
  161. * @param $encrypt
  162. * @return array
  163. */
  164. public function getUserInfoCacheKey($encrypt)
  165. {
  166. return [
  167. 'tags' => ['user'],
  168. 'key' => 'SYS:user:' . $this->siteAliasName . ':' . $encrypt
  169. ];
  170. }
  171. public function getUserCsrCacheKey()
  172. {
  173. if (empty($this->getUniqueIdentifier())) {
  174. abort(508, '唯一标识符不存在!请重试!');
  175. }
  176. return [
  177. 'tags' => ['user'],
  178. 'key' => 'SYS:user_csr:' . $this->getUniqueIdentifier()
  179. ];
  180. }
  181. public function getOpensslCacheKey($index)
  182. {
  183. return [
  184. 'tags' => ['openssl'],
  185. 'key' => 'SYS:openssl:' . $index
  186. ];
  187. }
  188. public function getOpensslTotalCacheKey()
  189. {
  190. return [
  191. 'tags' => ['openssl'],
  192. 'key' => 'SYS:openssl_total'
  193. ];
  194. }
  195. /**
  196. * 获取前端的唯一标识符session
  197. *
  198. * @return string|null
  199. */
  200. protected function getUniqueIdentifier(): string|null
  201. {
  202. return Request::header('x-session');
  203. }
  204. protected function getToken(): string|null
  205. {
  206. $token = Request::header('Authorization');
  207. if (empty($token)) {
  208. // 兼容老版本 2022-7-28
  209. $token = Request::header('x-token');
  210. }
  211. return $token;
  212. }
  213. }