123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 |
- <?php
- /**
- * @copyright Copyright (c) 2021 勾股工作室
- * @license https://opensource.org/licenses/Apache-2.0
- * @link https://www.gougucms.com
- */
- declare (strict_types = 1);
- namespace app\admin\controller;
- use app\admin\BaseController;
- use app\admin\model\Admin as AdminList;
- use app\admin\validate\AdminCheck;
- use avatars\MDAvatars;
- use think\exception\ValidateException;
- use think\facade\Db;
- use think\facade\View;
- use think\facade\Session;
- use think\App;
- use think\facade\Cache;
- use app\admin\controller\Role;
- use app\admin\model\Department as DepartmentModel;
- class Admin extends BaseController
- {
- public function __construct(App $app)
- {
- parent::__construct($app);
- $this->Role = new Role($this->app);
- $this->Department = new DepartmentModel();
- }
- public function index()
- {
- if (request()->isAjax()) {
- // 获取单位名称
- $session_admin = get_config('app.session_admin');
- $id = Session::get($session_admin)['id'];
- $unit_name = Db::name('admin')->where('id', $id)->value('unit_name');
- $admin_permission = Db::name('admin')->where('id', $id)->value('permission');
- // halt($unit_name);
- $param = get_params();
-
- if (!empty($param['keywords'])) {
- $where[] = ['id|username|nickname|desc|mobile', 'like', '%' . $param['keywords'] . '%'];
- }
-
- $where = array();
- $permission = array();
- $where[] = ['status', '>=', 0];
- if($admin_permission == 0){
- $permission[] = ['unit_name', '=',$unit_name];
- }
-
- $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
- $admin = AdminList::where($where)->with('Department')
- ->where($permission)
- ->order('create_time asc')
- ->paginate($rows, false, ['query' => $param])
- ->each(function ($item, $key) {
- $groupId = Db::name('AdminGroupAccess')->where(['uid' => $item->id])->column('group_id');
- $groupName = Db::name('AdminGroup')->where('id', 'in', $groupId)->column('title');
- $item->groupName = implode(',', $groupName);
- $item->last_login_time = empty($item->last_login_time) ? '-' : date('Y-m-d H:i', $item->last_login_time);
- })->toArray();
- // dump($admin);
- if(get_login_admin('user_type') == -1){
- $admin_ = $admin['data'];
- unset($admin['data'][count($admin['data']) - 1]); //移除超级管理
- }
- return table_assign(0, '', $admin);
- } else {
- View::assign('user_type', get_login_admin('user_type'));
- return view();
- }
- }
-
- //添加
- public function add()
- {
- if (request()->isAjax()) {
- $param = get_params();
- // halt($param);
- if (!empty($param['id']) && $param['id'] > 0) { //对已存在账号的编辑
- try {
- validate(AdminCheck::class)->scene('edit')->check($param);
- } catch (ValidateException $e) {
- // 验证失败 输出错误信息
- return to_assign(1, $e->getError());
- }
- if (!empty($param['edit_pwd'])) {
- //重置密码
- if (empty($param['edit_pwd_confirm']) or $param['edit_pwd_confirm'] !== $param['edit_pwd']) {
- return to_assign(1, '两次密码不一致');
- }
-
- $param['salt'] = set_salt(20);
- $param['pwd'] = set_password($param['edit_pwd'], $param['salt']);
- }
- // halt($param);
- // 启动事务
- Db::startTrans();
- try {
- // halt($param);
- Db::name('Admin')->where(['id' => $param['id']])->strict(false)->field(true)->update($param); //更新密码
- Db::name('AdminGroupAccess')->where(['uid' => $param['id']])->delete(); //先删除权限组
- foreach ($param['group_id'] as $k => $v) {
- //为了系统安全,只有系统所有者才可创建id为1的管理员分组
- if ($v == 1 and get_login_admin('id') !== 1) {
- throw new ValidateException("你没有权限创建系统所有者", 1);
- }
- $data[$k] = [
- 'uid' => $param['id'],
- 'group_id' => $v,
- ];
- $param['admin_group_id'] = $v;
- }
- Db::name('AdminGroupAccess')->strict(false)->field(true)->insertAll($data); //再新增权限组
- if (!isset($param['thumb']) || $param['thumb'] == '') {
- $char = mb_substr($param['nickname'], 0, 1, 'utf-8');
- Db::name('Admin')->where('id', $param['id'])->update(['thumb' => $this->to_avatars($char)]);
- }
- //
- if($param['is_principal'] == 1){
-
- // $res = Db::name('Department')->where('id', $param['did'])->value('leader_id');
- // halt($res);
- $res = Db::name('Department')->where('id', $param['did'])->update(['leader_id' => $param['id']]);
- // $this->Department->where('id', $param['did'])->force()->save(['leader_id' => $param['id']]);
-
- // halt($res);
- }
- // 获取单位名称
- $unit_name = self::get_unitName($param['id']);
- $data = [
- 'unit_name' => $unit_name,
- 'user_type' => Db::name('Department')->where('id', $unit_name)->value('type'),
- 'admin_group_id' => $param['admin_group_id'],
- ];
- // unset($data['group_id']);
- // halt($data);
- $res = Db::name('admin')->where('id', $param['id'])->data($data)->update();
- // halt($res);
- add_log('edit', $param['id'], $param);
- //清除菜单\权限缓存
- clear_cache('adminMenu');
- clear_cache('adminRules');
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- return to_assign(1, '提交失败:' . $e->getMessage());
- }
- } else { //新增账号
- // halt($param);
- try {
- validate(AdminCheck::class)->scene('add')->check($param);
- } catch (ValidateException $e) {
- // 验证失败 输出错误信息
- return to_assign(1, $e->getError());
- }
- $param['salt'] = set_salt(20);
- $param['pwd'] = set_password($param['pwd'], $param['salt']);
- // 启动事务
- Db::startTrans();
- try {
-
- foreach ($param['group_id'] as $k => $v) {
- $param['admin_group_id'] = $v;
- }
- // halt($param);
- $uid = Db::name('Admin')->strict(false)->field(true)->insertGetId($param);
- unset($param['admin_group_id']);
- if($param['is_principal'] == 1){
- Db::name('department')->where('id', $param['did'])->data(['leader_id' => $uid])->update();
- }
- // halt($uid);
- foreach ($param['group_id'] as $k => $v) {
- //为了系统安全,只有系统所有者才可创建id为1的管理员分组
- if ($v == 1 and get_login_admin('id') !== 1) {
- throw new ValidateException("你没有权限创建系统所有者", 1);
- }
- $data[$k] = [
- 'uid' => $uid,
- 'group_id' => $v,
- ];
- }
- Db::name('AdminGroupAccess')->strict(false)->field(true)->insertAll($data);
- if (!isset($param['thumb']) || $param['thumb'] == '') {
- $char = mb_substr($param['nickname'], 0, 1, 'utf-8');
- Db::name('Admin')->where('id', $uid)->update(['thumb' => $this->to_avatars($char)]);
- }
- // 获取单位名称
- $unit_name = self::get_unitName($uid);
- $data = [
- 'unit_name' => $unit_name,
- 'user_type' => Db::name('Department')->where('id', $unit_name)->value('type'),
- ];
- // halt($data);
- Db::name('admin')->where('id', $uid)->data($data)->update();
- add_log('add', $uid, $param);
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- return to_assign(1, '提交失败:' . $e->getMessage());
- }
- }
- return to_assign(0,"操作成功");
- }else{
-
- $id = empty(get_params('id')) ? 0 : get_params('id');
- // dump($id);
- $permission = Db::name('admin')->where('id', get_login_admin('id'))->value('permission');
- $unit_name = Db::name('admin')->where('id', get_login_admin('id'))->value('unit_name');
- $group_id = Db::name('AdminGroupAccess')->where('uid', get_login_admin('id'))->value('group_id');
- $where_d = array();
- $where_p = array();
- $where_g = array();
- if($permission == 0){
- $where_d[] = ['unit_name', '=', $unit_name];
- $where_p[] = ['did', '=', $unit_name];
- $where_g[] = [
- ['unit_name', '=', $unit_name],
- ];
- }
-
- $department = Db::name('Department')
- ->where('status', '>=', 0)
- ->where($where_d)
- ->select()
- ->toArray();
- // dump($department);
- $department = set_recursion($department);
- // dump($department);
- $position = Db::name('Position')
- ->where('status', '>=', 0)
- ->where($where_p)
- ->order('create_time asc')
- ->select()
- ->toArray();
- foreach($position as $key => $value){
- $dep = Db::name('Department')->where('id', $value['did'])->value('title');
- $position[$key]['did_title'] = $dep;
- }
-
- $group = Db::name('AdminGroup')
- ->where($where_g)
- ->select()->toarray();
-
- if($permission == 1){
- $group = Db::name('AdminGroup')
- ->where('unit_name', '=', $unit_name)
- ->select()->toarray();
- // dump($group);
- }
-
- $group_mine = Db::name('AdminGroup')->where('id', $group_id)->select()->toArray();
- // dump($permission);
- if(get_login_admin('user_type') == -1){
- unset($group[0]); //移除超管权限组
- }
-
- $group = array_merge($group_mine, $group);
-
- if ($id > 0) {
- $admin = get_admin(get_params('id'));
- // dump($admin);
- // $did = get_login_admin('did');
- $leader_id = Db::name('Department')->where('id', $admin['did'])->value('leader_id');
- // dump($leader_id);
- if($leader_id == $id){
- $is_principal = 1;
- }else{
- $is_principal = 0;
- }
-
- View::assign('is_principal', $is_principal);
- View::assign('admin', $admin);
- }
- // dump($group);
- // dump($department);
- View::assign('permission', $permission);
- View::assign('department', $department);
- View::assign('position', $position);
- View::assign('group', $group);
- View::assign('id', $id);
- return view();
- }
- }
- public function to_avatars($char)
- {
- $defaultData = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
- 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'S', 'Y', 'Z',
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
- '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖', '拾',
- '一', '二', '三', '四', '五', '六', '七', '八', '九', '十');
- if (isset($char)) {
- $Char = $char;
- } else {
- $Char = $defaultData[mt_rand(0, count($defaultData) - 1)];
- }
- $OutputSize = min(512, empty($_GET['size']) ? 36 : intval($_GET['size']));
- $Avatar = new MDAvatars($Char, 256, 1);
- $avatar_name = '/avatars/avatar_256_' . set_salt(10) . time() . '.png';
- $path = get_config('filesystem.disks.public.url') . $avatar_name;
- $res = $Avatar->Save('.' . $path, 256);
- $Avatar->Free();
- /*
- if ($res) {
- //写入到附件表
- $data = [];
- $data['filepath'] = $path;
- $data['name'] = $Char;
- $data['mimetype'] = 'image/png';
- $data['fileext'] = 'png';
- $data['filesize'] = 0;
- $data['filename'] = $avatar_name;
- $data['sha1'] = '';
- $data['md5'] = '';
- $data['module'] = \think\facade\App::initialize()->http->getName();
- $data['action'] = app('request')->action();
- $data['uploadip'] = app('request')->ip();
- $data['create_time'] = time();
- $data['user_id'] = get_login_admin('id') ? get_login_admin('id') : 0;
- if ($data['module'] = 'admin') {
- //通过后台上传的文件直接审核通过
- $data['status'] = 1;
- $data['admin_id'] = $data['user_id'];
- $data['audit_time'] = time();
- }
- $data['use'] = 'avatar'; //附件用处
- $fid = Db::name('file')->insertGetId($data);
- return $fid;
- }
- */
- return $path;
- }
- //查看
- public function view()
- {
- $id = get_params('id');
- $rule = get_admin_rule();
- $user_groups = Db::name('AdminGroupAccess')
- ->alias('a')
- ->join("AdminGroup g", "a.group_id=g.id", 'LEFT')
- ->where("a.uid='{$id}' and g.status='1'")
- ->select()
- ->toArray();
- $groups = $user_groups ?: [];
- $rules = [];
- foreach ($groups as $g) {
- $rules = array_merge($rules, explode(',', trim($g['rules'], ',')));
- }
- $rules = array_unique($rules);
- $role_rule = create_tree_list(0, $rule, $rules);
- $role_rule = $this->Role->remove_checked_false($role_rule);
- $department = get_department();
- // dump($department);
- $position = Db::name('Position')->where('status', '>=', 0)->order('create_time asc')->select();
- View::assign('department', $department);
- View::assign('position', $position);
- View::assign('role_rule', $role_rule);
- View::assign('admin', get_admin($id));
- add_log('view', get_params('id'));
- return view('', ['admin' => get_admin(get_params('id'))]);
- }
- //删除
- public function delete()
- {
- $id = get_params("id");
- if($id == 1){
- return to_assign(0, "超级管理员,不能删除");
- }
- $data['status'] = '-1';
- $data['id'] = $id;
- $data['update_time'] = time();
- if (Db::name('Admin')->update($data) !== false) {
- add_log('delete', $id);
- return to_assign(0, "删除管理员成功");
- } else {
- return to_assign(1, "删除失败");
- }
- }
- //管理员操作日志
- public function log()
- {
- if (request()->isAjax()) {
- $param = get_params();
- $where = array();
- if (!empty($param['keywords'])) {
- $where[] = ['nickname|rule_menu|param_id', 'like', '%' . $param['keywords'] . '%'];
- }
- if (!empty($param['title_cate'])) {
- $where['title'] = $param['title_cate'];
- }
- if (!empty($param['rule_menu'])) {
- $where['rule_menu'] = $param['rule_menu'];
- }
- $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
- $content = DB::name('AdminLog')
- ->field("id,uid,nickname,title,content,rule_menu,ip,param_id,param,FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s') create_time")
- ->order('create_time desc')
- ->where($where)
- ->paginate($rows, false, ['query' => $param]);
- $content->toArray();
- foreach ($content as $k => $v) {
- $data = $v;
- $param_array = json_decode($v['param'], true);
- if(is_array($param_array)){
- $param_value = '';
- foreach ($param_array as $key => $value) {
- if (is_array($value)) {
- $value = implode(',', $value);
- }
- $param_value .= $key . ':' . $value . ' | ';
- }
- $data['param'] = $param_value;
- }
- else{
- $data['param'] = $param_array;
- }
- $content->offsetSet($k, $data);
- }
- return table_assign(0, '', $content);
- } else {
- return view();
- }
- }
- public function get_unitName($id){
- $did = Db::name('Admin')->where('id', $id)->value('did');
- $pid = $did;
-
- while($pid != 0){
- $value = Db::name('department')->where('id', $pid)->column('id,pid,title')[0];
- $id = $value['id'];
- $pid = $value['pid'];
- $title = $value['title'];
- }
-
- // halt($value);
- return $id;
- }
-
- }
|