Admin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. namespace app\company\controller\auth;
  3. use app\company\model\AuthGroup;
  4. use app\company\model\AuthGroupAccess;
  5. use app\common\controller\Apic;
  6. use fast\Random;
  7. use fast\Tree;
  8. use think\Db;
  9. use think\Validate;
  10. /**
  11. * 管理员管理
  12. *
  13. * @icon fa fa-users
  14. * @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
  15. */
  16. class Admin extends Apic
  17. {
  18. /**
  19. * @var \app\company\model\Admin
  20. */
  21. protected $model = null;
  22. protected $selectpageFields = 'id,username,nickname,avatar';
  23. protected $searchFields = 'id,username,nickname';
  24. protected $childrenGroupIds = [];
  25. protected $childrenAdminIds = [];
  26. public function _initialize()
  27. {
  28. parent::_initialize();
  29. $this->model = model('Admin');
  30. $this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isSuperAdmin());//下级管理员
  31. $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin());//下级角色组
  32. /*dump($this->childrenAdminIds);
  33. dump($this->childrenGroupIds);
  34. exit;*/
  35. $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
  36. Tree::instance()->init($groupList);
  37. $groupdata = [];
  38. if ($this->auth->isSuperAdmin()) {
  39. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  40. foreach ($result as $k => $v) {
  41. $groupdata[$v['id']] = $v['name'];
  42. }
  43. } else {
  44. $result = [];
  45. $groups = $this->auth->getGroups();
  46. foreach ($groups as $m => $n) {
  47. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
  48. $temp = [];
  49. foreach ($childlist as $k => $v) {
  50. $temp[$v['id']] = $v['name'];
  51. }
  52. $result[__($n['name'])] = $temp;
  53. }
  54. $groupdata = $result;
  55. }
  56. /*$this->view->assign('groupdata', $groupdata);
  57. $this->assignconfig("admin", ['id' => $this->auth->id]);*/
  58. }
  59. /**
  60. * 查看
  61. */
  62. public function index()
  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. $list = Db::name('pc_admin')
  81. ->where('company_id',$this->auth->company_id) //多此一举
  82. ->where('id', 'in', $this->childrenAdminIds)
  83. ->field(['password', 'salt', 'token'], true)
  84. ->order('id', 'asc')
  85. ->paginate();
  86. $list2 = $list->items();
  87. foreach ($list2 as $k => &$v) {
  88. $v['avatar'] = localpath_to_netpath($v['avatar']);
  89. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  90. $v['groups'] = implode(',', array_keys($groups));
  91. $v['groups_text'] = implode(',', array_values($groups));
  92. }
  93. unset($v);
  94. $result = array("total" => $list->total(), "list" => $list2);
  95. $this->success(1,$result);
  96. }
  97. /**
  98. * 添加
  99. */
  100. public function add()
  101. {
  102. $params = [
  103. 'username' => input('username',''),//手机号
  104. 'nickname' => input('nickname',''),//姓名
  105. 'password' => input('password',''),//密码
  106. 'gonghao' => input('gonghao',''), //工号
  107. 'avatar' => input('avatar',''), //头像
  108. ];
  109. $group_id = input('group_id',0);
  110. if(empty($group_id)){
  111. $this->error();
  112. }
  113. Db::startTrans();
  114. try {
  115. if (!Validate::is($params['password'], '\S{6,30}')) {
  116. exception(__("Please input correct password"));
  117. }
  118. $params['mobile'] = $params['username'];
  119. $params['company_id'] = $this->auth->company_id;
  120. $params['salt'] = Random::alnum();
  121. $params['password'] = $this->auth->getEncryptPassword($params['password'], $params['salt']);
  122. // $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  123. $result = $this->model->validate('Admin.add')->save($params);
  124. if ($result === false) {
  125. exception($this->model->getError());
  126. }
  127. $group = [$group_id];
  128. //过滤不允许的组别,避免越权
  129. $group = array_intersect($this->childrenGroupIds, $group);
  130. if (!$group) {
  131. exception(__('The parent group exceeds permission limit'));
  132. }
  133. $dataset = [];
  134. foreach ($group as $value) {
  135. $dataset[] = ['uid' => $this->model->id, 'group_id' => $value];
  136. }
  137. model('AuthGroupAccess')->saveAll($dataset);
  138. Db::commit();
  139. } catch (\Exception $e) {
  140. Db::rollback();
  141. $this->error($e->getMessage());
  142. }
  143. $this->success();
  144. }
  145. public function info(){
  146. $ids = input('id',0);
  147. $row = $this->model->get(['id' => $ids]);
  148. if (!$row) {
  149. $this->error(__('No Results were found'));
  150. }
  151. if (!in_array($row->id, $this->childrenAdminIds)) {
  152. $this->error(__('You have no permission'));
  153. }
  154. $grouplist = $this->auth->getGroups($row['id']);
  155. $groupids = [];
  156. foreach ($grouplist as $k => $v) {
  157. $groupids = $v['id'];
  158. }
  159. $row['groupids'] = $groupids;
  160. $row['avatar'] = localpath_to_netpath($row['avatar']);
  161. unset($row['password']);
  162. unset($row['salt']);
  163. $this->success(1,$row);
  164. }
  165. /**
  166. * 编辑
  167. */
  168. public function edit()
  169. {
  170. $ids = input('id',0);
  171. $row = $this->model->get(['id' => $ids]);
  172. if (!$row) {
  173. $this->error(__('No Results were found'));
  174. }
  175. if (!in_array($row->id, $this->childrenAdminIds)) {
  176. $this->error(__('You have no permission'));
  177. }
  178. $params = [
  179. 'username' => input('username',''),//手机号
  180. 'nickname' => input('nickname',''),//姓名
  181. 'password' => input('password',''),//密码
  182. 'gonghao' => input('gonghao',''), //工号
  183. 'avatar' => input('avatar',''), //头像
  184. ];
  185. $group_id = input('group_id',0);
  186. if(empty($group_id)){
  187. $this->error();
  188. }
  189. Db::startTrans();
  190. try {
  191. if ($params['password']) {
  192. if (!Validate::is($params['password'], '\S{6,30}')) {
  193. exception(__("Please input correct password"));
  194. }
  195. $params['salt'] = Random::alnum();
  196. $params['password'] = $this->auth->getEncryptPassword($params['password'], $params['salt']);
  197. } else {
  198. unset($params['password'], $params['salt']);
  199. }
  200. $params['mobile'] = $params['username'];
  201. //这里需要针对username和email做唯一验证
  202. $adminValidate = \think\Loader::validate('Admin');
  203. $adminValidate->rule([
  204. 'mobile' => 'require|regex:1\d{10}|unique:PcAdmin,mobile,' . $row->id,
  205. 'username' => 'require|regex:1\d{10}|unique:PcAdmin,username,' . $row->id,
  206. 'password' => 'regex:\S{32}',
  207. ]);
  208. $result = $row->validate('Admin.edit')->save($params);
  209. if ($result === false) {
  210. exception($row->getError());
  211. }
  212. // 先移除所有权限
  213. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  214. $group = [$group_id];
  215. // 过滤不允许的组别,避免越权
  216. $group = array_intersect($this->childrenGroupIds, $group);
  217. if (!$group) {
  218. exception(__('The parent group exceeds permission limit'));
  219. }
  220. $dataset = [];
  221. foreach ($group as $value) {
  222. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  223. }
  224. model('AuthGroupAccess')->saveAll($dataset);
  225. Db::commit();
  226. } catch (\Exception $e) {
  227. Db::rollback();
  228. $this->error($e->getMessage());
  229. }
  230. $this->success();
  231. }
  232. /**
  233. * 删除
  234. */
  235. public function del()
  236. {
  237. $ids = input('ids','');
  238. if ($ids) {
  239. $ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $ids)));
  240. // 避免越权删除管理员
  241. $childrenGroupIds = $this->childrenGroupIds;
  242. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
  243. $query->name('pc_auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  244. })->select();
  245. if ($adminList) {
  246. $deleteIds = [];
  247. foreach ($adminList as $k => $v) {
  248. $deleteIds[] = $v->id;
  249. }
  250. $deleteIds = array_values(array_diff($deleteIds, [$this->auth->id]));
  251. if ($deleteIds) {
  252. Db::startTrans();
  253. try {
  254. $this->model->destroy($deleteIds);
  255. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  256. Db::commit();
  257. } catch (\Exception $e) {
  258. Db::rollback();
  259. $this->error($e->getMessage());
  260. }
  261. $this->success();
  262. }
  263. $this->error(__('No rows were deleted'));
  264. }
  265. }
  266. $this->error(__('You have no permission'));
  267. }
  268. /**
  269. * 设置客服
  270. */
  271. public function set_kefu(){
  272. $id = input('id',0);
  273. Db::name('pc_admin')->where('company_id',$this->auth->company_id)->update(['is_kefu'=>0]);
  274. Db::name('pc_admin')->where('id',$id)->update(['is_kefu'=>1]);
  275. $this->success();
  276. }
  277. /**
  278. * 下拉搜索
  279. */
  280. public function selectpage()
  281. {
  282. $this->dataLimit = 'auth';
  283. $this->dataLimitField = 'id';
  284. return parent::selectpage();
  285. }
  286. }