18 changed files with 395 additions and 19 deletions
-
5app/Constants/ErrorCode.php
-
19app/Constants/LogLabel.php
-
19app/Constants/SsdbKeysPrefix.php
-
27app/Controller/AdController.php
-
22app/Controller/ParamsTokenController.php
-
14app/Exception/Handler/SsdbExceptionHandler.php
-
35app/Exception/Handler/ValidationExceptionHandler.php
-
113app/Listener/ValidatorFactoryResolvedListener.php
-
70app/Model/Ad.php
-
20app/Service/AdService.php
-
11app/Service/AdServiceInterface.php
-
13app/Service/ParamsTokenServiceInterface.php
-
41app/Service/ParamsTokenSsdbService.php
-
1config/autoload/dependencies.php
-
1config/autoload/exceptions.php
-
1config/autoload/listeners.php
-
1config/autoload/middlewares.php
-
1config/routes.php
@ -0,0 +1,19 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
|
||||
|
namespace App\Constants; |
||||
|
|
||||
|
use Hyperf\Constants\AbstractConstants; |
||||
|
use Hyperf\Constants\Annotation\Constants; |
||||
|
|
||||
|
/** |
||||
|
* @Constants |
||||
|
*/ |
||||
|
class LogLabel extends AbstractConstants |
||||
|
{ |
||||
|
/** |
||||
|
* @Message("Ssdb Log Label") |
||||
|
*/ |
||||
|
const SSDB_LOG = 'ssdb_log'; |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
|
||||
|
namespace App\Constants; |
||||
|
|
||||
|
use Hyperf\Constants\AbstractConstants; |
||||
|
use Hyperf\Constants\Annotation\Constants; |
||||
|
|
||||
|
/** |
||||
|
* @Constants |
||||
|
*/ |
||||
|
class SsdbKeysPrefix extends AbstractConstants |
||||
|
{ |
||||
|
/** |
||||
|
* @Message("Params Token Key Prefix") |
||||
|
*/ |
||||
|
const PARAMS_TOKEN = 'params_token_'; |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
|
||||
|
namespace App\Controller; |
||||
|
|
||||
|
use App\Service\AdServiceInterface; |
||||
|
use Hyperf\Di\Annotation\Inject; |
||||
|
|
||||
|
/** |
||||
|
* 系统AD广告/宣传图控制器 |
||||
|
* Class AdController |
||||
|
* @package App\Controller |
||||
|
*/ |
||||
|
class AdController extends BaseController |
||||
|
{ |
||||
|
/** |
||||
|
* @Inject |
||||
|
* @var AdServiceInterface |
||||
|
*/ |
||||
|
protected $adService; |
||||
|
|
||||
|
public function banners() |
||||
|
{ |
||||
|
return $this->success($this->adService->banners()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Exception\Handler; |
||||
|
|
||||
|
use App\Constants\ErrorCode; |
||||
|
use Hyperf\ExceptionHandler\ExceptionHandler; |
||||
|
use Hyperf\HttpMessage\Stream\SwooleStream; |
||||
|
use Hyperf\Validation\ValidationException; |
||||
|
use Psr\Http\Message\ResponseInterface; |
||||
|
use Throwable; |
||||
|
|
||||
|
class ValidationExceptionHandler extends ExceptionHandler |
||||
|
{ |
||||
|
|
||||
|
public function handle(Throwable $throwable, ResponseInterface $response) |
||||
|
{ |
||||
|
$this->stopPropagation(); |
||||
|
|
||||
|
$content = json_encode([ |
||||
|
"status" => 'error', |
||||
|
"code" => ErrorCode::PARAMS_INVALID, |
||||
|
"result" => [], |
||||
|
"message" => $throwable->validator->errors()->first() |
||||
|
]); |
||||
|
|
||||
|
return $response->withHeader('Content-Type', 'application/json') |
||||
|
->withStatus($throwable->status) |
||||
|
->withBody(new SwooleStream($content)); |
||||
|
} |
||||
|
|
||||
|
public function isValid(Throwable $throwable): bool |
||||
|
{ |
||||
|
return $throwable instanceof ValidationException; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,113 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Listener; |
||||
|
|
||||
|
use Hyperf\DbConnection\Db; |
||||
|
use Hyperf\Event\Contract\ListenerInterface; |
||||
|
use Hyperf\Validation\Contract\ValidatorFactoryInterface; |
||||
|
use Hyperf\Validation\Event\ValidatorFactoryResolved; |
||||
|
|
||||
|
class ValidatorFactoryResolvedListener implements ListenerInterface |
||||
|
{ |
||||
|
|
||||
|
public function listen(): array |
||||
|
{ |
||||
|
return [ |
||||
|
ValidatorFactoryResolved::class, |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
public function process(object $event) |
||||
|
{ |
||||
|
/** |
||||
|
* @var ValidatorFactoryInterface $validatorFactory |
||||
|
*/ |
||||
|
$validatorFactory = $event->validatorFactory; |
||||
|
|
||||
|
// 注册了 nonempty 验证器规则
|
||||
|
$validatorFactory->extend('nonempty', function ($attribute, $value, $parameters, $validator) { |
||||
|
|
||||
|
return isset($value) |
||||
|
&& $value |
||||
|
&& !empty($value) |
||||
|
&& !is_null($value) |
||||
|
&& $value != 'undefined' |
||||
|
&& $value != 'unknown' |
||||
|
&& $value != 'null' |
||||
|
&& $value != 'false'; |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
// 注册了 strlen 验证器规则,参数是min,max,最小长度和最大长度
|
||||
|
$validatorFactory->extend('strlen', function ($attribute, $value, $parameters, $validator) { |
||||
|
return mb_strlen($value)>=$parameters[0] && mb_strlen($value)<=$parameters[1]; |
||||
|
}); |
||||
|
$validatorFactory->replacer('strlen', function ($message, $attribute, $rule, $parameters) { |
||||
|
$message = str_replace(':min', $parameters[0], $message); |
||||
|
$message = str_replace(':max', $parameters[1], $message); |
||||
|
return $message; |
||||
|
}); |
||||
|
|
||||
|
// 注册了 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); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
// 注册了 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(); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,70 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare (strict_types=1); |
||||
|
namespace App\Model; |
||||
|
|
||||
|
use Hyperf\DbConnection\Model\Model; |
||||
|
/** |
||||
|
*/ |
||||
|
class Ad extends Model |
||||
|
{ |
||||
|
/** |
||||
|
* 跳转类型及说明 |
||||
|
*/ |
||||
|
const ITEM = [ |
||||
|
1 => 'page', |
||||
|
2 => 'webview', |
||||
|
3 => 'applet', |
||||
|
]; |
||||
|
|
||||
|
/** |
||||
|
* 类型 1 = 首页banners |
||||
|
*/ |
||||
|
const TYPE_BANNER = 1; |
||||
|
|
||||
|
/** |
||||
|
* 启用状态 |
||||
|
*/ |
||||
|
const STATUS_YES = 1; |
||||
|
const STATUS_NO = 2; |
||||
|
|
||||
|
/** |
||||
|
* The table associated with the model. |
||||
|
* |
||||
|
* @var string |
||||
|
*/ |
||||
|
protected $table = 'ims_cjdc_ad'; |
||||
|
/** |
||||
|
* The attributes that are mass assignable. |
||||
|
* |
||||
|
* @var array |
||||
|
*/ |
||||
|
protected $fillable = []; |
||||
|
/** |
||||
|
* The attributes that should be cast to native types. |
||||
|
* |
||||
|
* @var array |
||||
|
*/ |
||||
|
protected $casts = []; |
||||
|
|
||||
|
protected $appends = [ |
||||
|
'item_text', |
||||
|
'redirect_url' |
||||
|
]; |
||||
|
|
||||
|
/** |
||||
|
* 获取跳转说明 |
||||
|
*/ |
||||
|
public function getItemTextAttribute() |
||||
|
{ |
||||
|
return self::ITEM[$this->item]; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取跳转连接 |
||||
|
*/ |
||||
|
public function getRedirectUrlAttribute() |
||||
|
{ |
||||
|
return $this->src ?: $this->src2; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,20 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Service; |
||||
|
|
||||
|
use App\Model\Ad; |
||||
|
|
||||
|
class AdService implements AdServiceInterface |
||||
|
{ |
||||
|
/** |
||||
|
* 跳转类型、跳转连接、标题、图片地址 |
||||
|
*/ |
||||
|
public function banners() |
||||
|
{ |
||||
|
return Ad::query()->select(['id','title','logo','item','src','src2']) |
||||
|
->where([ |
||||
|
'type' => Ad::TYPE_BANNER, |
||||
|
'status' => Ad::STATUS_YES |
||||
|
])->get(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Service; |
||||
|
|
||||
|
interface AdServiceInterface |
||||
|
{ |
||||
|
/** |
||||
|
* 获取首页banners |
||||
|
*/ |
||||
|
public function banners(); |
||||
|
} |
||||
@ -1,11 +1,20 @@ |
|||||
<?php |
<?php |
||||
|
|
||||
|
|
||||
namespace App\Service; |
namespace App\Service; |
||||
|
|
||||
|
|
||||
interface ParamsTokenServiceInterface |
interface ParamsTokenServiceInterface |
||||
{ |
{ |
||||
|
/** |
||||
|
* 生成并存储token |
||||
|
* @param $params |
||||
|
* @return mixed |
||||
|
*/ |
||||
public function generate($params); |
public function generate($params); |
||||
|
|
||||
|
/** |
||||
|
* 通过token解析返回参数 |
||||
|
* @param $token |
||||
|
* @return mixed |
||||
|
*/ |
||||
public function analyze($token); |
public function analyze($token); |
||||
} |
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue