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.

56 lines
1.5 KiB

  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Constants\v3\ErrorCode;
  4. use App\Constants\v3\SsdbKeys;
  5. use App\Exception\ErrorCodeException;
  6. use App\Model\v3\User;
  7. use App\TaskWorker\SSDBTask;
  8. use EasyWeChat\Factory;
  9. use Hyperf\Guzzle\CoroutineHandler;
  10. use Hyperf\Utils\ApplicationContext;
  11. class WxLoginService implements \App\Service\v3\Interfaces\WxLoginServiceInterface
  12. {
  13. public function do($code)
  14. {
  15. // 微信登录
  16. $config = config('applet');
  17. $app = Factory::miniProgram($config);
  18. $app['guzzle_handler'] = CoroutineHandler::class;
  19. $result = $app->auth->session($code);
  20. if (isset($result['errcode'])&&$result['errcode'] != 0) {
  21. throw new ErrorCodeException(ErrorCode::WXLOGIN_INVALID_CODE);
  22. }
  23. // 更新或者插入用户数据
  24. $user = User::query()->firstOrCreate(
  25. ['openid' => $result['openid']],
  26. ['unionid' => $result['unionid']]
  27. )->toArray();
  28. $return = array_merge($user, $result);
  29. $kvs = [];
  30. foreach ($return as $k => $v) {
  31. $kvs[] = $k;
  32. $kvs[] = $v;
  33. }
  34. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  35. $ssdb->exec('multi_hset', SsdbKeys::USER_INFO.$user['id'], $kvs);
  36. return $user;
  37. }
  38. public function check($userId)
  39. {
  40. // TODO: Implement check() method.
  41. }
  42. public function undo($userId)
  43. {
  44. // TODO: Implement undo() method.
  45. }
  46. }