Dli.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace app\admin\controller\daili;
  3. use app\admin\model\AuthGroup;
  4. use app\admin\model\AuthGroupAccess;
  5. use app\common\controller\Backend;
  6. use fast\Random;
  7. use fast\Tree;
  8. use think\Validate;
  9. use think\Db;
  10. /**
  11. * 管理员管理
  12. *
  13. * @icon fa fa-users
  14. * @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
  15. */
  16. class Dli extends Backend
  17. {
  18. /**
  19. * @var \app\admin\model\Admin
  20. */
  21. protected $model = null;
  22. protected $childrenGroupIds = [];
  23. protected $childrenAdminIds = [];
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = model('Admin');
  28. $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
  29. $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
  30. $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
  31. Tree::instance()->init($groupList);
  32. $groupdata = [];
  33. if ($this->auth->isSuperAdmin()) {
  34. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  35. foreach ($result as $k => $v) {
  36. $groupdata[$v['id']] = $v['name'];
  37. }
  38. } else {
  39. $result = [];
  40. $groups = $this->auth->getGroups();
  41. foreach ($groups as $m => $n) {
  42. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
  43. $temp = [];
  44. foreach ($childlist as $k => $v) {
  45. $temp[$v['id']] = $v['name'];
  46. }
  47. $result[__($n['name'])] = $temp;
  48. }
  49. $groupdata = $result;
  50. }
  51. $this->view->assign('groupdata', $groupdata);
  52. $this->assignconfig("admin", ['id' => $this->auth->id]);
  53. }
  54. /**
  55. * 查看
  56. */
  57. public function index()
  58. {
  59. if ($this->request->isAjax()) {
  60. //如果发送的来源是Selectpage,则转发到Selectpage
  61. if ($this->request->request('keyField')) {
  62. return $this->selectpage();
  63. }
  64. $childrenGroupIds = $this->childrenGroupIds;
  65. $groupName = AuthGroup::where('id', 'in', $childrenGroupIds)
  66. ->column('id,name');
  67. $authGroupList = AuthGroupAccess::where('group_id', 'in', $childrenGroupIds)
  68. ->field('uid,group_id')
  69. ->select();
  70. $adminGroupName = [];
  71. foreach ($authGroupList as $k => $v) {
  72. if (isset($groupName[$v['group_id']])) {
  73. $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
  74. }
  75. }
  76. $groups = $this->auth->getGroups();
  77. foreach ($groups as $m => $n) {
  78. $adminGroupName[$this->auth->id][$n['id']] = $n['name'];
  79. }
  80. $map=[];
  81. if($this->auth->id>1){
  82. $map['pid']=$this->auth->id;
  83. }
  84. $map['islx']=1;
  85. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  86. $total = $this->model
  87. ->where($where)
  88. ->where($map)
  89. ->order($sort, $order)
  90. ->count();
  91. $list = $this->model
  92. ->where($where)
  93. ->where($map)
  94. ->field(['password', 'salt', 'token'], true)
  95. ->order($sort, $order)
  96. ->limit($offset, $limit)
  97. ->select();
  98. foreach ($list as $k => &$v) {
  99. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  100. $v['groups'] = implode(',', array_keys($groups));
  101. $v['groups_text'] = implode(',', array_values($groups));
  102. }
  103. unset($v);
  104. $result = array("total" => $total, "rows" => $list);
  105. return json($result);
  106. }
  107. return $this->view->fetch();
  108. }
  109. /**
  110. * 添加
  111. */
  112. public function add()
  113. {
  114. if ($this->request->isPost()) {
  115. $this->token();
  116. $params = $this->request->post("row/a");
  117. if ($params) {
  118. if (!Validate::is($params['password'], '\S{6,16}')) {
  119. $this->error(__("Please input correct password"));
  120. }
  121. $params['salt'] = Random::alnum();
  122. $params['password'] = md5(md5($params['password']) . $params['salt']);
  123. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  124. $result = $this->model->validate('Admin.add')->save($params);
  125. if ($result === false) {
  126. $this->error($this->model->getError());
  127. }
  128. Db::name('auth_group_access')->insertGetId(['uid' => $this->model->id, 'group_id' => 8]);
  129. $this->success();
  130. }
  131. $this->error();
  132. }
  133. $this->view->assign("id", $this->auth->id);
  134. return $this->view->fetch();
  135. }
  136. /**
  137. * 编辑
  138. */
  139. public function edit($ids = null)
  140. {
  141. $row = $this->model->get(['id' => $ids]);
  142. if (!$row) {
  143. $this->error(__('No Results were found'));
  144. }
  145. if ($this->request->isPost()) {
  146. $this->token();
  147. $params = $this->request->post("row/a");
  148. if ($params) {
  149. if ($params['password']) {
  150. if (!Validate::is($params['password'], '\S{6,16}')) {
  151. $this->error(__("Please input correct password"));
  152. }
  153. $params['salt'] = Random::alnum();
  154. $params['password'] = md5(md5($params['password']) . $params['salt']);
  155. } else {
  156. unset($params['password'], $params['salt']);
  157. }
  158. //这里需要针对username和email做唯一验证
  159. $adminValidate = \think\Loader::validate('Admin');
  160. $adminValidate->rule([
  161. 'username' => 'require|regex:\w{3,12}|unique:admin,username,' . $row->id,
  162. 'email' => 'require|email|unique:admin,email,' . $row->id,
  163. 'password' => 'regex:\S{32}',
  164. ]);
  165. $result = $row->validate('Admin.edit')->save($params);
  166. if ($result === false) {
  167. $this->error($row->getError());
  168. }
  169. $this->success();
  170. }
  171. $this->error();
  172. }
  173. $grouplist = $this->auth->getGroups($row['id']);
  174. $groupids = [];
  175. foreach ($grouplist as $k => $v) {
  176. $groupids[] = $v['id'];
  177. }
  178. $this->view->assign("id", $this->auth->id);
  179. $this->view->assign("row", $row);
  180. $this->view->assign("groupids", $groupids);
  181. return $this->view->fetch();
  182. }
  183. /**
  184. * 删除
  185. */
  186. public function del($ids = "")
  187. {
  188. if ($ids) {
  189. $ids = array_filter(explode(',', $ids));
  190. Db::name('admin')->where('id', 'in', $ids)->delete();
  191. Db::name('auth_group_access')->where('uid', 'in', $ids)->delete();
  192. $this->success();
  193. }
  194. $this->error(__('You have no permission'));
  195. }
  196. /**
  197. * 批量更新
  198. * @internal
  199. */
  200. public function multi($ids = "")
  201. {
  202. // 管理员禁止批量操作
  203. $this->error();
  204. }
  205. /**
  206. * 下拉搜索
  207. */
  208. public function selectpage()
  209. {
  210. $this->dataLimit = 'auth';
  211. $this->dataLimitField = 'id';
  212. return parent::selectpage();
  213. }
  214. }