123 lines
3.8 KiB
PHP
123 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\admin\controller\nk;
|
|
|
|
use app\admin\BaseController;
|
|
use think\facade\Db;
|
|
use think\facade\View;
|
|
|
|
|
|
class Category extends BaseController
|
|
{
|
|
|
|
|
|
public function __construct()
|
|
{
|
|
$this->adminInfo = get_login_admin();
|
|
// 类型
|
|
$map[] = ['pid','=','0'];
|
|
$map[] = ['status','=','normal'];
|
|
$this->type = Db::table('fa_category')->where($map)->field('id,type,name')->select();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
if (request()->isAjax()) {
|
|
$list = Db::table('fa_category')->order('weigh asc,id asc')->select()->toArray();
|
|
foreach ($list as $k =>$v){
|
|
if($v['status'] == 'normal'){
|
|
$list[$k]['status'] = '正常';
|
|
}else{
|
|
$list[$k]['status'] = '禁用';
|
|
}
|
|
}
|
|
return to_assign(0, '', $list);
|
|
} else {
|
|
return view();
|
|
}
|
|
}
|
|
|
|
//添加
|
|
public function add()
|
|
{
|
|
$param = get_params();
|
|
if (request()->isAjax()) {
|
|
|
|
if($param['pid'] == 0){
|
|
$www['pid'] = 0;
|
|
$www['type'] = $param['type'];
|
|
$pid = Db::table('fa_category')->where($www)->value('id');
|
|
if($pid){
|
|
$param['pid'] = $pid;
|
|
}else{
|
|
return to_assign(1, '操作失败,请联系系统管理员');
|
|
}
|
|
}
|
|
if ($param['id'] > 0) {
|
|
$param['updatetime'] = time();
|
|
$res = Db::table('fa_category')->strict(false)->field(true)->where('id',$param['id'])->update($param);
|
|
if ($res){
|
|
add_log('edit', $param['id'], $param);
|
|
return to_assign(0,'操作成功',['aid'=>$res]);
|
|
}
|
|
return to_assign(1, '操作失败,原因:'.$res);
|
|
|
|
}else{
|
|
$param['status'] = 'normal';
|
|
$param['nickname'] = $param['name'];
|
|
$param['createtime'] = time();
|
|
unset($param['id']);
|
|
$res=Db::table('fa_category')->strict(false)->field(true)->insertGetId($param);
|
|
if ($res){
|
|
add_log('add', $res, $param);
|
|
return to_assign(0,'操作成功',['aid'=>$res]);
|
|
}
|
|
return to_assign(1, '操作失败,原因:'.$res);
|
|
}
|
|
|
|
} else {
|
|
$id = isset($param['id']) ? $param['id'] : 0;
|
|
$pid = isset($param['pid']) ? $param['pid'] : 0;
|
|
if($id>0){
|
|
$detail = Db::table('fa_category')->where('id',$id)->find();
|
|
View::assign('detail', $detail);
|
|
}else{
|
|
$detail['pid'] = '';
|
|
$detail['type'] = '';
|
|
View::assign('detail', $detail);
|
|
}
|
|
View::assign('id', $id);
|
|
View::assign('pid', $pid);
|
|
View::assign('type', $this->type);
|
|
return view();
|
|
}
|
|
}
|
|
//删除
|
|
public function delete()
|
|
{
|
|
$id = get_params("id");
|
|
$count = Db::name('AdminRule')->where(["pid" => $id])->count();
|
|
if ($count > 0) {
|
|
return to_assign(1, "该节点下还有子节点,无法删除");
|
|
}
|
|
if (Db::name('AdminRule')->delete($id) !== false) {
|
|
clear_cache('adminRules');
|
|
add_log('delete', $id, []);
|
|
return to_assign(0, "删除节点成功");
|
|
} else {
|
|
return to_assign(1, "删除失败");
|
|
}
|
|
}
|
|
|
|
public function getbytype(){
|
|
$type = get_params("type");
|
|
$where['type'] = $type;
|
|
$where['status'] = 'normal';
|
|
$list = Db::table('fa_category')->where($where)->order('weigh asc,id asc')->select();
|
|
return to_assign(0, '', $list);
|
|
}
|
|
|
|
}
|