Daren.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 Daren 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. //var_dump($groups);
  81. $map=[];
  82. if($this->auth->id>1){
  83. $map['pid']=$this->auth->id;
  84. }
  85. $map['islx']=2;
  86. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  87. $total = $this->model
  88. ->where($where)
  89. ->where($map)
  90. ->order($sort, $order)
  91. ->count();
  92. $list = $this->model
  93. ->where($where)
  94. ->where($map)
  95. ->field(['password', 'salt', 'token'], true)
  96. ->order($sort, $order)
  97. ->limit($offset, $limit)
  98. ->select();
  99. foreach ($list as $k => &$v) {
  100. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  101. $v['groups'] = implode(',', array_keys($groups));
  102. $v['groups_text'] = implode(',', array_values($groups));
  103. }
  104. unset($v);
  105. $result = array("total" => $total, "rows" => $list);
  106. return json($result);
  107. }
  108. return $this->view->fetch();
  109. }
  110. /**
  111. * 添加
  112. */
  113. public function add()
  114. {
  115. if ($this->request->isPost()) {
  116. $this->token();
  117. $params = $this->request->post("row/a");
  118. if ($params) {
  119. if (!Validate::is($params['password'], '\S{6,16}')) {
  120. $this->error(__("Please input correct password"));
  121. }
  122. $params['salt'] = Random::alnum();
  123. $params['password'] = md5(md5($params['password']) . $params['salt']);
  124. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  125. $result = $this->model->validate('Admin.add')->save($params);
  126. if ($result === false) {
  127. $this->error($this->model->getError());
  128. }
  129. Db::name('auth_group_access')->insertGetId(['uid' => $this->model->id, 'group_id' => 9]);
  130. $this->success();
  131. }
  132. $this->error();
  133. }
  134. $this->view->assign("id", $this->auth->id);
  135. return $this->view->fetch();
  136. }
  137. /**
  138. * 编辑
  139. */
  140. public function edit($ids = null)
  141. {
  142. $row = $this->model->get(['id' => $ids]);
  143. if (!$row) {
  144. $this->error(__('No Results were found'));
  145. }
  146. if ($this->request->isPost()) {
  147. $this->token();
  148. $params = $this->request->post("row/a");
  149. if ($params) {
  150. if ($params['password']) {
  151. if (!Validate::is($params['password'], '\S{6,16}')) {
  152. $this->error(__("Please input correct password"));
  153. }
  154. $params['salt'] = Random::alnum();
  155. $params['password'] = md5(md5($params['password']) . $params['salt']);
  156. } else {
  157. unset($params['password'], $params['salt']);
  158. }
  159. //这里需要针对username和email做唯一验证
  160. $adminValidate = \think\Loader::validate('Admin');
  161. $adminValidate->rule([
  162. 'username' => 'require|regex:\w{3,12}|unique:admin,username,' . $row->id,
  163. 'email' => 'require|email|unique:admin,email,' . $row->id,
  164. 'password' => 'regex:\S{32}',
  165. ]);
  166. $result = $row->validate('Admin.edit')->save($params);
  167. if ($result === false) {
  168. $this->error($row->getError());
  169. }
  170. $this->success();
  171. }
  172. $this->error();
  173. }
  174. $grouplist = $this->auth->getGroups($row['id']);
  175. $groupids = [];
  176. foreach ($grouplist as $k => $v) {
  177. $groupids[] = $v['id'];
  178. }
  179. $this->view->assign("id", $this->auth->id);
  180. $this->view->assign("row", $row);
  181. $this->view->assign("groupids", $groupids);
  182. return $this->view->fetch();
  183. }
  184. /**
  185. * 删除
  186. */
  187. public function del($ids = "")
  188. {
  189. if ($ids) {
  190. $ids = array_filter(explode(',', $ids));
  191. Db::name('admin')->where('id', 'in', $ids)->delete();
  192. Db::name('auth_group_access')->where('uid', 'in', $ids)->delete();
  193. $this->success();
  194. }
  195. $this->error(__('You have no permission'));
  196. }
  197. /**
  198. * 批量更新
  199. * @internal
  200. */
  201. public function multi($ids = "")
  202. {
  203. // 管理员禁止批量操作
  204. $this->error();
  205. }
  206. /**
  207. * 下拉搜索
  208. */
  209. public function selectpage()
  210. {
  211. $this->dataLimit = 'auth';
  212. $this->dataLimitField = 'id';
  213. return parent::selectpage();
  214. }
  215. }