LuckyDrawValidate.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\shopapi\validate;
  20. use app\common\enum\LuckyDrawEnum;
  21. use app\common\model\LuckyDraw;
  22. use app\common\model\LuckyDrawRecord;
  23. use app\common\model\User;
  24. use app\common\validate\BaseValidate;
  25. class LuckyDrawValidate extends BaseValidate
  26. {
  27. protected $rule = [
  28. 'id' => 'require|checkActivity|checkUser',
  29. ];
  30. protected $message = [
  31. 'id.require' => '参数缺失',
  32. ];
  33. public function sceneLottery()
  34. {
  35. return $this->only(['id']);
  36. }
  37. public function sceneRecord()
  38. {
  39. return $this->only(['id'])
  40. ->remove('id', 'checkActivity|checkUser');
  41. }
  42. public function sceneActivity()
  43. {
  44. return $this->only(['id'])
  45. ->remove('id', 'checkActivity|checkUser');
  46. }
  47. public function sceneWinningList()
  48. {
  49. return $this->only(['id'])
  50. ->remove('id', 'checkActivity|checkUser');
  51. }
  52. /**
  53. * @notes 校验活动
  54. * @param $id
  55. * @return bool|string
  56. * @author Tab
  57. * @date 2021/11/24 17:18
  58. */
  59. public function checkActivity($value, $rule, $data)
  60. {
  61. $activity = LuckyDraw::findOrEmpty($data['id']);
  62. if ($activity->isEmpty()) {
  63. return '活动不存在';
  64. }
  65. if ($activity->status != LuckyDrawEnum::ING) {
  66. return '活动未开始或已结束';
  67. }
  68. if ($activity->start_time > time()) {
  69. return '活动未开始';
  70. }
  71. if ($activity->end_time <= time()) {
  72. return '活动已结束';
  73. }
  74. return true;
  75. }
  76. /**
  77. * @notes 校验用户
  78. * @author Tab
  79. * @date 2021/11/24 17:22
  80. */
  81. public function checkUser($value, $rule, $data)
  82. {
  83. $activity = LuckyDraw::findOrEmpty($data['id'])->toArray();
  84. $user = User::findOrEmpty($data['user_id']);
  85. if ($user->isEmpty()) {
  86. return '用户不存在';
  87. }
  88. $user = $user->toArray();
  89. // 校验参与次数
  90. if ($activity['frequency_type'] == 1 && (int)$activity['frequency'] <= $this->joinCount($data)) {
  91. return '今天抽奖次数已用完';
  92. }
  93. if ((int)$user['user_integral'] < $activity['need_integral']) {
  94. return '积分不足';
  95. }
  96. return true;
  97. }
  98. /**
  99. * @notes 当天参与次数
  100. * @param $userId
  101. * @author Tab
  102. * @date 2021/11/24 17:36
  103. */
  104. public function joinCount($data)
  105. {
  106. return LuckyDrawRecord::whereDay('create_time')
  107. ->where('user_id', $data['user_id'])
  108. ->where('activity_id', $data['id'])
  109. ->count();
  110. }
  111. }