处理网关的考勤系统功能路由前缀,及完善请求处理公共类的逻辑

develop
fantasticbin 2 years ago
parent 936eb10eec
commit c5f301b9ad

@ -2,7 +2,7 @@
declare(strict_types=1); declare(strict_types=1);
namespace App\Controller\Wechat; namespace App\Controller\PunchCard\Wechat;
use App\Controller\BaseController; use App\Controller\BaseController;
use App\Exception\BusinessException; use App\Exception\BusinessException;
@ -10,12 +10,12 @@ use app\Constants\Wechat\AuthErrorCode;
use App\Service\Wechat\AuthService; use App\Service\Wechat\AuthService;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping; use Hyperf\HttpServer\Annotation\RequestMapping;
/** /**
* 微信授权处理控制器 * 微信授权处理控制器
*/ */
#[Controller] #[Controller(prefix: "kq")]
class AuthController extends BaseController class AuthController extends BaseController
{ {
#[Inject] #[Inject]
@ -26,7 +26,7 @@ class AuthController extends BaseController
* *
* @return array * @return array
*/ */
#[PostMapping(path: "auth/code2OpenID")] #[RequestMapping(path: "auth/code2OpenID", methods: "get,post")]
public function code2OpenID() : array public function code2OpenID() : array
{ {
$code = $this->request->input('code', ''); $code = $this->request->input('code', '');

@ -7,19 +7,46 @@ namespace App\Helper;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Hyperf\Guzzle\HandlerStackFactory; use Hyperf\Guzzle\HandlerStackFactory;
/**
* 请求处理公共类
*/
class Curl class Curl
{ {
public static function get(string $url) : array /**
* 获取Guzzle客户端实例使用连接池
*
* @return Client
*/
protected static function getClient() : Client
{ {
$factory = new HandlerStackFactory(); $factory = new HandlerStackFactory();
$stack = $factory->create(); $stack = $factory->create();
$client = make(Client::class, [ return make(Client::class, [
'config' => [ 'config' => [
'handler' => $stack, 'handler' => $stack,
] ]
]); ]);
$response = $client->get($url, ['timeout' => 2]); }
/**
* 发送Get请求
*
* @param string $url
* @param array $headers
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public static function get(string $url, array $headers = []) : array
{
$client = self::getClient();
$options = ['timeout' => 2, 'verify' => false];
if (!empty($headers)) {
$options['headers'] = $headers;
}
$response = $client->get($url, $options);
if ($response->getStatusCode() === 200) { if ($response->getStatusCode() === 200) {
return json_decode($response->getBody()->getContents(), true); return json_decode($response->getBody()->getContents(), true);
@ -28,7 +55,30 @@ class Curl
return []; return [];
} }
public static function post(string $url, array $data = []) /**
* 发送POST请求application/x-www-form-urlencoded
*
* @param string $url
* @param array $data
* @param array $headers
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public static function post(string $url, array $data = [], array $headers = []) : array
{ {
$client = self::getClient();
$options = ['timeout' => 2, 'verify' => false, 'form_params' => $data];
if (!empty($headers)) {
$options['headers'] = $headers;
}
$response = $client->post($url, $options);
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody()->getContents(), true);
}
return [];
} }
} }

@ -8,17 +8,40 @@ use App\JsonRpc\UserExternalServiceInterface;
use Hyperf\Config\Annotation\Value; use Hyperf\Config\Annotation\Value;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
/**
* 微信授权处理服务层
*/
class AuthService class AuthService
{ {
/**
* 考勤系统小程序APPID
*
* @var string
*/
#[Value("app.wechat_punch_card_appid")] #[Value("app.wechat_punch_card_appid")]
protected string $appid; protected string $appid;
/**
* 考勤系统小程序APPSecret
*
* @var string
*/
#[Value("app.wechat_punch_card_secret")] #[Value("app.wechat_punch_card_secret")]
protected string $secret; protected string $secret;
/**
* 用户中心对外RPC服务
*
* @var UserExternalServiceInterface
*/
#[Inject] #[Inject]
protected UserExternalServiceInterface $userExternalService; protected UserExternalServiceInterface $userExternalService;
/**
* 微信通过code获取session信息请求地址
*
* @var string
*/
protected string $wechat_auth_url = 'https://api.weixin.qq.com/sns/jscode2session'; protected string $wechat_auth_url = 'https://api.weixin.qq.com/sns/jscode2session';
/** /**

Loading…
Cancel
Save