LoginServices.php 6.6 KB

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