zzwy2/app/controller/manager/Member.php

182 lines
6.7 KiB
PHP
Raw Normal View History

2022-10-08 09:31:39 +00:00
<?php
namespace app\controller\manager;
use app\model\{Category, AuthGroup, Member as MMember, Log};
use Exception;
use think\facade\Db;
class Member extends Base
{
/**
* 删除管理用户
*/
public function del()
{
if ($this->request->isPost()) {
$id = input('post.id/d');
if (is_numeric($id) === true && $id > 0) {
$item = MMember::getByID($id);
if(!empty($item)){
MMember::destroy($id);
Log::write('member', 'del', "管理员删除ID{$id}, 管理员:{$item['username']}");
return $this->json();
}
}
return $this->json(2, '参数错误,请核对之后再操作!');
}
return $this->json(1, '非法请求!');
}
/**
* 修改管理用户信息
* 由于try语法中抛出的异常类型与$this->json()抛出的异常类型不一致,因此需要利用$errorMsg 来判断返回情况
*/
public function edit()
{
if($this->request->isPost()){
$id = input('post.id/d');
$username = trim(input('post.username'));
$password = trim(input('post.password'));
$groupId = input('post.group_id/d');
if ((is_numeric($id) === true && $id > 0) && ((is_numeric($groupId) === true && $groupId > 0) && !empty($username))) {
$member = MMember::getByUserName($username);
if(!empty($member) && $member['id'] != $id){
return $this->json(2, '该用户名已被使用!');
}
$errorMsg = '';
Db::startTrans();
try {
$member = MMember::getById($id);
$item = [
'username' => $username,
'group_id' => $groupId
];
//角色权限重新赋值
$group = AuthGroup::getById($groupId);
$item['rules'] = $group['rules'];
if(!empty($password)){
$item['password'] = md5($password);
}
MMember::updateById($id, $item);
Log::write('member', 'edit', "管理员编辑ID{$id}, 管理员:{$item['username']}");
Db::commit();
} catch (Exception $e) {
Db::rollback();
$errorMsg = '用户信息修改失败!'.$e->getMessage();
}
if (empty($errorMsg)) {
return $this->json();
}
return $this->json(3, $errorMsg);
}
return $this->json(1, '参数错误,请核对之后再操作!');
}else{
$id = input('param.id/d');
if (is_numeric($id) === true && $id > 0) {
$member = MMember::getByID($id);
$item = [
'id' => $member['id'],
'username' => $member['username'],
'group_id' => $member['group_id']
];
$auth = session('auth');
$groups = AuthGroup::getListById($auth['groupId']);
$this->data['groups'] = $groups;
$this->data['item'] = $item;
return $this->view();
}
return $this->json(1, '参数错误,请核对之后再操作!');
}
}
/**
* 新增管理用户
*/
public function add()
{
if($this->request->isPost()){
$groupId = input('post.group_id/d');
$username = trim(input('post.username'));
$password = trim(input('post.password'));
if ((is_numeric($groupId) === true && $groupId > 0) && ($username != "" && $password != "")) {
$member = MMember::getByUserName($username);
if(!empty($member)){
return $this->json(2, '该用户名已被使用!');
}
$group = AuthGroup::getById($groupId);
$newMember = MMember::create([
'username' => $username,
'group_id' => $groupId,
'password' => md5($password),
'rules' => $group['rules'] ?? '',
'cates' => '',
'login_time' => 0,
]);
Log::write('member', 'add', "管理员新增ID{$newMember->id}, 管理员:{$newMember['username']}");
return $this->json();
}
return $this->json(1, '参数错误,请核对之后再操作!');
}
$auth = session('auth');
$groups = AuthGroup::getListById($auth['groupId']);
$this->data['groups'] = $groups;
return $this->view();
}
/**
* 栏目菜单分配
*/
public function menuAlloter()
{
if(request()->isPost()) {
$cates = input('post.cates/a');
$id = input('post.id/d');
if (is_array($cates) && (is_numeric($id) === true && $id > 0)) {
$member = MMember::getById($id);
if(empty($member)){
return $this->json(2, '无此用户信息,请核对之后再操作!');
}
MMember::updateCates($id, $cates);
Log::write('member', 'menuAlloter', "管理员栏目分配ID{$id}, 管理员:{$member['username']}");
return $this->json();
}else{
return $this->json(3, '传入参数错误,请核对之后再操作!');
}
} else {
$id = input('param.id/d');
if (is_numeric($id) && $id > 0) {
$member = MMember::getById($id);
if (empty($member)) {
return $this->json(2, '该管理员信息不存在,请核对之后再操作!');
}
$cates = Category::getListTree(false);
$memberCates = array_filter(explode(',', $member['cates']));
$this->data['id'] = $id;
$this->data['member'] = $member;
$this->data['memberCates'] = $memberCates;
$this->data['cates'] = $cates;
return $this->view();
}
return $this->json(1, '参数错误,请核对之后再操作!',$id);
}
}
/**
* 所有用户列表
*/
public function index()
{
$auth = session('auth');
if ($auth['groupId'] == 1) {
$items = MMember::getList(40);
} else {
$items = MMember::getListNotAdmin(40);
}
$this->data['items'] = $items;
return $this->view();
}
}