From 2433a367ab2bf17ae5be6eb0779903c4bf446d45 Mon Sep 17 00:00:00 2001 From: yhf320 Date: Wed, 7 Nov 2018 09:53:02 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E5=9F=BA=E7=B1=BB=EF=BC=8C=E4=BE=BF=E4=BA=8E=E6=89=A9=E5=B1=95?= =?UTF-8?q?=E5=92=8C=E9=87=8D=E5=86=99=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/common/model/Base.php | 99 +++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 application/common/model/Base.php diff --git a/application/common/model/Base.php b/application/common/model/Base.php new file mode 100644 index 000000000..fe640aa44 --- /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 -- Gitee From 52e8a5eb2bdf9005bd93ef58898331f25202ca7b Mon Sep 17 00:00:00 2001 From: yhf320 Date: Wed, 7 Nov 2018 10:02:42 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E7=BB=A7=E6=89=BF=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E5=9F=BA=E7=B1=BB=EF=BC=8C=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=88=97=E8=A1=A8=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/model/Admin.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/application/admin/model/Admin.php b/application/admin/model/Admin.php index 8c47f6f74..d1263b31a 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 ]; + } } -- Gitee From 2bd87e2f3163ef461a916dc8e0ae8d93c81fc5a5 Mon Sep 17 00:00:00 2001 From: yhf320 Date: Wed, 7 Nov 2018 10:04:27 +0800 Subject: [PATCH 3/4] =?UTF-8?q?1.=E9=87=8D=E5=86=99index()=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=95=B0=E6=8D=AE=E6=96=B9=E5=BC=8F=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E6=A8=A1=E5=9E=8B=E8=8E=B7=E5=8F=96=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=88=97=E8=A1=A8=E6=96=B9=E6=B3=95=EF=BC=9B2.add()?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E4=B8=AD=E5=A2=9E=E5=8A=A0=E4=BA=8B=E5=8A=A1?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/controller/auth/Admin.php | 60 ++++++++++++++------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/application/admin/controller/auth/Admin.php b/application/admin/controller/auth/Admin.php index 40c6c8018..1995171d2 100644 --- a/application/admin/controller/auth/Admin.php +++ b/application/admin/controller/auth/Admin.php @@ -96,7 +96,16 @@ class Admin extends Backend $adminGroupName[$this->auth->id][$n['id']] = $n['name']; } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); - $total = $this->model + $params = [ + 'where' => $where, + 'sort' => $sort, + 'order' => $order, + 'offset' => $offset, + 'limit' => $limit, + 'extra_where' => ['IN',$this->childrenAdminIds] + ]; + $list = $this->model->getAdminList($params); + /* $total = $this->model ->where($where) ->where('id', 'in', $this->childrenAdminIds) ->order($sort, $order) @@ -108,17 +117,17 @@ class Admin extends Backend ->field(['password', 'salt', 'token'], true) ->order($sort, $order) ->limit($offset, $limit) - ->select(); - foreach ($list as $k => &$v) + ->select(); */ + 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); + //$result = array("total" => $total, "rows" => $list); - return json($result); + return json($list); } return $this->view->fetch(); } @@ -136,22 +145,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()); - } - $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]; + 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()); } - model('AuthGroupAccess')->saveAll($dataset); - $this->success(); + } $this->error(); } -- Gitee From 271932f35a60f7de674f22b1e5fa9b2cf6778287 Mon Sep 17 00:00:00 2001 From: yhf320 Date: Wed, 7 Nov 2018 10:31:58 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E9=87=8D=E5=86=99index()=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=96=B9=E5=BC=8F=E6=94=B9=E4=B8=BA=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E6=A8=A1=E5=9E=8B=E8=8E=B7=E5=8F=96=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/controller/auth/Admin.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/application/admin/controller/auth/Admin.php b/application/admin/controller/auth/Admin.php index 1995171d2..1da8cd377 100644 --- a/application/admin/controller/auth/Admin.php +++ b/application/admin/controller/auth/Admin.php @@ -105,19 +105,6 @@ class Admin extends Backend 'extra_where' => ['IN',$this->childrenAdminIds] ]; $list = $this->model->getAdminList($params); - /* $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['rows'] as $k => &$v) { $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : []; @@ -125,7 +112,6 @@ class Admin extends Backend $v['groups_text'] = implode(',', array_values($groups)); } unset($v); - //$result = array("total" => $total, "rows" => $list); return json($list); } -- Gitee