|
@@ -0,0 +1,79 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\api\validate;
|
|
|
+
|
|
|
+use think\Validate;
|
|
|
+use app\common\Enum\ChannelEnum;
|
|
|
+
|
|
|
+class ShareValidate extends Validate
|
|
|
+{
|
|
|
+ protected $rule = [
|
|
|
+ 'shareId' => 'require|max:50',
|
|
|
+ 'spm' => 'max:100',
|
|
|
+ 'page' => 'require',
|
|
|
+ 'query' => 'max:500',
|
|
|
+ 'from' => 'require',
|
|
|
+ 'platform' => 'checkSharePlatform',
|
|
|
+ ];
|
|
|
+
|
|
|
+ protected $message = [
|
|
|
+ 'shareId.require' => '分享ID不能为空',
|
|
|
+ 'shareId.max' => '分享ID长度不能超过50个字符',
|
|
|
+ 'spm.max' => 'SPM参数长度不能超过100个字符',
|
|
|
+ 'page.integer' => '页码必须为整数',
|
|
|
+ 'page.gt' => '页码必须大于0',
|
|
|
+ 'query.max' => '查询参数长度不能超过500个字符',
|
|
|
+ 'from.max' => '来源参数长度不能超过50个字符',
|
|
|
+ ];
|
|
|
+
|
|
|
+ protected $scene = [
|
|
|
+ 'add' => ['shareId', 'spm', 'page', 'query', 'from', 'platform'],
|
|
|
+ ];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证分享类型
|
|
|
+ * @param $value
|
|
|
+ * @param $rule
|
|
|
+ * @param $data
|
|
|
+ * @return bool|string
|
|
|
+ */
|
|
|
+ public function checkShareType($value, $rule, $data)
|
|
|
+ {
|
|
|
+ $validTypes = ['goods', 'activity', 'page'];
|
|
|
+ if (!in_array($value, $validTypes)) {
|
|
|
+ return '分享类型参数错误';
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证分享渠道
|
|
|
+ * @param $value
|
|
|
+ * @param $rule
|
|
|
+ * @param $data
|
|
|
+ * @return bool|string
|
|
|
+ */
|
|
|
+ public function checkSharePlatform($value, $rule, $data)
|
|
|
+ {
|
|
|
+ $validChannels = ChannelEnum::getChannelMap();
|
|
|
+ if (!in_array($value, $validChannels)) {
|
|
|
+ return '分享渠道参数错误';
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证分享URL
|
|
|
+ * @param $value
|
|
|
+ * @param $rule
|
|
|
+ * @param $data
|
|
|
+ * @return bool|string
|
|
|
+ */
|
|
|
+ public function checkShareUrl($value, $rule, $data)
|
|
|
+ {
|
|
|
+ if (!filter_var($value, FILTER_VALIDATE_URL)) {
|
|
|
+ return '分享链接格式不正确';
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|