GoodsItemValidate.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\goods;
  20. use app\common\{enum\FreightEnum,
  21. enum\GoodsEnum,
  22. model\Freight,
  23. model\GoodsItem,
  24. model\GoodsSpec,
  25. model\GoodsSpecValue,
  26. validate\BaseValidate};
  27. /**
  28. * 商品规格验证器
  29. * Class GoodsItemValidate
  30. * @package app\adminapi\validate\goods
  31. */
  32. class GoodsItemValidate extends BaseValidate{
  33. protected $rule = [
  34. 'spec_value' => 'requireIf:spec_type,'.GoodsEnum::SEPC_TYPE_MORE.'|checkSpecValue',
  35. 'spec_value_list' => 'requireIf:spec_type,'.GoodsEnum::SEPC_TYPE_MORE.'|checkSpecValueList',
  36. ];
  37. protected $message = [
  38. 'spec_value.requireIf' => '请输入商品规格项',
  39. 'spec_value_list.requireIf' => '请输入商品规格信息',
  40. ];
  41. //验证添加规格不允许重复
  42. public function checkSpecValue($value, $rule, $data)
  43. {
  44. $specNameArray = [];
  45. $specValueArray = [];
  46. //编辑时验证规格名和规格值
  47. if(isset($data['id'])){
  48. $specIds = GoodsSpec::where(['goods_id'=>$data['id']])->column('id');
  49. $specVluaeIds = GoodsSpecValue::where(['goods_id'=>$data['id']])->column('id');
  50. }
  51. foreach ($value as $specVal){
  52. //编辑时验证数据是否缺少
  53. if(isset($data['id'])){
  54. if(!isset($specVal['id'])){
  55. return '规格名id缺少';
  56. }
  57. if($specVal['id'] > 0 && !in_array($specVal['id'],$specIds)){
  58. return '规格名信息不匹配';
  59. }
  60. }
  61. if(empty($specVal['name'])){
  62. return '规格名称不能为空';
  63. }
  64. //验证规格名是否存在重复
  65. if(in_array($specVal['name'],$specNameArray)){
  66. return '规格名:'.$specVal['name'].'重复';
  67. }
  68. $specNameArray[] = $specVal['name'];
  69. //验证规格值是否存在重复
  70. foreach ($specVal['spec_list'] as $spec){
  71. //编辑时验证数据是否缺少
  72. if(isset($data['id'])){
  73. if(!isset($spec['id'])){
  74. return '规格值id缺少';
  75. }
  76. if($spec['id'] > 0 && !in_array($spec['id'],$specVluaeIds)){
  77. return '规格值信息不匹配';
  78. }
  79. }
  80. if(empty($spec['value'])){
  81. return '规格值不能为空';
  82. }
  83. if(in_array($spec['value'],$specValueArray)){
  84. return '规格值:'.$spec.'重复';
  85. }
  86. $specValueArray[] = $spec['value'];
  87. }
  88. $specValueArray = [];
  89. }
  90. return true;
  91. }
  92. //验证规格
  93. public function checkSpecValueList($value, $rule, $data){
  94. //编辑时验证规格信息
  95. if(isset($data['id'])){
  96. $itemIds = GoodsItem::where(['goods_id'=>$data['id']])->column('id');
  97. }
  98. $chargeWay = 0;
  99. if(3 == $data['express_type']){
  100. $chargeWay = Freight::where(['id'=>$data['express_template_id']])->value('charge_way');
  101. }
  102. //单规格验证
  103. if (GoodsEnum::SEPC_TYPE_SIGNLE == $data['spec_type']) {
  104. foreach ($value as $spec_list) {
  105. //编辑时验证单规格信息
  106. if(isset($data['id'])){
  107. //编辑时必须带上id,新增留空
  108. if(!isset($spec_list['id'])){
  109. return '规格id缺少';
  110. }
  111. if($spec_list['id'] > 0 && !in_array($spec_list['id'],$itemIds)){
  112. return '规格信息不匹配';
  113. }
  114. }
  115. //验证必填是否缺少信息
  116. if ('' == $spec_list['sell_price']) {
  117. return '请输入的价格';
  118. }
  119. if($spec_list['sell_price'] < 0){
  120. return '价格不能小于零';
  121. }
  122. // if ('' == $spec_list['lineation_price']) {
  123. // return '请输入划线价格';
  124. // }
  125. if($spec_list['lineation_price'] && $spec_list['lineation_price'] < 0){
  126. return '划线价格不能小于零';
  127. }
  128. if ('' ==$spec_list['stock']) {
  129. return '请输入库存';
  130. }
  131. if($spec_list['stock'] < 0){
  132. return '库存不能小于零';
  133. }
  134. if(FreightEnum::CHARGE_WAY_VOLUME == $chargeWay && (empty($spec_list['volume']) || $spec_list['volume'] <= 0)){
  135. return '当前运费模板是按体积计算运费,请输入体积';
  136. }
  137. if(FreightEnum::CHARGE_WAY_WEIGHT == $chargeWay && (empty($spec_list['weight']) || $spec_list['weight'] <= 0)){
  138. return '当前运费模板是按重量计算运费,请输入重量';
  139. }
  140. }
  141. }else {
  142. //转换数据结构,ids为索引
  143. $spec_value_list = array_column($value, null, 'ids');
  144. foreach ($data['server_spec_value_list'] as $spec_list){
  145. $spec = $spec_value_list[$spec_list['ids']] ?? [];
  146. if (empty($spec)) {
  147. return '规格信息错误';
  148. }
  149. //编辑时验证多规格信息
  150. if(isset($data['id'])){
  151. //编辑时必须带上id,新增留空
  152. if(!isset($spec['id'])){
  153. return '规格id缺少';
  154. }
  155. if($spec['id'] > 0 && !in_array($spec['id'],$itemIds)){
  156. return '规格信息不匹配';
  157. }
  158. }
  159. //验证必填是否缺少信息
  160. if('' == $spec['sell_price'] ){
  161. return '请输入' . $spec_list['spec_value'] . '的价格';
  162. }
  163. if ($spec['sell_price'] < 0) {
  164. return $spec_list['spec_value'] . '的价格不能小于零';
  165. }
  166. // if('' == $spec['lineation_price']){
  167. // return '请输入' . $spec_list['spec_value'] . '的划线价格';
  168. // }
  169. if ($spec['lineation_price'] && $spec['lineation_price'] < 0) {
  170. return $spec_list['spec_value'] . '的划线价格不能小于零';
  171. }
  172. if('' == $spec['stock']){
  173. return '请输入' . $spec_list['spec_value'] . '的库存';
  174. }
  175. if ($spec['stock'] < 0) {
  176. return $spec_list['spec_value'] . '的库存不能小于零';
  177. }
  178. if(FreightEnum::CHARGE_WAY_VOLUME == $chargeWay && empty($spec['volume'])){
  179. return '当前运费模板是按体积计算运费,请填写规格:'.$spec_list['spec_value'] .'的体积';
  180. }
  181. if(FreightEnum::CHARGE_WAY_VOLUME == $chargeWay && $spec['volume'] <= 0){
  182. return '当前运费模板是按体积计算运费,规格:'.$spec_list['spec_value'] .'的体积必须大于零';
  183. }
  184. if(FreightEnum::CHARGE_WAY_WEIGHT == $chargeWay && empty($spec['weight'])){
  185. return '当前运费模板是按重量计算运费,请填写规格:'.$spec_list['spec_value'] .'的重量';
  186. }
  187. if(FreightEnum::CHARGE_WAY_WEIGHT == $chargeWay && $spec['weight'] <= 0){
  188. return '当前运费模板是按重量计算运费,规格:'.$spec_list['spec_value'] .'的重量必须大于零';
  189. }
  190. }
  191. }
  192. return true;
  193. }
  194. }