From d93fbd78a2eec988d5c63b469471d17ab237c997 Mon Sep 17 00:00:00 2001 From: weigang Date: Mon, 10 Aug 2020 19:09:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A4=BE=E5=8C=BA=E6=89=AB=E7=A0=81=E7=BB=91?= =?UTF-8?q?=E5=AE=9A=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/CommunityController.php | 26 ++++++++ .../ValidatorFactoryResolvedListener.php | 66 +++++++++++++++++++ app/Model/ActivityBind.php | 34 ++++++++++ app/Request/CommunityBindRequest.php | 46 +++++++++++++ app/Service/CommunityService.php | 30 +++++++++ app/Service/CommunityServiceInterface.php | 9 +++ config/autoload/dependencies.php | 3 +- config/routes.php | 1 + 8 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 app/Controller/CommunityController.php create mode 100644 app/Model/ActivityBind.php create mode 100644 app/Request/CommunityBindRequest.php create mode 100644 app/Service/CommunityService.php create mode 100644 app/Service/CommunityServiceInterface.php 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 0bfb047..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; @@ -80,5 +81,70 @@ class ValidatorFactoryResolvedListener implements ListenerInterface return $builder->exists(); }); + // 注册了 base64 验证器规则 + $validatorFactory->extend('base64', function ($attribute, $value, $parameters, $validator) { + + preg_match('/^(data:\s*image\/(\w+);base64,)/', $value, $result); + + if (empty($result)) { + return false; + } + + if ( + in_array('image', $parameters) + && !in_array($result[2], ['jpg','jpeg','png','gif','svg','bmp']) + ) { + return false; + } + + return true; + + }); + + // 注册了 ext_not_in 验证器规则 + $validatorFactory->extend('ext_not_in', function ($attribute, $value, $parameters, $validator) { + + if (empty($parameters)) { + $parameters = ['', 'php', 'exe', 'sql', 'sh', 'bat', 'py', 'go', 'c', 'cpp']; + } + return !in_array($value->getExtension(), $parameters); + + }); + + // 注册了 file_different_if_file 验证器规则 + // file_different_if_file:field 两文件字段不能是同一个文件 + $validatorFactory->extend('file_different_if_file', function ($attribute, $value, $parameters, $validator) { + + // 获取比较字段参数值 + $anotherFile = $validator->getData()[$parameters[0]]; + if (!($value instanceof UploadedFile && $anotherFile instanceof UploadedFile)) { + return true; + } + return md5_file($value->getRealPath()) !== md5_file($anotherFile->getRealPath()); + + }); + + // 注册了 tel 验证器规则 + // 手机号码验证器 + $validatorFactory->extend('tel', function ($attribute, $value, $parameters, $validator) { + return boolval(preg_match('/^1[3-9]\d{9}$/', $value)); + }); + + // 注册了 image_if_file 验证器规则 + // 如果是文件就必须是图片类型的 + $validatorFactory->extend('image_if_file', function ($attribute, $value, $parameters, $validator) { + + if (!($value instanceof UploadedFile)) { + return true; + } + + $rules = $validator->getRules(); + $rules[$attribute] = ['image']; + $validator->setRules($rules); + + return $validator->passes(); + + }); + } } \ No newline at end of file diff --git a/app/Model/ActivityBind.php b/app/Model/ActivityBind.php new file mode 100644 index 0000000..2f4dda0 --- /dev/null +++ b/app/Model/ActivityBind.php @@ -0,0 +1,34 @@ +attributes['json_data'] = 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..6332750 --- /dev/null +++ b/app/Service/CommunityService.php @@ -0,0 +1,30 @@ +isBinded($data['bind_type'], $data['source_id'], $data['user_id'])) { + return $res; + } + return ActivityBind::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 ActivityBind::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\ServiceEvaluateService::class + \App\Service\ServiceEvaluateServiceInterface::class => \App\Service\ServiceEvaluateService::class, + \App\Service\CommunityServiceInterface::class => \App\Service\CommunityService::class, ]; diff --git a/config/routes.php b/config/routes.php index 706a9ab..a5c4985 100644 --- a/config/routes.php +++ b/config/routes.php @@ -23,4 +23,5 @@ Router::addGroup('/v1/',function (){ Router::post('ServiceEvaluate/isPersonnel', 'App\Controller\ServiceEvaluateController@isPersonnel'); Router::post('ServiceEvaluate/getPersonnelInfo', 'App\Controller\ServiceEvaluateController@getPersonnelInfo'); Router::post('ServiceEvaluate/getEvaluateList', 'App\Controller\ServiceEvaluateController@getEvaluateList'); + Router::post('Community/bind', 'App\Controller\CommunityController@bind'); }); \ No newline at end of file