From 6e29d99bad9293bedd6bde348afd2fc6723602f9 Mon Sep 17 00:00:00 2001 From: weigang Date: Mon, 31 Aug 2020 20:44:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E6=8E=88=E6=9D=83=E7=99=BB?= =?UTF-8?q?=E5=BD=95=EF=BC=8C=E6=9B=B4=E6=96=B0=E7=94=A8=E6=88=B7=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Constants/v3/ErrorCode.php | 12 +++++ app/Controller/v3/CategoryController.php | 8 +++- app/Controller/v3/LoginController.php | 36 +++++++++++++++ app/Controller/v3/UserController.php | 32 ++++++++++++- app/Model/v3/User.php | 12 +++++ app/Request/v3/UserUpdateRequest.php | 45 +++++++++++++++++++ app/Request/v3/WxLoginRequest.php | 38 ++++++++++++++++ app/Service/MiniprogramService.php | 2 +- .../v3/Implementations/CategoryService.php | 2 + .../v3/Implementations/UserInfoService.php | 40 +++++++++++++++++ .../v3/Implementations/WxLoginService.php | 43 ++++++++++++++++++ .../Interfaces/UserInfoServiceInterface.php | 10 +++++ .../v3/Interfaces/WxLoginServiceInterface.php | 10 +++++ config/autoload/dependencies.php | 2 + config/config.php | 2 +- config/routes.php | 2 + 16 files changed, 291 insertions(+), 5 deletions(-) create mode 100644 app/Controller/v3/LoginController.php create mode 100644 app/Request/v3/UserUpdateRequest.php create mode 100644 app/Request/v3/WxLoginRequest.php create mode 100644 app/Service/v3/Implementations/UserInfoService.php create mode 100644 app/Service/v3/Implementations/WxLoginService.php create mode 100644 app/Service/v3/Interfaces/UserInfoServiceInterface.php create mode 100644 app/Service/v3/Interfaces/WxLoginServiceInterface.php diff --git a/app/Constants/v3/ErrorCode.php b/app/Constants/v3/ErrorCode.php index b49c2ea..e203d2d 100644 --- a/app/Constants/v3/ErrorCode.php +++ b/app/Constants/v3/ErrorCode.php @@ -48,6 +48,18 @@ class ErrorCode extends AbstractConstants */ const UNBIND_TEL_ERROR = 703; + /** + * 微信登录失败:无效的code + * @Message("微信登录失败:无效的code") + */ + const WXLOGIN_INVALID_CODE = 704; + + /** + * 更新失败 + * @Message("更新失败") + */ + const USER_INFO_UPDATE_ERROR = 705; + /************************************/ /* 定位相关 751-800 */ /************************************/ diff --git a/app/Controller/v3/CategoryController.php b/app/Controller/v3/CategoryController.php index 78aeb16..42ef214 100644 --- a/app/Controller/v3/CategoryController.php +++ b/app/Controller/v3/CategoryController.php @@ -14,8 +14,14 @@ class CategoryController extends BaseController */ protected $categoryService; + /** + * 获取分类页所有分类 + * 1、无参数 + * 2、远程关联获取所有分类信息 + * @return \Psr\Http\Message\ResponseInterface + */ public function all() { - return $this->success($this->categoryService->all()); + return $this->success(['category' => $this->categoryService->all()]); } } \ No newline at end of file diff --git a/app/Controller/v3/LoginController.php b/app/Controller/v3/LoginController.php new file mode 100644 index 0000000..f4b5d83 --- /dev/null +++ b/app/Controller/v3/LoginController.php @@ -0,0 +1,36 @@ +validated()['code']; + $result = $this->wxLoginService->do($code); + + return $this->success(['user' => $result]); + } +} \ No newline at end of file diff --git a/app/Controller/v3/UserController.php b/app/Controller/v3/UserController.php index 122680d..eb007aa 100644 --- a/app/Controller/v3/UserController.php +++ b/app/Controller/v3/UserController.php @@ -2,10 +2,11 @@ namespace App\Controller\v3; -use App\Constants\v3\ErrorCode; use App\Controller\BaseController; use App\Request\v3\UserBindTelRequest; +use App\Request\v3\UserUpdateRequest; use App\Service\v3\Interfaces\UserBindTelServiceInterface; +use App\Service\v3\Interfaces\UserInfoServiceInterface; use App\Service\v3\Interfaces\VerifyCodeServiceInterface; use Hyperf\Di\Annotation\Inject; @@ -29,9 +30,22 @@ class UserController extends BaseController */ protected $userBindTelService; + /** + * @Inject + * @var UserInfoServiceInterface + */ + protected $userInfoService; + + /** + * 用户绑定手机号 + * 1、获取参数,tel、user_id、verify_code + * 2、校验用户和验证码 + * 3、执行绑定 + * @param UserBindTelRequest $request + * @return \Psr\Http\Message\ResponseInterface + */ public function bindTel(UserBindTelRequest $request) { - // 获取参数 $params = $request->validated(); // 校验验证码 @@ -41,4 +55,18 @@ class UserController extends BaseController return $this->success([]); } + + /** + * 更新用户信息,上传保存unionid等 + * 1、wxlogin成功后,会有user_id,传上来,同时传一些其他的参数 + * 2、更新用户数据,头像、昵称、unionid、国家、省份、城市、性别、语言等 + */ + public function updateInfo(UserUpdateRequest $request) + { + $data = $request->validated(); + $userId = $data['user_id']; + unset($data['user_id']); + $this->userInfoService->do($userId, $data); + return $this->success([]); + } } \ No newline at end of file diff --git a/app/Model/v3/User.php b/app/Model/v3/User.php index af37927..67021d3 100644 --- a/app/Model/v3/User.php +++ b/app/Model/v3/User.php @@ -7,4 +7,16 @@ use App\Model\Model; class User extends Model { protected $table = 'lanzu_user'; + + protected $fillable = [ + 'nick_name', + 'avatar', + 'openid', + 'unionid', + 'country', + 'province', + 'city', + 'gender', + 'language', + ]; } \ No newline at end of file diff --git a/app/Request/v3/UserUpdateRequest.php b/app/Request/v3/UserUpdateRequest.php new file mode 100644 index 0000000..b22072e --- /dev/null +++ b/app/Request/v3/UserUpdateRequest.php @@ -0,0 +1,45 @@ + '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(); + } +} diff --git a/app/Request/v3/WxLoginRequest.php b/app/Request/v3/WxLoginRequest.php new file mode 100644 index 0000000..3ada66d --- /dev/null +++ b/app/Request/v3/WxLoginRequest.php @@ -0,0 +1,38 @@ + 'required|nonempty', + ]; + } + + /** + * @return array + */ + public function messages(): array + { + return [ + 'code.*' => ':attribute无效', + ]; + } + + public function attributes(): array + { + return [ + 'code' => 'CODE', + ]; + } +} diff --git a/app/Service/MiniprogramService.php b/app/Service/MiniprogramService.php index c74c0ac..0be9576 100644 --- a/app/Service/MiniprogramService.php +++ b/app/Service/MiniprogramService.php @@ -205,7 +205,7 @@ class MiniprogramService implements MiniprogramServiceInterface $template['mp_template_msg']['data']['remark']['value'] = $data['remark']; } - $app = Factory::miniProgram(config('wxtempmsg')); + $app = Factory::miniProgram(config('applet')); $app['guzzle_handler'] = CoroutineHandler::class; $app->uniform_message->send($template); } diff --git a/app/Service/v3/Implementations/CategoryService.php b/app/Service/v3/Implementations/CategoryService.php index c85ecbf..f171bc9 100644 --- a/app/Service/v3/Implementations/CategoryService.php +++ b/app/Service/v3/Implementations/CategoryService.php @@ -27,6 +27,8 @@ class CategoryService implements CategoryServiceInterface { return StoreType::query() ->with('goodsTypes') + ->orderBy('sort', 'DESC') + ->orderBy('id', 'DESC') ->get() ->toArray(); } diff --git a/app/Service/v3/Implementations/UserInfoService.php b/app/Service/v3/Implementations/UserInfoService.php new file mode 100644 index 0000000..b3753d7 --- /dev/null +++ b/app/Service/v3/Implementations/UserInfoService.php @@ -0,0 +1,40 @@ +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. + } +} \ No newline at end of file diff --git a/app/Service/v3/Implementations/WxLoginService.php b/app/Service/v3/Implementations/WxLoginService.php new file mode 100644 index 0000000..d59f19d --- /dev/null +++ b/app/Service/v3/Implementations/WxLoginService.php @@ -0,0 +1,43 @@ +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. + } +} \ No newline at end of file diff --git a/app/Service/v3/Interfaces/UserInfoServiceInterface.php b/app/Service/v3/Interfaces/UserInfoServiceInterface.php new file mode 100644 index 0000000..f2f1ae1 --- /dev/null +++ b/app/Service/v3/Interfaces/UserInfoServiceInterface.php @@ -0,0 +1,10 @@ + \App\Service\v3\Implementations\UserBindTelService::class, \App\Service\v3\Interfaces\OrderServiceInterface::class => \App\Service\v3\Implementations\OrderService::class, \App\Service\v3\Interfaces\CategoryServiceInterface::class => \App\Service\v3\Implementations\CategoryService::class, + \App\Service\v3\Interfaces\WxLoginServiceInterface::class => \App\Service\v3\Implementations\WxLoginService::class, + \App\Service\v3\Interfaces\UserInfoServiceInterface::class => \App\Service\v3\Implementations\UserInfoService::class, ]; diff --git a/config/config.php b/config/config.php index d731652..ad49d28 100644 --- a/config/config.php +++ b/config/config.php @@ -37,7 +37,7 @@ return [ 'key_path' => env('KEY_PATH',''), 'notify_url' => env('NOTIFY_URL',''), ], - 'wxtempmsg' => [ + 'applet' => [ 'app_id' => env('APP_ID',''), 'secret' => env('APP_SECRET',''), ], diff --git a/config/routes.php b/config/routes.php index 598bcad..60f2fe7 100644 --- a/config/routes.php +++ b/config/routes.php @@ -83,10 +83,12 @@ Router::addGroup('/v3/', function () { Router::post('goodsRecommend/getByTabsForAppletIndex', 'App\Controller\v3\GoodsRecommendController@getByTabsForAppletIndex'); Router::post('Order/detail', 'App\Controller\v3\OrderController@detail'); Router::post('category/all', 'App\Controller\v3\CategoryController@all'); + Router::post('login/wxLogin', 'App\Controller\v3\LoginController@wxLogin'); },['middleware' => [\App\Middleware\Auth\ApiMiddleware::class]]); // 需要登录的路由 Router::addGroup('/v3/', function () { Router::post('sms/getVerifyCode', 'App\Controller\v3\SmsController@getVerifyCode'); Router::post('user/bindTel', 'App\Controller\v3\UserController@bindTel'); + Router::post('user/updateInfo', 'App\Controller\v3\UserController@updateInfo'); },['middleware' => [\App\Middleware\Auth\ApiMiddleware::class, \App\Middleware\Auth\UserMiddleware::class]]); \ No newline at end of file