LoginServices.php 6.4 KB

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