Admin.php 11 KB

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