You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
9.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\admin\controller;
use app\admin\model\Department;
use app\admin\model\Role;
use app\admin\model\Role_Permission as Role_PermissionModel;
use app\admin\model\User;
use app\admin\model\User as UserModel;
use app\admin\model\User_Role;
use app\lib\enum\ExceptionType;
use app\lib\exception\HtmlException;
use app\lib\exception\JsonException;
use app\lib\exception\Success;
use app\lib\validate\IdMustBePositiveInt;
use app\lib\validate\ModifyMemberCheck;
use think\Request;
use think\Session;
class MemberManager extends BaseController
{
public function index(){
$map = [];
if (Request::instance()->isPost()){
$input = Request::instance()->post();
}else {
$input = Request::instance()->get();
}
if (!empty($input['name'])){
$map['name'] = $input['name'];
}
if (!empty($input['roleId'])){
//得到前台选的角色在user_role表中得到该角色的所有职工的id
$roleId = $input['roleId'];
$userId = User_Role::where('role_id','=',$roleId)->column('user_id');
if (empty($userId[1])){
$map['id'] = $userId[0];
}else {
$map['id'] = ['in',$userId];
}
}
$roles = Role::where(1)->select();
$users = User::where($map)->paginate(10,false,[
"query" => [
'name' => $input['name'],
'roleId' => $input['roleId'],
]
]);
$total = $users->total();
$page = $users->render();
// foreach ($users as $user){
// $roleId = User_Role::where('user_id','=',$user['id'])->column('role_id');
// $roleNames = Role::Where('id','=',$roleId[0])->select();
// foreach ($roleNames as $roleName){
// if (empty($roleName['role'])){
// $user['position'] = "无描述";
// } else{
// $user['position'] = $roleName['role'];
// }
// }
// }
$this->assign('page',$page);
$this->assign('total',$total);
$this->assign('users',$users);
$this->assign('username',$input['username']);
$this->assign('roleId',$input['roleId']);
$this->assign('roles',$roles);
$this->assign('name',$input['name']);
return $this->fetch();
}
//添加新职工
public function addMember(){
$roles = Role::where(1)->select();
$department = Department::where(1)->select();
if (Request::instance()->isPost()){
$input = Request::instance()->post();
// p($input);die();
// $username = $input['name'];
$password = $input['password'];
//检查密码的格式是否正确
$msg = $this->checkPassword($password);
if(!empty($msg)){
throw new HtmlException($msg);
}
if(empty($input['sex'])){
throw new HtmlException('请选择性别');
}
if(empty($input['roleId'])){
throw new HtmlException('请选择职位');
}
if($input['roleId']!=1 && empty($input['departmentId'])){
throw new HtmlException('请选择科室');
}
$roleId = $input['roleId'];
// if ($roleId == 0){
// $roleId = 4;
// }
// $users = User::where(1)->select();
// foreach ($users as $user){
// if ($user['username'] == $username){
// $username = $username."1";
// }
// }
//入库user表
$userData = [
'name' => $input['name'],
'password' => md5($password),
'mobile' => $input['mobile'],
'sex' => $input['sex'],
'department' => $input['departmentId'],
'entry_time' => strtotime($input['entry_time']),
'position' => $roleId,
];
$userRes = User::create($userData);
//入库user_role表
$user_id = UserModel::where($userData)->column('id');
//user表中的id自增防止数据库表里的数据被删除后id确实而错误
$count = count($user_id);
$user_id = $user_id[$count-1];
// p($user_id);die();
$roleDate =[
'role_id' => $roleId,
'user_id' => $user_id,
];
$roleRes = User_Role::create($roleDate);
if ($userRes === 0 || $roleRes === 0){
throw new HtmlException('添加用户失败!');
}else {
$this->success('添加用户成功!');
}
}
$this->assign('roles',$roles);
$this->assign('department',$department);
return $this->fetch();
}
//密码复杂度
public function checkPassword($pwd)
{
$msg = "";
if (strlen($pwd) > 30 || strlen($pwd) < 6) {
$msg = "密码必须为6-30位的字符串!";
return $msg;
}
if (preg_match("/^\d*$/", $pwd)) {
$msg = "该密码只有数字类型!";
return $msg;
}
if (preg_match("/^[a-z]*$/i", $pwd)) {
$msg = "该密码只有字母类型!";
return $msg;
}
}
//修改职工信息
public function write(){
$userId = Request::instance()->param();
//find得到一维数组 select得到二维数组
$user = User::where(['id'=>$userId['id']])->find();
$roles = Role::where(1)->select();
$department = Department::where(1)->select();
if(Request::instance()->isPost()){
$data = [];
$input = Request::instance()->param();
$type = (new ModifyMemberCheck(ExceptionType::HTML))->goCheck();
// p($memberOld);die();
if($type['nameType'] == 1){
if(!empty($input['name'])){
$data['name'] = $input['name'];
}
else{
throw new HtmlException("请填写姓名");
}
}
if($type['mobileType'] == 1){
if(!empty($input['mobile'])){
$data['mobile'] = $input['mobile'];
}
else{
throw new HtmlException("请填写手机号");
}
}
if($type['sexType'] == 1){
if(!empty($input['sex'])){
$data['sex'] = $input['sex'];
}
else{
throw new HtmlException("请选择性别");
}
}
if($type['positionType'] == 1){
if(!empty($input['roleId'])){
$data['position'] = $input['roleId'];
User_Role::where('user_id','=',$input['id'])->update(['role_id' =>$input['roleId']]);
}
else{
throw new HtmlException("请选择职位");
}
}
if($type['departmentType'] == 1){
if(!empty($input['departmentId'])){
$data['department'] = $input['departmentId'];
}
else{
throw new HtmlException("请选择科室");
}
}
if($type['passwordType'] == 1){
if(!empty($input['password'])&&!empty($input['surePassword'])){
if($input['password'] == $input['surePassword']){
$data['password'] = md5($input['password']);
}
else{
throw new HtmlException("请确认您输入的密码和确认密码是一致的");
}
}
else{
throw new HtmlException("请输入密码");
}
}
if($type['timeType'] == 1){
if(!empty($input['entry_time'])){
$data['entry_time'] = strtotime($input['entry_time']);
}
else{
throw new HtmlException("请选择入职时间");
}
}
$member = UserModel::where(['id'=>$input['id']])->find();
$member->save($data);
if ($member === false)
return $this->error('修改失败');
return $this->success('修改成功');
}
$this->assign('roles',$roles);
$this->assign('department',$department);
$this->assign('user',$user);
return $this->fetch();
}
//裁员
public function delHandle($id){
// $roleId = Session::get('roleId');
// $permission_ids = Role_PermissionModel::where('role_id','=',$roleId)->column('permission_id');
// if (!in_array(2,$permission_ids)){
// throw new JsonException('1','您没有权限进行该操作!');
// }
// $userId = Session::get('id');
// if ($id == $userId){
// throw new JsonException('1','您不能删除自己!');
// }
(new IdMustBePositiveInt(ExceptionType::JSON))->goCheck();
if ($id == 10){
throw new JsonException('1','不能删除总管理员!');
}
$res = User::where(['id' => $id])->delete();
$temp = User_Role::where(['user_id'=>$id])->delete();
if($res == 0 && $temp == 0)
{
throw new JsonException('1','删除失败!');
}
throw new Success('删除成功!');
}
}