LoginServices.php 5.4 KB

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