完善微信授权新建用户逻辑
parent
9c8a3b5fac
commit
10c669e002
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Constants\Wechat;
|
||||
|
||||
use Hyperf\Constants\AbstractConstants;
|
||||
use Hyperf\Constants\Annotation\Constants;
|
||||
|
||||
#[Constants]
|
||||
class AuthErrorCode extends AbstractConstants
|
||||
{
|
||||
/**
|
||||
* @Message("请传入微信Code")
|
||||
*/
|
||||
public const CODE_EMPTY_ERROR = 2000001;
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Wechat;
|
||||
|
||||
use App\Controller\BaseController;
|
||||
use App\Exception\BusinessException;
|
||||
use app\Constants\Wechat\AuthErrorCode;
|
||||
use App\Service\Wechat\AuthService;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\PostMapping;
|
||||
|
||||
/**
|
||||
* 微信授权处理控制器
|
||||
*/
|
||||
#[Controller]
|
||||
class AuthController extends BaseController
|
||||
{
|
||||
#[Inject]
|
||||
protected AuthService $service;
|
||||
|
||||
/**
|
||||
* 通过微信授权新建用户
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
#[PostMapping(path: "auth/code2OpenID")]
|
||||
public function code2OpenID() : array
|
||||
{
|
||||
$code = $this->request->input('code', '');
|
||||
|
||||
if (empty($code)) {
|
||||
throw new BusinessException(AuthErrorCode::CODE_EMPTY_ERROR);
|
||||
}
|
||||
|
||||
return $this->service->codeToOpenID($code);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Helper;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Hyperf\Guzzle\HandlerStackFactory;
|
||||
|
||||
class Curl
|
||||
{
|
||||
public static function get(string $url) : array
|
||||
{
|
||||
$factory = new HandlerStackFactory();
|
||||
$stack = $factory->create();
|
||||
|
||||
$client = make(Client::class, [
|
||||
'config' => [
|
||||
'handler' => $stack,
|
||||
]
|
||||
]);
|
||||
$response = $client->get($url, ['timeout' => 2]);
|
||||
|
||||
if ($response->getStatusCode() === 200) {
|
||||
return json_decode($response->getBody()->getContents(), true);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function post(string $url, array $data = [])
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\JsonRpc;
|
||||
|
||||
interface UserExternalServiceInterface
|
||||
{
|
||||
/**
|
||||
* 通过微信授权新建用户
|
||||
*
|
||||
* @param string $open_id
|
||||
* @return array
|
||||
*/
|
||||
public function wechatNewUser(string $open_id) : array;
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Wechat;
|
||||
|
||||
use App\Exception\BusinessException;
|
||||
use App\Helper\Curl;
|
||||
use App\JsonRpc\UserExternalServiceInterface;
|
||||
use Hyperf\Config\Annotation\Value;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class AuthService
|
||||
{
|
||||
#[Value("app.wechat_punch_card_appid")]
|
||||
protected string $appid;
|
||||
|
||||
#[Value("app.wechat_punch_card_secret")]
|
||||
protected string $secret;
|
||||
|
||||
#[Inject]
|
||||
protected UserExternalServiceInterface $userExternalService;
|
||||
|
||||
protected string $wechat_auth_url = 'https://api.weixin.qq.com/sns/jscode2session';
|
||||
|
||||
/**
|
||||
* 通过微信授权新建用户
|
||||
*
|
||||
* @param string $code
|
||||
* @return array
|
||||
*/
|
||||
public function codeToOpenID(string $code) : array
|
||||
{
|
||||
$url = $this->wechat_auth_url . '?' . http_build_query([
|
||||
'appid' => $this->appid,
|
||||
'secret' => $this->secret,
|
||||
'js_code' => $code,
|
||||
'grant_type' => 'authorization_code'
|
||||
]);
|
||||
|
||||
$result = Curl::get($url);
|
||||
$res = $this->userExternalService->wechatNewUser($result['data']['openid']);
|
||||
|
||||
if (!$res['code'] !== 200) {
|
||||
throw new BusinessException($res['code'], $res['msg']);
|
||||
}
|
||||
|
||||
return ['openid' => $result['data']['openid']];
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
return [
|
||||
'wechat_punch_card_appid' => 'wx73b7c1bc1e41f7ee',
|
||||
'wechat_punch_card_secret' => 'd4edc723b0f268dd5156096d3a293a29',
|
||||
];
|
Loading…
Reference in New Issue