LoginServices.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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->online = 1;
  63. $kefuInfo->save();
  64. return [
  65. 'token' => $token['token'],
  66. 'exp_time' => $token['params']['exp'],
  67. 'kefuInfo' => $kefuInfo->hidden(['password', 'ip', 'update_time', 'add_time', 'status', 'mer_id', 'customer', 'notify'])->toArray()
  68. ];
  69. }
  70. /**
  71. * 解析token
  72. * @param string $token
  73. * @return array
  74. * @throws \Psr\SimpleCache\InvalidArgumentException
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function parseToken(string $token)
  80. {
  81. $noCli = !request()->isCli();
  82. /** @var CacheService $cacheService */
  83. $cacheService = app()->make(CacheService::class);
  84. //检测token是否过期
  85. $md5Token = md5($token);
  86. if (!$token || !$cacheService->has($md5Token) || !($cacheService->get($md5Token, '', NULL, 'kefu'))) {
  87. throw new AuthException(110005);
  88. }
  89. if ($token === 'undefined') {
  90. throw new AuthException(110005);
  91. }
  92. /** @var JwtAuth $jwtAuth */
  93. $jwtAuth = app()->make(JwtAuth::class);
  94. //设置解析token
  95. [$id, $type] = $jwtAuth->parseToken($token);
  96. //验证token
  97. try {
  98. $jwtAuth->verifyToken();
  99. } catch (\Throwable $e) {
  100. $noCli && $cacheService->delete($md5Token);
  101. throw new AuthException(110006);
  102. }
  103. //获取管理员信息
  104. $adminInfo = $this->dao->get($id);
  105. if (!$adminInfo || !$adminInfo->id) {
  106. $noCli && $cacheService->delete($md5Token);
  107. throw new AuthException(110007);
  108. }
  109. $adminInfo->type = $type;
  110. return $adminInfo->hidden(['password', 'ip', 'status']);
  111. }
  112. /**
  113. * @return array
  114. * @throws \think\db\exception\DataNotFoundException
  115. * @throws \think\db\exception\DbException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. */
  118. public function wechatAuth()
  119. {
  120. /** @var OAuth $oauth */
  121. $oauth = app()->make(OAuth::class);
  122. $original = $oauth->oauth(null, ['open' => true]);
  123. if (!isset($original['unionid'])) {
  124. throw new AuthException(410132);
  125. }
  126. /** @var WechatUserServices $userService */
  127. $userService = app()->make(WechatUserServices::class);
  128. $uid = $userService->value(['unionid' => $original['unionid']], 'uid');
  129. if (!$uid) {
  130. throw new AuthException(410133);
  131. }
  132. $kefuInfo = $this->dao->get(['uid' => $uid]);
  133. if (!$kefuInfo) {
  134. throw new AuthException(410142);
  135. }
  136. if (!$kefuInfo->status) {
  137. throw new AuthException(410027);
  138. }
  139. $token = $this->createToken($kefuInfo->id, 'kefu');
  140. $kefuInfo->update_time = time();
  141. $kefuInfo->ip = request()->ip();
  142. $kefuInfo->save();
  143. return [
  144. 'token' => $token['token'],
  145. 'exp_time' => $token['params']['exp'],
  146. 'kefuInfo' => $kefuInfo->hidden(['password', 'ip', 'update_time', 'add_time', 'status', 'mer_id', 'customer', 'notify'])->toArray()
  147. ];
  148. }
  149. /**
  150. * 检测有没有人扫描登录
  151. * @param string $key
  152. * @return array|int[]
  153. * @throws \Psr\SimpleCache\InvalidArgumentException
  154. * @throws \think\db\exception\DataNotFoundException
  155. * @throws \think\db\exception\DbException
  156. * @throws \think\db\exception\ModelNotFoundException
  157. */
  158. public function scanLogin(string $key)
  159. {
  160. $hasKey = Cache::has($key);
  161. if ($hasKey === false) {
  162. $status = 0;//不存在需要刷新二维码
  163. } else {
  164. $keyValue = CacheService::get($key);
  165. if ($keyValue === '0') {
  166. $status = 1;//正在扫描中
  167. $kefuInfo = $this->dao->get(['uniqid' => $key], ['account', 'uniqid']);
  168. if ($kefuInfo) {
  169. $tokenInfo = $this->authLogin($kefuInfo->account);
  170. $tokenInfo['status'] = 3;
  171. $kefuInfo->uniqid = '';
  172. $kefuInfo->save();
  173. CacheService::delete($key);
  174. return $tokenInfo;
  175. }
  176. } else {
  177. $status = 2;//没有扫描
  178. }
  179. }
  180. return ['status' => $status];
  181. }
  182. }