You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.0 KiB

  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Constants\v3\ErrorCode;
  4. use App\Exception\ErrorCodeException;
  5. use App\Model\v3\User;
  6. use EasyWeChat\Factory;
  7. use Hyperf\Guzzle\CoroutineHandler;
  8. class WxLoginService implements \App\Service\v3\Interfaces\WxLoginServiceInterface
  9. {
  10. public function do($code)
  11. {
  12. // 微信登录
  13. $config = config('applet');
  14. $app = Factory::miniProgram($config);
  15. $app['guzzle_handler'] = CoroutineHandler::class;
  16. $result = $app->auth->session($code);
  17. if (isset($result['errcode'])&&$result['errcode'] != 0) {
  18. throw new ErrorCodeException(ErrorCode::WXLOGIN_INVALID_CODE);
  19. }
  20. // 更新或者插入用户数据
  21. return User::query()->firstOrCreate(
  22. ['openid' => $result['openid']],
  23. ['unionid' => $result['unionid']]
  24. );
  25. }
  26. public function check($userId)
  27. {
  28. // TODO: Implement check() method.
  29. }
  30. public function undo($userId)
  31. {
  32. // TODO: Implement undo() method.
  33. }
  34. }