UserLevelValidate.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\user;
  20. use app\common\{
  21. validate\BaseValidate,
  22. model\UserLevel
  23. };
  24. /**
  25. * 会员等级验证器
  26. * Class UserLevelValidate
  27. * @package app\adminapi\validate\user
  28. */
  29. class UserLevelValidate extends BaseValidate
  30. {
  31. protected $regex = ['condition'=>'/^[0-9]+(.[0-9]{1,2})?$/'];
  32. protected $rule = [
  33. 'id' => 'require',
  34. 'name' => 'require|max:32|unique:'.UserLevel::class.',name',
  35. 'rank' => 'require|unique:'.UserLevel::class.',rank|gt:0',
  36. 'level_discount' => 'require|in:0,1',
  37. 'discount' => 'requireIf:level_discount,1|between:0,10',
  38. 'condition' => 'requireCallback:checkRequire|array|checkCondition',
  39. // 'single_money' => 'requireIf:condition_type,1|regex:condition',
  40. // 'total_money' => 'requireIf:condition_type,1|regex:condition',
  41. // 'total_num' => 'requireIf:condition_type,1|regex:condition',
  42. ];
  43. protected $message = [
  44. 'id.require' => '请选择等级',
  45. 'name.require' => '请输入等级名称',
  46. 'name.max' => '等级名称最多为32个字符',
  47. 'name.unique' => '等级名称已存在',
  48. 'rank.require' => '请输入级别',
  49. 'rank.unique' => '级别重复,请重新输入',
  50. 'rank.gt' => '级别必须大于零',
  51. 'level_discount.require' => '请选择等级折扣',
  52. 'level_discount.in' => '等级折扣类型错误',
  53. 'discount.requireIf' => '请填写等级折扣',
  54. 'discount.between' => '等级折扣值在0~10之间',
  55. 'condition.requireCallback' => '请选择会员条件',
  56. 'condition.array' => '会员条件数据格式错误',
  57. ];
  58. //添加验证
  59. public function sceneAdd()
  60. {
  61. return $this->remove(['id'=>'require']);
  62. }
  63. //删除验证
  64. public function sceneDel(){
  65. return $this->only(['id'])
  66. ->append('id','checkLevel');
  67. }
  68. public function checkRequire($value,$data){
  69. if(1 != $data['rank'] && empty($data['condition'])){
  70. return true;
  71. }
  72. }
  73. public function checkLevel($value,$rule,$data){
  74. $userLevel = UserLevel::find($value);
  75. if(1 == $userLevel->rank) {
  76. return '系统默认等级不允许删除';
  77. }
  78. return true;
  79. }
  80. public function checkCondition($value,$rule,$data)
  81. {
  82. if(1 == $data['rank']){
  83. return true;
  84. }
  85. //
  86. $conditionType = $value['condition_type'] ?? '';
  87. $singleMoney = $value['single_money'] ?? '';
  88. $isSingleMoney = $value['is_single_money'] ?? '';
  89. $totalMoney = $value['total_money'] ?? '';
  90. $isTotalMoney = $value['is_total_money'] ?? '';
  91. $totalNum = $value['total_num'] ?? '';
  92. $isTotalNum = $value['is_total_num'] ?? '';
  93. //数据缺少情况
  94. if('' === $conditionType){
  95. return '请选择升级条件类型';
  96. }
  97. if('' === $isSingleMoney || '' === $isTotalMoney || '' === $isTotalNum){
  98. return '等级条件数据错误';
  99. }
  100. //没选择条件情况下
  101. if('0' === $isSingleMoney && '0' === $isTotalMoney && '0' === $isTotalNum){
  102. return '至少选择一个条件';
  103. }
  104. if($isSingleMoney){
  105. if(empty($singleMoney)){
  106. return '请输入单笔消费金额条件';
  107. }
  108. if($singleMoney <= 0){
  109. return '单笔消费金额条件必须大于零';
  110. }
  111. }
  112. if($isTotalMoney){
  113. if(empty($totalMoney)) {
  114. return '请输入累计消费金额条件';
  115. }
  116. if($totalMoney <= 0){
  117. return '单笔消费金额条件必须大于零';
  118. }
  119. }
  120. if($isTotalNum ){
  121. if(empty($totalNum)) {
  122. return '请输入累计消费次数条件';
  123. }
  124. if($totalNum <= 0){
  125. return '累计消费次数条件必须大于零';
  126. }
  127. }
  128. return true;
  129. }
  130. }