完善微信授权新建用户逻辑
This commit is contained in:
		
							parent
							
								
									9c8a3b5fac
								
							
						
					
					
						commit
						10c669e002
					
				
							
								
								
									
										17
									
								
								app/Constants/Wechat/AuthErrorCode.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								app/Constants/Wechat/AuthErrorCode.php
									
									
									
									
									
										Normal file
									
								
							| @ -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; | ||||
| } | ||||
							
								
								
									
										40
									
								
								app/Controller/Wechat/AuthController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								app/Controller/Wechat/AuthController.php
									
									
									
									
									
										Normal file
									
								
							| @ -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); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										34
									
								
								app/Helper/Curl.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								app/Helper/Curl.php
									
									
									
									
									
										Normal file
									
								
							| @ -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 = []) | ||||
|     { | ||||
|     } | ||||
| } | ||||
							
								
								
									
										14
									
								
								app/JsonRpc/UserExternalServiceInterface.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								app/JsonRpc/UserExternalServiceInterface.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace App\JsonRpc; | ||||
| 
 | ||||
| interface UserExternalServiceInterface | ||||
| { | ||||
|     /** | ||||
|      * 通过微信授权新建用户 | ||||
|      * | ||||
|      * @param string $open_id | ||||
|      * @return array | ||||
|      */ | ||||
|     public function wechatNewUser(string $open_id) : array; | ||||
| } | ||||
							
								
								
									
										48
									
								
								app/Service/Wechat/AuthService.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								app/Service/Wechat/AuthService.php
									
									
									
									
									
										Normal file
									
								
							| @ -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']]; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										8
									
								
								config/autoload/app.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								config/autoload/app.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,8 @@ | ||||
| <?php | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| return [ | ||||
|     'wechat_punch_card_appid' => 'wx73b7c1bc1e41f7ee', | ||||
|     'wechat_punch_card_secret' => 'd4edc723b0f268dd5156096d3a293a29', | ||||
| ]; | ||||
| @ -15,6 +15,7 @@ use Swoole\Constant; | ||||
| 
 | ||||
| return [ | ||||
|     'mode' => SWOOLE_PROCESS, | ||||
|     'type' => Hyperf\Server\CoroutineServer::class, | ||||
|     'servers' => [ | ||||
|         [ | ||||
|             'name' => 'http', | ||||
|  | ||||
| @ -14,7 +14,54 @@ return [ | ||||
|         'discovery' => true, | ||||
|         'register' => true, | ||||
|     ], | ||||
|     'consumers' => [], | ||||
|     'consumers' => value(function () { | ||||
|         $consumers = []; | ||||
|         // 这里示例自动创建代理消费者类的配置形式,顾存在 name 和 service 两个配置项,这里的做法不是唯一的,仅说明可以通过 PHP 代码来生成配置
 | ||||
|         $services = [ | ||||
|             'UserExternalService' => App\JsonRpc\UserExternalServiceInterface::class, | ||||
|         ]; | ||||
|         foreach ($services as $name => $interface) { | ||||
|             $consumers[] = [ | ||||
|                 'name' => $name, | ||||
|                 'service' => $interface, | ||||
|                 'protocol' => 'jsonrpc-http', | ||||
|                 'registry' => [ | ||||
|                    'protocol' => 'consul', | ||||
|                    'address' => env('CONSUL_URL', '127.0.0.1:8500'), | ||||
|                 ], | ||||
|                 // 配置项,会影响到 Packer 和 Transporter
 | ||||
|                 'options' => [ | ||||
|                     'connect_timeout' => 5.0, | ||||
|                     'recv_timeout' => 5.0, | ||||
|                     'settings' => [ | ||||
|                         // 根据协议不同,区分配置
 | ||||
|                         'open_eof_split' => true, | ||||
|                         'package_eof' => "\r\n", | ||||
|                         // 'open_length_check' => true,
 | ||||
|                         // 'package_length_type' => 'N',
 | ||||
|                         // 'package_length_offset' => 0,
 | ||||
|                         // 'package_body_offset' => 4,
 | ||||
|                     ], | ||||
|                     // 重试次数,默认值为 2,收包超时不进行重试。暂只支持 JsonRpcPoolTransporter
 | ||||
|                     'retry_count' => 2, | ||||
|                     // 重试间隔,毫秒
 | ||||
|                     'retry_interval' => 100, | ||||
|                     // 使用多路复用 RPC 时的心跳间隔,null 为不触发心跳
 | ||||
|                     'heartbeat' => 30, | ||||
|                     // 当使用 JsonRpcPoolTransporter 时会用到以下配置
 | ||||
|                     'pool' => [ | ||||
|                         'min_connections' => 1, | ||||
|                         'max_connections' => 32, | ||||
|                         'connect_timeout' => 10.0, | ||||
|                         'wait_timeout' => 3.0, | ||||
|                         'heartbeat' => -1, | ||||
|                         'max_idle_time' => 60.0, | ||||
|                     ], | ||||
|                 ], | ||||
|             ]; | ||||
|         } | ||||
|         return $consumers; | ||||
|     }), | ||||
|     'providers' => [], | ||||
|     'drivers' => [ | ||||
|         'consul' => [ | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user