feat : wx login
This commit is contained in:
@@ -10,6 +10,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Service\ServiceTrait\Api;
|
||||
|
||||
use App\Cache\Redis\Api\ApiRedisKey;
|
||||
use App\Cache\Redis\Common\CommonRedisKey;
|
||||
use App\Cache\Redis\RedisCache;
|
||||
use App\Exception\ErrException;
|
||||
use App\Lib\Log;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
@@ -35,19 +38,90 @@ trait WxMiniTrait
|
||||
#[Inject]
|
||||
protected Log $log;
|
||||
|
||||
/**
|
||||
* 注入缓存类
|
||||
* @var RedisCache
|
||||
*/
|
||||
#[Inject]
|
||||
protected RedisCache $redisCache;
|
||||
|
||||
/**
|
||||
* baseUri
|
||||
* @var string
|
||||
*/
|
||||
protected string $BaseUri = 'https://api.weixin.qq.com';
|
||||
|
||||
private string $appId = '';
|
||||
private string $appSecret = '';
|
||||
public function __construct()
|
||||
{
|
||||
$this->appId = config('system.wx_appid');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return false|mixed|\Redis|string
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
protected function getAccessTokenCache(): mixed
|
||||
{
|
||||
$key = CommonRedisKey::getWxAccessToken();
|
||||
|
||||
if ($this->redisCache->exists($key)) {
|
||||
return $this->redisCache->get($key) ?? '';
|
||||
}
|
||||
|
||||
$data = $this->getAccessToken();
|
||||
|
||||
if (empty($data) || empty($data['access_token']) || empty($data['expires_in'])) return '';
|
||||
|
||||
$this->redisCache->set($key, $data['access_token'], $data['expires_in'] - 10);
|
||||
|
||||
return $data['access_token'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 access Token
|
||||
* @return mixed
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
private function getAccessToken(): mixed
|
||||
{
|
||||
$url = sprintf(
|
||||
'/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s',
|
||||
config('system.wx_appid'),
|
||||
config('system.wx_secret')
|
||||
);
|
||||
|
||||
try {
|
||||
$tencentResponse = $this->clientFactory->create([
|
||||
'base_uri' => $this->BaseUri,
|
||||
'timeout' => 5
|
||||
])->get($url);
|
||||
|
||||
$contents = $tencentResponse->getBody()->getContents();
|
||||
|
||||
$this->log->callbackLog(__class__.':微信服务器返回getAccessToken信息:'.json_encode($contents));
|
||||
|
||||
$res = json_decode($contents,true);
|
||||
|
||||
if (!empty($res['errcode']) && $res['errcode'] != 0) throw new ErrException($res['errmsg'] ?? '系统繁忙');
|
||||
|
||||
return $res;
|
||||
}catch (GuzzleException $e) {
|
||||
$this->log->debug(__CLASS__.':debug:'.$e->getMessage());
|
||||
throw new ErrException($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $code
|
||||
* @return mixed
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function jsCodeGetOpenId(string $code): mixed
|
||||
protected function jsCodeGetOpenId(string $code): mixed
|
||||
{
|
||||
$url = sprintf(
|
||||
'/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code',
|
||||
@@ -77,8 +151,46 @@ trait WxMiniTrait
|
||||
}
|
||||
}
|
||||
|
||||
public function jsCodeGetPhoneNumber(string $code): mixed
|
||||
/**
|
||||
* @param string $code
|
||||
* @return mixed
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
protected function jsCodeGetPhoneNumber(string $code): mixed
|
||||
{
|
||||
$url = sprintf(
|
||||
'/wxa/business/getuserphonenumber?access_token=%s',
|
||||
$this->getAccessTokenCache()
|
||||
);
|
||||
|
||||
$params = [
|
||||
'code' => $code,
|
||||
];
|
||||
|
||||
try {
|
||||
$wxResponse = $this->clientFactory->create([
|
||||
'base_uri' => $this->BaseUri,
|
||||
'timeout' => 5
|
||||
])->post($url,[
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json'
|
||||
],
|
||||
'body' => json_encode($params)
|
||||
]);
|
||||
|
||||
$contents = $wxResponse->getBody()->getContents();
|
||||
|
||||
$this->log->callbackLog(__class__.':微信服务器返回get_phone_number信息:'.json_encode($contents));
|
||||
|
||||
$res = json_decode($contents,true);
|
||||
|
||||
if (!empty($res['errcode']) && $res['errcode'] != 0) throw new ErrException($res['errmsg'] ?? '系统繁忙');
|
||||
|
||||
return $res;
|
||||
}catch (GuzzleException $e){
|
||||
$this->log->debug(__CLASS__.':debug:'.$e->getMessage());
|
||||
throw new ErrException($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user