Browse Source

request

master
ZLW-PC\Administrator 7 years ago
parent
commit
49eaccedd0
  1. 6
      app/Http/Controllers/Admin/ArticleController.php
  2. 36
      app/Http/Controllers/Admin/GoodsController.php
  3. 12
      app/Http/Model/Goods.php
  4. 3
      app/Http/Requests/ArticleRequest.php
  5. 155
      app/Http/Requests/GoodsRequest.php
  6. 36
      resources/views/admin/goods/edit.blade.php

6
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

36
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

12
app/Http/Model/Goods.php

@ -1,6 +1,7 @@
<?php
namespace App\Http\Model;
use DB;
use Validator;
class Goods extends BaseModel
{
@ -150,6 +151,17 @@ class Goods extends BaseModel
public static function add(array $data)
{
$validator = Validator::make($data, [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
if ($id = self::insertGetId($data))
{
return $id;

3
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必须为数字',

155
app/Http/Requests/GoodsRequest.php

@ -0,0 +1,155 @@
<?php
namespace App\Http\Requests;
use Illuminate\Http\Request;
class GoodsRequest extends Request
{
//总的验证规则
protected $rules = [
'id' => '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;
}
}

36
resources/views/admin/goods/edit.blade.php

@ -77,6 +77,42 @@ function upImage()
}
</script>
<script type="text/plain" id="ueditorimg"></script>
<tr>
<td align="right" style="vertical-align:middle;">商品图片:</td>
<td style="vertical-align:middle;" id="duotulist"><button type="button" onclick="upImage2();">选择图片</button><?php if($goods_img_list){foreach($goods_img_list as $k=>$v){echo '<img style="margin-left:10px;" src="'.$v['url'].'" width="120" height="80"><input name="goods_img[]" type="text" value="'.$v['url'].'" style="display:none;">';}} ?></td>
</tr>
<script type="text/javascript">
var _editor2;
$(function() {
//重新实例化一个编辑器,防止在上面的editor编辑器中显示上传的图片或者文件
_editor2 = UE.getEditor('ueditordimg');
_editor2.ready(function () {
//设置编辑器不可用
_editor2.setDisabled('insertimage');
//隐藏编辑器,因为不会用到这个编辑器实例,所以要隐藏
_editor2.hide();
//侦听图片上传
_editor2.addListener('beforeInsertImage', function (t, arg) {
$("#duotulist").html('<button type="button" onclick="upImage2();">选择图片</button>');
for (var i=0;i<arg.length;i++)
{
if(i<8)
{
$("#duotulist").append('<img style="margin-left:10px;" src="'+arg[i].src+'" width="120" height="80"><input name="goods_img[]" type="text" value="'+arg[i].src+'" style="display:none;">');
}
}
})
});
});
//弹出图片上传的对话框
function upImage2()
{
var myImage = _editor2.getDialog("insertimage");
myImage.render();
myImage.open();
}
</script>
<script type="text/plain" id="ueditordimg"></script>
<tr>
<td align="right">商品类目:</td>
<td>

Loading…
Cancel
Save