Share.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\api\validate;
  3. use think\Validate;
  4. use app\common\Enum\ChannelEnum;
  5. class ShareValidate extends Validate
  6. {
  7. protected $rule = [
  8. 'shareId' => 'require|max:50',
  9. 'spm' => 'max:100',
  10. 'page' => 'require',
  11. 'query' => 'max:500',
  12. 'from' => 'require',
  13. 'platform' => 'checkSharePlatform',
  14. ];
  15. protected $message = [
  16. 'shareId.require' => '分享ID不能为空',
  17. 'shareId.max' => '分享ID长度不能超过50个字符',
  18. 'spm.max' => 'SPM参数长度不能超过100个字符',
  19. 'page.integer' => '页码必须为整数',
  20. 'page.gt' => '页码必须大于0',
  21. 'query.max' => '查询参数长度不能超过500个字符',
  22. 'from.max' => '来源参数长度不能超过50个字符',
  23. ];
  24. protected $scene = [
  25. 'add' => ['shareId', 'spm', 'page', 'query', 'from', 'platform'],
  26. ];
  27. /**
  28. * 验证分享类型
  29. * @param $value
  30. * @param $rule
  31. * @param $data
  32. * @return bool|string
  33. */
  34. public function checkShareType($value, $rule, $data)
  35. {
  36. $validTypes = ['goods', 'activity', 'page'];
  37. if (!in_array($value, $validTypes)) {
  38. return '分享类型参数错误';
  39. }
  40. return true;
  41. }
  42. /**
  43. * 验证分享渠道
  44. * @param $value
  45. * @param $rule
  46. * @param $data
  47. * @return bool|string
  48. */
  49. public function checkSharePlatform($value, $rule, $data)
  50. {
  51. $validChannels = ChannelEnum::getChannelMap();
  52. if (!in_array($value, $validChannels)) {
  53. return '分享渠道参数错误';
  54. }
  55. return true;
  56. }
  57. /**
  58. * 验证分享URL
  59. * @param $value
  60. * @param $rule
  61. * @param $data
  62. * @return bool|string
  63. */
  64. public function checkShareUrl($value, $rule, $data)
  65. {
  66. if (!filter_var($value, FILTER_VALIDATE_URL)) {
  67. return '分享链接格式不正确';
  68. }
  69. return true;
  70. }
  71. }