CouponValidate.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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\marketing;
  20. use app\common\enum\CouponEnum;
  21. use app\common\validate\BaseValidate;
  22. class CouponValidate extends BaseValidate
  23. {
  24. protected $rule = [
  25. 'id' => 'require|number',
  26. 'name' => 'require|min:2',
  27. 'money' => 'require|float',
  28. 'condition_type' => 'require|number|checkCondition',
  29. 'send_total_type' => 'require|number|checkSendTotalType',
  30. 'use_time_type' => 'require|checkUseTimeType',
  31. 'get_type' => 'require|in:1,2',
  32. 'get_num_type' => 'require|checkGetNumType',
  33. 'use_goods_type' => 'require|checkUseGoodsType',
  34. 'send_user_num' => 'require|min:1|max:10',
  35. 'send_user' => 'require|array',
  36. 'cl_id' => 'require|array',
  37. ];
  38. /**
  39. * @notes 新增场景
  40. * @return CouponValidate
  41. * @author 张无忌
  42. * @date 2021/8/6 20:14
  43. */
  44. public function sceneAdd()
  45. {
  46. return $this->remove('id', 'require')
  47. ->remove('send_user_num', 'require')
  48. ->remove('send_user', 'require')
  49. ->remove('cl_id', 'require');
  50. }
  51. /**
  52. * @notes 编辑场景
  53. * @return CouponValidate
  54. * @author 张无忌
  55. * @date 2021/8/6 20:14
  56. */
  57. public function sceneEdit()
  58. {
  59. return $this->remove('send_user_num', 'require')
  60. ->remove('send_user', 'require')
  61. ->remove('cl_id', 'require');
  62. }
  63. /**
  64. * @notes 作废场景
  65. * @author 张无忌
  66. * @date 2021/8/6 20:14
  67. */
  68. public function sceneVoid()
  69. {
  70. return $this->only(['cl_id']);
  71. }
  72. public function sceneDelete()
  73. {
  74. return $this->only(['id']);
  75. }
  76. public function sceneDetail()
  77. {
  78. return $this->only(['id']);
  79. }
  80. public function sceneSend()
  81. {
  82. return $this->only(['send_user_num', 'send_user']);
  83. }
  84. /**
  85. * @notes 验证使用条件参数
  86. * @param $value
  87. * @param $rule
  88. * @param $data
  89. * @return bool
  90. * @author 张无忌
  91. * @date 2021/7/20 10:05
  92. */
  93. public function checkCondition($value, $rule, $data)
  94. {
  95. unset($value);
  96. unset($rule);
  97. if ($data["condition_type"] == CouponEnum::CONDITION_TYPE_FULL) {
  98. if (empty($data['condition_money'])) {
  99. return '请填写使用条件金额';
  100. }
  101. if (!is_numeric($data['condition_money'])) {
  102. return "使用条件金额请填写数字";
  103. }
  104. if ($data['condition_money'] <= 0) {
  105. return "使用条件金额需大于0元";
  106. }
  107. }
  108. if ($data["condition_type"] == CouponEnum::CONDITION_TYPE_DISCOUNT) {
  109. if (empty($data['condition_money'])) {
  110. return '请填写使用条件金额';
  111. }
  112. if (!is_numeric($data['condition_money'])) {
  113. return "使用条件金额请填写数字";
  114. }
  115. if ($data['condition_money'] <= 0) {
  116. return "使用条件金额需大于0元";
  117. }
  118. if (empty($data['discount_ratio'])) {
  119. return '请填写折扣';
  120. }
  121. if (! is_numeric($data['discount_ratio'])) {
  122. return '折扣请填写数字';
  123. }
  124. if ($data['discount_ratio'] < 1 || $data['discount_ratio'] >= 10) {
  125. return '折扣范围在1~10之间';
  126. }
  127. if (round($data['discount_ratio'], 1) != $data['discount_ratio']) {
  128. return '折扣最多一位小数';
  129. }
  130. if(empty($data['discount_max_money'])){
  131. return '请填写最高优惠';
  132. }
  133. if (! is_numeric($data['discount_max_money'])) {
  134. return '最高优惠金额请填写数字';
  135. }
  136. if ($data['discount_max_money'] <= 0) {
  137. return "最高优惠金额需大于0元";
  138. }
  139. }
  140. return true;
  141. }
  142. /**
  143. * @notes 验证发放数量条件
  144. * @param $value
  145. * @param $rule
  146. * @param $data
  147. * @return bool|string
  148. * @author 张无忌
  149. * @date 2021/7/20 10:12
  150. */
  151. public function checkSendTotalType($value, $rule, $data)
  152. {
  153. unset($value);
  154. unset($rule);
  155. if ($data["send_total_type"] == CouponEnum::SEND_TOTAL_TYPE_FIXED) {
  156. if (is_numeric($data['send_total']) and $data['send_total'] >= 0) {
  157. return true;
  158. }
  159. return "请填写发放数量";
  160. }
  161. return true;
  162. }
  163. /**
  164. * @notes 验证使用时间
  165. * @param $value
  166. * @param $rule
  167. * @param $data
  168. * @return bool|string
  169. * @author 张无忌
  170. * @date 2021/7/20 14:14
  171. */
  172. public function checkUseTimeType($value, $rule, $data)
  173. {
  174. unset($value);
  175. unset($rule);
  176. if ($data["use_time_type"] == CouponEnum::USE_TIME_TYPE_FIXED) {
  177. $start = strtotime($data['use_time_start']);
  178. $end = strtotime($data['use_time_end']);
  179. if ($start >= $end) {
  180. return "用券开始时间必须小于结束时间";
  181. }
  182. if ($end <= time()) {
  183. return "用券结束时间必须大于当前时间";
  184. }
  185. } elseif ($data["use_time_type"] == CouponEnum::USE_TIME_TYPE_TODAY
  186. or $data["use_time_type"] == CouponEnum::USE_TIME_TYPE_TOMORROW) {
  187. if (!is_numeric($data['use_time'])) {
  188. return '用券天数必须为数字';
  189. }
  190. if ($data['use_time'] <= 0) {
  191. return '用券天数必须大于0';
  192. }
  193. }
  194. return true;
  195. }
  196. /**
  197. * @notes 验证领取数量限制
  198. * @param $value
  199. * @param $rule
  200. * @param $data
  201. * @return bool|string
  202. * @author 张无忌
  203. * @date 2021/7/20 14:20
  204. */
  205. public function checkGetNumType($value, $rule, $data)
  206. {
  207. unset($value);
  208. unset($rule);
  209. if ($data["get_num_type"] == CouponEnum::GET_NUM_TYPE_LIMIT
  210. or $data["get_num_type"] == CouponEnum::GET_NUM_TYPE_DAY) {
  211. if (empty($data['get_num'])) {
  212. return "请填写领取数量";
  213. }
  214. if (!is_numeric($data['get_num'])) {
  215. return "领取数量必须为数字";
  216. }
  217. if ($data['get_num'] < 0) {
  218. return "领取数量需大于0";
  219. }
  220. }
  221. return true;
  222. }
  223. /**
  224. * @notes 验证允许使用的商品
  225. * @param $value
  226. * @param $rule
  227. * @param $data
  228. * @return bool|string
  229. * @author 张无忌
  230. * @date 2021/7/20 14:32
  231. */
  232. public function checkUseGoodsType($value, $rule, $data)
  233. {
  234. unset($value);
  235. unset($rule);
  236. if ($data["use_goods_type"] == CouponEnum::USE_GOODS_TYPE_ALLOW
  237. or $data["use_goods_type"] == CouponEnum::USE_GOODS_TYPE_BAN) {
  238. if (empty($data['use_goods_ids'])) {
  239. return '请选择商品';
  240. }
  241. if (!is_array($data['use_goods_ids'])) {
  242. return "允许使用商品参数有误,必须为数组格式";
  243. }
  244. foreach ($data['use_goods_ids'] as $id) {
  245. if (!is_numeric($id)) {
  246. return "选择的商品异常";
  247. }
  248. }
  249. }
  250. if ($data["use_goods_type"] == CouponEnum::USE_GOODS_TYPE_CATEGORY) {
  251. if (empty($data['use_goods_category_ids'])) {
  252. return '请选择分类';
  253. }
  254. if (!is_array($data['use_goods_category_ids'])) {
  255. return "允许使用分类参数有误,必须为数组格式";
  256. }
  257. foreach ($data['use_goods_category_ids'] as $id) {
  258. if (!is_numeric($id)) {
  259. return "选择的分类异常";
  260. }
  261. }
  262. }
  263. return true;
  264. }
  265. }