<?php
|
|
namespace app\admin\util;
|
|
use think\Exception;
|
use think\facade\Config;
|
use think\facade\Cache;
|
use think\facade\Db;
|
use think\facade\Session;
|
use think\facade\Cookie;
|
|
|
class MenuService {
|
/**
|
* 获取菜单列表
|
* @param string $tableName
|
* @param string $order
|
* @return array
|
*/
|
public function getMenuList(String $tableName, String $order){
|
$menulist = Db::name($tableName)->field("*")->withAttr('langs', function ($langs) {
|
if (empty($langs)) {
|
return [];
|
}
|
return json_decode($langs, true);
|
})->order($order)->select()->toArray();
|
$list = _generateListTree($menulist, 0, ['id', 'father_id']);
|
return $list;
|
}
|
|
/**
|
* 更新菜单显示状态
|
* @param string $tableName
|
* @param int $id
|
* @param string $showMenu
|
* @return bool
|
*/
|
public function upShowMenu(String $tableName, int $id, String $showMenu):bool {
|
$data = [];
|
$info = Db::name($tableName)->field('id')->where('id', $id)->find();
|
if (empty($info)) {
|
throw new Exception('信息不存在!', 400);
|
}
|
if ($showMenu == '1') {
|
$data['show_menu'] = 1;
|
} else {
|
$data['show_menu'] = 0;
|
}
|
$backval = Db::name($tableName)->where('id', $id)->save($data);
|
if ($backval === FALSE) {
|
throw new Exception('更新失败!', 400);
|
} else {
|
$this->delCache($tableName);
|
return true;
|
}
|
return false;
|
}
|
|
public function getMenuDetail(String $tableName, int $id){
|
return Db::name($tableName)->field('*')->withAttr('langs', function ($langs) {
|
if (empty($langs)) {
|
return [];
|
}
|
return json_decode($langs, true);
|
})->where('id', $id)->find();
|
}
|
|
/**
|
* 检查菜单数据
|
* @param array $data
|
*/
|
public function validateData(array $data){
|
$validate = new \app\admin\validate\SupplierMenu();
|
$result = $validate->check($data);
|
if (!$result) {
|
throw new Exception($validate->getError(), 400);
|
}
|
}
|
|
/**
|
* 更新菜单
|
* @param string $tableName
|
* @param int $id
|
* @param array $data
|
* @return bool
|
*/
|
public function upMenu(String $tableName, int $id, array $data) {
|
$this->validateData($data);
|
//更新数据
|
//////保存////////////
|
Db::name($tableName)->where('id', $id)->withoutField('id')->save($data);
|
////////////////////////
|
$this->delCache($tableName);
|
}
|
|
/**
|
* 添加菜单
|
* @param string $tableName
|
* @param int $id
|
* @param array $data
|
* @return bool
|
*/
|
public function addMenu(String $tableName, int $id, array $data):bool {
|
$this->validateData($data);
|
$data['add_time'] = time();
|
$new_id = Db::name($tableName)->insertGetId($data);
|
if ($new_id) {
|
Db::name($tableName)->where('id', $new_id)->save(['order_id' => $new_id]);
|
///////执行成功,提交事务///////
|
$this->delCache($tableName);
|
return true;
|
} else {
|
throw new Exception('菜单添加失败', 400);
|
}
|
}
|
|
/**
|
* 清除缓存
|
* @return void
|
*/
|
private function delCache($tableName) {
|
Cache::tag($tableName)->clear();
|
}
|
|
}
|