DistributionLevelValidate.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\distribution;
  20. use app\common\model\DistributionLevel;
  21. use app\common\validate\BaseValidate;
  22. /**
  23. * 分销会员等级验证器
  24. * Class DistributionLevelValidate
  25. * @package app\adminapi\validate\distribution
  26. */
  27. class DistributionLevelValidate extends BaseValidate
  28. {
  29. protected $rule = [
  30. 'name' => 'require|checkName',
  31. 'weights' => 'require|integer|gt:1|checkWeights',
  32. 'self_ratio' => 'require|between:0,100',
  33. 'first_ratio' => 'require|between:0,100',
  34. 'second_ratio' => 'require|between:0,100|checkRatioSum',
  35. 'update_relation' => 'require|integer|in:1,2',
  36. 'update_condition' => 'require|array|checkCondition',
  37. 'id' => 'require',
  38. ];
  39. protected $message = [
  40. 'name.require' => '请填写等级名称',
  41. 'weights.require' => '请输入级别',
  42. 'weights.integer' => '级别须为整型',
  43. 'weights.gt' => '级别须大于1',
  44. 'self_ratio.require' => '请输入自购佣金比例',
  45. 'self_ratio.between' => '自购佣金比例须在0-100之间',
  46. 'first_ratio.require' => '请输入一级佣金比例',
  47. 'first_ratio.between' => '一级佣金比例须在0-100之间',
  48. 'second_ratio.require' => '请输入二级佣金比例',
  49. 'second_ratio.between' => '二级佣金比例须在0-100之间',
  50. 'update_relation.require' => '请选择升级关系',
  51. 'update_relation.in' => '升级关系状态值错误',
  52. 'update_condition.require' => '请选择升级条件',
  53. 'update_condition.array' => '升级条件类型错误',
  54. 'id.require' => '参数缺失',
  55. ];
  56. /**
  57. * @notes 添加分销会员等级场景
  58. * @return DistributionLevelValidate
  59. * @author Tab
  60. * @date 2021/7/22 11:18
  61. */
  62. public function sceneAdd()
  63. {
  64. return $this->only(['name', 'weights', 'self_ratio', 'first_ratio', 'second_ratio', 'update_condition', 'update_relation']);
  65. }
  66. /**
  67. * @notes 获取分销会员等级详情
  68. * @return DistributionLevelValidate
  69. * @author Tab
  70. * @date 2021/7/22 14:50
  71. */
  72. public function sceneDetail()
  73. {
  74. return $this->only(['id']);
  75. }
  76. /**
  77. * @notes 编辑分销会员等级场景
  78. * @return DistributionLevelValidate
  79. * @author Tab
  80. * @date 2021/7/22 15:43
  81. */
  82. public function sceneEdit()
  83. {
  84. return $this->only(['id', 'name', 'weights', 'self_ratio', 'first_ratio', 'second_ratio'])
  85. ->remove('weights', 'gt');
  86. }
  87. /**
  88. * @notes 删除分销会员等级
  89. * @return DistributionLevelValidate
  90. * @author Tab
  91. * @date 2021/7/22 16:28
  92. */
  93. public function sceneDelete()
  94. {
  95. return $this->only(['id']);
  96. }
  97. /**
  98. * @notes 校验等级名称
  99. * @param $value
  100. * @param $rule
  101. * @param $data
  102. * @return bool|string
  103. * @author Tab
  104. * @date 2021/7/22 11:13
  105. */
  106. public function checkName($value, $rule, $data)
  107. {
  108. $where = [['name', '=', $value]];
  109. if(isset($data['id'])) {
  110. // 编辑的场景
  111. $where[] = ['id', '<>', $data['id']];
  112. }
  113. $level = DistributionLevel::where($where)->findOrEmpty();
  114. if(!$level->isEmpty()) {
  115. return '等级名称已存在';
  116. }
  117. return true;
  118. }
  119. /**
  120. * @notes 校验等级级别
  121. * @param $value
  122. * @param $rule
  123. * @param $data
  124. * @return bool|string
  125. * @author Tab
  126. * @date 2021/7/22 11:16
  127. */
  128. public function checkWeights($value, $rule, $data)
  129. {
  130. $where = [['weights', '=', $value]];
  131. if(isset($data['id'])) {
  132. // 编辑的场景
  133. $where[] = ['id', '<>', $data['id']];
  134. }
  135. $level = DistributionLevel::where($where)->findOrEmpty();
  136. if(!$level->isEmpty()) {
  137. return '等级级别已存在';
  138. }
  139. return true;
  140. }
  141. /**
  142. * @notes 校验升级条件
  143. * @param $value
  144. * @return string
  145. * @author Tab
  146. * @date 2021/7/22 16:13
  147. */
  148. public function checkCondition($value)
  149. {
  150. if(!count($value)) {
  151. return '请选择升级条件';
  152. }
  153. return true;
  154. }
  155. /**
  156. * @notes 校验佣金总比例
  157. * @param $value
  158. * @param $rule
  159. * @param $data
  160. * @return bool|string
  161. * @author Tab
  162. * @date 2021/12/28 18:55
  163. */
  164. public function checkRatioSum($value, $rule, $data)
  165. {
  166. $selfRatio = $data['self_ratio'] ?: 0;
  167. $firstRatio = $data['first_ratio'] ?: 0;
  168. $secondRatio = $data['second_ratio'] ?: 0;
  169. if ($selfRatio + $firstRatio + $secondRatio > 100) {
  170. return '等级佣金比例总和不能超过100%';
  171. }
  172. return true;
  173. }
  174. }