123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <?php
- namespace app\admin\controller;
- use think\facade\View;
- use think\facade\Lang;
- /**
-
- * 控制器
- */
- class Admin extends AdminControl
- {
- public function initialize()
- {
- parent::initialize();
- Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/admin.lang.php');
- }
- /**
- * 管理员列表
- */
- public function admin()
- {
- $admin_mod = model('admin');
- $condition = array();
- $admin_list = $admin_mod->getAdminList($condition, 10);
- View::assign('admin_list', $admin_list);
- View::assign('show_page', $admin_mod->page_info->render());
- $this->setAdminCurItem('admin');
- return View::fetch('admin');
- }
- /**
- * 管理员删除
- */
- public function admin_del()
- {
- $admin_id = intval(input('param.admin_id'));
- if (!empty($admin_id)) {
- if ($admin_id == 1) {
- $this->error(lang('ds_common_save_fail'));
- }
- $admin_mod = model('admin');
- $admin_mod->delAdmin(array(array('admin_id', '=', $admin_id)));
- $this->log(lang('ds_del') . lang('limit_admin') . '[ID:' . $admin_id . ']', 1);
- ds_json_encode(10000, lang('ds_common_del_succ'));
- } else {
- ds_json_encode(10001, lang('ds_common_del_fail'));
- }
- }
- /**
- * 管理员添加
- */
- public function admin_add()
- {
- $admin_model = model('admin');
- if (!request()->isPost()) {
- //得到权限组
- $gadmin = $admin_model->getGadminList('gname,gid');
- View::assign('gadmin', $gadmin);
- return View::fetch('admin_form');
- } else {
- $data['admin_name'] = input('post.admin_name');
- $data['admin_gid'] = input('post.gid');
- $data['admin_password'] = md5(input('post.admin_password'));
- if (empty(input('post.admin_password'))) {
- $this->error(lang('admin_add_password_null'));
- }
- $admin_validate = ds_validate('admin');
- if (!$admin_validate->scene('admin_add')->check($data)) {
- $this->error($admin_validate->getError());
- }
- //判断是否重名
- $admin_info = $admin_model->getOneAdmin(array(array('admin_name', '=', $data['admin_name'])));
- if ($admin_info) {
- $this->error(lang('admin_add_admin_not_exists'));
- }
- $rs = $admin_model->addAdmin($data);
- if ($rs) {
- $this->log(lang('ds_add') . lang('limit_admin') . '[' . input('post.admin_name') . ']', 1);
- dsLayerOpenSuccess(lang('ds_common_save_succ'));
- } else {
- $this->error(lang('ds_common_save_fail'));
- }
- }
- }
- /**
- * ajax操作
- */
- public function ajax()
- {
- $admin_model = model('admin');
- switch (input('get.branch')) {
- //管理人员名称验证
- case 'check_admin_name':
- $condition[] = array('admin_name', '=', input('get.admin_name'));
- $admin_info = $admin_model->infoAdmin($condition);
- if (!empty($admin_info)) {
- exit('false');
- } else {
- exit('true');
- }
- break;
- //权限组名称验证
- case 'check_gadmin_name':
- $condition = array();
- if (is_numeric(input('param.gid'))) {
- $condition[] = array('gid', '<>', intval(input('param.gid')));
- }
- $condition[] = array('gname', '=', input('get.gname'));
- $info = $admin_model->getOneGadmin($condition);
- if (!empty($info)) {
- exit('false');
- } else {
- exit('true');
- }
- break;
- }
- }
- /**
- * 设置管理员权限
- */
- public function admin_edit()
- {
- $admin_id = intval(input('param.admin_id'));
- if (request()->isPost()) {
- //没有更改密码
- if (input('post.admin_password') != '') {
- $data['admin_password'] = md5(input('post.admin_password'));
- }
- $data['admin_gid'] = intval(input('post.gid'));
- //查询管理员信息
- $admin_model = model('admin');
- $result = $admin_model->editAdmin($data, $admin_id);
- if ($result) {
- $this->log(lang('ds_edit') . lang('limit_admin') . '[ID:' . $admin_id . ']', 1);
- dsLayerOpenSuccess(lang('admin_edit_success'));
- } else {
- $this->error(lang('admin_edit_fail'));
- }
- } else {
- //查询用户信息
- $admin_model = model('admin');
- $admin = $admin_model->getOneAdmin(array(array('admin_id', '=', $admin_id)));
- if (!is_array($admin) || count($admin) <= 0) {
- $this->error(lang('admin_edit_admin_error'), (string)url('admin/admin'));
- }
- View::assign('admin', $admin);
- //得到权限组
- $gadmin = $admin_model->getGadminList('gname,gid');
- View::assign('gadmin', $gadmin);
- return View::fetch('admin_form');
- }
- }
- /**
- * 取得所有权限项
- *
- * @return array
- */
- private function permission()
- {
- $limit = $this->limitList();
- if (is_array($limit)) {
- foreach ($limit as $k => $v) {
- if (is_array($v['child'])) {
- $tmp = array();
- foreach ($v['child'] as $key => $value) {
- $controller = (!empty($value['controller'])) ? $value['controller'] : $v['controller'];
- if (strpos($controller, '|') == false) { //controller参数不带|
- $limit[$k]['child'][$key]['action'] = rtrim($controller . '.' . str_replace('|', '|' . $controller . '.', $value['action']), '.');
- } else { //controller参数带|
- $tmp_str = '';
- if (empty($value['action'])) {
- $limit[$k]['child'][$key]['action'] = $controller;
- } elseif (strpos($value['action'], '|') == false) { //action参数不带|
- foreach (explode('|', $controller) as $v1) {
- $tmp_str .= "$v1.{$value['action']}|";
- }
- $limit[$k]['child'][$key]['action'] = rtrim($tmp_str, '|');
- } elseif (strpos($value['action'], '|') != false && strpos($controller, '|') != false) { //action,controller都带|,交差权限
- foreach (explode('|', $controller) as $v1) {
- foreach (explode('|', $value['action']) as $v2) {
- $tmp_str .= "$v1.$v2|";
- }
- }
- $limit[$k]['child'][$key]['action'] = rtrim($tmp_str, '|');
- }
- }
- }
- }
- }
- return $limit;
- } else {
- return array();
- }
- }
- /**
- * 权限组
- */
- public function gadmin()
- {
- $admin_model = model('admin');
- $gadmin_list = $admin_model->getGadminList();
- View::assign('gadmin_list', $gadmin_list);
- $this->setAdminCurItem('gadmin');
- return View::fetch('gadmin');
- }
- /**
- * 添加权限组
- */
- public function gadmin_add()
- {
- if (!request()->isPost()) {
- View::assign('limit', $this->permission());
- return View::fetch('gadmin_add');
- } else {
- $limit_str = '';
- $permission_array = input('post.permission/a');
- if (is_array($permission_array)) {
- $limit_str = implode('|', $permission_array);
- }
- $data['glimits'] = ds_encrypt($limit_str, MD5_KEY . md5(input('post.gname')));
- $data['gname'] = input('post.gname');
- $admin_model = model('admin');
- if ($admin_model->addGadmin($data)) {
- $this->log(lang('ds_add') . lang('limit_gadmin') . '[' . input('post.gname') . ']', 1);
- dsLayerOpenSuccess(lang('ds_common_save_succ'));
- } else {
- $this->error(lang('ds_common_save_fail'));
- }
- }
- }
- /**
- * 设置权限组权限
- */
- public function gadmin_set()
- {
- $gid = intval(input('param.gid'));
- $admin_model = model('admin');
- $ginfo = $admin_model->getOneGadmin(array(array('gid', '=', $gid)));
- if (empty($ginfo)) {
- $this->error(lang('admin_set_admin_not_exists'));
- }
- if (!request()->isPost()) {
- //解析已有权限
- $hlimit = ds_decrypt($ginfo['glimits'], MD5_KEY . md5($ginfo['gname']));
- $ginfo['glimits'] = explode('|', $hlimit);
- View::assign('ginfo', $ginfo);
- View::assign('limit', $this->permission());
- return View::fetch('gadmin_set');
- } else {
- $limit_str = '';
- $permission_array = input('post.permission/a');
- if (is_array($permission_array)) {
- $limit_str = implode('|', $permission_array);
- }
- $limit_str = ds_encrypt($limit_str, MD5_KEY . md5(input('post.gname')));
- $data['glimits'] = $limit_str;
- $data['gname'] = input('post.gname');
- $update = $admin_model->editGadmin(array(array('gid', '=', $gid)), $data);
- if ($update) {
- $this->log(lang('ds_edit') . lang('limit_gadmin') . '[' . input('post.gname') . ']', 1);
- dsLayerOpenSuccess(lang('ds_common_save_succ'));
- } else {
- $this->error(lang('ds_common_save_succ'));
- }
- }
- }
- /**
- * 组删除
- */
- public function gadmin_del()
- {
- if (is_numeric(input('param.gid'))) {
- $admin_model = model('admin');
- $admin_model->delGadmin(array(array('gid', '=', intval(input('param.gid')))));
- $this->log(lang('ds_del') . lang('limit_gadmin') . '[ID' . intval(input('param.gid')) . ']', 1);
- ds_json_encode(10000, lang('ds_common_op_succ'));
- } else {
- ds_json_encode(10000, lang('ds_common_op_fail'));
- }
- }
- /**
- * 获取卖家栏目列表,针对控制器下的栏目
- */
- protected function getAdminItemList()
- {
- $menu_array = array(
- array(
- 'name' => 'admin',
- 'text' => lang('limit_admin'),
- 'url' => (string)url('admin/admin')
- ),
- array(
- 'name' => 'admin_add',
- 'text' => lang('admin_add_limit_admin'),
- 'url' => "javascript:dsLayerOpen('" . (string)url('admin/admin_add') . "','" . lang('admin_add_limit_admin') . "')"
- ),
- array(
- 'name' => 'gadmin',
- 'text' => lang('limit_gadmin'),
- 'url' => (string)url('admin/gadmin')
- ),
- array(
- 'name' => 'gadmin_add',
- 'text' => lang('admin_add_limit_gadmin'),
- 'url' => "javascript:dsLayerOpen('" . (string)url('admin/gadmin_add') . "','" . lang('admin_add_limit_gadmin') . "')"
- ),
- );
- return $menu_array;
- }
- }
|