diff --git a/application/admin/controller/auth/Admin.php b/application/admin/controller/auth/Admin.php index 40c6c8018e048acd710493bb68e7e5ef1d094118..1da8cd377cde35543b43d37db650fb74bfbf8e77 100644 --- a/application/admin/controller/auth/Admin.php +++ b/application/admin/controller/auth/Admin.php @@ -96,29 +96,24 @@ class Admin extends Backend $adminGroupName[$this->auth->id][$n['id']] = $n['name']; } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); - $total = $this->model - ->where($where) - ->where('id', 'in', $this->childrenAdminIds) - ->order($sort, $order) - ->count(); - - $list = $this->model - ->where($where) - ->where('id', 'in', $this->childrenAdminIds) - ->field(['password', 'salt', 'token'], true) - ->order($sort, $order) - ->limit($offset, $limit) - ->select(); - foreach ($list as $k => &$v) + $params = [ + 'where' => $where, + 'sort' => $sort, + 'order' => $order, + 'offset' => $offset, + 'limit' => $limit, + 'extra_where' => ['IN',$this->childrenAdminIds] + ]; + $list = $this->model->getAdminList($params); + foreach ($list['rows'] as $k => &$v) { $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : []; $v['groups'] = implode(',', array_keys($groups)); $v['groups_text'] = implode(',', array_values($groups)); } unset($v); - $result = array("total" => $total, "rows" => $list); - return json($result); + return json($list); } return $this->view->fetch(); } @@ -136,22 +131,33 @@ class Admin extends Backend $params['salt'] = Random::alnum(); $params['password'] = md5(md5($params['password']) . $params['salt']); $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。 - $result = $this->model->validate('Admin.add')->save($params); - if ($result === false) - { - $this->error($this->model->getError()); + try { + $this->model->startTrans(); + $result = $this->model->validate('Admin.add')->save($params); + if ($result === false) + { + exception($this->model->getError()); + } + $group = $this->request->post("group/a"); + + //过滤不允许的组别,避免越权 + $group = array_intersect($this->childrenGroupIds, $group); + $dataset = []; + foreach ($group as $value) + { + $dataset[] = ['uid' => $this->model->id, 'group_id' => $value]; + } + $result = model('AuthGroupAccess')->saveAll($dataset); + if ($result === false) + { + exception(model('AuthGroupAccess')->getError()); + } + $this->success(); + } catch (\Exception $e) { + $this->model->rollback(); + $this->error($e->getMessage()); } - $group = $this->request->post("group/a"); - - //过滤不允许的组别,避免越权 - $group = array_intersect($this->childrenGroupIds, $group); - $dataset = []; - foreach ($group as $value) - { - $dataset[] = ['uid' => $this->model->id, 'group_id' => $value]; - } - model('AuthGroupAccess')->saveAll($dataset); - $this->success(); + } $this->error(); } diff --git a/application/admin/model/Admin.php b/application/admin/model/Admin.php index 8c47f6f7447835740c41491caaf814143c0d0b0f..d1263b31ae68985b065448b089d874f33ad3d328 100644 --- a/application/admin/model/Admin.php +++ b/application/admin/model/Admin.php @@ -2,10 +2,9 @@ namespace app\admin\model; -use think\Model; -use think\Session; +use app\common\model\Base; -class Admin extends Model +class Admin extends Base { // 开启自动写入时间戳字段 @@ -30,5 +29,11 @@ class Admin extends Model { return $encrypt($password . $salt); } - + + public function getAdminList($params=null){ + $data = $this->listConditions($params); + $list = $data['model']->select(); + $rows = $this->formatList($list, $params); + return ['total' => $data['total'], 'rows' => $rows ]; + } } diff --git a/application/common/model/Base.php b/application/common/model/Base.php new file mode 100644 index 0000000000000000000000000000000000000000..fe640aa44226d4dabce64f37e296ba68f591523d --- /dev/null +++ b/application/common/model/Base.php @@ -0,0 +1,99 @@ +where($params['where']); + } + if( isset($params['extra_where']) && $params['extra_where']) { + $this->where($params['extra_where']); + } + + if( isset($params['field']) && $params['field'] ) { + $this->field($params['field']); + } + return $this; + } + /** + * 解析列表传入参数 + * @param string $params + * @return \app\common\model\Base|number + */ + public function listConditions($params) + { + if( isset($params['where']) && $params['where'] ) { + $this->where($params['where']); + } + if( isset($params['extra_where']) && $params['extra_where']) { + $this->where($params['extra_where']); + } + $total = $this->count(); + if( isset($params['get_count']) && $params['get_count'] ) { + return $total; + } + if( isset($params['where']) && $params['where'] ) { + $this->where($params['where']); + } + if( isset($params['extra_where']) && $params['extra_where']) { + $this->where($params['extra_where']); + } + if( isset($params['field']) && $params['field'] ) { + $this->field($params['field']); + } + if( isset($params['sort']) && $params['sort'] && isset($params['sort']) && $params['order'] ) { + $this->order($params['sort'], $params['order']); + } + if( isset($params['offset']) && $params['offset'] < 0 ) { + $params['offset'] = 0; + } + if( isset($params['limit']) && $params['limit'] > 0 ) { + $this->limit($params['offset'], $params['limit']); + } else { + if( isset($params['group']) && $params['group'] ) { + $this->group($params['group']); + } + } + return ['total'=>$total,'model'=>$this]; + } + /** + * 格式化列表 + * @param unknown $list + * @param unknown $format ['key_value'=>'id','key_list'=>false] 当key_value存在(必须为唯一值,例id),key_list为真,列表键值按此字段 ,否则返回原列表 + * @return array|unknown|unknown[]|unknown + */ + public function formatList($list,$format) { + if(!$list) { + return []; + } + $newList = []; + if( isset($format['key_value']) && $format['key_value'] ) { + foreach ($list as $val) { + $newList[$val[$format['key_value']]] = $val; + } + unset($val); + } + if( isset($format['key_list']) && $format['key_list'] ) { + return $newList ?? $list; + } + return $list; + } + +} \ No newline at end of file