132 lines
4.0 KiB
PHP
Executable File
132 lines
4.0 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* 首页栏目商品控制器
|
|
*/
|
|
|
|
namespace app\admin\controller;
|
|
|
|
use think\Request;
|
|
use app\common\model\GoodsIndex;
|
|
use app\common\model\Goods;
|
|
|
|
use app\common\validate\GoodsIndexValidate;
|
|
|
|
class GoodsIndexController extends Controller
|
|
{
|
|
|
|
//列表
|
|
public function index(Request $request, GoodsIndex $model)
|
|
{
|
|
$param = $request->param();
|
|
$model = $model->scope('where', $param);
|
|
|
|
$data = $model->paginate($this->admin['per_page'], false, ['query'=>$request->get()]);
|
|
//关键词,排序等赋值
|
|
$this->assign($request->get());
|
|
|
|
$this->assign([
|
|
'data' => $data,
|
|
'page' => $data->render(),
|
|
'total' => $data->total(),
|
|
]);
|
|
return $this->fetch();
|
|
}
|
|
|
|
//添加
|
|
public function add(Request $request, GoodsIndex $model, GoodsIndexValidate $validate)
|
|
{
|
|
if ($request->isPost()) {
|
|
$param = $request->param();
|
|
$validate_result = $validate->scene('add')->check($param);
|
|
if (!$validate_result) {
|
|
return error($validate->getError());
|
|
}
|
|
$goods_ids=$param['goods_id'];
|
|
$field_data = array();
|
|
foreach ($goods_ids as $key => $value) {
|
|
if($param['goods_id'][$key][0]){
|
|
$field_data[] =$param['goods_id'][$key][0];
|
|
}
|
|
}
|
|
if(count($field_data)=='0'){
|
|
return error('请选择商品');
|
|
}
|
|
$param['goods_ids']=json_encode(array_unique($field_data));
|
|
$result = $model::create($param);
|
|
|
|
$url = URL_BACK;
|
|
if(isset($param['_create']) && $param['_create']==1){
|
|
$url = URL_RELOAD;
|
|
}
|
|
|
|
return $result ? success('添加成功',$url) : error();
|
|
}
|
|
$this->assign([
|
|
'goods_list' => Goods::all(),
|
|
'number'=>0,
|
|
'goods_ids'=>array()
|
|
]);
|
|
return $this->fetch();
|
|
}
|
|
|
|
//修改
|
|
public function edit($id, Request $request, GoodsIndex $model, GoodsIndexValidate $validate)
|
|
{
|
|
|
|
$data = $model::get($id);
|
|
if ($request->isPost()) {
|
|
$param = $request->param();
|
|
$validate_result = $validate->scene('edit')->check($param);
|
|
if (!$validate_result) {
|
|
return error($validate->getError());
|
|
}
|
|
$goods_ids=$param['goods_id'];
|
|
$field_data = array();
|
|
foreach ($goods_ids as $key => $value) {
|
|
if($param['goods_id'][$key][0]){
|
|
$field_data[] =$param['goods_id'][$key][0];
|
|
}
|
|
}
|
|
if(count($field_data)=='0'){
|
|
return error('请选择商品');
|
|
}
|
|
$param['goods_ids']=json_encode(array_unique($field_data));
|
|
$result = $data->save($param);
|
|
return $result ? success() : error();
|
|
}
|
|
$goods_ids=json_decode($data['goods_ids']);
|
|
$this->assign([
|
|
'data' => $data,
|
|
'number'=>count($goods_ids),
|
|
'goods_list' => Goods::all(),
|
|
'goods_ids'=>$goods_ids
|
|
]);
|
|
return $this->fetch('add');
|
|
|
|
}
|
|
|
|
//删除
|
|
public function del($id, GoodsIndex $model)
|
|
{
|
|
if (count($model->noDeletionId) > 0) {
|
|
if (is_array($id)) {
|
|
if (array_intersect($model->noDeletionId, $id)) {
|
|
return error('ID为' . implode(',', $model->noDeletionId) . '的数据无法删除');
|
|
}
|
|
} else if (in_array($id, $model->noDeletionId)) {
|
|
return error('ID为' . $id . '的数据无法删除');
|
|
}
|
|
}
|
|
|
|
if ($model->softDelete) {
|
|
$result = $model->whereIn('id', $id)->useSoftDelete('delete_time', time())->delete();
|
|
} else {
|
|
$result = $model->whereIn('id', $id)->delete();
|
|
}
|
|
|
|
return $result ? success('操作成功', URL_RELOAD) : error();
|
|
}
|
|
|
|
|
|
}
|