SignValidate.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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\sign;
  20. use app\common\enum\YesNoEnum;
  21. use app\common\model\SignDaily;
  22. use app\common\validate\BaseValidate;
  23. use think\facade\Validate;
  24. /**
  25. * 签到验证器
  26. * Class SignValidate
  27. * @package app\adminapi\validate\sign
  28. */
  29. class SignValidate extends BaseValidate
  30. {
  31. protected $rule = [
  32. 'is_open' => 'require',
  33. 'daily' => 'require|array|checkDaily',
  34. 'remark' => 'require',
  35. 'days' => 'require|egt:2|checkDays',
  36. 'integral_status' => 'require|in:0,1',
  37. 'integral' => 'require|gt:0',
  38. 'id' => 'require',
  39. 'sort' => 'number',
  40. 'continuous' => 'array|checkContinuous',
  41. ];
  42. protected $message = [
  43. 'is_open.require' => '请选择签到状态',
  44. 'daily.require' => '请填写每日签到规则',
  45. 'daily.array' => '每日签到规则格式须为数组',
  46. 'remark.require' => '请填写签到说明',
  47. 'days.require' => '请填写连续签到天数',
  48. 'days.egt' => '连续签到天数须大于或等于2天',
  49. 'integral_status.require' => '请选择是否赠送积分',
  50. 'integral_status.in' => '赠送积分状态错误',
  51. 'integral.require' => '请填写赠送积分数量',
  52. 'integral.gt' => '赠送积分数量须大于0',
  53. 'id.require' => '参数缺失',
  54. 'sort.number' => '排序值错误',
  55. 'continuous.array' => '连续签到规则格式须为数组',
  56. ];
  57. /**
  58. * @notes 设置签到规则场景
  59. * @param $value
  60. * @param $rule
  61. * @param $data
  62. * @return SignValidate
  63. * @author Tab
  64. * @date 2021/8/16 9:52
  65. */
  66. public function sceneSetConfig()
  67. {
  68. return $this->only(['is_open', 'daily', 'remark', 'continuous']);
  69. }
  70. /**
  71. * @notes 添加连续签到规则场景
  72. * @return SignValidate
  73. * @author Tab
  74. * @date 2021/8/16 15:23
  75. */
  76. public function sceneAdd()
  77. {
  78. return $this->only(['days', 'integral_status', 'integral']);
  79. }
  80. /**
  81. * @notes 编辑连续签到规则场景
  82. * @return SignValidate
  83. * @author Tab
  84. * @date 2021/8/16 15:42
  85. */
  86. public function sceneEdit()
  87. {
  88. return $this->only(['id', 'days', 'integral_status', 'integral']);
  89. }
  90. /**
  91. * @notes 查看连续签到规则详情场景
  92. * @return SignValidate
  93. * @author Tab
  94. * @date 2021/8/16 11:36
  95. */
  96. public function sceneDetail()
  97. {
  98. return $this->only(['id']);
  99. }
  100. /**
  101. * @notes 删除连续签到规则场景
  102. * @return SignValidate
  103. * @author Tab
  104. * @date 2021/8/16 15:49
  105. */
  106. public function sceneDelete()
  107. {
  108. return $this->only(['id']);
  109. }
  110. /**
  111. * @notes 校验每日签到参数
  112. * @param $value
  113. * @return bool|string
  114. * @author Tab
  115. * @date 2021/8/16 10:06
  116. */
  117. public function checkDaily($value)
  118. {
  119. if(
  120. !isset($value['integral_status']) ||
  121. !isset($value['integral'])
  122. ) {
  123. return '每日签到参数缺失';
  124. }
  125. if(
  126. !in_array($value['integral_status'], [YesNoEnum::YES, YesNoEnum::NO]) ||
  127. $value['integral'] < 0
  128. ) {
  129. return '每日签到参数不符合要求';
  130. }
  131. return true;
  132. }
  133. /**
  134. * @notes 校验连续签到天数
  135. * @param $value
  136. * @param $rule
  137. * @param $data
  138. * @return bool|string
  139. * @author Tab
  140. * @date 2021/8/16 15:32
  141. */
  142. public function checkDays($value, $rule, $data)
  143. {
  144. $where[] = ['days', '=', $value];
  145. if(isset($data['id'])) {
  146. $where[] = ['id', '<>', $data['id']];
  147. }
  148. $signDaily = SignDaily::where($where)->findOrEmpty();
  149. if(!$signDaily->isEmpty()) {
  150. return '已有相同的连续签到规则';
  151. }
  152. return true;
  153. }
  154. /**
  155. * @notes 校验签到规则
  156. * @param $value
  157. * @return bool|string
  158. * @author ljj
  159. * @date 2023/5/6 7:10 下午
  160. */
  161. public function checkContinuous($value)
  162. {
  163. $days_arr = array_column($value,'days');
  164. if (count($days_arr) != count(array_unique($days_arr))) {
  165. return '存在重复的连续天数';
  166. }
  167. foreach ($value as $key=>$val) {
  168. if(!isset($val['days']) || $val['days'] == '' || !Validate::number($val['days'])) {
  169. return '第'.($key+1).'行连续签到规则的连续天数错误';
  170. }
  171. if(!isset($val['integral']) || $val['integral'] == '' || !Validate::number($val['integral'])) {
  172. return '第'.($key+1).'行连续签到规则的连续奖励错误';
  173. }
  174. if(isset($val['sort']) && $val['sort'] != '' && !Validate::number($val['sort'])) {
  175. return '第'.($key+1).'行连续签到规则的排序错误';
  176. }
  177. }
  178. return true;
  179. }
  180. }