first commit
This commit is contained in:
49
application/admin/controller/Base.php
Normal file
49
application/admin/controller/Base.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use think\Controller;
|
||||
use app\traits\ControllerTrait;
|
||||
|
||||
abstract class Base extends Controller
|
||||
{
|
||||
use ControllerTrait;
|
||||
|
||||
protected $limit = 20;
|
||||
|
||||
protected $page = 1;
|
||||
|
||||
protected $middleware = ['checkLogin', 'auth'];
|
||||
|
||||
/**
|
||||
* 过滤参数
|
||||
*
|
||||
* @time at 2018年11月15日
|
||||
* @param $params
|
||||
* @return void
|
||||
*/
|
||||
protected function checkParams(&$params)
|
||||
{
|
||||
$this->limit = $params['limit'] ?? $this->limit;
|
||||
$this->page = $params['page'] ?? $this->page;
|
||||
|
||||
foreach ($params as $key => $param) {
|
||||
if (!$param || $key == 'limit' || $key == 'page') {
|
||||
unset($params[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->start = $this->start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Table ID Start
|
||||
*
|
||||
* @time at 2018年11月16日
|
||||
* @return float|int
|
||||
*/
|
||||
protected function start()
|
||||
{
|
||||
return (int)$this->limit * (int)$this->page;
|
||||
}
|
||||
}
|
37
application/admin/controller/Index.php
Normal file
37
application/admin/controller/Index.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use think\permissions\facade\Permissions;
|
||||
use think\permissions\facade\Roles;
|
||||
use app\service\MenuService;
|
||||
|
||||
class Index extends Base
|
||||
{
|
||||
protected $middleware = [ 'checkLogin' ];
|
||||
|
||||
/**
|
||||
* 首页
|
||||
*
|
||||
* @time at 2018年11月15日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function index(MenuService $menuService)
|
||||
{
|
||||
$loginUser = $this->getLoginUser();
|
||||
$userHasRoles = $loginUser->getRoles();
|
||||
$permissionIds = [];
|
||||
$userHasRoles->each(function ($role, $key) use (&$permissionIds) {
|
||||
$permissionIds = array_merge($permissionIds, Roles::getRoleBy($role->id)->getPermissions(false));
|
||||
});
|
||||
$permissions = Permissions::whereIn('id', $permissionIds)->where('is_show', 1)->select();
|
||||
$this->permissions = $menuService->tree($permissions);
|
||||
$this->loginUser = $loginUser;
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
public function main()
|
||||
{
|
||||
return "this is main";
|
||||
}
|
||||
}
|
55
application/admin/controller/Login.php
Normal file
55
application/admin/controller/Login.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\traits\Auth;
|
||||
use think\Controller;
|
||||
|
||||
class Login extends Controller
|
||||
{
|
||||
use Auth;
|
||||
|
||||
protected $redirect = '/index';
|
||||
|
||||
/**
|
||||
* Login Page
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
// 登录逻辑
|
||||
if ($this->request->isPost()) {
|
||||
$this->authLogin($this->request);
|
||||
}
|
||||
|
||||
return $this->fetch('/index/login');
|
||||
}
|
||||
|
||||
/**
|
||||
* 登出
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\think\response\Redirect
|
||||
*/
|
||||
public function logout()
|
||||
{
|
||||
$this->authLogout();
|
||||
|
||||
return redirect(url('login'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证规则
|
||||
*
|
||||
* @time at 2018年11月13日
|
||||
* @return array
|
||||
*/
|
||||
protected function rule()
|
||||
{
|
||||
return [
|
||||
$this->name() => 'require',
|
||||
'password|密码' => 'require',
|
||||
//'captcha|验证码' => 'require|captcha'
|
||||
];
|
||||
}
|
||||
|
||||
}
|
84
application/admin/controller/Permission.php
Normal file
84
application/admin/controller/Permission.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
namespace app\admin\controller;
|
||||
|
||||
use think\Collection;
|
||||
use think\permissions\facade\Permissions;
|
||||
use app\validates\PermissionValidate;
|
||||
use app\service\MenuService;
|
||||
|
||||
class Permission extends Base
|
||||
{
|
||||
public function index(MenuService $menuService)
|
||||
{
|
||||
$this->permissions = new Collection($menuService->sort(Permissions::select()));
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Data
|
||||
*
|
||||
* @time at 2018年11月13日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function create(PermissionValidate $validate, MenuService $menuService)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if ($err = $validate->getErrors($data)) {
|
||||
$this->error($err);
|
||||
}
|
||||
Permissions::store($data) ? $this->success('添加成功', url('permission/index')) : $this->error('添加失败');
|
||||
}
|
||||
|
||||
$this->permissions = $menuService->sort(Permissions::select());
|
||||
$this->permissionId = $this->request->param('id') ?? 0;
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit Data
|
||||
*
|
||||
* @time at 2018年11月13日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function edit(PermissionValidate $validate, MenuService $menuService)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if ($err = $validate->getErrors($data)) {
|
||||
$this->error($err);
|
||||
}
|
||||
Permissions::updateBy($data['id'], $data) !== false ? $this->success('编辑成功', url('permission/index')) : $this->error('');
|
||||
}
|
||||
$permissionId = $this->request->param('id');
|
||||
if (!$permissionId) {
|
||||
$this->error('不存在的数据');
|
||||
}
|
||||
$this->permissions = $menuService->sort(Permissions::select());
|
||||
$this->permission = Permissions::getPermissionBy($permissionId);
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete Data
|
||||
*
|
||||
* @time at 2018年11月13日
|
||||
* @return void
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
$permissionId = $this->request->post('id');
|
||||
if (!$permissionId) {
|
||||
$this->error('不存在数据');
|
||||
}
|
||||
if (Permissions::where('pid', $permissionId)->find()) {
|
||||
$this->error('请先删除子菜单');
|
||||
}
|
||||
// 删除权限关联的角色信息
|
||||
Permissions::detachRole($permissionId);
|
||||
if (Permissions::deleteBy($permissionId)) {
|
||||
$this->success('删除成功', url('permission/index'));
|
||||
}
|
||||
$this->error('删除失败');
|
||||
}
|
||||
}
|
124
application/admin/controller/Role.php
Normal file
124
application/admin/controller/Role.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
namespace app\admin\controller;
|
||||
|
||||
use think\permissions\facade\Roles;
|
||||
use app\validates\RoleValidate;
|
||||
use think\permissions\facade\Permissions;
|
||||
use app\service\MenuService;
|
||||
|
||||
class Role extends Base
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$this->roles = Roles::paginate(10);
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* create Data
|
||||
*
|
||||
* @time at 2018年11月13日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function create(RoleValidate $validate)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if ($err = $validate->getErrors($data)) {
|
||||
$this->error($err);
|
||||
}
|
||||
Roles::store($data) ? $this->success('创建成功', url('role/index')) : $this->error('创建失败');
|
||||
}
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit Data
|
||||
*
|
||||
* @time at 2018年11月13日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function edit(RoleValidate $validate)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if ($err = $validate->getErrors($data)) {
|
||||
$this->error($err);
|
||||
}
|
||||
Roles::updateBy($data['id'], $data) !== false ? $this->success('编辑成功', url('role/index')) : $this->error('编辑失败');
|
||||
}
|
||||
$roleId = $this->request->param('id');
|
||||
$role = Roles::getRoleBy($roleId);
|
||||
|
||||
$this->role = $role;
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete Data
|
||||
*
|
||||
* @time at 2018年11月13日
|
||||
* @return void
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
$roleId = $this->request->post('id');
|
||||
if (!$roleId) {
|
||||
$this->error('角色信息不存在');
|
||||
}
|
||||
// 删除角色相关的用户
|
||||
Roles::detachUsers($roleId);
|
||||
// 删除角色相关的权限
|
||||
Roles::detachPermissions($roleId);
|
||||
if (Roles::deleteBy($roleId)) {
|
||||
$this->success('删除成功', url('role/index'));
|
||||
}
|
||||
$this->error('删除失败');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色权限
|
||||
*
|
||||
* @time at 2018年09月21日
|
||||
* @return void
|
||||
*/
|
||||
public function getPermissionsOfRole(MenuService $menuService)
|
||||
{
|
||||
$field = ['name', 'id', 'pid'];
|
||||
$roleId = $this->request->param('role_id');
|
||||
$permissions = Permissions::field($field)->all();
|
||||
$roleHasPermissions = Roles::getRoleBy($roleId)->getPermissions(false);
|
||||
$permissions = $permissions->each(function ($item, $key) use ($roleHasPermissions){
|
||||
if (!$item->pid) {
|
||||
$item->open = true;
|
||||
}
|
||||
$item->checked = in_array($item->id, $roleHasPermissions) ? true : false;
|
||||
return $item;
|
||||
});
|
||||
|
||||
$this->success('', '', $menuService->sort($permissions));
|
||||
}
|
||||
|
||||
/**
|
||||
* 分配权限
|
||||
*
|
||||
* @time at 2018年11月15日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function givePermissions()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$postData = $this->request->post();
|
||||
$roleId = $postData['role_id'];
|
||||
if (!isset($postData['permissions'])) {
|
||||
Roles::detachPermissions($roleId);
|
||||
$this->success('分配成功', url('role/index'));
|
||||
}
|
||||
$permissions = $postData['permissions'];
|
||||
Roles::detachPermissions($roleId);
|
||||
Roles::attachPermissions($roleId, $permissions) ? $this->success('分配成功', url('role/index')) : $this->error('分配失败');
|
||||
}
|
||||
$this->role_id = $this->request->param('id');
|
||||
return $this->fetch('role/givePermissions');
|
||||
}
|
||||
}
|
131
application/admin/controller/User.php
Normal file
131
application/admin/controller/User.php
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\model\UserModel;
|
||||
use app\validates\UserValidate;
|
||||
use think\permissions\facade\Roles;
|
||||
|
||||
class User extends Base
|
||||
{
|
||||
/**
|
||||
* User List
|
||||
*
|
||||
* @time at 2018年11月12日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function index(UserModel $userModel)
|
||||
{
|
||||
$params = $this->request->param();
|
||||
$this->checkParams($params);
|
||||
$this->users = $userModel->getList($params, $this->limit);
|
||||
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* create Data
|
||||
*
|
||||
* @time at 2018年11月12日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function create(UserModel $userModel, UserValidate $validate)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if ($err = $validate->getErrors($data)) {
|
||||
$this->error($err);
|
||||
}
|
||||
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
|
||||
if ($userId = $userModel->store($data)) {
|
||||
// 分配角色
|
||||
$this->giveRoles($userModel, $userId, $data);
|
||||
$this->success('添加成功', url('user/index'));
|
||||
}
|
||||
$this->error('添加失败');
|
||||
}
|
||||
|
||||
$this->roles = Roles::all();
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit Data
|
||||
*
|
||||
* @time at 2018年11月12日
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function edit(UserModel $userModel, UserValidate $validate)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if ($err = $validate->getErrors($data)) {
|
||||
$this->error($err);
|
||||
}
|
||||
$this->giveRoles($userModel, $data['id'], $data);
|
||||
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
|
||||
$userModel->updateBy($data['id'], $data) ? $this->success('修改成功', url('user/index')) : $this->error('修改失败');
|
||||
}
|
||||
|
||||
$id = $this->request->param('id');
|
||||
if (!$id) {
|
||||
$this->error('数据不存在');
|
||||
}
|
||||
$user = $userModel->findBy($id);
|
||||
$userHasRoles = $user->getRoles(false);
|
||||
$roles = Roles::all()->each(function($item, $key) use ($userHasRoles){
|
||||
$item->checked = in_array($item->id, $userHasRoles) ? true : false;
|
||||
return $item;
|
||||
});
|
||||
|
||||
$this->user = $user;
|
||||
$this->roles = $roles;
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete Data
|
||||
*
|
||||
* @time at 2018年11月12日
|
||||
* @return void
|
||||
*/
|
||||
public function delete(UserModel $userModel)
|
||||
{
|
||||
$id = $this->request->post('id');
|
||||
|
||||
if (!$id) {
|
||||
$this->error('不存在的数据');
|
||||
}
|
||||
// 删除用户相关的角色
|
||||
$userModel->detachRoles($id);
|
||||
if ($userModel->deleteBy($id)) {
|
||||
$this->success('删除成功', url('user/index'));
|
||||
}
|
||||
$this->error('删除失败');
|
||||
}
|
||||
|
||||
/**
|
||||
* 分配角色
|
||||
*
|
||||
* @time at 2018年11月15日
|
||||
* @param \app\model\UserModel $userModel
|
||||
* @param int $userId
|
||||
* @param $data
|
||||
* @return bool
|
||||
*/
|
||||
protected function giveRoles(UserModel $userModel, int $userId, &$data)
|
||||
{
|
||||
if (isset($data['roles'])) {
|
||||
$rolesIds = $data['roles'];
|
||||
if (!is_array($rolesIds)) {
|
||||
$rolesIds = [$rolesIds];
|
||||
}
|
||||
$userModel->detachRoles($userId);
|
||||
$userModel->attachRoles($userId, $rolesIds);
|
||||
unset($data['roles']);
|
||||
return true;
|
||||
}
|
||||
$userModel->detachRoles($userId);
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user