FreeShippingValidate.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\free_shipping;
  20. use app\common\enum\CouponEnum;
  21. use app\common\enum\FreeShippingEnum;
  22. use app\common\enum\LuckyDrawEnum;
  23. use app\common\model\Coupon;
  24. use app\common\model\FreeShipping;
  25. use app\common\model\LuckyDraw;
  26. use app\common\model\Region;
  27. use app\common\validate\BaseValidate;
  28. /**
  29. * 包邮活动
  30. */
  31. class FreeShippingValidate extends BaseValidate
  32. {
  33. protected $rule = [
  34. 'id' => 'require|checkActivity',
  35. 'name' => 'require|max:120',
  36. 'start_time' => 'require|dateFormat:Y-m-d H:i:s',
  37. 'end_time' => 'require|dateFormat:Y-m-d H:i:s|checkTime',
  38. 'target_user_type' => 'require|in:1',
  39. 'target_goods_type' => 'require|in:1',
  40. 'condition_type' => 'require|in:1,2',
  41. 'region' => 'require|array|checkRegion',
  42. ];
  43. protected $message = [
  44. 'id.require' => 'id参数缺失',
  45. 'name.require' => '请输入活动名称',
  46. 'name.max' => '活动名称不能超过120个字符',
  47. 'start_time.require' => '请选择开始时间',
  48. 'start_time.dateFormat' => '开始时间格式错误,须为Y-m-d H:i:s',
  49. 'end_time.require' => '请选择结束时间',
  50. 'end_time.dateFormat' => '结束时间格式错误,须为Y-m-d H:i:s',
  51. 'target_user_type.require' => '请选择活动对象类型',
  52. 'target_user_type.in' => '非法的活动对象类型',
  53. 'target_goods_type.require' => '请选择活动商品类型',
  54. 'target_goods_type.in' => '非法的活动商品类型',
  55. 'condition_type.require' => '请选择活动规则类型',
  56. 'condition_type.in' => '非法的活动规则类型',
  57. 'region.require' => '请填写包邮信息',
  58. 'region.array' => '包邮信息须为数组格式',
  59. ];
  60. /**
  61. * @notes 添加场景
  62. */
  63. public function sceneAdd()
  64. {
  65. return $this->only(['name', 'start_time', 'end_time', 'target_user_type', 'target_goods_type', 'condition_type', 'region']);
  66. }
  67. /**
  68. * @notes 详情场景
  69. */
  70. public function sceneDetail()
  71. {
  72. return $this->only(['id']);
  73. }
  74. /**
  75. * @notes 开始活动场景
  76. */
  77. public function sceneStart()
  78. {
  79. return $this->only(['id']);
  80. }
  81. /**
  82. * @notes 结束活动场景
  83. */
  84. public function sceneEnd()
  85. {
  86. return $this->only(['id']);
  87. }
  88. /**
  89. * @notes 删除活动场景
  90. */
  91. public function sceneDelete()
  92. {
  93. return $this->only(['id']);
  94. }
  95. /**
  96. * @notes 编辑场景
  97. */
  98. public function sceneEdit()
  99. {
  100. return $this->only(['id', 'name', 'start_time', 'end_time', 'target_user_type', 'target_goods_type', 'condition_type', 'region']);
  101. }
  102. /**
  103. * @notes 验证活动时间
  104. */
  105. public function checkTime($region, $rule, $data) {
  106. $startTime = strtotime($data['start_time']);
  107. $endTime = strtotime($data['end_time']);
  108. if ($startTime >= $endTime) {
  109. return '开始时间须小于结束时间';
  110. }
  111. $where = [
  112. ['status', 'in', [FreeShippingEnum::WAIT, FreeShippingEnum::ING]]
  113. ];
  114. if (isset($data['id'])) {
  115. // 编辑场景
  116. $where[] = ['id', '<>', $data['id']];
  117. }
  118. $activities = FreeShipping::field('start_time, end_time')
  119. ->where($where)
  120. ->select()
  121. ->toArray();
  122. if (!$activities) {
  123. // 未有活动的情况
  124. return true;
  125. }
  126. foreach($activities as $item) {
  127. if (
  128. ($startTime >= strtotime($item['start_time']) && $startTime <= strtotime($item['end_time']))
  129. ||
  130. ($endTime >= strtotime($item['start_time']) && $endTime <= strtotime($item['end_time']))
  131. ) {
  132. return '所选活动时间与现有活动有重叠';
  133. }
  134. }
  135. return true;
  136. }
  137. /**
  138. * @notes 验证包邮规则
  139. */
  140. public function checkRegion($region, $rule, $data) {
  141. $regonIds = Region::column('id');
  142. foreach($region as $key => $item) {
  143. $item = (array)$item;
  144. if (!isset($item['region_id']) || !isset($item['region_name']) || !isset($item['threshold'])) {
  145. return '规则'. ($key + 1) . '参数缺失' ;
  146. }
  147. if (!is_numeric($item['threshold'])) {
  148. if ($data['condition_type'] == 1) {
  149. return '规则'. ($key + 1) . '订单金额须为数字';
  150. } else {
  151. return '规则'. ($key + 1) . '购买件数须为数字';
  152. }
  153. }
  154. if ($item['threshold'] < 0) {
  155. if ($data['condition_type'] == 1) {
  156. return '规则' . ($key + 1) . '订单金额不能小于0';
  157. } else {
  158. return '规则' . ($key + 1) . '购买件数不能小于0';
  159. }
  160. }
  161. $targetRegion = explode(',', $item['region_id']);
  162. $interset = array_intersect($regonIds, $targetRegion);
  163. $targetRegionLen = count($targetRegion);
  164. $interset = count($interset);
  165. if ($targetRegionLen != $interset) {
  166. return '规则'. ($key + 1) . '存在无法识别的区域';
  167. }
  168. }
  169. return true;
  170. }
  171. /**
  172. * @notes 验证活动
  173. */
  174. public function checkActivity($activityId)
  175. {
  176. $activity = FreeShipping::findOrEmpty($activityId);
  177. if ($activity->isEmpty()) {
  178. return '活动不存在';
  179. }
  180. return true;
  181. }
  182. }