Auth.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace app\common\library;
  3. use app\common\model\UserModel;
  4. use think\Config;
  5. use think\Db;
  6. use think\Exception;
  7. use think\Hook;
  8. use think\Request;
  9. class Auth
  10. {
  11. protected static $instance = null;
  12. protected $_error = '';
  13. protected $_logined = false;
  14. protected $_user = null;
  15. //Token信息中的用户ID
  16. protected $_tokenUserID = 0;
  17. //Token信息中的Token
  18. protected $_tokenString = '';
  19. protected $requestUri = '';
  20. //默认配置
  21. protected $config = [];
  22. protected $options = [];
  23. protected $allowFields = ['id', 'address', 'nickname', 'avatar', 'createtime'];
  24. public function __construct($options = [])
  25. {
  26. if ($config = Config::get('user')) {
  27. $this->config = array_merge($this->config, $config);
  28. }
  29. $this->options = array_merge($this->config, $options);
  30. }
  31. /**
  32. *
  33. * @param array $options 参数
  34. * @return Auth
  35. */
  36. public static function instance($options = [])
  37. {
  38. if (is_null(self::$instance)) {
  39. self::$instance = new static($options);
  40. }
  41. return self::$instance;
  42. }
  43. /**
  44. * 获取User模型
  45. * @return UserModel
  46. */
  47. public function getUser()
  48. {
  49. return $this->_user;
  50. }
  51. /**
  52. * 兼容调用user模型的属性
  53. *
  54. * @param string $name
  55. * @return mixed
  56. */
  57. public function __get($name)
  58. {
  59. return $this->_user ? $this->_user->$name : null;
  60. }
  61. /**
  62. * 兼容调用user模型的属性
  63. */
  64. public function __isset($name)
  65. {
  66. return isset($this->_user) ? isset($this->_user->$name) : false;
  67. }
  68. /**
  69. * 根据Token初始化
  70. *
  71. * @param string $token 请求头里的加密过的token
  72. * @return boolean
  73. */
  74. public function init(string $token)
  75. {
  76. if ($this->_logined) {
  77. return true;
  78. }
  79. if ($this->_error) {
  80. return false;
  81. }
  82. // 解析Token
  83. $tokenInfo = Token::unmarshal($token);
  84. if (count($tokenInfo) == 0) {
  85. return false;
  86. }
  87. // 校验Token有效性
  88. $check = Token::check($tokenInfo["user_id"], $tokenInfo["token"]);
  89. if (!$check) {
  90. return false;
  91. }
  92. $user_id = intval($tokenInfo["user_id"]);
  93. if ($user_id > 0) {
  94. $user = (new UserModel())->get($user_id);
  95. if (!$user) {
  96. $this->setError('Account not exist');
  97. return false;
  98. }
  99. $this->_user = $user;
  100. $this->_logined = true;
  101. $this->_tokenUserID = $tokenInfo["user_id"];
  102. $this->_tokenString = $tokenInfo["token"];
  103. //初始化成功的事件
  104. Hook::listen("user_init_successed", $this->_user);
  105. return true;
  106. } else {
  107. $this->setError('You are not logged in');
  108. return false;
  109. }
  110. }
  111. /**
  112. * 判断是否登录
  113. * @return boolean
  114. */
  115. public function isLogin()
  116. {
  117. if ($this->_logined) {
  118. return true;
  119. }
  120. return false;
  121. }
  122. /**
  123. * 获取当前Token的用户ID
  124. * @return int
  125. */
  126. public function getTokenUserID(): int
  127. {
  128. return $this->_tokenUserID;
  129. }
  130. /**
  131. * 获取会员基本信息
  132. */
  133. public function getUserinfo()
  134. {
  135. $data = $this->_user->toArray();
  136. $allowFields = $this->getAllowFields();
  137. return array_intersect_key($data, array_flip($allowFields));
  138. }
  139. /**
  140. * 设置当前请求的URI
  141. * @param string $uri
  142. */
  143. public function setRequestUri($uri)
  144. {
  145. $this->requestUri = $uri;
  146. }
  147. /**
  148. * 获取允许输出的字段
  149. * @return array
  150. */
  151. public function getAllowFields()
  152. {
  153. return $this->allowFields;
  154. }
  155. /**
  156. * 设置允许输出的字段
  157. * @param array $fields
  158. */
  159. public function setAllowFields($fields)
  160. {
  161. $this->allowFields = $fields;
  162. }
  163. /**
  164. * 删除一个指定会员
  165. * @param int $user_id 会员ID
  166. * @return boolean
  167. */
  168. public function delete($user_id)
  169. {
  170. $user = (new UserModel())->get($user_id);
  171. if (!$user) {
  172. return false;
  173. }
  174. Db::startTrans();
  175. try {
  176. // 删除会员
  177. (new UserModel())->destroy($user_id);
  178. // 删除会员Token
  179. Token::delete($user_id);
  180. Hook::listen("user_delete_successed", $user);
  181. Db::commit();
  182. } catch (Exception $e) {
  183. Db::rollback();
  184. $this->setError($e->getMessage());
  185. return false;
  186. }
  187. return true;
  188. }
  189. /**
  190. * 获取密码加密后的字符串
  191. * @param string $password 密码
  192. * @param string $salt 密码盐
  193. * @return string
  194. */
  195. public function getEncryptPassword($password, $salt = '')
  196. {
  197. return md5(md5($password) . $salt);
  198. }
  199. /**
  200. * 检测当前控制器和方法是否匹配传递的数组
  201. *
  202. * @param array $arr 需要验证权限的数组
  203. * @return boolean
  204. */
  205. public function match($arr = [])
  206. {
  207. $request = Request::instance();
  208. $arr = is_array($arr) ? $arr : explode(',', $arr);
  209. if (!$arr) {
  210. return false;
  211. }
  212. $arr = array_map('strtolower', $arr);
  213. // 是否存在
  214. if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr)) {
  215. return true;
  216. }
  217. // 没找到匹配
  218. return false;
  219. }
  220. /**
  221. * 设置错误信息
  222. *
  223. * @param string $error 错误信息
  224. * @return Auth
  225. */
  226. public function setError($error)
  227. {
  228. $this->_error = $error;
  229. return $this;
  230. }
  231. /**
  232. * 获取错误信息
  233. * @return string
  234. */
  235. public function getError()
  236. {
  237. return $this->_error ? __($this->_error) : '';
  238. }
  239. }