feat : wx login
This commit is contained in:
@@ -39,4 +39,13 @@ class CommonRedisKey
|
|||||||
{
|
{
|
||||||
return '__system:activate:site:list';
|
return '__system:activate:site:list';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取微信access_token
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function getWxAccessToken(): string
|
||||||
|
{
|
||||||
|
return '__system:wx:AccessToken:';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace App\Controller\Api;
|
namespace App\Controller\Api;
|
||||||
|
|
||||||
use App\Middleware\Api\JwtAuthMiddleware;
|
use App\Middleware\Api\JwtAuthMiddleware;
|
||||||
|
use App\Request\Api\UserRequest;
|
||||||
use App\Service\Api\User\BindPhoneByWxService;
|
use App\Service\Api\User\BindPhoneByWxService;
|
||||||
use Hyperf\HttpServer\Annotation\Controller;
|
use Hyperf\HttpServer\Annotation\Controller;
|
||||||
use Hyperf\HttpServer\Annotation\Middlewares;
|
use Hyperf\HttpServer\Annotation\Middlewares;
|
||||||
@@ -19,7 +20,7 @@ class UserController
|
|||||||
{
|
{
|
||||||
#[RequestMapping(path: 'bind_phone/wx_code',methods: 'post')]
|
#[RequestMapping(path: 'bind_phone/wx_code',methods: 'post')]
|
||||||
#[Scene(scene: 'bind_phone_wx')]
|
#[Scene(scene: 'bind_phone_wx')]
|
||||||
public function bind_phone_by_wx_code()
|
public function bind_phone_by_wx_code(UserRequest $request)
|
||||||
{
|
{
|
||||||
return (new BindPhoneByWxService)->handle();
|
return (new BindPhoneByWxService)->handle();
|
||||||
}
|
}
|
||||||
|
|||||||
28
app/Request/Api/UserRequest.php
Normal file
28
app/Request/Api/UserRequest.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Request\Api;
|
||||||
|
|
||||||
|
use Hyperf\Validation\Request\FormRequest;
|
||||||
|
|
||||||
|
class UserRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'code' => 'required|string',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,16 +14,31 @@ use App\Exception\ErrException;
|
|||||||
use App\Service\Api\BaseService;
|
use App\Service\Api\BaseService;
|
||||||
use App\Service\ServiceTrait\Api\GetUserInfoTrait;
|
use App\Service\ServiceTrait\Api\GetUserInfoTrait;
|
||||||
use App\Service\ServiceTrait\Api\WxMiniTrait;
|
use App\Service\ServiceTrait\Api\WxMiniTrait;
|
||||||
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
|
|
||||||
class BindPhoneByWxService extends BaseService
|
class BindPhoneByWxService extends BaseService
|
||||||
{
|
{
|
||||||
use GetUserInfoTrait,WxMiniTrait;
|
use GetUserInfoTrait,
|
||||||
|
WxMiniTrait;
|
||||||
|
|
||||||
public function handle()
|
/**
|
||||||
|
* @return array
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
|
public function handle(): array
|
||||||
{
|
{
|
||||||
$this->checkBindPhone($this->userId);
|
$this->checkBindPhone($this->userId);
|
||||||
|
|
||||||
|
$wxPhone = $this->jsCodeGetPhoneNumber($this->request->input('code'));
|
||||||
|
|
||||||
|
if (empty($wxPhone['phone_info']['purePhoneNumber'])) throw new ErrException('微信手机号查询失败,请联系人工客服处理');
|
||||||
|
|
||||||
|
$userInfo = $this->getUserInfo($this->userId);
|
||||||
|
|
||||||
|
$userInfo->phone = $wxPhone['phone_info']['purePhoneNumber'];
|
||||||
|
if (!$userInfo->save()) throw new ErrException('绑定手机号失败,请联系人工客服处理');
|
||||||
|
|
||||||
return $this->return->success();
|
return $this->return->success();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,9 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Service\ServiceTrait\Api;
|
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\Exception\ErrException;
|
||||||
use App\Lib\Log;
|
use App\Lib\Log;
|
||||||
use GuzzleHttp\Exception\GuzzleException;
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
@@ -35,19 +38,90 @@ trait WxMiniTrait
|
|||||||
#[Inject]
|
#[Inject]
|
||||||
protected Log $log;
|
protected Log $log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注入缓存类
|
||||||
|
* @var RedisCache
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected RedisCache $redisCache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* baseUri
|
* baseUri
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected string $BaseUri = 'https://api.weixin.qq.com';
|
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
|
* @param string $code
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws ContainerExceptionInterface
|
* @throws ContainerExceptionInterface
|
||||||
* @throws NotFoundExceptionInterface
|
* @throws NotFoundExceptionInterface
|
||||||
*/
|
*/
|
||||||
public function jsCodeGetOpenId(string $code): mixed
|
protected function jsCodeGetOpenId(string $code): mixed
|
||||||
{
|
{
|
||||||
$url = sprintf(
|
$url = sprintf(
|
||||||
'/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code',
|
'/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