16 changed files with 291 additions and 5 deletions
-
12app/Constants/v3/ErrorCode.php
-
8app/Controller/v3/CategoryController.php
-
36app/Controller/v3/LoginController.php
-
32app/Controller/v3/UserController.php
-
12app/Model/v3/User.php
-
45app/Request/v3/UserUpdateRequest.php
-
38app/Request/v3/WxLoginRequest.php
-
2app/Service/MiniprogramService.php
-
2app/Service/v3/Implementations/CategoryService.php
-
40app/Service/v3/Implementations/UserInfoService.php
-
43app/Service/v3/Implementations/WxLoginService.php
-
10app/Service/v3/Interfaces/UserInfoServiceInterface.php
-
10app/Service/v3/Interfaces/WxLoginServiceInterface.php
-
2config/autoload/dependencies.php
-
2config/config.php
-
2config/routes.php
@ -0,0 +1,36 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Controller\v3; |
||||
|
|
||||
|
use App\Controller\BaseController; |
||||
|
use App\Request\v3\WxLoginRequest; |
||||
|
use App\Service\v3\Interfaces\WxLoginServiceInterface; |
||||
|
use Hyperf\Di\Annotation\Inject; |
||||
|
|
||||
|
/** |
||||
|
* 登录控制器 |
||||
|
* Class LoginController |
||||
|
* @package App\Controller\v3 |
||||
|
*/ |
||||
|
class LoginController extends BaseController |
||||
|
{ |
||||
|
/** |
||||
|
* @Inject |
||||
|
* @var WxLoginServiceInterface |
||||
|
*/ |
||||
|
protected $wxLoginService; |
||||
|
|
||||
|
/** |
||||
|
* 用户微信登录 |
||||
|
* 1、前端上传code |
||||
|
* 2、后端处理登录获取信息并生成或更新用户登录然后返回用户信息 |
||||
|
* @param WxLoginRequest $request |
||||
|
*/ |
||||
|
public function wxLogin(WxLoginRequest $request) |
||||
|
{ |
||||
|
$code = $request->validated()['code']; |
||||
|
$result = $this->wxLoginService->do($code); |
||||
|
|
||||
|
return $this->success(['user' => $result]); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,45 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
|
||||
|
namespace App\Request\v3; |
||||
|
|
||||
|
use App\Request\BaseFormRequest; |
||||
|
|
||||
|
class UserUpdateRequest extends BaseFormRequest |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* Get the validation rules that apply to the request. |
||||
|
*/ |
||||
|
public function rules(): array |
||||
|
{ |
||||
|
return [ |
||||
|
'user_id' => 'required|nonempty', |
||||
|
'nick_name' => 'required|nonempty', |
||||
|
'avatar' => 'required|nonempty', |
||||
|
'openid' => 'required|nonempty', |
||||
|
'unionid' => 'required|nonempty', |
||||
|
'country' => 'string', |
||||
|
'province' => 'string', |
||||
|
'city' => 'string', |
||||
|
'gender' => 'string', |
||||
|
'language' => 'string', |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @return array |
||||
|
*/ |
||||
|
public function messages(): array |
||||
|
{ |
||||
|
return [ |
||||
|
'*.*' => ':attribute无效', |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
public function attributes(): array |
||||
|
{ |
||||
|
return parent::attributes(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,38 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
|
||||
|
namespace App\Request\v3; |
||||
|
|
||||
|
use App\Request\BaseFormRequest; |
||||
|
|
||||
|
class WxLoginRequest extends BaseFormRequest |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* Get the validation rules that apply to the request. |
||||
|
*/ |
||||
|
public function rules(): array |
||||
|
{ |
||||
|
return [ |
||||
|
'code' => 'required|nonempty', |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @return array |
||||
|
*/ |
||||
|
public function messages(): array |
||||
|
{ |
||||
|
return [ |
||||
|
'code.*' => ':attribute无效', |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
public function attributes(): array |
||||
|
{ |
||||
|
return [ |
||||
|
'code' => 'CODE', |
||||
|
]; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,40 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Service\v3\Implementations; |
||||
|
|
||||
|
use App\Constants\v3\ErrorCode; |
||||
|
use App\Exception\ErrorCodeException; |
||||
|
use App\Model\v3\User; |
||||
|
use App\Service\v3\Interfaces\UserInfoServiceInterface; |
||||
|
|
||||
|
class UserInfoService implements UserInfoServiceInterface |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* 更新 |
||||
|
* @param $userId |
||||
|
* @param $data |
||||
|
* @return int |
||||
|
*/ |
||||
|
public function do($userId, $data) |
||||
|
{ |
||||
|
$user = User::query()->find($userId); |
||||
|
$res = $user->fill($data)->save(); |
||||
|
|
||||
|
if (!$res) { |
||||
|
throw new ErrorCodeException(ErrorCode::USER_INFO_UPDATE_ERROR); |
||||
|
} |
||||
|
|
||||
|
return $res; |
||||
|
} |
||||
|
|
||||
|
public function check($userId) |
||||
|
{ |
||||
|
// TODO: Implement check() method.
|
||||
|
} |
||||
|
|
||||
|
public function undo($userId) |
||||
|
{ |
||||
|
// TODO: Implement undo() method.
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,43 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Service\v3\Implementations; |
||||
|
|
||||
|
use App\Constants\v3\ErrorCode; |
||||
|
use App\Exception\ErrorCodeException; |
||||
|
use App\Model\v3\User; |
||||
|
use EasyWeChat\Factory; |
||||
|
use Hyperf\Guzzle\CoroutineHandler; |
||||
|
|
||||
|
class WxLoginService implements \App\Service\v3\Interfaces\WxLoginServiceInterface |
||||
|
{ |
||||
|
|
||||
|
public function do($code) |
||||
|
{ |
||||
|
|
||||
|
// 微信登录
|
||||
|
$config = config('applet'); |
||||
|
$app = Factory::miniProgram($config); |
||||
|
$app['guzzle_handler'] = CoroutineHandler::class; |
||||
|
$result = $app->auth->session($code); |
||||
|
|
||||
|
if (isset($result['errcode'])&&$result['errcode'] != 0) { |
||||
|
throw new ErrorCodeException(ErrorCode::WXLOGIN_INVALID_CODE); |
||||
|
} |
||||
|
|
||||
|
// 更新或者插入用户数据
|
||||
|
return User::query()->firstOrCreate( |
||||
|
['openid' => $result['openid']], |
||||
|
['unionid' => $result['unionid']] |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
public function check($userId) |
||||
|
{ |
||||
|
// TODO: Implement check() method.
|
||||
|
} |
||||
|
|
||||
|
public function undo($userId) |
||||
|
{ |
||||
|
// TODO: Implement undo() method.
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Service\v3\Interfaces; |
||||
|
|
||||
|
interface UserInfoServiceInterface |
||||
|
{ |
||||
|
public function do($userId, $data); |
||||
|
public function check($userId); |
||||
|
public function undo($userId); |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Service\v3\Interfaces; |
||||
|
|
||||
|
interface WxLoginServiceInterface |
||||
|
{ |
||||
|
public function do($code); |
||||
|
public function check($userId); |
||||
|
public function undo($userId); |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue