User.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\service\api;
  3. use app\model\Order;
  4. use laytp\library\Str;
  5. use laytp\library\Token;
  6. use laytp\traits\Error;
  7. use laytp\library\Random;
  8. /**
  9. * Api用户服务实现者
  10. * @package app\api\service
  11. */
  12. class User
  13. {
  14. use Error;
  15. protected $_user = null;//实例化的用户对象
  16. protected $_token = null;//用户登录凭证,token
  17. protected $_isLogin = null;//当前用户是否登录
  18. protected $userModel = null;//用户数据模型
  19. protected $allowFields = ['id', 'nickname', 'avatar', 'status', 'create_time','create_time_int','phone','name','openid_miniapp','openid_wechat','coin','id_number','vip_time'];
  20. protected $tokenKeepTime = 0;//Token默认有效时长,单位秒,365天
  21. /**
  22. * 初始化
  23. * @param $token
  24. * @return bool
  25. * @throws \think\db\exception\DataNotFoundException
  26. * @throws \think\db\exception\DbException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. */
  29. public function init($token)
  30. {
  31. global $_GPC;
  32. if (!$token) {
  33. $this->setError('token不能为空,请重新登录');
  34. return false;
  35. }
  36. $data = Token::get($token);
  37. if (!$data) {
  38. $this->setError('token无效,请重新登录');
  39. return false;
  40. }
  41. $userId = intval($data['user_id']);
  42. if ($userId > 0) {
  43. $user = \app\model\User::find($userId);
  44. if (!$user) {
  45. $this->setError('账号不存在,请重新登录');
  46. return false;
  47. }
  48. //用户状态 1正常 2锁定
  49. if ($user['status'] != 1) {
  50. $this->setError('账号被锁定,请联系管理员'.$user);
  51. return false;
  52. }
  53. $this->_user = $user;
  54. $this->_isLogin = true;
  55. $this->_token = $token;
  56. return true;
  57. } else {
  58. $this->setError('账号不存在,请重新登录');
  59. return false;
  60. }
  61. }
  62. /**
  63. * 退出登录
  64. */
  65. public function logout()
  66. {
  67. if (!$this->_isLogin) {
  68. $this->setError('你没有登录');
  69. return false;
  70. }
  71. //设置登录标识
  72. $this->_isLogin = false;
  73. //删除Token
  74. Token::delete($this->_token);
  75. return true;
  76. }
  77. /**
  78. * 兼容调用user模型的属性
  79. *
  80. * @param string $name
  81. * @return mixed
  82. */
  83. public function __get($name)
  84. {
  85. return $this->_user ? $this->_user->$name : null;
  86. }
  87. /**
  88. * 获取登录用户信息
  89. */
  90. public function getUserInfo()
  91. {
  92. $data = $this->_user->toArray();
  93. $allowFields = $this->getAllowFields();
  94. $userInfo = array_intersect_key($data, array_flip($allowFields));
  95. $userInfo = array_merge($userInfo, ['token' => $this->_token]);
  96. $order = Order::where('uid',$userInfo['id'])->where('paid',1)->find();
  97. $userInfo = array_merge($userInfo, ['is_new' => empty($userInfo['phone']) && $order]);
  98. return $userInfo;
  99. }
  100. /**
  101. * 获取允许输出的字段
  102. * @return array
  103. */
  104. public function getAllowFields()
  105. {
  106. return $this->allowFields;
  107. }
  108. /**
  109. * 获取User模型
  110. * @return User
  111. */
  112. public function getUser()
  113. {
  114. return $this->_user;
  115. }
  116. /**
  117. * 判断是否登录
  118. * @return boolean
  119. */
  120. public function isLogin()
  121. {
  122. if ($this->_isLogin) {
  123. return true;
  124. }
  125. return false;
  126. }
  127. /**
  128. * 获取当前Token
  129. * @return string
  130. */
  131. public function getToken()
  132. {
  133. return $this->_token;
  134. }
  135. }