diff --git a/app/admin/controller/Goods.php b/app/admin/controller/Goods.php index 481b66e..0ecb5b2 100755 --- a/app/admin/controller/Goods.php +++ b/app/admin/controller/Goods.php @@ -38,6 +38,16 @@ class Goods extends Base $field = "*"; $order = "id desc"; $data = GoodsModel::getList($whe, $field, $order, $this->page); + + // 查询可用的盒子类型 + $goodsTypeList = Db::name('goods_type') + ->field('value,sort_order,remark,is_fenlei,fl_name') + ->where('is_fenlei', 1) + ->order('sort_order') + ->select() + ->toArray(); + + View::assign('goodsTypeList', $goodsTypeList); View::assign('list', $data['list']); View::assign('count', $data['count']); View::assign('page', $data['page']); @@ -52,7 +62,16 @@ class Goods extends Base if (!$request->isPost()) { $item_card = \app\common\model\ItemCard::where('id', '<', 3)->select()->toArray(); $shang = Shang::where('id', 'between', [34, 38])->select()->toArray(); - + + // 查询可用的盒子类型 + $goodsTypeList = Db::name('goods_type') + ->field('value,sort_order,remark,is_fenlei,fl_name') + ->where('is_fenlei', 1) + ->order('sort_order') + ->select() + ->toArray(); + + View::assign('goodsTypeList', $goodsTypeList); View::assign('shang', $shang); View::assign('item_card', $item_card); return View::fetch("Goods/goods_add"); @@ -224,6 +243,16 @@ class Goods extends Base $type = $info['type']; $item_card = \app\common\model\ItemCard::where('id', '<', 3)->select()->toArray(); $shang = Shang::where('id', 'between', [34, 38])->select()->toArray(); + + // 查询可用的盒子类型 + $goodsTypeList = Db::name('goods_type') + ->field('value,sort_order,remark,is_fenlei,fl_name') + ->where('is_fenlei', 1) + ->order('sort_order') + ->select() + ->toArray(); + + View::assign('goodsTypeList', $goodsTypeList); View::assign('shang', $shang); View::assign('item_card', $item_card); View::assign('type', $type); diff --git a/app/admin/controller/GoodsType.php b/app/admin/controller/GoodsType.php new file mode 100644 index 0000000..82b14be --- /dev/null +++ b/app/admin/controller/GoodsType.php @@ -0,0 +1,213 @@ +renderError('类型名称不能为空'); + } + + if (!is_numeric($data['value'])) { + return $this->renderError('类型key必须是数字'); + } + + // 检查key是否已存在 + $exists = GoodsTypeModel::getInfo(['value' => $data['value']]); + if ($exists) { + return $this->renderError('类型key已存在,请更换'); + } + + // 验证分类显示设置 + if (isset($data['is_fenlei']) && $data['is_fenlei'] == 1 && empty($data['fl_name'])) { + return $this->renderError('选择分类显示时,必须填写分类名称'); + } + + // 处理表单数据 + $data['is_show'] = isset($data['is_show']) ? 1 : 0; + $data['is_fenlei'] = isset($data['is_fenlei']) ? 1 : 0; + $data['sort_order'] = intval($data['sort_order']); + + // 保存数据 + $result = GoodsTypeModel::add($data); + if ($result) { + return $this->renderSuccess('添加成功'); + } else { + return $this->renderError('添加失败'); + } + } + + // 渲染视图,去掉布局,以便在弹出层中使用 + View::engine()->layout(false); + return View::fetch('Goods/type_add'); + } + + /** + * 编辑盒子类型 + */ + public function edit($id = 0) + { + if (Request::isPost()) { + // 获取表单数据 + $data = input('post.'); + + // 表单验证 + if (empty($data['name'])) { + return $this->renderError('类型名称不能为空'); + } + + if (!is_numeric($data['value'])) { + return $this->renderError('类型key必须是数字'); + } + + // 检查key是否已存在(排除自身) + $exists = GoodsTypeModel::getInfo([ + ['value', '=', $data['value']], + ['id', '<>', $id] + ]); + if ($exists) { + return $this->renderError('类型key已存在,请更换'); + } + + // 验证分类显示设置 + if (isset($data['is_fenlei']) && $data['is_fenlei'] == 1 && empty($data['fl_name'])) { + return $this->renderError('选择分类显示时,必须填写分类名称'); + } + + // 处理表单数据 + $data['is_show'] = isset($data['is_show']) ? 1 : 0; + $data['is_fenlei'] = isset($data['is_fenlei']) ? 1 : 0; + $data['sort_order'] = intval($data['sort_order']); + + // 更新数据 + $result = GoodsTypeModel::edit($id, $data); + if ($result) { + return $this->renderSuccess('更新成功'); + } else { + return $this->renderError('更新失败'); + } + } + + // 获取盒子类型信息 + $info = GoodsTypeModel::getInfo($id); + if (empty($info)) { + return $this->renderError('盒子类型不存在'); + } + + // 渲染视图,去掉布局,以便在弹出层中使用 + View::engine()->layout(false); + View::assign('info', $info); + return View::fetch('Goods/type_edit'); + } + + /** + * 删除盒子类型 + */ + public function delete($id = 0) + { + // 检查盒子类型是否存在 + $info = GoodsTypeModel::getInfo($id); + if (empty($info)) { + return $this->renderError('盒子类型不存在'); + } + + // 删除盒子类型 + $result = GoodsTypeModel::remove($id); + if ($result) { + return $this->renderSuccess('删除成功'); + } else { + return $this->renderError('删除失败'); + } + } + + /** + * 修改状态 + */ + public function status() + { + // 获取参数 + $id = input('id', 0); + $type = input('type', ''); + $value = input('value', 0); + + // 参数检查 + if (empty($id) || empty($type)) { + return $this->renderError('参数错误'); + } + + // 检查盒子类型是否存在 + $info = GoodsTypeModel::getInfo($id); + if (empty($info)) { + return $this->renderError('盒子类型不存在'); + } + + // 更新状态 + $data = []; + if ($type == 'is_show') { + $data['is_show'] = $value; + } elseif ($type == 'is_fenlei') { + $data['is_fenlei'] = $value; + } + + $result = GoodsTypeModel::edit($id, $data); + if ($result) { + return $this->renderSuccess('操作成功'); + } else { + return $this->renderError('操作失败'); + } + } + + /** + * 获取盒子类型列表数据(用于前端DataTable调用) + */ + public function getTypeList() + { + // 获取请求参数 + $page = input('page', 1); + $limit = input('limit', 15); + + // 计算偏移量 + $offset = ($page - 1) * $limit; + + // 获取数据总数 + $count = GoodsTypeModel::count(); + + // 获取当前页数据 + $list = GoodsTypeModel::order('sort_order asc') + ->limit($offset, $limit) + ->select() + ->toArray(); + + // 返回JSON格式数据 + return $this->renderSuccess('获取成功', [ + 'list' => $list, + 'count' => $count + ]); + } +} \ No newline at end of file diff --git a/app/admin/route/app.php b/app/admin/route/app.php index ea7ae92..5ebcfad 100755 --- a/app/admin/route/app.php +++ b/app/admin/route/app.php @@ -299,4 +299,13 @@ Route::rule('user_list', 'User/list', 'GET'); Route::rule('user_box', 'User/user_box', 'GET'); Route::rule('user_box_list', 'User/user_box_list', 'GET'); Route::rule('user_order', 'User/user_order', 'GET'); -Route::rule('user_order_list', 'User/user_order_list', 'GET'); \ No newline at end of file +Route::rule('user_order_list', 'User/user_order_list', 'GET'); + +// 盒子类型管理 +Route::get('goodstype/index', 'GoodsType/index'); +Route::get('goodstype/add', 'GoodsType/add'); +Route::post('goodstype/add', 'GoodsType/add'); +Route::rule('goodstype/edit', 'GoodsType/edit', 'GET|POST'); +Route::post('goodstype/delete', 'GoodsType/delete'); +Route::post('goodstype/status', 'GoodsType/status'); +Route::get('goodstype/getTypeList', 'GoodsType/getTypeList'); \ No newline at end of file diff --git a/app/admin/view/Goods/goods.html b/app/admin/view/Goods/goods.html index ef2bcec..3066942 100755 --- a/app/admin/view/Goods/goods.html +++ b/app/admin/view/Goods/goods.html @@ -25,15 +25,9 @@