Browse Source

社区扫码绑定接口

master
weigang 5 years ago
parent
commit
d93fbd78a2
  1. 26
      app/Controller/CommunityController.php
  2. 66
      app/Listener/ValidatorFactoryResolvedListener.php
  3. 34
      app/Model/ActivityBind.php
  4. 46
      app/Request/CommunityBindRequest.php
  5. 30
      app/Service/CommunityService.php
  6. 9
      app/Service/CommunityServiceInterface.php
  7. 3
      config/autoload/dependencies.php
  8. 1
      config/routes.php

26
app/Controller/CommunityController.php

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Request\CommunityBindRequest;
use App\Service\CommunityServiceInterface;
use Hyperf\Di\Annotation\Inject;
class CommunityController extends BaseController
{
/**
* @Inject
* @var CommunityServiceInterface
*/
protected $communityService;
public function bind(CommunityBindRequest $request)
{
$res = $this->communityService->bind($request->validated());
return $this->success(['id' => $res->id]);
}
}

66
app/Listener/ValidatorFactoryResolvedListener.php

@ -4,6 +4,7 @@ namespace App\Listener;
use Hyperf\DbConnection\Db; use Hyperf\DbConnection\Db;
use Hyperf\Event\Contract\ListenerInterface; use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\HttpMessage\Upload\UploadedFile;
use Hyperf\Validation\Contract\ValidatorFactoryInterface; use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Hyperf\Validation\Event\ValidatorFactoryResolved; use Hyperf\Validation\Event\ValidatorFactoryResolved;
@ -80,5 +81,70 @@ class ValidatorFactoryResolvedListener implements ListenerInterface
return $builder->exists(); 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();
});
} }
} }

34
app/Model/ActivityBind.php

@ -0,0 +1,34 @@
<?php
declare (strict_types=1);
namespace App\Model;
/**
*/
class ActivityBind extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'lanzu_activity_bind';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['bind_type', 'source_id', 'user_id', 'json_data'];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [];
public function setJsonDataAttribute($value)
{
$this->attributes['json_data'] = json_encode(json_decode($value, true));
}
}

46
app/Request/CommunityBindRequest.php

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\Request;
use Hyperf\Validation\Request\FormRequest;
class CommunityBindRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'bind_type' => '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 [
];
}
}

30
app/Service/CommunityService.php

@ -0,0 +1,30 @@
<?php
namespace App\Service;
use App\Model\ActivityBind;
class CommunityService implements CommunityServiceInterface
{
public function bind($data)
{
if ($res = $this->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();
}
}

9
app/Service/CommunityServiceInterface.php

@ -0,0 +1,9 @@
<?php
namespace App\Service;
interface CommunityServiceInterface
{
public function isBinded($bind_type, $source_id, $user_id);
public function bind($data);
}

3
config/autoload/dependencies.php

@ -10,5 +10,6 @@ declare(strict_types=1);
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/ */
return [ return [
\App\Service\ServiceEvaluateServiceInterface::class => \App\Service\ServiceEvaluateService::class
\App\Service\ServiceEvaluateServiceInterface::class => \App\Service\ServiceEvaluateService::class,
\App\Service\CommunityServiceInterface::class => \App\Service\CommunityService::class,
]; ];

1
config/routes.php

@ -23,4 +23,5 @@ Router::addGroup('/v1/',function (){
Router::post('ServiceEvaluate/isPersonnel', 'App\Controller\ServiceEvaluateController@isPersonnel'); Router::post('ServiceEvaluate/isPersonnel', 'App\Controller\ServiceEvaluateController@isPersonnel');
Router::post('ServiceEvaluate/getPersonnelInfo', 'App\Controller\ServiceEvaluateController@getPersonnelInfo'); Router::post('ServiceEvaluate/getPersonnelInfo', 'App\Controller\ServiceEvaluateController@getPersonnelInfo');
Router::post('ServiceEvaluate/getEvaluateList', 'App\Controller\ServiceEvaluateController@getEvaluateList'); Router::post('ServiceEvaluate/getEvaluateList', 'App\Controller\ServiceEvaluateController@getEvaluateList');
Router::post('Community/bind', 'App\Controller\CommunityController@bind');
}); });
Loading…
Cancel
Save