Browse Source

社区绑定

master
weigang 5 years ago
parent
commit
f0efbbf7df
  1. 26
      app/Controller/CommunityController.php
  2. 81
      app/Listener/ValidatorFactoryResolvedListener.php
  3. 34
      app/Model/UserRelationBind.php
  4. 46
      app/Request/CommunityBindRequest.php
  5. 30
      app/Service/CommunityService.php
  6. 9
      app/Service/CommunityServiceInterface.php
  7. 1
      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]);
}
}

81
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();
});
}
}

34
app/Model/UserRelationBind.php

@ -0,0 +1,34 @@
<?php
declare (strict_types=1);
namespace App\Model;
/**
*/
class UserRelationBind extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'lanzu_user_relation_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'] = $value ? 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\UserRelationBind;
class CommunityService implements CommunityServiceInterface
{
public function bind($data)
{
if ($res = $this->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();
}
}

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);
}

1
config/autoload/dependencies.php

@ -26,4 +26,5 @@ return [
\App\Service\FeiePrintServiceInterface::class => \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,
];

1
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 () {

Loading…
Cancel
Save