LoginServices.php 7.1 KB

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