feature/main20240421
lqmac 6 months ago
parent e5454f73e3
commit 0251d52567
  1. 124
      app/admin/controller/Channel.php
  2. 116
      app/admin/model/Channel.php
  3. 15
      app/common/model/Channel.php

@ -0,0 +1,124 @@
<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\admin\controller;
use think\response\Json;
use app\admin\controller\Controller;
use app\admin\model\Channel as ChannelModel;
/**
* 商家后台菜单控制器
* Class Menu
* @package app\store\controller
*/
class Channel extends Controller
{
/**
* 菜单列表
* @return Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function index(): Json
{
$model = new ChannelModel;
$list = $model->getList();
return $this->renderSuccess(compact('list'));
}
/**
* 菜单详情
* @param int $menuId
* @return Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function info(int $id): Json
{
// 菜单详情
$model = ChannelModel::detail($id);
return $this->renderSuccess(['info' => $model]);
}
/**
* 新增菜单
* @return Json
*/
public function add(): Json
{
// 新增记录
$model = new ChannelModel;
if ($model->add($this->postForm())) {
return $this->renderSuccess('添加成功');
}
return $this->renderError($model->getError() ?: '添加失败');
}
/**
* 编辑菜单
* @param $menuId
* @return Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function edit(int $id): Json
{
// 菜单详情
$model = ChannelModel::detail($id);
// 更新记录
if ($model->edit($this->postForm())) {
return $this->renderSuccess('更新成功');
}
return $this->renderError($model->getError() ?: '更新失败');
}
/**
* 设置菜单绑定的Api
* @param int $menuId
* @return Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function setApis(int $menuId): Json
{
// 菜单详情
$model = MenuModel::detail($menuId);
// 更新记录
if ($model->setApis($this->postForm())) {
return $this->renderSuccess('操作成功');
}
return $this->renderError($model->getError() ?: '操作失败');
}
/**
* 删除菜单
* @param int $menuId
* @return Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function delete(int $menuId): Json
{
// 菜单详情
$model = MenuModel::detail($menuId);
if (!$model->remove()) {
return $this->renderError($model->getError() ?: '删除失败');
}
return $this->renderSuccess('删除成功');
}
}

@ -0,0 +1,116 @@
<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\admin\model;
use app\common\model\Channel as ChannelModel;
/**
* 商家后台菜单模型
* Class Menu
* @package app\admin\model\store
*/
class Channel extends ChannelModel
{
/**
* 获取列表数据
* @param bool $isRecycle
* @return \think\Paginator
* @throws \think\db\exception\DbException
*/
public function getList(bool $isRecycle = false): \think\Paginator
{
return $this->where('status', '=', 1)
->order(['weigh' => 'asc', 'create_time' => 'desc'])
->paginate(15);
}
/**
* 新增记录
* @param array $data
* @return bool
*/
public function add(array $data): bool
{
return $this->save($data);
}
/**
* 更新记录
* @param array $data
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function edit(array $data): bool
{
return $this->save($data);
}
/**
* 设置菜单的API权限
* @param array $data
* @return bool
*/
public function setApis(array $data): bool
{
if (empty($data['apiIds'])) {
$this->error = 'API权限不能为空';
return false;
}
// 根据菜单id批量更新API关联记录
return (new MenuApiModel)->updateByMenuId($this['menu_id'], $data['apiIds']);
}
/**
* 删除菜单
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \Exception
*/
public function remove(): bool
{
// 判断是否存在下级菜单
if (self::detail(['parent_id' => $this['menu_id']])) {
$this->error = '当前菜单下存在子菜单或操作,请先删除';
return false;
}
return $this->delete();
}
/**
* 获取所有上级id集
* @param int $menuId
* @param null $menuList
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function getTopMenuIds(int $menuId, $menuList = null): array
{
static $ids = [];
is_null($menuList) && $menuList = $this->getAll();
foreach ($menuList as $item) {
if ($item['menu_id'] == $menuId && $item['parent_id'] > 0) {
$ids[] = $item['parent_id'];
$this->getTopMenuIds($item['parent_id'], $menuList);
}
}
return $ids;
}
}

@ -32,4 +32,19 @@ class Channel extends BaseModel
// self::$storeId = 0;
// app()->request->setStoreId(0);
}
/**
* 菜单信息
* @param int|array $where
* @return static|array|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function detail($where)
{
$query = static::withoutGlobalScope();
is_array($where) ? $query->where($where) : $query->where('id', '=', $where);
return $query->find();
}
}

Loading…
Cancel
Save