LoginServices.php 5.1 KB

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