ShopNoticeValidate.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\validate\shop_notice;
  20. use app\common\model\ShopNotice;
  21. use app\common\validate\BaseValidate;
  22. class ShopNoticeValidate extends BaseValidate
  23. {
  24. protected $rule = [
  25. 'id' => 'require|checkId',
  26. 'name' => 'require|checkName',
  27. 'content' => 'require',
  28. 'sort' => 'number|max:5',
  29. 'status' => 'require|in:0,1',
  30. ];
  31. protected $message = [
  32. 'name.require' => '公告标题不能为空',
  33. 'content.require' => '公告内容不能为空',
  34. 'sort.number' => '排序必须为纯数字',
  35. 'sort.max' => '排序最大不能超过五位数',
  36. 'status.require' => '公告状态不能为空',
  37. 'status.in' => '公告状态取值范围[0,1]',
  38. ];
  39. public function sceneAdd()
  40. {
  41. return $this->only(['name','content','sort','status']);
  42. }
  43. public function sceneEdit()
  44. {
  45. return $this->only(['id','name','content','sort','status']);
  46. }
  47. public function sceneDetail()
  48. {
  49. return $this->only(['id']);
  50. }
  51. public function sceneStatus()
  52. {
  53. return $this->only(['id','status'])
  54. ->append('status','require');
  55. }
  56. public function sceneDel()
  57. {
  58. return $this->only(['id']);
  59. }
  60. /**
  61. * @notes 检查公告标题是否已存在
  62. * @param $value
  63. * @param $rule
  64. * @param $data
  65. * @return bool|string
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. * @author ljj
  70. * @date 2021/8/23 12:00 下午
  71. */
  72. public function checkName($value,$rule,$data)
  73. {
  74. $where[] = ['name', '=', $value];
  75. // 编辑的情况,要排除自身ID
  76. if (isset($data['id'])) {
  77. $where[] = ['id', '<>', $data['id']];
  78. }
  79. $result = ShopNotice::where($where)->select()->toArray();
  80. if ($result) {
  81. return '公告标题已存在';
  82. }
  83. return true;
  84. }
  85. /**
  86. * @notes 检查商城公告ID是否存在
  87. * @param $value
  88. * @param $rule
  89. * @param $data
  90. * @return bool|string
  91. * @author ljj
  92. * @date 2021/8/23 2:40 下午
  93. */
  94. public function checkId($value,$rule,$data)
  95. {
  96. $result = ShopNotice::findOrEmpty($value);
  97. if ($result->isEmpty()) {
  98. return '商城公告不存在';
  99. }
  100. return true;
  101. }
  102. }