LoginServices.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\kefu;
  12. use crmeb\exceptions\AuthException;
  13. use crmeb\utils\ApiErrorCode;
  14. use crmeb\utils\JwtAuth;
  15. use Firebase\JWT\ExpiredException;
  16. use FormBuilder\Factory\Base;
  17. use think\facade\Cache;
  18. use app\services\BaseServices;
  19. use crmeb\services\CacheService;
  20. use app\dao\service\StoreServiceDao;
  21. use crmeb\services\WechatOpenService;
  22. use think\exception\ValidateException;
  23. use app\services\wechat\WechatUserServices;
  24. /**
  25. * 客服登录
  26. * Class LoginServices
  27. * @package app\services\kefu
  28. * @method get($id, ?array $field = [], ?array $with = []) 获取一条数据
  29. */
  30. class LoginServices extends BaseServices
  31. {
  32. /**
  33. * LoginServices constructor.
  34. * @param StoreServiceDao $dao
  35. */
  36. public function __construct(StoreServiceDao $dao)
  37. {
  38. $this->dao = $dao;
  39. }
  40. /**
  41. * 客服账号密码登录
  42. * @param string $account
  43. * @param string $password
  44. * @return array
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\DbException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. */
  49. public function authLogin(string $account, string $password = null)
  50. {
  51. $kefuInfo = $this->dao->get(['account' => $account]);
  52. if (!$kefuInfo) {
  53. throw new ValidateException('没有此用户');
  54. }
  55. if ($password && !password_verify($password, $kefuInfo->password)) {
  56. throw new ValidateException('账号或密码错误');
  57. }
  58. if (!$kefuInfo->status) {
  59. throw new ValidateException('您已被禁止登录');
  60. }
  61. $token = $this->createToken($kefuInfo->id, 'kefu');
  62. $kefuInfo->update_time = time();
  63. $kefuInfo->ip = request()->ip();
  64. $kefuInfo->save();
  65. return [
  66. 'token' => $token['token'],
  67. 'exp_time' => $token['params']['exp'],
  68. 'kefuInfo' => $kefuInfo->hidden(['password', 'ip', 'update_time', 'add_time', 'status', 'mer_id', 'customer', 'notify'])->toArray()
  69. ];
  70. }
  71. /**
  72. * 解析token
  73. * @param string $token
  74. * @return array
  75. * @throws \Psr\SimpleCache\InvalidArgumentException
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function parseToken(string $token)
  81. {
  82. $noCli = !request()->isCli();
  83. /** @var CacheService $cacheService */
  84. $cacheService = app()->make(CacheService::class);
  85. //检测token是否过期
  86. $md5Token = md5($token);
  87. if (!$token || !$cacheService->hasToken($md5Token) || !($cacheToken = $cacheService->getTokenBucket($md5Token))) {
  88. throw new AuthException(ApiErrorCode::ERR_LOGIN,410003);
  89. }
  90. if ($token === 'undefined') {
  91. throw new AuthException(ApiErrorCode::ERR_LOGIN,410003);
  92. }
  93. //是否超出有效次数
  94. if (isset($cacheToken['invalidNum']) && $cacheToken['invalidNum'] >= 3) {
  95. $cacheService->clearToken($md5Token);
  96. throw new AuthException(ApiErrorCode::ERR_LOGIN_INVALID,410003);
  97. }
  98. /** @var JwtAuth $jwtAuth */
  99. $jwtAuth = app()->make(JwtAuth::class);
  100. //设置解析token
  101. [$id, $type] = $jwtAuth->parseToken($token);
  102. //验证token
  103. try {
  104. $jwtAuth->verifyToken();
  105. $cacheService->setTokenBucket($md5Token, $cacheToken, $cacheToken['exp']);
  106. } catch (ExpiredException $e) {
  107. $cacheToken['invalidNum'] = isset($cacheToken['invalidNum']) ? $cacheToken['invalidNum']++ : 1;
  108. $cacheService->setTokenBucket($md5Token, $cacheToken, $cacheToken['exp']);
  109. } catch (\Throwable $e) {
  110. $noCli && $cacheService->clearToken($md5Token);
  111. throw new AuthException(ApiErrorCode::ERR_LOGIN_INVALID,410003);
  112. }
  113. //获取管理员信息
  114. $adminInfo = $this->dao->get($id);
  115. if (!$adminInfo || !$adminInfo->id) {
  116. $noCli && $cacheService->clearToken($md5Token);
  117. throw new AuthException(ApiErrorCode::ERR_LOGIN_STATUS,410003);
  118. }
  119. $adminInfo->type = $type;
  120. return $adminInfo->hidden(['password', 'ip', 'status']);
  121. }
  122. /**
  123. * @return array
  124. * @throws \think\db\exception\DataNotFoundException
  125. * @throws \think\db\exception\DbException
  126. * @throws \think\db\exception\ModelNotFoundException
  127. */
  128. public function wechatAuth()
  129. {
  130. /** @var WechatOpenService $service */
  131. $service = app()->make(WechatOpenService::class);
  132. $info = $service->getAuthorizationInfo();
  133. if (!$info) {
  134. throw new ValidateException('授权失败');
  135. }
  136. $original = $info->getOriginal();
  137. if (!isset($original['unionid'])) {
  138. throw new ValidateException('unionid不存在');
  139. }
  140. /** @var WechatUserServices $userService */
  141. $userService = app()->make(WechatUserServices::class);
  142. $uid = $userService->value(['unionid' => $original['unionid']], 'uid');
  143. if (!$uid) {
  144. throw new ValidateException('获取用户UID失败');
  145. }
  146. $kefuInfo = $this->dao->get(['uid' => $uid]);
  147. if (!$kefuInfo) {
  148. throw new ValidateException('客服不存在');
  149. }
  150. if (!$kefuInfo->status) {
  151. throw new ValidateException('您已被禁止登录');
  152. }
  153. $token = $this->createToken($kefuInfo->id, 'kefu');
  154. $kefuInfo->update_time = time();
  155. $kefuInfo->ip = request()->ip();
  156. $kefuInfo->save();
  157. return [
  158. 'token' => $token['token'],
  159. 'exp_time' => $token['params']['exp'],
  160. 'kefuInfo' => $kefuInfo->hidden(['password', 'ip', 'update_time', 'add_time', 'status', 'mer_id', 'customer', 'notify'])->toArray()
  161. ];
  162. }
  163. /**
  164. * 检测有没有人扫描登录
  165. * @param string $key
  166. * @return array|int[]
  167. * @throws \Psr\SimpleCache\InvalidArgumentException
  168. * @throws \think\db\exception\DataNotFoundException
  169. * @throws \think\db\exception\DbException
  170. * @throws \think\db\exception\ModelNotFoundException
  171. */
  172. public function scanLogin(string $key)
  173. {
  174. $hasKey = Cache::has($key);
  175. if ($hasKey === false) {
  176. $status = 0;//不存在需要刷新二维码
  177. } else {
  178. $keyValue = CacheService::get($key);
  179. if ($keyValue === '0') {
  180. $status = 1;//正在扫描中
  181. $kefuInfo = $this->dao->get(['uniqid' => $key], ['account', 'uniqid']);
  182. if ($kefuInfo) {
  183. $tokenInfo = $this->authLogin($kefuInfo->account);
  184. $tokenInfo['status'] = 3;
  185. $kefuInfo->uniqid = '';
  186. $kefuInfo->save();
  187. CacheService::delete($key);
  188. return $tokenInfo;
  189. }
  190. } else {
  191. $status = 2;//没有扫描
  192. }
  193. }
  194. return ['status' => $status];
  195. }
  196. }