GGoods.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace app\common\validate;
  3. use think\Validate;
  4. use think\Db;
  5. class GGoods extends Validate{
  6. protected $rule = [
  7. 'title|商品名称' => 'require|length:1,20|checkUnique',
  8. 'price|商品价格' => 'require',
  9. 'stock|库存' => 'require',
  10. 'cate_id|商品分类' => 'require|gt:0',
  11. 'img|商品图片' => 'require',
  12. 'imgs|宣传图' => 'require',
  13. 'content|详情' => 'require',
  14. ];
  15. protected $message = [
  16. 'cate_id.require'=>'商品分类不能为空',
  17. 'cate_id.gt'=>'商品分类不能为空'
  18. ];
  19. protected function checkUnique($value, $rule, $data=[]){
  20. if(isset($data['id']) && $data['id'] > 0){
  21. $ret = Db::name('g_goods')
  22. ->where('del',0)
  23. ->where('title',$data['title'])
  24. ->where('org_id',$data['org_id'])
  25. ->where('id','<>',$data['id'])
  26. ->find();
  27. }else{
  28. $ret = Db::name('g_goods')
  29. ->where('del',0)
  30. ->where('title',$data['title'])
  31. ->where('org_id',$data['org_id'])
  32. ->find();
  33. }
  34. return $ret?'名称已被使用':true;
  35. }
  36. }