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.
67 lines
1.9 KiB
67 lines
1.9 KiB
<?php
|
|
|
|
namespace App\Service\v3\Implementations;
|
|
|
|
use App\Constants\v3\ErrorCode;
|
|
use App\Constants\v3\SsdbKeys;
|
|
use App\Exception\ErrorCodeException;
|
|
use App\Model\v3\User;
|
|
use App\TaskWorker\SSDBTask;
|
|
use EasyWeChat\Factory;
|
|
use Hyperf\Guzzle\CoroutineHandler;
|
|
use Hyperf\Utils\ApplicationContext;
|
|
use Hashids\Hashids;
|
|
|
|
class WxLoginService implements \App\Service\v3\Interfaces\WxLoginServiceInterface
|
|
{
|
|
|
|
public function do($code)
|
|
{
|
|
|
|
// 微信登录
|
|
$config = config('applet');
|
|
$app = Factory::miniProgram($config);
|
|
$app['guzzle_handler'] = CoroutineHandler::class;
|
|
$result = $app->auth->session($code);
|
|
|
|
if (isset($result['errcode'])&&$result['errcode'] != 0) {
|
|
throw new ErrorCodeException(ErrorCode::WXLOGIN_INVALID_CODE, '', ['message' => '微信登录失败', 'data' => $result, 'code' => $code]);
|
|
}
|
|
|
|
// 更新或者插入用户数据
|
|
$user = User::query()->firstOrCreate(
|
|
['openid' => $result['openid']],
|
|
['unionid' => ($result['unionid'] ?? '')]
|
|
)->toArray();
|
|
|
|
// 登录成功
|
|
$hash = ApplicationContext::getContainer()->get(Hashids::class);
|
|
$hashIds = $hash->encode((int)$user['id']);
|
|
$user['user_token'] = $hashIds;
|
|
|
|
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
|
|
$ssdb->exec('setnx', $hashIds, 1);
|
|
$ssdb->exec('expire', $hashIds, config('auth.user.expire_time'));
|
|
|
|
$return = array_merge($user, $result);
|
|
|
|
$kvs = [];
|
|
foreach ($return as $k => $v) {
|
|
$kvs[] = $k;
|
|
$kvs[] = $v;
|
|
}
|
|
$ssdb->exec('multi_hset', SsdbKeys::USER_INFO.$user['id'], $kvs);
|
|
|
|
return $return;
|
|
}
|
|
|
|
public function check($userId)
|
|
{
|
|
// TODO: Implement check() method.
|
|
}
|
|
|
|
public function undo($userId)
|
|
{
|
|
// TODO: Implement undo() method.
|
|
}
|
|
}
|