Api.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Exceptions\Api\VerifySignException;
  4. use App\Exceptions\Api\LoginException;
  5. use App\Exceptions\Api\ApiException;
  6. use App\Facades\Servers\Encrypts\AccessToken;
  7. use App\Http\Controllers\Controller;
  8. use App\Models\Api\Personnel\Employee as EmployeeModel;
  9. use App\Http\Requests\Manager\Login as Request;
  10. /**
  11. * 接口控制器
  12. * @author 唐远望
  13. * @version 1.0
  14. * @date 2025-12-09
  15. *
  16. */
  17. class Api extends Controller
  18. {
  19. protected $token;
  20. /**
  21. * $prefix表前缀
  22. */
  23. public function __construct(Request $request)
  24. {
  25. $this->token = $request->input('access_token_api', '');
  26. }
  27. /**
  28. * 登录验证
  29. * @author 唐远望
  30. * @version 1.0
  31. * @date 2025-12-09
  32. *
  33. */
  34. public function checkLogin()
  35. {
  36. // 获取登录结果
  37. // 解码
  38. $userInfo = AccessToken::decode($this->token);
  39. // 判断登录时效
  40. if (isset($userInfo['error'])) throw new LoginException("login_error");
  41. if ($userInfo['expire'] < time()) throw new LoginException("login_exprie");
  42. // 获取就得令牌
  43. $oldToken = (new EmployeeModel())->getLogin($userInfo['uid'], 'api');
  44. if ($oldToken != md5($this->token)) throw new LoginException("login_error");
  45. // 追加入
  46. return $userInfo;
  47. }
  48. /**
  49. * 登录验证
  50. * @author 唐远望
  51. * @version 1.0
  52. * @date 2025-12-09
  53. *
  54. */
  55. public function getUid()
  56. {
  57. // 尝试执行
  58. try {
  59. // 解码
  60. $decode = AccessToken::decode($this->token);
  61. // 错误返回
  62. if (isset($decode['error'])) return 0;
  63. // 如果没有过期时间,过期处理
  64. if (empty($decode['expire'])) return 0;
  65. // 如果过期,过期处理
  66. if ($decode['expire'] <= time()) return 0;
  67. // 返回用户ID
  68. return $decode['uid'];
  69. } catch (\Throwable $th) {
  70. // 返回0
  71. return 0;
  72. }
  73. }
  74. }