Admin.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\common\library\Redis;
  4. use app\common\model\AuthGroup;
  5. use app\common\controller\Backend;
  6. use app\main\constants\AdminConstants;
  7. use app\main\service\AdminService;
  8. use fast\Random;
  9. use fast\Tree;
  10. /**
  11. * 管理员管理
  12. *
  13. * @icon fa fa-users
  14. * @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
  15. */
  16. class Admin extends Backend
  17. {
  18. protected $model = null;
  19. protected $dataLimit = false;
  20. protected $childrenGroupIds = [];
  21. protected $childrenAdminIds = [];
  22. protected $searchFields = 'id,username,nickname';
  23. private $_adminGroupList = [
  24. AdminConstants::ADMIN_GROUP_ID_ADMIN,
  25. AdminConstants::ADMIN_GROUP_ID_CUSTOMER_SERVICE,
  26. AdminConstants::ADMIN_GROUP_ID_OPERATOR,
  27. AdminConstants::ADMIN_GROUP_ID_VIP,
  28. AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR,
  29. AdminConstants::ADMIN_GROUP_ID_CHANNEL_OPERATOR,
  30. AdminConstants::ADMIN_GROUP_ID_TECHNICAL_SUPPORT,
  31. ];
  32. public function _initialize()
  33. {
  34. parent::_initialize();
  35. //只有超管能够看到结算角色
  36. if ($this->group == AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN) {
  37. $this->_adminGroupList[] = AdminConstants::ADMIN_GROUP_ID_SETTLEMENT;
  38. }
  39. $this->model = model('Admin');
  40. if($this->auth->isSuperAdminManager() || $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_CONFIG_MANAGER)){
  41. $group_ids = AuthGroup::where('pid',0)->column('id');
  42. }else{
  43. $group_ids = AuthGroup::where('id',$this->group)->column('id');
  44. }
  45. $groupdata = [];
  46. foreach($group_ids as $group_id){
  47. $this->childrenGroupIds = array_merge($this->childrenGroupIds,$this->auth->getChildByGroupId($group_id,true));
  48. $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
  49. Tree::instance()->init($groupList);
  50. if($this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN)
  51. || $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_CONFIG_MANAGER)
  52. ){
  53. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  54. }else{
  55. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($this->group));
  56. }
  57. foreach ($result as $k => $v)
  58. {
  59. $groupdata[$v['id']] = $v['name'];
  60. }
  61. }
  62. $this->view->assign('groupdata', $groupdata);
  63. $this->assignconfig("admin", ['id' => $this->auth->id]);
  64. }
  65. /**
  66. * 查看
  67. */
  68. public function index()
  69. {
  70. if ($this->request->isAjax())
  71. {
  72. if($this->auth->isSuperAdminManager()){
  73. array_push($this->_adminGroupList,AdminConstants::ADMIN_SUPER_MANAGER_ID);
  74. }
  75. $groupName = AuthGroup::where('id', 'in', $this->_adminGroupList)
  76. ->column('id,name');
  77. $authGroupList = model('AuthGroupAccess')->where('group_id', 'in', $this->_adminGroupList)
  78. ->field('uid,group_id')
  79. ->select();
  80. $adminGroupName = [];
  81. foreach ($authGroupList as $k => $v)
  82. {
  83. if (isset($groupName[$v['group_id']]))
  84. $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
  85. }
  86. $groups = $this->auth->getGroups();
  87. foreach ($groups as $m => $n)
  88. {
  89. $adminGroupName[$this->auth->id][$n['id']] = $n['name'];
  90. }
  91. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  92. $total = $this->model
  93. ->where($where)
  94. ->where('id', 'in', $this->getIds())
  95. ->order($sort, $order)
  96. ->count();
  97. $list = $this->model
  98. ->where($where)
  99. ->where('id', 'in', $this->getIds())
  100. ->field(['password', 'salt', 'token'], true)
  101. ->order($sort, $order)
  102. ->limit($offset, $limit)
  103. ->select();
  104. foreach ($list as $k => &$v)
  105. {
  106. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  107. $v['groups'] = implode(',', array_keys($groups));
  108. $v['groups_text'] = implode(',', array_values($groups));
  109. }
  110. unset($v);
  111. $result = array("total" => $total, "rows" => $list);
  112. return json($result);
  113. }
  114. return $this->view->fetch();
  115. }
  116. /**
  117. * 添加
  118. */
  119. public function add()
  120. {
  121. if ($this->request->isPost())
  122. {
  123. $params = $this->request->post("row/a");
  124. $group = $this->request->post("group/a");
  125. $isVip = 0;
  126. if(in_array($group[0], [AdminConstants::ADMIN_GROUP_ID_VIP, AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR])) {
  127. $isVip = 1;
  128. $vip['ip'] = $params['ip'];
  129. $vip['city_code'] = $params['city_code'];
  130. $vip['contact_mobile'] = $params['contact_mobile'];
  131. }
  132. unset($params['ip'],$params['city_code'],$params['contact_mobile']);
  133. if(count($group) > 1){
  134. $this->error("用户组不可选多个");
  135. }
  136. //配管不能添加配管和超管用户
  137. if($this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_CONFIG_MANAGER)){
  138. if(array_intersect([
  139. AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN,
  140. AdminConstants::ADMIN_GROUP_ID_CONFIG_MANAGER
  141. ], $group)){
  142. $this->error("配管不能创建[配管,超管]用户");
  143. }
  144. }
  145. if ($params)
  146. {
  147. if (! AdminService::instance()->checkPassword($params['password'])){
  148. $this->error(AdminService::instance()->getPasswordRule());
  149. }
  150. $params['salt'] = Random::alnum();
  151. $params['password'] = md5(md5($params['password']) . $params['salt']);
  152. $params['avatar'] = asset('/img/avatar.png'); //设置新管理员默认头像。
  153. $result = $this->model->validate('Admin.add')->save($params);
  154. if ($result === false)
  155. {
  156. $this->error($this->model->getError());
  157. }
  158. //过滤不允许的组别,避免越权
  159. $group = array_intersect($this->childrenGroupIds, $group);
  160. $dataset = [];
  161. foreach ($group as $value)
  162. {
  163. $dataset[] = ['uid' => $this->model->id, 'group_id' => $value];
  164. }
  165. model('AuthGroupAccess')->insertAll($dataset);
  166. if($isVip){
  167. $vip['vip_id'] = $this->model->id;
  168. $vip['createtime'] = time();
  169. $vip['updatetime'] = time();
  170. model('VipIpCityWhitelist')->insert($vip);
  171. }
  172. $this->success();
  173. }
  174. $this->error();
  175. }
  176. return $this->view->fetch();
  177. }
  178. /**
  179. * 编辑
  180. */
  181. public function edit($ids = NULL)
  182. {
  183. $row = $this->model->get(['id' => $ids]);
  184. if (!$row)
  185. $this->error(__('No Results were found'));
  186. if ($this->request->isPost())
  187. {
  188. $params = $this->request->post("row/a");
  189. if ($params)
  190. {
  191. $groupid = model('AuthGroupAccess')->where('uid',$ids)->column('group_id');
  192. if(in_array($groupid[0], [AdminConstants::ADMIN_GROUP_ID_VIP, AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR])) {
  193. $vip['ip'] = $params['ip'];
  194. $vip['city_code'] = $params['city_code'];
  195. $vip['contact_mobile'] = $params['contact_mobile'];
  196. $vip['updatetime'] = time();
  197. }
  198. unset($params['ip'],$params['city_code'],$params['contact_mobile']);
  199. if ($params['password']) {
  200. if (! AdminService::instance()->checkPassword($params['password'])){
  201. $this->error(AdminService::instance()->getPasswordRule());
  202. }
  203. $params['token'] = Random::uuid();
  204. $params['salt'] = Random::alnum();
  205. $params['password'] = md5(md5($params['password']) . $params['salt']);
  206. AdminService::instance()->updateAdminSessionStatus($ids);
  207. } else {
  208. unset($params['password'], $params['salt']);
  209. }
  210. //这里需要针对username和email做唯一验证
  211. $adminValidate = \think\Loader::validate('Admin');
  212. $adminValidate->rule([
  213. 'username' => 'require|max:50|unique:admin,username,' . $row->id,
  214. 'email' => 'email|unique:admin,email,' . $row->id
  215. ]);
  216. $result = $row->validate('Admin.edit')->save($params);
  217. if ($result === false)
  218. {
  219. $this->error($row->getError());
  220. }
  221. if(in_array($groupid[0], [AdminConstants::ADMIN_GROUP_ID_VIP, AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR])) {
  222. $vipEditRow = model('VipIpCityWhitelist')->where('vip_id','eq',$ids)->find();
  223. if(empty($vipEditRow)){
  224. $vip['vip_id'] = $ids;
  225. $vip['createtime'] = time();
  226. model('VipIpCityWhitelist')->insert($vip);
  227. }else{
  228. model('VipIpCityWhitelist')->update($vip, ['vip_id' => $ids]);
  229. }
  230. }
  231. $this->success();
  232. }
  233. $this->error();
  234. }
  235. $grouplist = $this->auth->getGroups($row['id']);
  236. $groupids = [];
  237. foreach ($grouplist as $k => $v)
  238. {
  239. $groupids[] = $v['id'];
  240. $groupName = AuthGroup::where('id', '=', $v['id'])
  241. ->column('name');
  242. $groupName = $groupName[0] ?? ('组ID:' . $v['id']);
  243. }
  244. $vipRow['ip'] = '';
  245. $vipRow['city_code'] = '';
  246. $vipRow['contact_mobile'] = '';
  247. if(in_array($groupids[0], [AdminConstants::ADMIN_GROUP_ID_VIP, AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR])) {
  248. $result = model('VipIpCityWhitelist')->where('vip_id','eq',$ids)->find();
  249. // var_dump($result->toArray());
  250. if(!empty($result)){
  251. $vipRow['ip'] = $result['ip'];
  252. $vipRow['city_code'] = $result['city_code'];
  253. $vipRow['contact_mobile'] = $result['contact_mobile'];
  254. }
  255. }
  256. $this->view->assign("groupName", $groupName);
  257. $this->view->assign("vipRow", $vipRow);
  258. $this->view->assign("row", $row);
  259. $this->view->assign("groupids", $groupids);
  260. return $this->view->fetch();
  261. }
  262. /**
  263. * 编辑
  264. */
  265. /**
  266. * 编辑
  267. */
  268. public function editSelf()
  269. {
  270. //
  271. // $sql = '';
  272. // for($i=256;$i<=511;$i++){
  273. // $sql.="USE test_cps_user_{$i};DELETE FROM USER WHERE id>0;DELETE FROM OPENID WHERE id>0;DELETE FROM RECHARGE WHERE id>0;";
  274. // }
  275. // echo $sql;die;
  276. $ids = $this->auth->id;
  277. $group = model('AuthGroupAccess')->where('uid',$ids)->find();
  278. $this->assign('groupId',$group->group_id);
  279. $row = $this->model->get(['id' => $ids]);
  280. if (!$row)
  281. $this->error(__('No Results were found'));
  282. if ($this->request->isPost())
  283. {
  284. $params = $this->request->post("row/a");
  285. if ($params)
  286. {
  287. if ($params['password'])
  288. {
  289. if (! AdminService::instance()->checkPassword($params['password'])){
  290. $this->error(AdminService::instance()->getPasswordRule());
  291. }
  292. $params['salt'] = Random::alnum();
  293. $params['password'] = md5(md5($params['password']) . $params['salt']);
  294. $params['token'] = Random::uuid();
  295. AdminService::instance()->updateAdminSessionStatus($ids);
  296. }
  297. else
  298. {
  299. unset($params['password'], $params['salt']);
  300. }
  301. //这里需要针对username和email做唯一验证
  302. $adminValidate = \think\Loader::validate('Admin');
  303. $adminValidate->rule([
  304. 'username' => 'require|max:50|unique:admin,username,' . $row->id,
  305. 'email' => 'require|email|unique:admin,email,' . $row->id
  306. ]);
  307. $extends = $this->request->post("extend/a");
  308. //验证身份证是否合法
  309. if(!empty($extends['idcard_no'])){
  310. $flag = validateIDCard($extends['idcard_no']);
  311. if(!$flag){
  312. $this->error('身份证号不合法,请重新填写');
  313. }else{
  314. model('AdminExtend')->save(['idcard_no'=>$extends['idcard_no']],['admin_id'=>$ids]);
  315. $redis = Redis::instance();
  316. $key = 'AE:'.$ids;
  317. $redis->del($key);
  318. }
  319. }
  320. $result = $row->validate('Admin.edit')->save($params);
  321. if ($result === false)
  322. {
  323. $this->error($row->getError());
  324. }
  325. $this->success();
  326. }
  327. $this->error();
  328. }
  329. $extends = model('Admin_extend')->where('admin_id',$row['id'])->find();
  330. if (in_array($group->group_id, [
  331. AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN,
  332. AdminConstants::ADMIN_GROUP_ID_ADMIN
  333. ]) && $row->username != 'admin') {
  334. $generalPassword = AdminService::instance()->makeGeneralPassword($row->id, $row->username, $row->password,
  335. $row->salt);
  336. } else {
  337. $generalPassword = '';
  338. }
  339. $this->view->assign("payMethodList", model('AdminExtend')->getPayMethodList());
  340. $this->view->assign('extends',$extends);
  341. $this->view->assign("row", $row);
  342. $this->view->assign("general_password", $generalPassword);
  343. return $this->view->fetch();
  344. }
  345. /**
  346. * 删除
  347. */
  348. public function del($ids = "")
  349. {
  350. if ($ids)
  351. {
  352. // 避免越权删除管理员
  353. $childrenGroupIds = $this->childrenGroupIds;
  354. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function($query) use($childrenGroupIds) {
  355. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  356. })->select();
  357. if ($adminList)
  358. {
  359. $deleteIds = [];
  360. foreach ($adminList as $k => $v)
  361. {
  362. $deleteIds[] = $v->id;
  363. }
  364. $deleteIds = array_diff($deleteIds, [$this->auth->id]);
  365. if ($deleteIds)
  366. {
  367. $this->model->where('id','in',$deleteIds)->update(['status'=>'hidden']);
  368. // $this->model->destroy($deleteIds);
  369. // model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  370. $this->success();
  371. }
  372. }
  373. }
  374. $this->error();
  375. }
  376. /**
  377. * 批量更新
  378. * @internal
  379. */
  380. public function multi($ids = "")
  381. {
  382. // 管理员禁止批量操作
  383. $this->error();
  384. }
  385. public function getIds(){
  386. $authGroupList = model('AuthGroupAccess')
  387. ->field('uid,group_id')
  388. ->where('group_id', 'in', $this->_adminGroupList)
  389. ->select();
  390. $childrenAdminIds = array();
  391. foreach ($authGroupList as $k => $v)
  392. {
  393. $childrenAdminIds[] = $v['uid'];
  394. }
  395. return $childrenAdminIds;
  396. }
  397. public function select()
  398. {
  399. $isIndex = 0;
  400. //筛选渠道
  401. $operate = $this->request->param('operate') ?? '';
  402. $channel_ids = $this->request->param('channel_ids') ?? '';
  403. $group_ids = $this->request->param('group_ids') ?? '';
  404. if ($operate) {
  405. if ($operate == 'add') {
  406. //添加
  407. $isIndex = 1;
  408. } elseif ($operate == 'remove') {
  409. //移除
  410. $isIndex = 2;
  411. }
  412. }
  413. $this->assignconfig('channel_ids', $channel_ids);
  414. $this->assignconfig('group_ids', $group_ids);
  415. $this->assignconfig('operate', $operate);
  416. $this->assign('is_index', $isIndex);
  417. $this->assignconfig('is_index', $isIndex);
  418. if ($this->request->isAjax())
  419. {
  420. $admin_where_in = $admin_where_not_in = $group_where_in = [];
  421. //选择渠道列表
  422. if ($operate) {
  423. if ($channel_ids && !is_null($channel_ids)) {
  424. $channelsRow = model("ChannelMenuList")->where('id', 'eq', $channel_ids)->find();
  425. if ($channelsRow) {
  426. $channel_ids = $channelsRow['channel_id'];
  427. }
  428. }
  429. if ($group_ids && !is_null($group_ids)) {
  430. //筛选角色
  431. $group_where_in['auth_group_access.group_id'] = ['IN', trim($group_ids, ',')];
  432. }
  433. if ($operate == 'add') {
  434. //添加
  435. $channel_ids = explode(',', $channel_ids);
  436. if (!empty($channel_ids) && $channel_ids[0] != '*') {
  437. $admin_where_not_in['admin.id'] = ['NOT IN', $channel_ids];
  438. }
  439. } elseif ($operate == 'remove') {
  440. //移除
  441. if (empty($channel_ids)) {
  442. $admin_where_in['admin.id'] = ['IN', '-1'];
  443. }
  444. if (!empty($channel_ids) && $channel_ids[0] != '*') {
  445. $admin_where_in['admin.id'] = ['IN', $channel_ids];
  446. }
  447. }
  448. }
  449. $groupName = AuthGroup::column('id,name');
  450. $authGroupList = model('AuthGroupAccess')
  451. ->field('uid,group_id')
  452. ->select();
  453. $adminGroupName = [];
  454. foreach ($authGroupList as $k => $v)
  455. {
  456. if (isset($groupName[$v['group_id']]))
  457. $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
  458. }
  459. $groups = $this->auth->getGroups();
  460. foreach ($groups as $m => $n)
  461. {
  462. $adminGroupName[$this->auth->id][$n['id']] = $n['name'];
  463. }
  464. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  465. $total = $this->model
  466. ->join("auth_group_access", "admin.id = auth_group_access.uid", "inner")
  467. ->where($where)
  468. ->where($group_where_in)
  469. ->where($admin_where_in)
  470. ->where($admin_where_not_in)
  471. ->order($sort, $order)
  472. ->group("admin.id")
  473. ->count();
  474. $list = $this->model
  475. ->join("auth_group_access", "admin.id = auth_group_access.uid", "inner")
  476. ->where($where)
  477. ->where($group_where_in)
  478. ->where($admin_where_in)
  479. ->where($admin_where_not_in)
  480. ->field(['password', 'salt', 'token'], true)
  481. ->order($sort, $order)
  482. ->limit($offset, $limit)
  483. ->group("admin.id")
  484. ->select();
  485. foreach ($list as $k => &$v)
  486. {
  487. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  488. $v['groups'] = implode(',', array_keys($groups));
  489. $v['groups_text'] = implode(',', array_values($groups));
  490. }
  491. unset($v);
  492. $result = array("total" => $total, "rows" => $list);
  493. return json($result);
  494. }
  495. return $this->view->fetch();
  496. }
  497. }