diff --git a/app/Constants/v3/ErrorCode.php b/app/Constants/v3/ErrorCode.php index 87d03d1..40490b9 100644 --- a/app/Constants/v3/ErrorCode.php +++ b/app/Constants/v3/ErrorCode.php @@ -55,6 +55,17 @@ class ErrorCode extends AbstractConstants */ const NOT_BIND_TEL_ERROR = 704; + /** + * 微信登录失败:无效的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\CategoryService::class, \App\Service\v3\Interfaces\UpdateShopCartServiceInterface::class => \App\Service\v3\Implementations\UpdateShopCartService::class, \App\Service\v3\Interfaces\ShopCartServiceInterface::class => \App\Service\v3\Implementations\ShopCartService::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 4c76818..014d72a 100644 --- a/config/routes.php +++ b/config/routes.php @@ -86,10 +86,12 @@ Router::addGroup('/v3/', function () { Router::post('category/all', 'App\Controller\v3\CategoryController@all'); Router::post('updateShopCart/update', 'App\Controller\v3\UpdateShopCartController@update'); Router::post('shopCart/detail', 'App\Controller\v3\ShopCartController@detail'); + 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