This commit is contained in:
2024-10-29 14:04:59 +08:00
commit 48bf3e6f33
2839 changed files with 762707 additions and 0 deletions

281
app/admin/controller/User.php Executable file
View File

@@ -0,0 +1,281 @@
<?php
namespace app\admin\controller;
use think\Lang;
use think\Loader;
use think\Config;
use think\Session;
class User extends BaseController {
public function index() {
$this->redirect('/admin/user/lists');
}
public function lists() {
$skeyword = $this->request->get('skeyword', '', 'urldecode');
if (!empty($skeyword)) {
$skeyword = trim($skeyword);
$arg_where['u.username'] = ['like', '%' . $skeyword . '%'];
$search['skeyword'] = $skeyword;
Config::set('paginate.query', ['skeyword' => $skeyword]); //分页参数
} else {
$search['skeyword'] = '';
}
$arg_where['u.stat'] = ['gt', '-1'];
$dataObject = Loader::model('User')->getPageLists($arg_where, null, ['u.*', 'ur.name' => 'role'], 24);
//$roleOption = Loader::model('UserRole')->getOption($user['role_id'], ['stat' => 0, 'id' => ['neq', $this->user_id == 1 ? 0 : 1]], ['id' => 'desc'], ['id', 'name',], 50);
$value = [
//'roleOption' => $roleOption,
'list' => $dataObject->isEmpty() ? null : $dataObject->items(), //$dataObject->getCollection()->toArray()
'page' => $dataObject->render(),
//'page_previous' => $dataObject->getUrl($dataObject->currentPage() - 1),
//'page_next' => $dataObject->getUrl($dataObject->currentPage() + 1),
'search' => $search,
];
$this->assign($value);
return $this->fetch();
}
public function setting() {
if ($this->user_id) {
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->error(Lang::get('incorrect operation'));
}
$data['id'] = $this->user_id;
$model = Loader::model('User')->updateRow($data);
if ($model && $model->getData('id')) {
return $this->success('操作成功,重新登录后信息会同步更新');
} else {
return $this->error(Lang::get('operation failed'));
}
}
$user = Loader::model('User')->getRow((int) $this->user_id);
if (empty($user)) {
return $this->error(Lang::get('incorrect operation'));
}
$value['user'] = $user;
$this->assign($value);
return $this->fetch();
}
return $this->error(Lang::get('incorrect operation'));
}
public function edit($id = 0) {
$id = intval($id);
if ($id > 0) {
if ($id == 1 && !$this->administrator) {
return $this->error(Lang::get('incorrect operation'));
}
$user = Loader::model('User')->getRow(['id' => $id, 'stat' => ['gt', '-1']]);
if (empty($user)) {
return $this->error(Lang::get('incorrect operation'));
}
$value['user'] = $user;
$roleOption = Loader::model('UserRole')->getOption($user['role_id'], ['stat' => 0, 'id' => ['neq', $this->administrator ? 0 : 1]], ['id' => 'desc'], ['id', 'name',], 50);
$value['roleOption'] = $roleOption;
$this->assign($value);
return $this->fetch();
} else {
return $this->fetch('add');
}
}
public function update() {
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->error(Lang::get('incorrect operation'));
}
//验证规则
$validaterule = [
'id' => 'require',
'username' => 'require|length:2,64|unique:user,username',
'role_id' => 'require|between:0,2147483647',
];
//验证提示信息
$validatemsg = [
'id.require' => 'ID参数错误',
'username.requier' => '用户名不能为空',
'username.unique' => '用户名已经被注册',
'username.length' => '用户名在2-64个字符之间',
'role_id.require' => '用户角色不能为空',
];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
if ($data['id'] == 1 && !$this->administrator) {
return $this->error(Lang::get('incorrect operation'));
}
$model = Loader::model('User')->updateRow($data);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/user/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
} else {
return $this->error(Lang::get('operation failed'));
}
}
public function add() {
$roleOption = Loader::model('UserRole')->getOption(0, ['stat' => 0, 'id' => ['neq', $this->administrator ? 0 : 1]], ['id' => 'desc'], ['id', 'name',], 50);
$value = ['roleOption' => $roleOption,];
$this->assign($value);
return $this->fetch();
}
public function create() {
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->error(Lang::get('incorrect operation'));
}
//验证规则
$validaterule = [
'username' => 'require|length:2,64|unique:user,username',
'password' => 'require|min:6',
'repassword' => 'require|confirm:password',
'role_id' => 'require|between:0,2147483647',
];
//验证提示信息
$validatemsg = [
'username.require' => '用户名不能为空',
'username.unique' => '用户名已经被注册',
'username.length' => '用户名在2-64个字符之间',
'password.require' => '密码不能为空',
'password.min' => '密码最低6个字符',
'repassword.require' => '确认密码不能为空',
'repassword.confirm' => '两次密码不相符',
'role_id.require' => '用户角色不能为空',
];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
$model = Loader::model('User')->insertRow($data);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/user/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
} else {
return $this->error(Lang::get('operation failed'));
}
}
public function delete($id = 0) {
$id = intval($id);
if ($id > 0) {
$result = Loader::model('User')->deleteRow($id);
if ($result) {
if ($id == Session::get('user_auth.id')) {
Session::delete('user_auth', null);
Session::delete('user_auth_sign', null);
}
return $this->success(Lang::get('operation successed'), url('/admin/user/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function deletes() {
if ($this->request->isPost()) {
$data = $this->request->post();
$result = Loader::model('User')->deleteRows($data['ids']);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/user/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function destroy($id = 0) {
$id = intval($id);
if ($id > 0) {
$result = Loader::model('User')->destroyRow($id);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/user/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function destroys() {
if ($this->request->isPost()) {
$data = $this->request->post();
$result = Loader::model('User')->destroyRows($data['ids']);
if ($result) {
return $this->success(Lang::get('operation successed'), url('/admin/user/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function updatepassword() {
// echo 1;die;
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->error(Lang::get('incorrect operation'));
}
//验证规则
$validaterule = [
'id' => "require",
'newpassword' => 'require|min:6',
'repassword' => 'require|confirm:newpassword',
];
if (!$this->administrator) {
$validaterule['oldpassword'] = 'require|min:6';
}
//验证提示信息
$validatemsg = [
'id.require' => 'ID参数错误',
'oldpassword.require' => '密码不能为空',
'oldpassword.min' => '密码最低6个字符',
'newpassword.require' => '密码不能为空',
'newpassword.min' => '密码最低6个字符',
'repassword.require' => '确认密码不能为空',
'repassword.confirm' => '两次密码不相符',
];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
$usersModel = Loader::model('User');
if (!$this->administrator) {
$user = $usersModel->find($this->user_id);
if (empty($user)) {
return $this->error(Lang::get('incorrect operation'));
}
if ($user['password'] != md5($data['oldpassword'])) {
return $this->error('旧密码输入错误');
}
$data['id'] = $this->user_id;
}
$model = $usersModel->updatePassword($data);
if ($model && $model->getData('id')) {
return $this->success(Lang::get('operation successed'), url('/admin/user/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('operation failed'));
}
}