pull/1/head
wanghousheng 10 months ago
parent 8c41c84c32
commit 861aa9519d
  1. 15
      app/common/model/Goods.php
  2. 58
      app/common/model/ServerCategory.php
  3. 13
      app/store/controller/Goods.php
  4. 83
      app/store/controller/Server.php
  5. 47
      app/store/model/ServerCategory.php

@ -1,4 +1,5 @@
<?php <?php
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ] // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
@ -13,6 +14,8 @@ declare (strict_types=1);
namespace app\common\model; namespace app\common\model;
use cores\BaseModel; use cores\BaseModel;
use think\db\BaseQuery;
use think\db\exception\DbException;
use think\model\relation\BelongsTo; use think\model\relation\BelongsTo;
use think\model\relation\HasMany; use think\model\relation\HasMany;
use think\Paginator; use think\Paginator;
@ -170,7 +173,7 @@ class Goods extends BaseModel
* @param array $param 查询条件 * @param array $param 查询条件
* @param int $listRows 分页数量 * @param int $listRows 分页数量
* @return mixed * @return mixed
* @throws \think\db\exception\DbException * @throws DbException
*/ */
public function getList(array $param = [], int $listRows = 15) public function getList(array $param = [], int $listRows = 15)
{ {
@ -217,9 +220,9 @@ class Goods extends BaseModel
/** /**
* 检索查询条件 * 检索查询条件
* @param array $param * @param array $param
* @return \think\db\BaseQuery * @return BaseQuery
*/ */
private function getQueryFilter(array $param): \think\db\BaseQuery private function getQueryFilter(array $param): BaseQuery
{ {
// 商品列表获取条件 // 商品列表获取条件
$params = $this->setQueryDefaultValue($param, [ $params = $this->setQueryDefaultValue($param, [
@ -246,7 +249,7 @@ class Goods extends BaseModel
// 商品分类 // 商品分类
if ($params['categoryId'] > 0) { if ($params['categoryId'] > 0) {
// 关联商品与分类关系记录表 // 关联商品与分类关系记录表
$GoodsCategoryRelName = (new GoodsCategoryRelModel)->getName(); $GoodsCategoryRelName = (new GoodsCategoryRelModel())->getName();
$query->join($GoodsCategoryRelName, "{$GoodsCategoryRelName}.goods_id = {$this->name}.goods_id"); $query->join($GoodsCategoryRelName, "{$GoodsCategoryRelName}.goods_id = {$this->name}.goods_id");
// 设置分类ID条件 // 设置分类ID条件
$query->where('goods_category_rel.category_id', '=', (int)$params['categoryId']); $query->where('goods_category_rel.category_id', '=', (int)$params['categoryId']);
@ -267,7 +270,9 @@ class Goods extends BaseModel
*/ */
protected function setGoodsListData($list, callable $callback = null) protected function setGoodsListData($list, callable $callback = null)
{ {
if ($list->isEmpty()) return $list; if ($list->isEmpty()) {
return $list;
}
// 遍历商品列表整理数据 // 遍历商品列表整理数据
foreach ($list as &$goods) { foreach ($list as &$goods) {
$goods = $this->setGoodsData($goods, $callback); $goods = $this->setGoodsData($goods, $callback);

@ -0,0 +1,58 @@
<?php
namespace app\common\model;
use cores\BaseModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\model\relation\HasOne;
class ServerCategory extends BaseModel
{
// 定义表名
protected $name = 'server_category';
// 定义主键
protected $pk = 'category_id';
/**
* 分类图片
* @return HasOne
*/
public function image(): HasOne
{
return $this->hasOne('UploadFile', 'file_id', 'image_id');
}
/**
* @notes:分类详情
* @param $where
* @param array $with
* @return ServerCategory|array|null
* @author: wanghousheng
*/
public static function detail($where, array $with = [])
{
return static::get($where, $with);
}
/**
* @notes:获取全部记录
* @param array $where
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public function getList(array $where = []): array
{
$where = $this->setQueryDefaultValue($where);
return $this->with(['image'])
->where($where)
->order(['sort', 'create_time'])
->select()
->toArray();
}
}

@ -12,6 +12,7 @@ declare (strict_types=1);
namespace app\store\controller; namespace app\store\controller;
use think\db\exception\DbException;
use think\response\Json; use think\response\Json;
use cores\exception\BaseException; use cores\exception\BaseException;
use app\store\model\Goods as GoodsModel; use app\store\model\Goods as GoodsModel;
@ -26,13 +27,13 @@ class Goods extends Controller
/** /**
* 商品列表 * 商品列表
* @return Json * @return Json
* @throws \think\db\exception\DbException * @throws DbException
*/ */
public function list(): Json public function list(): Json
{ {
// 获取列表记录 // 获取列表记录
$model = new GoodsModel; $model = new GoodsModel;
$list = $model->getList($this->request->param()); $list= $model->getList($this->request->param());
return $this->renderSuccess(compact('list')); return $this->renderSuccess(compact('list'));
} }
@ -55,7 +56,7 @@ class Goods extends Controller
* @return Json * @return Json
* @throws BaseException * @throws BaseException
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public function detail(int $goodsId): Json public function detail(int $goodsId): Json
@ -72,7 +73,7 @@ class Goods extends Controller
* @return Json * @return Json
* @throws BaseException * @throws BaseException
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public function basic(int $goodsId): Json public function basic(int $goodsId): Json
@ -88,7 +89,7 @@ class Goods extends Controller
* @return Json * @return Json
* @throws BaseException * @throws BaseException
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public function add(): Json public function add(): Json
@ -106,7 +107,7 @@ class Goods extends Controller
* @return Json * @return Json
* @throws BaseException * @throws BaseException
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public function edit(int $goodsId): Json public function edit(int $goodsId): Json

@ -0,0 +1,83 @@
<?php
namespace app\store\controller;
use app\store\model\ServerCategory;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;
class Server extends Controller
{
/**
* @notes:分类列表 10269
* @return Json
* @author: wanghousheng
*/
public function categoryList(): Json
{
$name = $this->request->post('name');
$where = [];
if (!empty($name)) {
$where[] = ['name', 'like', `%$name%`];
}
$model = new ServerCategory();
try {
$list = $model->getList($where);
} catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
return $this->renderError($e->getMessage() ?: '接口异常');
}
return $this->renderSuccess(compact('list'));
}
/**
* @notes:添加分类
* @return Json
* @author: wanghousheng
*/
public function addCategory(): Json
{
$name = $this->request->post('name');
if (!$name) {
return $this->renderError('名称不能为空');
}
$image_id = intval($this->request->post('image_id'));
if (!$image_id) {
return $this->renderError('图片不能为空');
}
$status = intval($this->request->post('status'));
$sort = intval($this->request->post('sort'));
$model = new ServerCategory();
if ($model->add(compact('image_id', 'name', 'status', 'sort'))) {
return $this->renderSuccess('添加成功');
}
return $this->renderError($model->getError() ?: '添加失败');
}
/**
* @notes:编辑分类
* @param int $categoryId
* @return Json
* @author: wanghousheng
*/
public function editCategory(int $categoryId)
{
$name = $this->request->post('name');
if (!$name) {
return $this->renderError('名称不能为空');
}
$image_id = intval($this->request->post('image_id'));
if (!$image_id) {
return $this->renderError('图片不能为空');
}
$status = intval($this->request->post('status'));
$sort = intval($this->request->post('sort'));
$category_id = $categoryId;
$model = new ServerCategory();
if ($model->edit(compact('image_id', 'name', 'status', 'sort', 'category_id'))) {
return $this->renderSuccess('编辑成功');
}
return $this->renderError($model->getError() ?: '编辑失败');
}
}

@ -0,0 +1,47 @@
<?php
namespace app\store\model;
use app\common\model\ServerCategory as ServerCategoryModel;
class ServerCategory extends ServerCategoryModel
{
/**
* @notes:新增
* @param $data
* @return bool
* @author: wanghousheng
*/
public function add($data): bool
{
$data['store_id'] = self::$storeId;
return $this->save($data);
}
/**
* @notes:编辑
* @param $data
* @return bool
* @author: wanghousheng
*/
public function edit($data): bool
{
// 是否删除图片
!isset($data['image_id']) && $data['image_id'] = 0;
return $this->save($data) !== false;
}
/**
* @notes:删除
* @return bool
* @author: wanghousheng
*/
public function remove(): bool
{
if (!static::detail(['category_id' => $this['category_id']])) {
$this->error = '记录不存在';
return false;
}
return $this->delete();
}
}
Loading…
Cancel
Save