8 changed files with 214 additions and 1 deletions
-
26app/Controller/CommunityController.php
-
66app/Listener/ValidatorFactoryResolvedListener.php
-
34app/Model/ActivityBind.php
-
46app/Request/CommunityBindRequest.php
-
30app/Service/CommunityService.php
-
9app/Service/CommunityServiceInterface.php
-
3config/autoload/dependencies.php
-
1config/routes.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]); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -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)); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -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 [ |
||||
|
|
||||
|
]; |
||||
|
} |
||||
|
} |
||||
@ -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(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Service; |
||||
|
|
||||
|
interface CommunityServiceInterface |
||||
|
{ |
||||
|
public function isBinded($bind_type, $source_id, $user_id); |
||||
|
public function bind($data); |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue