LoginServices.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. declare (strict_types=1);
  12. namespace app\services\pc;
  13. use app\services\BaseServices;
  14. use app\services\user\UserServices;
  15. use app\services\wechat\WechatUserServices;
  16. use crmeb\exceptions\ApiException;
  17. use crmeb\services\CacheService;
  18. use crmeb\services\oauth\OAuth;
  19. use think\facade\Cache;
  20. class LoginServices extends BaseServices
  21. {
  22. /**
  23. * 扫码登陆
  24. * @param string $key
  25. * @return array|int[]
  26. * @throws \Psr\SimpleCache\InvalidArgumentException
  27. */
  28. public function scanLogin(string $key)
  29. {
  30. $hasKey = Cache::has($key);
  31. if ($hasKey === false) {
  32. $status = 0;//不存在需要刷新二维码
  33. } else {
  34. $keyValue = CacheService::get($key);
  35. if ($keyValue === 0) {
  36. $status = 1;//正在扫描中
  37. /** @var UserServices $user */
  38. $user = app()->make(UserServices::class);
  39. $userInfo = $user->get(['uniqid' => $key], ['account', 'uniqid']);
  40. if ($userInfo) {
  41. $tokenInfo = $this->authLogin($userInfo->account);
  42. $tokenInfo['status'] = 3;
  43. $userInfo->uniqid = '';
  44. $userInfo->save();
  45. CacheService::delete($key);
  46. return $tokenInfo;
  47. }
  48. } else {
  49. $status = 2;//没有扫描
  50. }
  51. }
  52. return ['status' => $status];
  53. }
  54. /**
  55. * 扫码登陆
  56. * @param string $account
  57. * @param string|null $password
  58. * @return array
  59. */
  60. public function authLogin(string $account, string $password = null)
  61. {
  62. /** @var UserServices $user */
  63. $user = app()->make(UserServices::class);
  64. $userInfo = $user->get(['account' => $account]);
  65. if (!$userInfo) {
  66. throw new ApiException(410141);
  67. }
  68. if ($password && !password_verify($password, $userInfo->password)) {
  69. throw new ApiException(410025);
  70. }
  71. if (!$userInfo->status) {
  72. throw new ApiException(410027);
  73. }
  74. $token = $this->createToken($userInfo->id, 'api');
  75. $userInfo->update_time = time();
  76. $userInfo->ip = request()->ip();
  77. $userInfo->save();
  78. return [
  79. 'token' => $token['token'],
  80. 'exp_time' => $token['params']['exp'],
  81. 'userInfo' => $userInfo->hidden(['password', 'ip', 'update_time', 'add_time', 'status', 'mer_id', 'customer', 'notify'])->toArray()
  82. ];
  83. }
  84. /**
  85. * @return array
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. */
  90. public function wechatAuth()
  91. {
  92. /** @var OAuth $oauth */
  93. $oauth = app()->make(OAuth::class);
  94. $info = $oauth->oauth(null, ['open' => true]);
  95. if (!$info) {
  96. throw new ApiException(410131);
  97. }
  98. $wechatInfo = $info->getOriginal();
  99. if (!isset($wechatInfo['unionid'])) {
  100. throw new ApiException(410132);
  101. }
  102. if (!isset($wechatInfo['nickname'])) {
  103. $wechatInfo = $oauth->getUserInfo($wechatInfo['openid']);
  104. if (!isset($wechatInfo['nickname']))
  105. throw new ApiException(410131);
  106. if (isset($wechatInfo['tagid_list']))
  107. $wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
  108. } else {
  109. if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
  110. /** @var WechatUserServices $wechatUser */
  111. $wechatUser = app()->make(WechatUserServices::class);
  112. if (!$wechatUser->getOne(['openid' => $wechatInfo['openid']])) {
  113. $wechatInfo['subscribe'] = 0;
  114. }
  115. }
  116. $wechatInfo['user_type'] = 'pc';
  117. $openid = $wechatInfo['openid'];
  118. /** @var WechatUserServices $wechatUserServices */
  119. $wechatUserServices = app()->make(WechatUserServices::class);
  120. $user = $wechatUserServices->getAuthUserInfo($openid, 'pc');
  121. $createData = [$openid, $wechatInfo, 0, 'pc', 'pc'];
  122. if (!$user) {
  123. $user = $wechatUserServices->wechatOauthAfter($createData);
  124. } else {
  125. //更新用户信息
  126. $wechatUserServices->wechatUpdata([$user['uid'], $wechatInfo]);
  127. }
  128. $token = $this->createToken((int)$user->uid, 'api');
  129. return [
  130. 'token' => $token['token'],
  131. 'exp_time' => $token['params']['exp']
  132. ];
  133. }
  134. }