Member.php 4.4 KB

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