123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- <?php
- namespace app\admin\controller\auth;
- use app\common\library\Redis;
- use app\common\model\AuthGroup;
- use app\common\controller\Backend;
- use app\main\service\AdminService;
- use fast\Random;
- use fast\Tree;
- use app\common\model\Admin;
- use app\common\model\VipAdminBind;
- /**
- * 管理员管理
- *
- * @icon fa fa-users
- * @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
- */
- class Operator extends Backend
- {
- /**
- * @var Admin
- */
- protected $model = null;
- /**
- * @var VipAdminBind
- */
- protected $vipAdminBind = null;
- protected $dataLimit = false;
- protected $childrenGroupIds = [];
- protected $childrenAdminIds = [];
- protected $searchFields = 'id,username,nickname';
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('Admin');
- $this->vipAdminBind = model('VipAdminBind');
- // $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin() ? true : false);
- //
- // $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
- // Tree::instance()->init($groupList);
- // $result = [];
- // if ($this->auth->isSuperAdmin())
- // {
- // $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
- // }
- // else
- // {
- // $groups = $this->auth->getGroups();
- // foreach ($groups as $m => $n)
- // {
- // $result = array_merge($result, Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['pid'])));
- // }
- // }
- // $groupName = [];
- // foreach ($result as $k => $v)
- // {
- // $groupName[$v['id']] = $v['name'];
- // }
- // $this->view->assign('groupdata', $groupName);
- $this->assignconfig("admin", ['id' => $this->auth->id]);
- }
- /**
- * 查看
- */
- public function index()
- {
- if ($this->request->isAjax())
- {
- $operatorids = $this->model->getOperatorIdsByVipId($this->auth->id);
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $this->model
- ->where($where)
- ->where('id', 'in', $operatorids)
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->where($where)
- ->join("admin_extend ae","admin.id = ae.admin_id")
- ->where('id', 'in', $operatorids)
- ->field("id,username,nickname,mobile,status,ae.remark remark")
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- if ($params)
- {
- if(! AdminService::instance()->checkPassword($params['password'])){
- $this->error(AdminService::instance()->getPasswordRule());
- }
- $params['salt'] = Random::alnum();
- $params['password'] = md5(md5($params['password']) . $params['salt']);
- $params['avatar'] = asset('/img/avatar.png'); //设置新管理员默认头像。
- $result = $this->model->validate('Admin.add')->save($params);
- if ($result === false)
- {
- $this->error($this->model->getError());
- }
- $dataset = ['uid' => $this->model->id, 'group_id' => 8];
- model('AuthGroupAccess')->save($dataset);
- $extends = $this->request->post("extends/a");
- $extends['admin_id'] = $this->model->id;
- $extends['create_by'] = $this->auth->id;
- $extends['benefit'] = 0.00;
- $extends['card_holder'] = '';
- $extends['card_num'] = '';
- model("AdminExtend")->save($extends);
- $this->success();
- }
- $this->error();
- }
- return $this->view->fetch();
- }
- /**
- * 编辑
- */
- public function edit($ids = NULL)
- {
- $row = $this->model->get(['id' => $ids]);
- if (!$row)
- $this->error(__('No Results were found'));
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- if ($params)
- {
- if ($params['password'])
- {
- if(! AdminService::instance()->checkPassword($params['password'])){
- $this->error(AdminService::instance()->getPasswordRule());
- }
- $params['salt'] = Random::alnum();
- $params['password'] = md5(md5($params['password']) . $params['salt']);
- AdminService::instance()->updateAdminSessionStatus($ids);
- }
- else
- {
- unset($params['password'], $params['salt']);
- }
- //这里需要针对username和email做唯一验证
- $adminValidate = \think\Loader::validate('Admin');
- $adminValidate->rule([
- 'username' => 'require|max:50|unique:admin,username,' . $row->id,
- 'email' => 'email|unique:admin,email,' . $row->id
- ]);
- $result = $row->validate('Admin.edit')->save($params);
- if ($result === false)
- {
- $this->error($row->getError());
- }
- //删除当前运营与渠道商的关系
- if ($params['status'] == 'hidden') {
- $this->vipAdminBind->where('admin_id_master', $ids)->delete();
- }
- $extends = $this->request->post("extends/a");
- $extends['admin_id'] = $row['id'];
- model("AdminExtend")->update($extends);
- $this->success();
- }
- $this->error();
- }
- $row['remark'] = model("AdminExtend")->where("admin_id",$row['id'])->value('remark');
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- /**
- * 编辑
- */
- /**
- * 编辑
- */
- public function editSelf()
- {
- //
- // $sql = '';
- // for($i=256;$i<=511;$i++){
- // $sql.="USE test_cps_user_{$i};DELETE FROM USER WHERE id>0;DELETE FROM OPENID WHERE id>0;DELETE FROM RECHARGE WHERE id>0;";
- // }
- // echo $sql;die;
- $ids = $this->auth->id;
- $group = model('AuthGroupAccess')->where('uid',$ids)->find();
- $this->assign('groupId',$group->group_id);
- $row = $this->model->get(['id' => $ids]);
- if (!$row)
- $this->error(__('No Results were found'));
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- if ($params)
- {
- if ($params['password'])
- {
- $params['salt'] = Random::alnum();
- $params['password'] = md5(md5($params['password']) . $params['salt']);
- }
- else
- {
- unset($params['password'], $params['salt']);
- }
- //这里需要针对username和email做唯一验证
- $adminValidate = \think\Loader::validate('Admin');
- $adminValidate->rule([
- 'username' => 'require|max:50|unique:admin,username,' . $row->id,
- 'email' => 'require|email|unique:admin,email,' . $row->id
- ]);
- $extends = $this->request->post("extend/a");
- //验证身份证是否合法
- if(!empty($extends['idcard_no'])){
- $flag = validateIDCard($extends['idcard_no']);
- if(!$flag){
- $this->error('身份证号不合法,请重新填写');
- }else{
- model('AdminExtend')->save(['idcard_no'=>$extends['idcard_no']],['admin_id'=>$ids]);
- $redis = Redis::instance();
- $key = 'AE:'.$ids;
- $redis->del($key);
- }
- }
- $result = $row->validate('Admin.edit')->save($params);
- if ($result === false)
- {
- $this->error($row->getError());
- }
- $this->success();
- }
- $this->error();
- }
- $extends = model('Admin_extend')->where('admin_id',$row['id'])->find();
- $this->view->assign("payMethodList", model('AdminExtend')->getPayMethodList());
- $this->view->assign('extends',$extends);
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- /**
- * 删除
- */
- public function del($ids = "")
- {
- if ($ids)
- {
- // 避免越权删除管理员
- $childrenGroupIds = $this->childrenGroupIds;
- $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function($query) use($childrenGroupIds) {
- $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
- })->select();
- if ($adminList)
- {
- $deleteIds = [];
- foreach ($adminList as $k => $v)
- {
- $deleteIds[] = $v->id;
- }
- $deleteIds = array_diff($deleteIds, [$this->auth->id]);
- if ($deleteIds)
- {
- $this->model->where('id','in',$deleteIds)->update(['status'=>'hidden']);
- // $this->model->destroy($deleteIds);
- // model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
- $this->success();
- }
- }
- }
- $this->error();
- }
- /**
- * 批量更新
- * @internal
- */
- public function multi($ids = "")
- {
- // 管理员禁止批量操作
- $this->error();
- }
- }
|