diff --git a/app/Controller/CommunityController.php b/app/Controller/CommunityController.php new file mode 100644 index 0000000..0505272 --- /dev/null +++ b/app/Controller/CommunityController.php @@ -0,0 +1,26 @@ +communityService->bind($request->validated()); + return $this->success(['id' => $res->id]); + } + +} diff --git a/app/Listener/ValidatorFactoryResolvedListener.php b/app/Listener/ValidatorFactoryResolvedListener.php index 75b3a36..7423594 100644 --- a/app/Listener/ValidatorFactoryResolvedListener.php +++ b/app/Listener/ValidatorFactoryResolvedListener.php @@ -4,6 +4,7 @@ namespace App\Listener; use Hyperf\DbConnection\Db; use Hyperf\Event\Contract\ListenerInterface; +use Hyperf\HttpMessage\Upload\UploadedFile; use Hyperf\Validation\Contract\ValidatorFactoryInterface; use Hyperf\Validation\Event\ValidatorFactoryResolved; @@ -48,6 +49,38 @@ class ValidatorFactoryResolvedListener implements ListenerInterface return $message; }); + // 注册了 exists_enable 验证器规则,参数是table,field,where1,where2... + $validatorFactory->extend('exists_enable', function ($attribute, $value, $parameters, $validator) { + + // 查询 + $builder = Db::table($parameters[0])->where($parameters[1], '=', $value); + + $whereArr = array_slice($parameters,2); + if (!empty($whereArr)) { + foreach ($whereArr as $key => $where) { + $builder->whereRaw($where); + } + } + + return $builder->exists(); + }); + + // 注册了 not_equal 验证器规则,参数是anotherfield,table,primary_field,foreign_field + // 要排除对比的字段请求参数名、表名、本参数对应表id、比较字段 + $validatorFactory->extend('not_equal', function ($attribute, $value, $parameters, $validator) { + + // 获取比较字段参数值 + $foreignValue = $validator->getData()[$parameters[0]]; + + // 查询 + $builder = Db::table($parameters[1]) + ->where($parameters[2], '=', $value) + ->where($parameters[3], '!=', $foreignValue) + ; + + return $builder->exists(); + }); + // 注册了 base64 验证器规则 $validatorFactory->extend('base64', function ($attribute, $value, $parameters, $validator) { @@ -78,36 +111,40 @@ class ValidatorFactoryResolvedListener implements ListenerInterface }); - // 注册了 exists_enable 验证器规则,参数是table,field,where1,where2... - $validatorFactory->extend('exists_enable', function ($attribute, $value, $parameters, $validator) { - - // 查询 - $builder = Db::table($parameters[0])->where($parameters[1], '=', $value); + // 注册了 file_different_if_file 验证器规则 + // file_different_if_file:field 两文件字段不能是同一个文件 + $validatorFactory->extend('file_different_if_file', function ($attribute, $value, $parameters, $validator) { - $whereArr = array_slice($parameters,2); - if (!empty($whereArr)) { - foreach ($whereArr as $key => $where) { - $builder->whereRaw($where); - } + // 获取比较字段参数值 + $anotherFile = $validator->getData()[$parameters[0]]; + if (!($value instanceof UploadedFile && $anotherFile instanceof UploadedFile)) { + return true; } + return md5_file($value->getRealPath()) !== md5_file($anotherFile->getRealPath()); - return $builder->exists(); }); - // 注册了 not_equal 验证器规则,参数是anotherfield,table,primary_field,foreign_field - // 要排除对比的字段请求参数名、表名、本参数对应表id、比较字段 - $validatorFactory->extend('not_equal', function ($attribute, $value, $parameters, $validator) { + // 注册了 tel 验证器规则 + // 手机号码验证器 + $validatorFactory->extend('tel', function ($attribute, $value, $parameters, $validator) { + return boolval(preg_match('/^1[3-9]\d{9}$/', $value)); + }); - // 获取比较字段参数值 - $foreignValue = $validator->getData()[$parameters[0]]; + // 注册了 image_if_file 验证器规则 + // 如果是文件就必须是图片类型的 + $validatorFactory->extend('image_if_file', function ($attribute, $value, $parameters, $validator) { - // 查询 - $builder = Db::table($parameters[1]) - ->where($parameters[2], '=', $value) - ->where($parameters[3], '!=', $foreignValue) - ; + if (!($value instanceof UploadedFile)) { + return true; + } + + $rules = $validator->getRules(); + $rules[$attribute] = ['image']; + $validator->setRules($rules); + + return $validator->passes(); - return $builder->exists(); }); + } } \ No newline at end of file diff --git a/app/Model/UserRelationBind.php b/app/Model/UserRelationBind.php new file mode 100644 index 0000000..f14f029 --- /dev/null +++ b/app/Model/UserRelationBind.php @@ -0,0 +1,34 @@ +attributes['json_data'] = $value ? json_encode(json_decode($value, true)) : ''; + } + +} \ No newline at end of file diff --git a/app/Request/CommunityBindRequest.php b/app/Request/CommunityBindRequest.php new file mode 100644 index 0000000..e207452 --- /dev/null +++ b/app/Request/CommunityBindRequest.php @@ -0,0 +1,46 @@ + 'required|nonempty', + 'source_id' => 'required|nonempty', + 'user_id' => 'required|nonempty|exists_enable:ims_cjdc_user,id', + 'json_data' => 'json', + ]; + } + + public function messages(): array + { + return [ + '*.nonempty' => ':attribute参数异常', + 'user_id.exists_enable' => '用户不存在', + ]; + } + + public function attributes(): array + { + return [ + + ]; + } +} diff --git a/app/Service/CommunityService.php b/app/Service/CommunityService.php new file mode 100644 index 0000000..1b5aa2e --- /dev/null +++ b/app/Service/CommunityService.php @@ -0,0 +1,30 @@ +isBinded($data['bind_type'], $data['source_id'], $data['user_id'])) { + return $res; + } + return UserRelationBind::query()->updateOrCreate( + ['bind_type' => $data['bind_type'], 'source_id' => $data['source_id'], 'user_id' => $data['user_id']], + ['json_data' => $data['json_data']] + ); + } + + public function isBinded($bind_type, $source_id, $user_id) + { + return UserRelationBind::query() + ->select('id') + ->where(['bind_type' => $bind_type, 'source_id' => $source_id, 'user_id' => $user_id]) + ->first(); + } +} \ No newline at end of file diff --git a/app/Service/CommunityServiceInterface.php b/app/Service/CommunityServiceInterface.php new file mode 100644 index 0000000..de7f29d --- /dev/null +++ b/app/Service/CommunityServiceInterface.php @@ -0,0 +1,9 @@ + \App\Service\FeiePrintService::class, \App\Service\MiniprogramServiceInterface::class => \App\Service\MiniprogramService::class, \App\Service\UserServiceInterface::class => \App\Service\UserService::class, + \App\Service\CommunityServiceInterface::class => \App\Service\CommunityService::class, ]; diff --git a/config/routes.php b/config/routes.php index b68b0e4..3146d21 100644 --- a/config/routes.php +++ b/config/routes.php @@ -54,6 +54,7 @@ Router::addGroup('/v1/',function (){ //小程序支付相关 Router::post('wxminipay/online', 'App\Controller\PaymentController@wxminiPayOnline'); Router::post('wxminipay/offline', 'App\Controller\PaymentController@wxminiPayOffline'); + Router::post('Community/bind', 'App\Controller\CommunityController@bind'); },['middleware' => [\App\Middleware\Auth\ApiMiddleware::class]]); Router::addGroup('/wechat/',function () {