LoginServices.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\out;
  12. use app\dao\out\OutAccountDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AuthException;
  15. use crmeb\services\CacheService;
  16. use crmeb\utils\JwtAuth;
  17. use Firebase\JWT\ExpiredException;
  18. /**
  19. * 获取token
  20. * Class LoginServices
  21. * @package app\services\kefu
  22. * @method get($id, ?array $field = [], ?array $with = []) 获取一条数据
  23. * @method update($id, array $data, ?string $key = null)
  24. * @method save(array $data)保存
  25. */
  26. class LoginServices extends BaseServices
  27. {
  28. /**
  29. * LoginServices constructor.
  30. * @param OutAccountDao $dao
  31. */
  32. public function __construct(OutAccountDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 账号密码登录
  38. * @param string $appid
  39. * @param string $appsecret
  40. * @return array
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function authLogin(string $appid, string $appsecret = null)
  46. {
  47. $autInfo = $this->dao->get(['appid' => $appid, 'is_del' => 0]);
  48. if (!$autInfo) {
  49. throw new AuthException(410141);
  50. }
  51. if ($appsecret && !password_verify($appsecret, $autInfo->appsecret)) {
  52. throw new AuthException(400744);
  53. }
  54. if ($autInfo->status == 0) {
  55. throw new AuthException(400595);
  56. }
  57. $token = $this->createToken($autInfo->id, 'out');
  58. $data['last_time'] = time();
  59. $data['ip'] = request()->ip();
  60. $this->update($autInfo['id'], $data);
  61. return [
  62. 'access_token' => $token['token'],
  63. 'exp_time' => $token['params']['exp'],
  64. 'auth_info' => $autInfo->hidden(['appsecret', 'ip', 'is_del', 'add_time', 'status', 'last_time'])->toArray()
  65. ];
  66. }
  67. /**
  68. * 解析token
  69. * @param string $token
  70. * @return array
  71. * @throws \Psr\SimpleCache\InvalidArgumentException
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. public function parseToken(string $token)
  77. {
  78. /** @var CacheService $cacheService */
  79. $cacheService = app()->make(CacheService::class);
  80. /** @var JwtAuth $jwtAuth */
  81. $jwtAuth = app()->make(JwtAuth::class);
  82. //获取token信息
  83. [$md5Token, $id, $type] = $this->verifyToken($token, $jwtAuth, $cacheService);
  84. //获取对外账号
  85. $authInfo = $this->dao->getOne(['id' => $id, 'is_del' => 0]);
  86. $this->checkAuth($authInfo, $md5Token, $cacheService);
  87. return $authInfo->hidden(['appsecret', 'ip', 'is_del', 'add_time', 'status', 'last_time'])->toArray();
  88. }
  89. /**
  90. * 获取一条
  91. * @return array|\think\Model|null
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. */
  96. public function getOne($where = [])
  97. {
  98. $info = $this->dao->getOne($where);
  99. return $info ? $info->toArray() : [];
  100. }
  101. /**
  102. * 获取列表
  103. * @param array $where
  104. * @return array
  105. */
  106. public function getList(array $where = [])
  107. {
  108. [$page, $limit] = $this->getPageValue();
  109. $where['is_del'] = 0;
  110. $list = $this->dao->getList($where, $page, $limit);
  111. $count = $this->dao->count($where);
  112. if ($list) {
  113. foreach ($list as &$item) {
  114. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
  115. $item['last_time'] = $item['last_time'] ? date('Y-m-d H:i:s', $item['last_time']) : '暂无';
  116. }
  117. }
  118. return compact('count', 'list');
  119. }
  120. /**
  121. * 刷新token
  122. * @param string $token
  123. * @return array
  124. */
  125. public function refresh(string $token): array
  126. {
  127. /** @var CacheService $cacheService */
  128. $cacheService = app()->make(CacheService::class);
  129. /** @var JwtAuth $jwtAuth */
  130. $jwtAuth = app()->make(JwtAuth::class);
  131. //获取token信息
  132. [$md5Token, $id, $type] = $this->verifyToken($token, $jwtAuth, $cacheService);
  133. //获取对外账号
  134. $authInfo = $this->dao->getOne(['id' => $id, 'is_del' => 0]);
  135. $this->checkAuth($authInfo, $md5Token, $cacheService);
  136. $cacheService->clearToken($md5Token);
  137. $token = $jwtAuth->createToken($id, $type);
  138. $data['last_time'] = time();
  139. $data['ip'] = request()->ip();
  140. $this->dao->update($id, $data);
  141. return [
  142. 'access_token' => $token['token'],
  143. 'exp_time' => $token['params']['exp'],
  144. ];
  145. }
  146. /**
  147. * 核对用户
  148. * @param $authInfo
  149. * @param string $md5Token
  150. * @param CacheService $cacheService
  151. * @return bool
  152. */
  153. protected function checkAuth($authInfo, string $md5Token, CacheService $cacheService): bool
  154. {
  155. if (!$authInfo) {
  156. if (!request()->isCli()) {
  157. $cacheService->clearToken($md5Token);
  158. }
  159. throw new AuthException(110003);
  160. }
  161. if ($authInfo->status == 2) {
  162. if (!request()->isCli()) {
  163. $cacheService->clearToken($md5Token);
  164. }
  165. throw new AuthException(400595);
  166. }
  167. return true;
  168. }
  169. /**
  170. * 获取token
  171. * @param string $token
  172. * @param JwtAuth $jwtAuth
  173. * @param CacheService $cacheService
  174. * @return array
  175. * @throws \Psr\SimpleCache\InvalidArgumentException
  176. */
  177. protected function verifyToken(string $token, JwtAuth $jwtAuth, CacheService $cacheService): array
  178. {
  179. if (!$token || $token === 'undefined') {
  180. throw new AuthException(400172);
  181. }
  182. $md5Token = md5($token);
  183. if (!$cacheService->hasToken($md5Token) || !($cacheToken = $cacheService->getTokenBucket($md5Token))) {
  184. throw new AuthException(110006);
  185. }
  186. //是否超出有效次数
  187. if (isset($cacheToken['invalidNum']) && $cacheToken['invalidNum'] >= 3) {
  188. if (!request()->isCli()) {
  189. $cacheService->clearToken($md5Token);
  190. }
  191. throw new AuthException(110006);
  192. }
  193. //解析token
  194. [$id, $type] = $jwtAuth->parseToken($token);
  195. if (!$id || $type != 'out') {
  196. throw new AuthException(400172);
  197. }
  198. try {
  199. $jwtAuth->verifyToken();
  200. $cacheService->setTokenBucket($md5Token, $cacheToken, $cacheToken['exp']);
  201. } catch (ExpiredException $e) {
  202. $cacheToken['invalidNum'] = isset($cacheToken['invalidNum']) ? $cacheToken['invalidNum']++ : 1;
  203. $cacheService->setTokenBucket($md5Token, $cacheToken, $cacheToken['exp']);
  204. } catch (\Throwable $e) {
  205. if (!request()->isCli()) {
  206. $cacheService->clearToken($md5Token);
  207. }
  208. throw new AuthException(400172);
  209. }
  210. return [$md5Token, $id, $type];
  211. }
  212. }