first commit
This commit is contained in:
121
catch/user/Auth.php
Normal file
121
catch/user/Auth.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
namespace catchAdmin\user;
|
||||
|
||||
use catchAdmin\permissions\model\Permissions;
|
||||
use catchAdmin\permissions\model\Roles;
|
||||
use catchAdmin\user\model\Users;
|
||||
use catcher\exceptions\LoginFailedException;
|
||||
use catcher\Tree;
|
||||
use thans\jwt\facade\JWTAuth;
|
||||
use think\facade\Session;
|
||||
|
||||
class Auth
|
||||
{
|
||||
protected const USER_ID = 'catch_uid';
|
||||
/**
|
||||
* 登陆
|
||||
*
|
||||
* @time 2019年11月28日
|
||||
* @param $params
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws LoginFailedException
|
||||
*/
|
||||
public static function login($params)
|
||||
{
|
||||
$user = Users::where('email', $params['email'])->find();
|
||||
|
||||
if (!$user) {
|
||||
throw new LoginFailedException('登陆失败, 请检查用户名和密码');
|
||||
}
|
||||
|
||||
if (!password_verify($params['password'], $user->password)) {
|
||||
throw new LoginFailedException('登陆失败, 请检查用户名和密码');
|
||||
}
|
||||
|
||||
if ($user->status == Users::DISABLE) {
|
||||
throw new LoginFailedException('该用户已被禁用');
|
||||
}
|
||||
|
||||
// 记录用户登录
|
||||
$user->last_login_ip = request()->ip();
|
||||
$user->last_login_time = time();
|
||||
$user->save();
|
||||
|
||||
// Session::set(self::getLoginUserKey(), $user);
|
||||
|
||||
return JWTAuth::builder([self::USER_ID => $user->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登陆
|
||||
*
|
||||
* @time 2019年11月28日
|
||||
* @return bool
|
||||
*/
|
||||
public static function logout(): bool
|
||||
{
|
||||
Session::delete(self::getLoginUserKey());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月15日
|
||||
* @return mixed
|
||||
*/
|
||||
public static function user()
|
||||
{
|
||||
$user = Users::where('id', JWTAuth::auth()[self::USER_ID])
|
||||
->field(['id', 'username', 'status'])->find();
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public static function getUserInfo()
|
||||
{
|
||||
$user = self::user();
|
||||
|
||||
$roles = $user->getRoles();
|
||||
|
||||
foreach ($roles as &$role) {
|
||||
$role['permissions'] = Roles::where('id', $role['id'])->find()->getPermissions([
|
||||
'type' => Permissions::MENU_TYPE
|
||||
], ['permission_name', 'route']);
|
||||
}
|
||||
|
||||
$user->roles = $roles;
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月15日
|
||||
* @return string
|
||||
*/
|
||||
protected static function getLoginUserKey(): string
|
||||
{
|
||||
// return md5(self::USER_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月15日
|
||||
* @param $mark
|
||||
* @param $module
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasPermissions($mark, $module): bool
|
||||
{
|
||||
$permissionIds = self::user()->get->getPermissionsBy();
|
||||
|
||||
$permissionId = Permissions::where('module', $module)
|
||||
->where('permission_mark', $mark)->value('id');
|
||||
|
||||
return in_array($permissionId, $permissionIds);
|
||||
}
|
||||
}
|
34
catch/user/AuthTokenMiddleware.php
Normal file
34
catch/user/AuthTokenMiddleware.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
namespace catchAdmin\user;
|
||||
|
||||
use catcher\Code;
|
||||
use catcher\exceptions\FailedException;
|
||||
use thans\jwt\exception\TokenBlacklistException;
|
||||
use thans\jwt\exception\TokenExpiredException;
|
||||
use thans\jwt\exception\TokenInvalidException;
|
||||
use thans\jwt\facade\JWTAuth;
|
||||
use think\Middleware;
|
||||
|
||||
class AuthTokenMiddleware extends Middleware
|
||||
{
|
||||
public function handle($request, \Closure $next)
|
||||
{
|
||||
try {
|
||||
JWTAuth::auth();
|
||||
} catch (\Exception $e) {
|
||||
if ($e instanceof TokenExpiredException) {
|
||||
throw new FailedException('token 过期', Code::LOST_LOGIN);
|
||||
}
|
||||
if ($e instanceof TokenBlacklistException) {
|
||||
throw new FailedException('token 被加入黑名单', Code::LOST_LOGIN);
|
||||
}
|
||||
if ($e instanceof TokenInvalidException) {
|
||||
throw new FailedException('token 不合法', Code::LOST_LOGIN);
|
||||
}
|
||||
|
||||
throw new FailedException('登录用户不合法', Code::LOST_LOGIN);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
212
catch/user/controller/User.php
Normal file
212
catch/user/controller/User.php
Normal file
@@ -0,0 +1,212 @@
|
||||
<?php
|
||||
namespace catchAdmin\user\controller;
|
||||
|
||||
use app\Request;
|
||||
use catchAdmin\permissions\model\Roles;
|
||||
use catchAdmin\user\Auth;
|
||||
use catchAdmin\user\model\Users;
|
||||
use catchAdmin\user\request\CreateRequest;
|
||||
use catchAdmin\user\request\UpdateRequest;
|
||||
use catcher\base\CatchController;
|
||||
use catcher\CatchForm;
|
||||
use catcher\CatchResponse;
|
||||
use catcher\Tree;
|
||||
|
||||
class User extends CatchController
|
||||
{
|
||||
protected $user;
|
||||
|
||||
public function __construct(Users $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月04日
|
||||
* @param Request $request
|
||||
* @return string
|
||||
* @throws \think\db\exception\DbException
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
return CatchResponse::paginate($this->user->getList($request->param()));
|
||||
}
|
||||
|
||||
public function info()
|
||||
{
|
||||
return CatchResponse::success(Auth::getUserInfo());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月06日
|
||||
* @throws \Exception
|
||||
* @return string
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$form = new CatchForm();
|
||||
|
||||
$form->formId('userForm');
|
||||
$form->text('username', '用户名', true)->verify('required')->placeholder('请输入用户名');
|
||||
$form->text('email', '邮箱', true)->verify('email')->placeholder('请输入邮箱');
|
||||
$form->password('password', '密码', true)->id('pwd')->verify('required|psw')->placeholder('请输入密码');
|
||||
$form->password('passwordConfirm', '确认密码', true)->verify('required|equalTo', ['pwd', '两次密码输入不一致'])->placeholder('请再次输入密码');
|
||||
$form->dom('<div id="roles"></div>', '角色');
|
||||
$form->formBtn('submitUser');
|
||||
|
||||
return $this->fetch([
|
||||
'form' => $form->render(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param CreateRequest $request
|
||||
* @time 2019年12月06日
|
||||
* @return Json
|
||||
*/
|
||||
public function save(CreateRequest $request)
|
||||
{
|
||||
$this->user->storeBy($request->post());
|
||||
|
||||
if (!empty($request->param('roleids'))) {
|
||||
$this->user->attach($request->param('roleids'));
|
||||
}
|
||||
|
||||
return CatchResponse::success();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月04日
|
||||
* @param $id
|
||||
* @return Json
|
||||
*/
|
||||
public function read($id)
|
||||
{
|
||||
return CatchResponse::success($this->user->findBy($id));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$user = $this->user->findBy($id, ['id','username', 'email']);
|
||||
$form = new CatchForm();
|
||||
|
||||
$form->formId('userForm');
|
||||
$form->text('username', '用户名', true)->verify('required')->default($user->username)->placeholder('请输入用户名');
|
||||
$form->text('email', '邮箱', true)->verify('email')->default($user->email)->placeholder('请输入邮箱');
|
||||
$form->password('password', '密码')->id('pwd')->placeholder('请输入密码');
|
||||
$form->password('passwordConfirm', '确认密码')->verify('equalTo', ['pwd', '两次密码输入不一致'])->placeholder('请再次输入密码');
|
||||
$form->dom('<div id="roles"></div>', '角色');
|
||||
$form->formBtn('submitUser');
|
||||
|
||||
return $this->fetch([
|
||||
'form' => $form->render(),
|
||||
'uid' => $user->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月04日
|
||||
* @param $id
|
||||
* @param UpdateRequest $request
|
||||
* @return Json
|
||||
*/
|
||||
public function update($id, UpdateRequest $request)
|
||||
{
|
||||
$this->user->updateBy($id, $request->post());
|
||||
|
||||
$user = $this->user->findBy($id);
|
||||
|
||||
$user->detach();
|
||||
|
||||
if (!empty($request->param('roleids'))) {
|
||||
$user->attach($request->param('roleids'));
|
||||
}
|
||||
|
||||
return CatchResponse::success();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月04日
|
||||
* @param $id
|
||||
* @return Json
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
// 删除角色
|
||||
$this->user->findBy($id)->detach();
|
||||
|
||||
$this->user->deleteBy($id);
|
||||
|
||||
return CatchResponse::success();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月07日
|
||||
* @param $id
|
||||
* @return Json
|
||||
*/
|
||||
public function switchStatus($id): Json
|
||||
{
|
||||
$user = $this->user->findBy($id);
|
||||
return CatchResponse::success($this->user->updateBy($id, [
|
||||
'status' => $user->status == Users::ENABLE ? Users::DISABLE : Users::ENABLE,
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月07日
|
||||
* @param $id
|
||||
* @return Json
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
*/
|
||||
public function recover($id): Json
|
||||
{
|
||||
$trashedUser = $this->user->findBy($id, ['*'], true);
|
||||
|
||||
if ($this->user->where('email', $trashedUser->email)->find()) {
|
||||
return CatchResponse::fail(sprintf('该恢复用户的邮箱 [%s] 已被占用', $trashedUser->email));
|
||||
}
|
||||
|
||||
return CatchResponse::success($this->user->recover($id));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月11日
|
||||
* @param Request $request
|
||||
* @param Roles $roles
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getRoles(Request $request, Roles $roles): \think\response\Json
|
||||
{
|
||||
$roles = Tree::done($roles->getList());
|
||||
|
||||
$roleIds = [];
|
||||
if ($request->param('uid')) {
|
||||
$userHasRoles = $this->user->findBy($request->param('uid'))->getRoles();
|
||||
foreach ($userHasRoles as $role) {
|
||||
$roleIds[] = $role->pivot->role_id;
|
||||
}
|
||||
}
|
||||
|
||||
return CatchResponse::success([
|
||||
'roles' => $roles,
|
||||
'hasRoles' => $roleIds,
|
||||
]);
|
||||
}
|
||||
}
|
43
catch/user/database/migrations/20191128114204_users.php
Normal file
43
catch/user/database/migrations/20191128114204_users.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Migrator;
|
||||
use think\migration\db\Column;
|
||||
|
||||
class Users extends Migrator
|
||||
{
|
||||
/**
|
||||
* Change Method.
|
||||
*
|
||||
* Write your reversible migrations using this method.
|
||||
*
|
||||
* More information on writing migrations is available here:
|
||||
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
|
||||
*
|
||||
* The following commands can be used in this method and Phinx will
|
||||
* automatically reverse them when rolling back:
|
||||
*
|
||||
* createTable
|
||||
* renameTable
|
||||
* addColumn
|
||||
* renameColumn
|
||||
* addIndex
|
||||
* addForeignKey
|
||||
*
|
||||
* Remember to call "create()" or "update()" and NOT "save()" when working
|
||||
* with the Table class.
|
||||
*/
|
||||
public function change()
|
||||
{
|
||||
$table = $this->table('users',array('engine'=>'Innodb', 'comment' => '用户表', 'signed' => false));
|
||||
$table->addColumn('username', 'string',array('limit' => 15,'default'=>'','comment'=>'用户名'))
|
||||
->addColumn('password', 'string',array('limit' => 255,'comment'=>'用户密码'))
|
||||
->addColumn('email', 'string',array('limit' => 100, 'comment'=>'邮箱 登录'))
|
||||
->addColumn('status', 'boolean',array('limit' => 1,'default'=> 1,'comment'=>'用户状态 1 正常 2 禁用'))
|
||||
->addColumn('last_login_ip', 'string',array('limit' => 30,'default'=>0,'comment'=>'最后登录IP'))
|
||||
->addColumn('last_login_time', 'integer',array('default'=>0,'comment'=>'最后登录时间', 'signed' => false))
|
||||
->addColumn('created_at', 'integer', array('default'=>0,'comment'=>'创建时间', 'signed' => false ))
|
||||
->addColumn('updated_at', 'integer', array('default'=>0,'comment'=>'更新时间', 'signed' => false))
|
||||
->addColumn('deleted_at', 'integer', array('default'=>0,'comment'=>'删除状态,0未删除 >0 已删除', 'signed' => false))
|
||||
->create();
|
||||
}
|
||||
}
|
23
catch/user/database/seeds/UsersSeed.php
Normal file
23
catch/user/database/seeds/UsersSeed.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Seeder;
|
||||
|
||||
class UsersSeed extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run Method.
|
||||
*
|
||||
* Write your database seeder using this method.
|
||||
*
|
||||
* More information on writing seeders is available here:
|
||||
* http://docs.phinx.org/en/latest/seeding.html
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
return \catchAdmin\user\model\Users::create([
|
||||
'username' => 'admin',
|
||||
'password' => 'admin',
|
||||
'email' => 'admin@gmail.com',
|
||||
]);
|
||||
}
|
||||
}
|
83
catch/user/model/Users.php
Normal file
83
catch/user/model/Users.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
namespace catchAdmin\user\model;
|
||||
|
||||
use catchAdmin\permissions\model\HasRolesTrait;
|
||||
use catcher\base\CatchModel;
|
||||
|
||||
class Users extends CatchModel
|
||||
{
|
||||
use HasRolesTrait;
|
||||
|
||||
protected $name = 'users';
|
||||
|
||||
protected $field = [
|
||||
'id', //
|
||||
'username', // 用户名
|
||||
'password', // 用户密码
|
||||
'email', // 邮箱 登录
|
||||
'status', // 用户状态 1 正常 2 禁用
|
||||
'last_login_ip', // 最后登录IP
|
||||
'last_login_time', // 最后登录时间
|
||||
'created_at', // 创建时间
|
||||
'updated_at', // 更新时间
|
||||
'deleted_at', // 删除状态,0未删除 >0 已删除
|
||||
|
||||
];
|
||||
|
||||
/**
|
||||
* set password
|
||||
*
|
||||
* @time 2019年12月07日
|
||||
* @param $value
|
||||
* @return false|string
|
||||
*/
|
||||
public function setPasswordAttr($value)
|
||||
{
|
||||
return password_hash($value, PASSWORD_DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户列表
|
||||
*
|
||||
* @time 2019年12月08日
|
||||
* @param $search
|
||||
* @throws \think\db\exception\DbException
|
||||
* @return \think\Paginator
|
||||
*/
|
||||
public function getList($search): \think\Paginator
|
||||
{
|
||||
return (($search['trash'] ?? false) ? static::onlyTrashed() : $this)
|
||||
->field(['id', 'username', 'email', 'status','last_login_time','last_login_ip', 'created_at', 'updated_at'])
|
||||
->when($search['username'] ?? false, function ($query) use ($search){
|
||||
return $query->whereLike('username', $search['username']);
|
||||
})
|
||||
->when($search['email'] ?? false, function ($query) use ($search){
|
||||
return $query->whereLike('email', $search['email']);
|
||||
})
|
||||
->when($search['status'] ?? false, function ($query) use ($search){
|
||||
return $query->where('status', $search['status']);
|
||||
})->paginate($search['limit'] ?? $this->limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取权限
|
||||
*
|
||||
* @time 2019年12月12日
|
||||
* @param $uid
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return array
|
||||
*/
|
||||
public function getPermissionsBy($uid = 0): array
|
||||
{
|
||||
$roles = $uid ? $this->findBy($uid)->getRoles() : $this->getRoles();
|
||||
|
||||
$permissionIds = [];
|
||||
foreach ($roles as $role) {
|
||||
$permissionIds = array_merge($permissionIds, $role->getPermissions()->column('id'));
|
||||
}
|
||||
|
||||
return array_unique($permissionIds);
|
||||
}
|
||||
}
|
13
catch/user/module.json
Normal file
13
catch/user/module.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "用户管理",
|
||||
"alias": "user",
|
||||
"description": "",
|
||||
"keywords": [],
|
||||
"order": 2,
|
||||
"services": [
|
||||
"catchAdmin\\user\\UserService"
|
||||
],
|
||||
"aliases": {},
|
||||
"files": [],
|
||||
"requires": []
|
||||
}
|
25
catch/user/request/CreateRequest.php
Normal file
25
catch/user/request/CreateRequest.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace catchAdmin\user\request;
|
||||
|
||||
use catchAdmin\user\model\Users;
|
||||
use catcher\base\CatchRequest;
|
||||
|
||||
class CreateRequest extends CatchRequest
|
||||
{
|
||||
|
||||
protected function rules(): array
|
||||
{
|
||||
// TODO: Implement rules() method.
|
||||
return [
|
||||
'username|用户名' => 'require|max:20',
|
||||
'password|密码' => 'require|min:5|max:12',
|
||||
'passwordConfirm|密码' => 'confirm:password',
|
||||
'email|邮箱' => 'require|email|unique:'.Users::class,
|
||||
];
|
||||
}
|
||||
|
||||
protected function message(): array
|
||||
{
|
||||
// TODO: Implement message() method.
|
||||
}
|
||||
}
|
24
catch/user/request/UpdateRequest.php
Normal file
24
catch/user/request/UpdateRequest.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace catchAdmin\user\request;
|
||||
|
||||
use catchAdmin\user\model\Users;
|
||||
use catcher\base\CatchRequest;
|
||||
|
||||
class UpdateRequest extends CatchRequest
|
||||
{
|
||||
protected function rules(): array
|
||||
{
|
||||
// TODO: Implement rules() method.
|
||||
return [
|
||||
'username|用户名' => 'require|max:20',
|
||||
'password|密码' => 'sometimes|min:5|max:12',
|
||||
'passwordConfirm|密码' => 'sometimes|confirm:password',
|
||||
'email|邮箱' => 'require|email|unique:'.Users::class,
|
||||
];
|
||||
}
|
||||
|
||||
protected function message(): array
|
||||
{
|
||||
// TODO: Implement message() method.
|
||||
}
|
||||
}
|
7
catch/user/route.php
Normal file
7
catch/user/route.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
$router->resource('user', '\catchAdmin\user\controller\User');
|
||||
// 切换状态
|
||||
$router->put('user/switch/status/<id>', '\catchAdmin\user\controller\User@switchStatus');
|
||||
$router->put('user/recover/<id>', '\catchAdmin\user\controller\User@recover');
|
||||
$router->get('user/get/roles', '\catchAdmin\user\controller\User@getRoles');
|
38
catch/user/view/create.html
Normal file
38
catch/user/view/create.html
Normal file
@@ -0,0 +1,38 @@
|
||||
{$form|raw}
|
||||
<script>
|
||||
layui.use(['layer', 'form', 'admin', 'formX','authtree'], function () {
|
||||
var $ = layui.jquery;
|
||||
var layer = layui.layer;
|
||||
var form = layui.form;
|
||||
var admin = layui.admin;
|
||||
var authtree = layui.authtree;
|
||||
var mUser = admin.getLayerData('#userForm'); // 列表页面传递的数据,#modelUserForm这个只要写弹窗内任意一个元素的id即可
|
||||
// 回显数据
|
||||
form.val('userForm', mUser);
|
||||
|
||||
// 表单提交事件
|
||||
form.on('submit(submitUser)', function (data) {
|
||||
var url = mUser ? '{:url("user")}' : '{:url("user")}';
|
||||
admin.req(url, data.field, function (response) {
|
||||
layer.msg(response.msg, {icon: 1});
|
||||
admin.putLayerData('formOk', true, '#userForm'); // 设置操作成功的标识,#modelUserForm这个只要写弹窗内任意一个元素的id即可
|
||||
admin.closeDialog('#userForm'); // 关闭页面层弹窗
|
||||
}, 'post');
|
||||
return false;
|
||||
});
|
||||
admin.req('{:url("/user/get/roles")}',{}, function (response) {
|
||||
authtree.render('#roles', response.data.roles,{
|
||||
inputname: 'roleids[]',
|
||||
layfilter: 'lay-check-auth',
|
||||
autowidth: true,
|
||||
nameKey: 'role_name',
|
||||
valueKey: 'id',
|
||||
childKey: 'children',
|
||||
collapseLeafNode: true,
|
||||
theme: 'auth-skin-default',
|
||||
autochecked: false,
|
||||
autoclose: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
40
catch/user/view/edit.html
Normal file
40
catch/user/view/edit.html
Normal file
@@ -0,0 +1,40 @@
|
||||
{$form|raw}
|
||||
<script>
|
||||
layui.use(['layer', 'form', 'admin', 'formX', 'authtree'], function () {
|
||||
var $ = layui.jquery;
|
||||
var layer = layui.layer;
|
||||
var form = layui.form;
|
||||
var admin = layui.admin;
|
||||
var authtree = layui.authtree;
|
||||
var mUser = admin.getLayerData('#userForm'); // 列表页面传递的数据,#modelUserForm这个只要写弹窗内任意一个元素的id即可
|
||||
// 回显数据
|
||||
form.val('userForm', mUser);
|
||||
var uid = "{$uid}";
|
||||
admin.req('{:url("/user/get/roles")}',{uid:uid}, function (response) {
|
||||
authtree.render('#roles', response.data.roles, {
|
||||
inputname: 'roleids[]',
|
||||
primaryKey: 'id',
|
||||
parentKey: 'parent_id',
|
||||
layfilter: 'lay-check-auth',
|
||||
autowidth: true,
|
||||
nameKey: 'role_name',
|
||||
valueKey: 'id',
|
||||
childKey: 'children',
|
||||
collapseLeafNode: true,
|
||||
theme: 'auth-skin-default',
|
||||
checkedKey: response.data.hasRoles,
|
||||
autochecked: false,
|
||||
autoclose: false,
|
||||
});
|
||||
});
|
||||
// 表单提交事件
|
||||
form.on('submit(submitUser)', function (data) {
|
||||
admin.req('/user/' + uid, data.field, function (response) {
|
||||
layer.msg(response.msg, {icon: 1});
|
||||
admin.putLayerData('formOk', true, '#userForm'); // 设置操作成功的标识,#modelUserForm这个只要写弹窗内任意一个元素的id即可
|
||||
admin.closeDialog('#userForm'); // 关闭页面层弹窗
|
||||
}, 'put');
|
||||
return false;
|
||||
});
|
||||
});
|
||||
</script>
|
194
catch/user/view/index.html
Normal file
194
catch/user/view/index.html
Normal file
@@ -0,0 +1,194 @@
|
||||
{extend name="$layout"}
|
||||
{block name="title"}用户管理{/block}
|
||||
{block name="search"}
|
||||
<div class="layui-form toolbar">
|
||||
<div class="layui-form-item">
|
||||
<div class="layui-inline">
|
||||
<label class="layui-form-label w-auto">用户名:</label>
|
||||
<div class="layui-input-inline">
|
||||
<input name="username" class="layui-input" type="text" placeholder="输入用户名"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-inline">
|
||||
<label class="layui-form-label w-auto">邮箱:</label>
|
||||
<div class="layui-input-inline mr0">
|
||||
<input name="email" class="layui-input" type="text" placeholder="输入邮箱"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-inline">
|
||||
<label class="layui-form-label w-auto">状态:</label>
|
||||
<div class="layui-input-inline mr0">
|
||||
<select name="status">
|
||||
<option value="">选择状态</option>
|
||||
<option value="1">正常</option>
|
||||
<option value="2">禁用</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-inline">
|
||||
<label class="layui-form-label w-auto">回收站:</label>
|
||||
<div class="layui-input-inline mr0">
|
||||
<select name="trash">
|
||||
<option value="">选择</option>
|
||||
<option value="1">恢复数据</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-inline" style="padding-right: 110px;">
|
||||
<button class="layui-btn icon-btn" lay-filter="formSubSearchUser" lay-submit>
|
||||
<i class="layui-icon"></i>搜索
|
||||
</button>
|
||||
<button id="btnAddUser" class="layui-btn icon-btn"><i class="layui-icon"></i>添加</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
{block name="table"}
|
||||
<table class="layui-table" id="tableUser" lay-filter="tableUser"></table>
|
||||
<!-- 表格操作列 -->
|
||||
<script type="text/html" id="tableBarUser">
|
||||
{:editButton()}
|
||||
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="{{d.deleted_at ? 'recover' : 'del'}}">{{d.deleted_at ? '恢复' : '删除'}}</a>
|
||||
</script>
|
||||
<!-- 表格状态列 -->
|
||||
<script type="text/html" id="tableStateUser">
|
||||
<input type="checkbox" lay-filter="ckStateUser" value="{{d.id}}" lay-skin="switch"
|
||||
lay-text="正常|禁用" {{d.status==1?'checked':''}}/>
|
||||
</script>
|
||||
{/block}
|
||||
{block name="script"}
|
||||
<script>
|
||||
layui.use(['layer', 'form', 'table', 'util', 'admin'], function () {
|
||||
var $ = layui.jquery;
|
||||
var layer = layui.layer;
|
||||
var form = layui.form;
|
||||
var table = layui.table;
|
||||
var util = layui.util;
|
||||
var admin = layui.admin;
|
||||
|
||||
// 渲染表格
|
||||
var insTb = table.render({
|
||||
elem: '#tableUser',
|
||||
url: '{:url("users")}',
|
||||
page: true,
|
||||
response: {
|
||||
statusCode: 10000,
|
||||
},
|
||||
toolbar: true,
|
||||
cellMinWidth: 100,
|
||||
cols: [[
|
||||
{type: 'id', title: '#', field: 'id'},
|
||||
{field: 'username', sort: true, title: '用户名'},
|
||||
{field: 'email', sort: true, title: '邮箱'},
|
||||
{field: 'status', sort: true, title: '状态', templet: '#tableStateUser'},
|
||||
{
|
||||
field: 'created_at', sort: true, templet: function (d) {
|
||||
return util.toDateString(d.created_at);
|
||||
}, title: '创建时间'
|
||||
},
|
||||
{
|
||||
field: 'updated_at', sort: true, templet: function (d) {
|
||||
return util.toDateString(d.updated_at);
|
||||
}, title: '更新时间'
|
||||
},
|
||||
{align: 'center', toolbar: '#tableBarUser', title: '操作', minWidth: 200}
|
||||
]],
|
||||
});
|
||||
|
||||
// 添加
|
||||
$('#btnAddUser').click(function () {
|
||||
showEditModel();
|
||||
});
|
||||
|
||||
// 搜索
|
||||
form.on('submit(formSubSearchUser)', function (data) {
|
||||
insTb.reload({where: data.field}, 'data');
|
||||
});
|
||||
|
||||
// 工具条点击事件
|
||||
table.on('tool(tableUser)', function (obj) {
|
||||
var data = obj.data;
|
||||
var layEvent = obj.event;
|
||||
if (layEvent === 'edit') { // 修改
|
||||
showEditModel(data);
|
||||
} else if (layEvent === 'del') { // 删除
|
||||
doDel(data.id, data.username);
|
||||
} else if (layEvent === 'reset') { // 重置密码
|
||||
resetPsw(data.id, data.username);
|
||||
} else if (layEvent === 'recover') {
|
||||
recover(data.id, data.username);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
function recover(uid, username) {
|
||||
layer.confirm('确定要恢复“' + username + '”吗?', {
|
||||
skin: 'layui-layer-admin',
|
||||
shade: .1
|
||||
}, function (i) {
|
||||
layer.close(i);
|
||||
admin.req('/user/recover/'+ uid,{}, function(res){
|
||||
layer.closeAll('loading');
|
||||
if (res.code == 10000) {
|
||||
layer.msg(res.msg, {icon: 1});
|
||||
insTb.reload({}, 'data');
|
||||
} else {
|
||||
layer.msg(res.msg, {icon: 2});
|
||||
}
|
||||
}, 'put');
|
||||
});
|
||||
}
|
||||
// 显示表单弹窗
|
||||
function showEditModel(mUser) {
|
||||
var layIndex = admin.open({
|
||||
title: (mUser ? '修改' : '添加') + '用户',
|
||||
url: mUser ? '/user/'+mUser.id + '/edit':'/user/create',
|
||||
data: mUser, // 传递数据到表单页面
|
||||
area: '500px',
|
||||
end: function () {
|
||||
if (admin.getLayerData(layIndex, 'formOk')) { // 判断表单操作成功标识
|
||||
insTb.reload(); // 成功刷新表格
|
||||
}
|
||||
},
|
||||
success: function (layero, dIndex) {
|
||||
// 弹窗超出范围不出现滚动条
|
||||
$(layero).children('.layui-layer-content').css('overflow', 'visible');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 删除
|
||||
function doDel(userId, nickName) {
|
||||
layer.confirm('确定要删除“' + nickName + '”吗?', {
|
||||
skin: 'layui-layer-admin',
|
||||
shade: .1
|
||||
}, function (i) {
|
||||
layer.close(i);
|
||||
admin.req('/user/'+ userId,{}, function(res){
|
||||
layer.closeAll('loading');
|
||||
if (res.code == 10000) {
|
||||
layer.msg(res.msg, {icon: 1});
|
||||
insTb.reload({}, 'data');
|
||||
} else {
|
||||
layer.msg(res.msg, {icon: 2});
|
||||
}
|
||||
}, 'delete');
|
||||
});
|
||||
}
|
||||
|
||||
// 修改用户状态
|
||||
form.on('switch(ckStateUser)', function (obj) {
|
||||
admin.req('/user/switch/status/'+obj.value,{}, function(res){
|
||||
layer.closeAll('loading');
|
||||
if (res.code == 10000) {
|
||||
layer.msg(res.msg, {icon: 1});
|
||||
} else {
|
||||
layer.msg(res.msg, {icon: 2});
|
||||
$(obj.elem).prop('checked', !obj.elem.checked);
|
||||
form.render('checkbox');
|
||||
}
|
||||
}, 'put');
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{/block}
|
Reference in New Issue
Block a user