AuthService.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\common\service;
  4. use app\common\model\Third;
  5. /**
  6. * 权限服务
  7. */
  8. abstract class AuthService extends BaseService
  9. {
  10. //允许返回给前台的字段
  11. protected $allowFields = [];
  12. protected $login_user = null;
  13. /**
  14. * 登录
  15. * @param string $username 用户名
  16. * @param string $password 密码
  17. */
  18. abstract public function login(string $username, string $password);
  19. /**
  20. * 手机验证码登录
  21. * @param string $mobile
  22. * @param string $code
  23. * @return mixed
  24. */
  25. abstract public function loginByMobile(string $mobile, string $code);
  26. /**
  27. * 第三方平台登录
  28. * @param string $platform
  29. * @param string $openid
  30. * @return mixed
  31. */
  32. //abstract public function loginByThirdPlatform(string $platform,Third $third);
  33. /**
  34. * 获取用户信息
  35. * @return array
  36. */
  37. abstract public function userinfo(bool $allinfo=false);
  38. /**
  39. * 退出登录
  40. * @return bool
  41. */
  42. abstract public function logout();
  43. public function __get($name)
  44. {
  45. return isset($this->login_user[$name])?$this->login_user[$name]:null;
  46. }
  47. /**
  48. * 检查是否登录
  49. * @return bool
  50. */
  51. public function isLogin()
  52. {
  53. if(!$this->login_user){
  54. return false;
  55. }
  56. return true;
  57. }
  58. protected function init()
  59. {
  60. $login_user = $this->userinfo(true);
  61. if($login_user){
  62. $this->login_user=$login_user;
  63. }
  64. }
  65. }