diff --git a/app/Http/Controllers/Admin/ArticleController.php b/app/Http/Controllers/Admin/ArticleController.php index 2c260c7..9803ea7 100644 --- a/app/Http/Controllers/Admin/ArticleController.php +++ b/app/Http/Controllers/Admin/ArticleController.php @@ -90,15 +90,15 @@ class ArticleController extends CommonController public function doadd() { //数据验证 - /* $validate = new ArticleRequest(); + $validate = new ArticleRequest(); $validator = Validator::make($_REQUEST, $validate->getSceneRules('add'), $validate->getSceneRulesMessages()); if ($validator->fails()) { - //$validator->errors()->first(); + dd($validator->errors()->first()); //$validator->errors()->all(); error_jump('参数错误'); - } */ + } $litpic="";if(!empty($_POST["litpic"])){$litpic = $_POST["litpic"];}else{$_POST['litpic']="";} //缩略图 if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}} //description diff --git a/app/Http/Controllers/Admin/GoodsController.php b/app/Http/Controllers/Admin/GoodsController.php index 21972ec..714ba0e 100644 --- a/app/Http/Controllers/Admin/GoodsController.php +++ b/app/Http/Controllers/Admin/GoodsController.php @@ -82,9 +82,25 @@ class GoodsController extends CommonController if(isset($_POST['promote_start_date'])){$_POST['promote_start_date'] = strtotime($_POST['promote_start_date']);} if(isset($_POST['promote_end_date'])){$_POST['promote_end_date'] = strtotime($_POST['promote_end_date']);} if(empty($_POST['promote_price'])){unset($_POST['promote_price']);} + if(!empty($_POST['goods_img'])) + { + $goods_img = $_POST['goods_img']; + $_POST['goods_img'] = $_POST['goods_img'][0]; + } - if(DB::table('goods')->insert(array_filter($_POST))) + if($goods_id = DB::table('goods')->insertGetId(array_filter($_POST))) { + if(isset($goods_img)) + { + $tmp = []; + foreach($goods_img as $k=>$v) + { + $tmp[] = ['url'=>$v,'goods_id'=>$goods_id,'add_time'=>time()]; + } + + DB::table('goods_img')->insert($tmp); + } + success_jump('添加成功!', route('admin_goods')); } else @@ -104,6 +120,7 @@ class GoodsController extends CommonController $data['post'] = object_to_array($goods, 1); $data['goodsbrand_list'] = object_to_array(DB::table('goods_brand')->where('status', 0)->orderBy('listorder', 'asc')->get()); //商品品牌 + $data['goods_img_list'] = object_to_array(DB::table('goods_img')->where(array('goods_id'=>$id))->orderBy('listorder', 'asc')->get()); //商品图片 return view('admin.goods.edit', $data); } @@ -138,9 +155,26 @@ class GoodsController extends CommonController if(isset($_POST['promote_start_date'])){$_POST['promote_start_date'] = strtotime($_POST['promote_start_date']);} if(isset($_POST['promote_end_date'])){$_POST['promote_end_date'] = strtotime($_POST['promote_end_date']);} if(empty($_POST['promote_price'])){unset($_POST['promote_price']);} + if(!empty($_POST['goods_img'])) + { + $goods_img = $_POST['goods_img']; + $_POST['goods_img'] = $_POST['goods_img'][0]; + } if(DB::table('goods')->where('id', $id)->update($_POST)) { + if(isset($goods_img)) + { + $tmp = []; + foreach($goods_img as $k=>$v) + { + $tmp[] = ['url'=>$v,'goods_id'=>$id,'add_time'=>time()]; + } + + DB::table('goods_img')->where(array('goods_id'=>$id))->delete(); + DB::table('goods_img')->insert($tmp); + } + success_jump('修改成功!', route('admin_goods')); } else diff --git a/app/Http/Model/Goods.php b/app/Http/Model/Goods.php index f14a841..4dc5c61 100644 --- a/app/Http/Model/Goods.php +++ b/app/Http/Model/Goods.php @@ -1,6 +1,7 @@ 'required|unique:posts|max:255', + 'body' => 'required', + ]); + + if ($validator->fails()) { + return redirect('post/create') + ->withErrors($validator) + ->withInput(); + } + if ($id = self::insertGetId($data)) { return $id; diff --git a/app/Http/Requests/ArticleRequest.php b/app/Http/Requests/ArticleRequest.php index 4790317..bf8092f 100644 --- a/app/Http/Requests/ArticleRequest.php +++ b/app/Http/Requests/ArticleRequest.php @@ -10,7 +10,7 @@ class ArticleRequest extends Request 'id' => 'required|integer', 'title' => 'required|max:150', 'typeid' => 'required|integer', - 'click' => 'required|integer', + 'click' => ['required','integer'], ]; //总的自定义错误信息 @@ -19,6 +19,7 @@ class ArticleRequest extends Request 'title.required' => '必须填写标题', 'typeid.required' => '类目ID必填', 'typeid.integer' => '栏目ID必须为数字', + 'click.required' => '必须填写点击量', 'click.integer' => '点击必须为数字', 'id.required' => 'ID必填', 'id.integer' => 'ID必须为数字', diff --git a/app/Http/Requests/GoodsRequest.php b/app/Http/Requests/GoodsRequest.php new file mode 100644 index 0000000..0c5a7f4 --- /dev/null +++ b/app/Http/Requests/GoodsRequest.php @@ -0,0 +1,155 @@ + 'required|integer', + 'title' => 'required|max:150', + 'typeid' => 'required|integer', + 'click' => 'required|integer', + 'tuijian' => 'integer', + 'sn' => 'max:60', + 'price' => ['required','regex:/^\d{0,10}(\.\d{0,2})?$/'], + 'litpic' => 'required|max:100', + 'pubdate' => 'integer', + 'add_time' => 'required|integer', + 'keywords' => 'max:60', + 'seotitle' => 'max:150', + 'description' => 'max:250', + 'status' => 'between:[0,5]', + 'shipping_fee' => ['regex:/^\d{0,10}(\.\d{0,2})?$/'], + 'market_price' => ['required','regex:/^\d{0,10}(\.\d{0,2})?$/'], + 'goods_number' => 'required|integer|between:[1,99999]', + 'user_id' => 'integer', + 'sale' => 'integer|between:[1,99999]', + 'cost_price' => ['regex:/^\d{0,10}(\.\d{0,2})?$/'], + 'goods_weight' => ['regex:/^\d{0,10}(\.\d{0,2})?$/'], + 'point' => 'integer|between:[1,99999]', + 'comments' => 'integer|between:[1,99999]', + 'promote_start_date' => 'integer', + 'promote_end_date' => 'integer', + 'promote_price' => ['regex:/^\d{0,10}(\.\d{0,2})?$/'], + 'goods_img' => 'max:250', + 'warn_number' => 'integer|between:[1,99]', + 'listorder' => 'integer|between:[1,9999]', + ]; + + //总的自定义错误信息 + protected $messages = [ + 'title.max' => '标题不能大于150个字', + 'title.required' => '必须填写标题', + 'typeid.required' => '栏目ID必填', + 'typeid.integer' => '栏目ID必须为数字', + 'click.integer' => '点击必须为数字', + 'id.required' => 'ID必填', + 'id.integer' => 'ID必须为数字', + 'tuijian.integer' => '推荐等级必须是数字', + 'sn.max' => '货号不能超过60个字符', + 'price.required' => '产品价格必填', + 'price.regex' => '产品价格只能带2位小数的数字', + 'litpic.required' => '缩略图必须上传', + 'litpic.max' => '缩略图不能超过100个字符', + 'pubdate.integer' => '更新时间格式不正确', + 'add_time.required' => '添加时间必填', + 'add_time.integer' => '添加时间必须是数字', + 'keywords.max' => '关键词不能超过60个字符', + 'seotitle.max' => 'seo标题不能超过150个字符', + 'description.max' => '描述不能超过250个字符', + 'status.between' => '商品状态 0正常 1已删除 2下架 3申请上架', + 'shipping_fee.regex' => '运费格式不正确,运费只能带2位小数的数字', + 'market_price.required' => '市场价格必填', + 'market_price.regex' => '市场价格格式不正确,市场价格只能带2位小数的数字', + 'goods_number.required' => '库存必填', + 'goods_number.integer' => '库存必须是数字', + 'goods_number.between' => '库存只能1-99999', + 'user_id.integer' => '发布者ID必须是数字', + 'sale.integer' => '销量必须是数字', + 'sale.between' => '销量只能1-99999', + 'cost_price.regex' => '成本价格格式不正确,成本价格只能带2位小数的数字', + 'goods_weight.regex' => '重量格式不正确,重量只能带2位小数的数字', + 'point.integer' => '购买该商品时每笔成功交易赠送的积分数量必须是数字', + 'point.between' => '购买该商品时每笔成功交易赠送的积分数量只能1-99999', + 'comments.integer' => '评论次数必须是数字', + 'comments.between' => '评论次数只能1-9999999', + 'promote_start_date.integer' => '促销价格开始日期必须是数字', + 'promote_end_date.integer' => '促销价格结束日期必须是数字', + 'promote_price.regex' => '促销价格格式不正确,促销价格只能带2位小数的数字', + 'goods_img.max' => '商品的实际大小图片不能超过250个字符', + 'warn_number.integer' => '商品报警数量必须是数字', + 'warn_number.between' => '商品报警数量只能1-99', + 'listorder.integer' => '排序必须是数字', + 'listorder.between' => '排序只能1-9999', + 'brand_id.integer' => '商品品牌ID必须是数字', + ]; + + //场景验证规则 + protected $scene = [ + 'add' => ['typeid', 'title', 'tuijian', 'click', 'sn', 'price', 'litpic', 'pubdate', 'add_time', 'keywords', 'seotitle', 'description', 'status', 'shipping_fee', 'market_price', 'goods_number', 'user_id', 'sale', 'cost_price', 'goods_weight', 'point', 'comments', 'promote_start_date', 'promote_price', 'promote_end_date', 'goods_img', 'warn_number', 'listorder', 'brand_id'], + 'edit' => ['typeid', 'title', 'tuijian', 'click', 'sn', 'price', 'litpic', 'pubdate', 'add_time', 'keywords', 'seotitle', 'description', 'status', 'shipping_fee', 'market_price', 'goods_number', 'user_id', 'sale', 'cost_price', 'goods_weight', 'point', 'comments', 'promote_start_date', 'promote_price', 'promote_end_date', 'goods_img', 'warn_number', 'listorder', 'brand_id'], + 'del' => ['id'], + ]; + + /** + * Determine if the user is authorized to make this request. + * + * @return bool + */ + public function authorize() + { + return true; //修改为true + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return $this->rules; + } + + /** + * 获取被定义验证规则的错误消息. + * + * @return array + */ + public function messages() + { + return $this->messages; + } + + //获取场景验证规则 + public function getSceneRules($name, $fields = null) + { + $res = array(); + + if(!isset($this->scene[$name])) + { + return false; + } + + $scene = $this->scene[$name]; + if($fields != null && is_array($fields)) + { + $scene = $fields; + } + + foreach($scene as $k=>$v) + { + if(isset($this->rules[$v])){$res[$v] = $this->rules[$v];} + } + + return $res; + } + + //获取场景验证规则自定义错误信息 + public function getSceneRulesMessages() + { + return $this->messages; + } +} \ No newline at end of file diff --git a/resources/views/admin/goods/edit.blade.php b/resources/views/admin/goods/edit.blade.php index ba5f8dd..fd17d17 100644 --- a/resources/views/admin/goods/edit.blade.php +++ b/resources/views/admin/goods/edit.blade.php @@ -77,6 +77,42 @@ function upImage() } +