Admin.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. <?php
  2. namespace app\admin\controller\department;
  3. use app\admin\model\department\Department;
  4. use app\admin\model\department\Department as DepartmentModel;
  5. use app\common\controller\Backend;
  6. use \app\admin\model\department\Admin as DepartmentAdminModel;
  7. use \app\admin\model\department\AuthAdmin as AdminModel;
  8. use fast\Random;
  9. use fast\Tree;
  10. use think\Db;
  11. use think\Model;
  12. use think\Validate;
  13. /**
  14. * 部门成员
  15. */
  16. class Admin extends Backend
  17. {
  18. protected $tree = null;
  19. /**
  20. * 部门人员对象
  21. * @var null
  22. */
  23. protected $dadminModel = null;
  24. protected $childrenGroupIds = [];//权限组
  25. /**
  26. * 部门所有
  27. * @var array
  28. */
  29. protected $allDepartment = [];//
  30. protected $noNeedRight=['selectpage','getDepartmentIds','departadmintotal'];
  31. public function _initialize()
  32. {
  33. parent::_initialize();
  34. $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin());
  35. $this->dadminModel = new DepartmentAdminModel;
  36. $departmentList = [];
  37. // $this->allDepartment = \app\admin\model\department\Admin::getAllDepartmentsArray($this->auth->id,($this->auth->isSuperAdmin()||$this->auth->data_scope)?true:false);
  38. $this->allDepartment = \app\admin\model\department\Admin::getAllDepartmentsArray($this->auth->id,true);
  39. $this->allDepartment=collection($this->allDepartment)->toArray();
  40. $departmentdata = [];
  41. foreach ($this->allDepartment as $k => $v) {
  42. $state = ['opened' => true];
  43. $departmentList[] = [
  44. 'id' => $v['id'],
  45. 'parent' => $v['parent_id'] ? $v['parent_id'] : '#',
  46. 'text' =>preg_replace('/&nbsp;|\s*|│|└|├\s*/','',__($v['name'])),//str_replace("&nbsp;",'', __($v['name'])) ,
  47. 'state' => $state
  48. ];
  49. }
  50. $tree = Tree::instance()->init($this->allDepartment, 'parent_id');
  51. $departmentOptions = $tree->getTree(0, "<option model='@model_id' value=@id @selected @disabled>@spacer@name</option>");
  52. $this->view->assign('departmentOptions', $departmentOptions);
  53. $this->assignconfig('departmentList', $departmentList);
  54. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  55. foreach ($result as $k => $v) {
  56. $departmentdata[$v['id']] = $v['name'];
  57. }
  58. $this->view->assign('departmentdata', $departmentdata);
  59. //兼容旧版本没有手机号
  60. $database=config('database');
  61. $exits_mobile=\think\Db::query("SELECT 1 FROM information_schema.COLUMNS WHERE table_name='{$database['prefix']}admin' AND COLUMN_NAME='mobile' limit 1");
  62. $this->view->assign('exits_mobile', $exits_mobile?1:0);
  63. $this->assignconfig('exits_mobile', $exits_mobile?1:0);
  64. }
  65. /**
  66. * 成员列表
  67. */
  68. public function index()
  69. {
  70. //设置过滤方法
  71. $this->request->filter(['strip_tags']);
  72. $department_id = $this->request->request("department_id");
  73. if ($this->request->isAjax()) {
  74. $this->model = new \app\admin\model\department\AuthAdmin();
  75. $filter = $this->request->get("filter", '');
  76. $filter = (array)json_decode($filter, true);
  77. $filter_w = [];
  78. $authGroupList = \app\admin\model\AuthGroupAccess::field('uid,group_id')
  79. ->where('group_id', 'in', 8)
  80. ->select();
  81. foreach ($authGroupList as $k => $v) {
  82. $childrenAdminIds[] = $v['uid'];
  83. }
  84. $filter_w['id']=['in',$childrenAdminIds];
  85. if (isset($filter['department_id'])) {
  86. $department_id = $filter['department_id'];
  87. unset($filter['department_id']);
  88. $this->request->get(['filter' => json_encode($filter)]);
  89. }
  90. if ($department_id) {
  91. if (!in_array($department_id,array_column($this->allDepartment,'id'))){
  92. $this->error("您所选的部门没有权限");
  93. }
  94. $admin_ids = $this->dadminModel->where('department_id', 'in', $department_id)->column('admin_id');
  95. $filter_w['id'] = ['in', $admin_ids];
  96. }
  97. // else if ($this->auth->data_scope!=1&&!$this->auth->isSuperAdmin()){
  98. // $this->childrenAdminIds = \app\admin\model\department\Admin::getChildrenAdminIds($this->auth->id, true);
  99. // $filter_w['admin_id']=['in',$this->childrenAdminIds];
  100. // }
  101. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  102. $total = $this->model
  103. ->where($where)
  104. ->where($filter_w)
  105. ->order($sort, $order)->fetchSql(false)
  106. ->count();
  107. $list = $this->model
  108. ->where($where)
  109. ->with(['dadmin.department'])
  110. ->with(['groups.getGroup'])
  111. ->where($filter_w)
  112. ->order($sort, $order)
  113. ->limit($offset, $limit)->fetchSql(false)
  114. ->select();
  115. $result = array("total" => $total, "rows" => $list);
  116. return json($result);
  117. }
  118. return $this->view->fetch();
  119. }
  120. /**
  121. * 添加
  122. * @return string|\think\response\Json
  123. * @throws \think\Exception
  124. * @throws \think\db\exception\DataNotFoundException
  125. * @throws \think\db\exception\ModelNotFoundException
  126. * @throws \think\exception\DbException
  127. */
  128. public function add()
  129. {
  130. $groupdata = DepartmentModel::getGroupdata(
  131. $this->childrenGroupIds,
  132. $this->auth->isSuperAdmin() ? null : $this->auth->getGroups()
  133. );
  134. if ($this->request->isPost()) {
  135. $adminModel = new AdminModel();
  136. $departmentModel = new DepartmentModel();
  137. $params = $this->request->post("row/a");
  138. if ($params) {
  139. Db::startTrans();
  140. try {
  141. $department_id = $this->request->post("department_id/a");
  142. $is_principal = $this->request->post("is_principal",'0','intval');
  143. //获取部门信息
  144. if (!$department_id) {
  145. exception(__("组织不能为空"));
  146. }
  147. $d_list = $departmentModel->where('id', 'in', $department_id)->select();
  148. if (!$d_list) {
  149. exception(__("组织不能为空"));
  150. }
  151. foreach ($d_list as $d_row){
  152. if ($d_row['level']!=5){
  153. exception(__("学员请选择到区队"));
  154. }
  155. }
  156. if (!Validate::is($params['password'], '\S{6,16}')) {
  157. exception(__("Please input correct password"));
  158. }
  159. $params['depart_id'] = $department_id[0]??0;
  160. $params['salt'] = Random::alnum();
  161. $params['password'] = md5(md5($params['password']) . $params['salt']);
  162. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  163. $result = $adminModel->validate('Admin.add')->save($params);
  164. if ($result === false) {
  165. exception($adminModel->getError());
  166. }
  167. if (!$this->auth->isSuperAdmin()&&isset($params['data_scope'])){
  168. unset($params['data_scope']);//超级管理人才能赋予全部数据权限
  169. }
  170. $admin_id = $adminModel->id;
  171. $dadmin = array();
  172. //添加部门信息
  173. foreach ($d_list as $d_row) {
  174. $dadmin[] = ['is_principal'=>$is_principal==1?1:0,'department_id' => $d_row->id, 'organise_id' => $d_row->organise_id ? $d_row->organise_id : $d_row->id, 'admin_id' => $admin_id];
  175. }
  176. $this->dadminModel->saveAll($dadmin);
  177. $group = $this->request->post("group/a");
  178. //过滤不允许的组别,避免越权
  179. $group = [8];//array_intersect($this->childrenGroupIds, $group);
  180. //添加权限默认
  181. if (!$group) {
  182. exception(__('The parent group exceeds permission limit'));
  183. }
  184. $dataset = [];
  185. foreach ($group as $value) {
  186. $dataset[] = ['uid' => $admin_id, 'group_id' => $value];
  187. }
  188. model('AuthGroupAccess')->saveAll($dataset);
  189. Db::commit();
  190. } catch (\Exception $e) {
  191. Db::rollback();
  192. $this->error($e->getMessage());
  193. }
  194. $this->success();
  195. }
  196. $this->error(__('Parameter %s can not be empty', ''));
  197. }
  198. $this->view->assign('groupdata', $groupdata);
  199. return $this->view->fetch();
  200. }
  201. /**
  202. * 修改
  203. * @param null $ids
  204. * @return string|\think\response\Json
  205. * @throws \think\Exception
  206. * @throws \think\db\exception\DataNotFoundException
  207. * @throws \think\db\exception\ModelNotFoundException
  208. * @throws \think\exception\DbException
  209. */
  210. public function edit($ids = null)
  211. {
  212. $this->model = new AdminModel();
  213. $departmentModel = new DepartmentModel();
  214. $row = $this->model->get($ids);
  215. if (!$row) {
  216. $this->error(__('No Results were found'));
  217. }
  218. // if (!\app\admin\model\department\Admin::checkDataAuth($this->auth,$row,'id')){
  219. // $this->error(__('You have no permission'));
  220. // }
  221. if ($this->request->isPost()) {
  222. //判断是不是超级管理员编辑
  223. // if (!$this->auth->isSuperAdmin() && DepartmentAdminModel::isSuperAdmin($row->id)) {
  224. // $this->error("您无权操作超级管理员");
  225. // }
  226. $params = $this->request->post("row/a");
  227. if ($params) {
  228. Db::startTrans();
  229. try {
  230. $department_id = $this->request->post("department_id/a");
  231. //获取部门信息
  232. if (!$department_id) {
  233. exception(__("Department can't null"));
  234. }
  235. $d_list = $departmentModel->where('id', 'in', $department_id)->select();
  236. if (!$d_list) {
  237. exception(__("Department can't null"));
  238. }
  239. foreach ($d_list as $d_row){
  240. if ($d_row['level']!=5){
  241. exception(__("学员请选择到区队"));
  242. }
  243. }
  244. if ($params['password']) {
  245. if (!Validate::is($params['password'], '\S{6,16}')) {
  246. exception(__("Please input correct password"));
  247. }
  248. $params['salt'] = Random::alnum();
  249. $params['password'] = md5(md5($params['password']) . $params['salt']);
  250. } else {
  251. unset($params['password'], $params['salt']);
  252. }
  253. if (!$this->auth->isSuperAdmin()&&isset($params['data_scope'])){
  254. unset($params['data_scope']);//超级管理人才能赋予全部数据权限
  255. }
  256. $params['depart_id'] = $department_id[0]??0;
  257. //这里需要针对username和email做唯一验证
  258. $adminValidate = \think\Loader::validate('Admin');
  259. $adminValidate->rule([
  260. 'username' => 'require|regex:\w{3,20}|unique:admin,username,' . $row->id,
  261. // 'email' => 'require|email|unique:admin,email,' . $row->id,
  262. // 'mobile' => 'regex:1[3-9]\d{9}|unique:admin,mobile,' . $row->id,
  263. 'password' => 'regex:\S{32}',
  264. ]);
  265. $result = $row->validate('Admin.edit')->save($params);
  266. if ($result === false) {
  267. exception($row->getError());
  268. }
  269. $exist_departmentids = $this->dadminModel->where('admin_id', $row->id)->column('department_id');
  270. $dadmin = array();
  271. $deleteids = array_diff($exist_departmentids, $department_id);
  272. //添加部门信息
  273. foreach ($d_list as $d_row) {
  274. if (!in_array($d_row->id, $exist_departmentids)) {
  275. $dadmin[] = ['department_id' => $d_row->id, 'organise_id' => $d_row->organise_id ? $d_row->organise_id : $d_row->id, 'admin_id' => $row->id];
  276. }
  277. }
  278. if ($deleteids) {
  279. $this->dadminModel->where('admin_id', $row->id)->where('department_id', 'in', $deleteids)->delete();
  280. }
  281. if (count($dadmin) > 0) {
  282. $this->dadminModel->saveAll($dadmin);
  283. }
  284. // 先移除所有权限
  285. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  286. $group = $this->request->post("group/a");
  287. // 过滤不允许的组别,避免越权
  288. $group = [8];//array_intersect($this->childrenGroupIds, $group);
  289. if (!$group) {
  290. exception(__('The parent group exceeds permission limit'));
  291. }
  292. $dataset = [];
  293. foreach ($group as $value) {
  294. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  295. }
  296. model('AuthGroupAccess')->saveAll($dataset);
  297. Db::commit();
  298. } catch (\Exception $e) {
  299. Db::rollback();
  300. $this->error($e->getMessage());
  301. }
  302. $this->success();
  303. }
  304. $this->error(__('Parameter %s can not be empty', ''));
  305. }
  306. $groupdata = DepartmentModel::getGroupdata(
  307. $this->childrenGroupIds,
  308. $this->auth->isSuperAdmin() ? null : $this->auth->getGroups()
  309. );
  310. $grouplist = $this->auth->getGroups($row['id']);
  311. $groupids = [];
  312. foreach ($grouplist as $k => $v) {
  313. $groupids[] = $v['id'];
  314. }
  315. $this->view->assign("groupids", $groupids);
  316. $this->view->assign('row', $row);
  317. $this->view->assign('department_ids', $this->dadminModel->getDepartmentIds($ids));
  318. $this->view->assign('groupdata', $groupdata);
  319. return $this->view->fetch();
  320. }
  321. /**
  322. * 删除
  323. */
  324. public function del($ids = "")
  325. {
  326. if (!$this->request->isPost()) {
  327. $this->error(__("Invalid parameters"));
  328. }
  329. $this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isSuperAdmin());
  330. $this->model = new AdminModel();
  331. $ids = $ids ? $ids : $this->request->post("ids");
  332. if ($ids) {
  333. $ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $ids)));
  334. // 避免越权删除管理员
  335. $childrenGroupIds = $this->childrenGroupIds;
  336. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
  337. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  338. })->select();
  339. if ($adminList) {
  340. $deleteIds = [];
  341. foreach ($adminList as $k => $v) {
  342. $deleteIds[] = $v->id;
  343. }
  344. $deleteIds = array_values(array_diff($deleteIds, [$this->auth->id]));
  345. if ($deleteIds) {
  346. Db::startTrans();
  347. try {
  348. $this->model->destroy($deleteIds);
  349. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  350. //删除部门员工信息
  351. $this->dadminModel->where('admin_id', 'in', $deleteIds)->delete();;
  352. Db::commit();
  353. } catch (\Exception $e) {
  354. Db::rollback();
  355. $this->error($e->getMessage());
  356. }
  357. $this->success();
  358. }
  359. $this->error(__('No rows were deleted'));
  360. }
  361. }
  362. $this->error(__('You have no permission'));
  363. }
  364. /**
  365. * 批量更新
  366. */
  367. public function multi($ids = "")
  368. {
  369. $this->model = new AdminModel();
  370. return parent::multi($ids);
  371. }
  372. /**
  373. * 设置部门负责人
  374. */
  375. public function principal($ids = "")
  376. {
  377. $this->model = new AdminModel();
  378. $departmentModel = new DepartmentModel();
  379. $row = $this->model->get($ids);
  380. if ($this->request->isPost()) {
  381. //判断是不是超级管理员编辑
  382. if (!DepartmentAdminModel::isSuperAdmin($this->auth->id) && DepartmentAdminModel::isSuperAdmin($row->id)) {
  383. $this->error("您无权操作超级管理员");
  384. }
  385. $department_id = $this->request->post("department_id/a");
  386. if ($department_id && $department_id[0]) {
  387. Db::startTrans();
  388. try {
  389. $d_list = $departmentModel->where('id', 'in', $department_id)->select();
  390. if (!$d_list) {
  391. exception(__("Department can't null"));
  392. }
  393. //先移除他所有负责的部门
  394. $this->dadminModel->where('admin_id', $row->id)->update(['is_principal' => 0]);
  395. //判断选择的部门是否存在,不存在就先把他加入
  396. $p_departmentids = array_column($d_list, 'id');
  397. $exist_departmentids = $this->dadminModel->where('admin_id', $row->id)->column('department_id');
  398. $dadmin = array();
  399. //添加部门信息
  400. foreach ($d_list as $d_row) {
  401. if (!in_array($d_row->id, $exist_departmentids)) {
  402. $dadmin[] = ['department_id' => $d_row->id, 'organise_id' => $d_row->organise_id ? $d_row->organise_id : $d_row->id, 'admin_id' => $row->id];
  403. }
  404. }
  405. if (count($dadmin) > 0) {
  406. $this->dadminModel->saveAll($dadmin);
  407. }
  408. //更改为负责人
  409. $this->dadminModel->where('admin_id', $row->id)->where('department_id', 'in', $p_departmentids)->update(['is_principal' => 1]);
  410. Db::commit();
  411. } catch (\Exception $e) {
  412. Db::rollback();
  413. $this->error($e->getMessage());
  414. }
  415. } else {
  416. //移除他所有负责的部门
  417. $this->dadminModel->where('admin_id', $row->id)->update(['is_principal' => 0]);
  418. }
  419. $this->success();
  420. }
  421. $this->view->assign('row', $row);
  422. $this->view->assign('department_ids', $this->dadminModel::getPrincipalIds($ids));
  423. return $this->view->fetch();
  424. }
  425. /**
  426. * 获取员工的部门IDs
  427. */
  428. public function getDepartmentIds($id = ""){
  429. $departmentIds=DepartmentAdminModel::getDepartmentIds($id);
  430. //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
  431. $this->success("",'',['departmentIds' => $departmentIds]);
  432. }
  433. /**
  434. * 下拉选择
  435. * @Internal
  436. */
  437. public function selectpage()
  438. {
  439. $type=$this->request->request('type');//all代表选择所有
  440. $custom = ['status' => 'normal'];
  441. if ($type != "all") {
  442. $childrenAdminIds = \app\admin\model\department\Admin::getChildrenAdminIds($this->auth->id, true);
  443. $custom['id'] = ['in', $childrenAdminIds];
  444. }
  445. $this->selectpageFields = ['id', 'concat(nickname,"(",id,")") as nickname'];
  446. $this->request->request(['custom' => $custom]);
  447. //搜索字段
  448. $searchfield = (array)$this->request->request("searchField/a");
  449. $searchfield[]='id';
  450. $this->request->request(['searchField'=>$searchfield]);
  451. $this->model = model('admin');
  452. //设置过滤方法
  453. $this->request->filter(['trim', 'strip_tags', 'htmlspecialchars']);
  454. //搜索关键词,客户端输入以空格分开,这里接收为数组
  455. $word = (array)$this->request->request("q_word/a");
  456. //当前页
  457. $page = $this->request->request("pageNumber");
  458. //分页大小
  459. $pagesize = $this->request->request("pageSize");
  460. //搜索条件
  461. $andor = $this->request->request("andOr", "and", "strtoupper");
  462. //排序方式
  463. $orderby = (array)$this->request->request("orderBy/a");
  464. //显示的字段
  465. $field = $this->request->request("showField");
  466. //主键
  467. $primarykey = $this->request->request("keyField");
  468. //主键值
  469. $primaryvalue = $this->request->request("keyValue");
  470. //搜索字段
  471. $searchfield = (array)$this->request->request("searchField/a");
  472. //自定义搜索条件
  473. $custom = (array)$this->request->request("custom/a");
  474. //是否返回树形结构
  475. $istree = $this->request->request("isTree", 0);
  476. $ishtml = $this->request->request("isHtml", 0);
  477. if ($istree) {
  478. $word = [];
  479. $pagesize = 999999;
  480. }
  481. $order = [];
  482. foreach ($orderby as $k => $v) {
  483. $order[$v[0]] = $v[1];
  484. }
  485. $field = $field ? $field : 'name';
  486. //如果有primaryvalue,说明当前是初始化传值
  487. if ($primaryvalue !== null) {
  488. $where = [$primarykey => ['in', $primaryvalue]];
  489. $pagesize = 999999;
  490. } else {
  491. $where = function ($query) use ($word, $andor, $field, $searchfield, $custom) {
  492. $logic = $andor == 'AND' ? '&' : '|';
  493. $searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;
  494. $searchfield = str_replace(',', $logic, $searchfield);
  495. $word = array_filter(array_unique($word));
  496. if (count($word) == 1) {
  497. $query->where($searchfield, "like", "%" . reset($word) . "%");
  498. } else {
  499. $query->where(function ($query) use ($word, $searchfield) {
  500. foreach ($word as $index => $item) {
  501. $query->whereOr(function ($query) use ($item, $searchfield) {
  502. $query->where($searchfield, "like", "%{$item}%");
  503. });
  504. }
  505. });
  506. }
  507. if ($custom && is_array($custom)) {
  508. foreach ($custom as $k => $v) {
  509. if (is_array($v) && 2 == count($v)) {
  510. $query->where($k, trim($v[0]), $v[1]);
  511. } else {
  512. $query->where($k, '=', $v);
  513. }
  514. }
  515. }
  516. };
  517. }
  518. $adminIds = $this->getDataLimitAdminIds();
  519. if (is_array($adminIds)) {
  520. $this->model->where($this->dataLimitField, 'in', $adminIds);
  521. }
  522. $list = [];
  523. $total = $this->model->where($where)->count();
  524. if ($total > 0) {
  525. if (is_array($adminIds)) {
  526. $this->model->where($this->dataLimitField, 'in', $adminIds);
  527. }
  528. $fields = is_array($this->selectpageFields) ? $this->selectpageFields : ($this->selectpageFields && $this->selectpageFields != '*' ? explode(',', $this->selectpageFields) : []);
  529. //如果有primaryvalue,说明当前是初始化传值,按照选择顺序排序
  530. if ($primaryvalue !== null && preg_match("/^[a-z0-9_\-]+$/i", $primarykey)) {
  531. $primaryvalue = array_unique(is_array($primaryvalue) ? $primaryvalue : explode(',', $primaryvalue));
  532. //修复自定义data-primary-key为字符串内容时,给排序字段添加上引号
  533. $primaryvalue = array_map(function ($value) {
  534. return '\'' . $value . '\'';
  535. }, $primaryvalue);
  536. $primaryvalue = implode(',', $primaryvalue);
  537. $this->model->orderRaw("FIELD(`{$primarykey}`, {$primaryvalue})");
  538. } else {
  539. $this->model->order($order);
  540. }
  541. $datalist = $this->model->where($where)
  542. ->field($this->selectpageFields)
  543. ->page($page, $pagesize)
  544. ->select();
  545. foreach ($datalist as $index => $item) {
  546. unset($item['password'], $item['salt']);
  547. if ($this->selectpageFields == '*') {
  548. $result = [
  549. $primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '',
  550. $field => isset($item[$field]) ? $item[$field] : '',
  551. ];
  552. } else {
  553. $result = (($item instanceof Model ? $item->toArray() : (array)$item));
  554. }
  555. $result['pid'] = isset($item['pid']) ? $item['pid'] : (isset($item['parent_id']) ? $item['parent_id'] : 0);
  556. $list[] = $result;
  557. }
  558. if ($istree && !$primaryvalue) {
  559. $tree = Tree::instance();
  560. $tree->init(collection($list)->toArray(), 'pid');
  561. $list = $tree->getTreeList($tree->getTreeArray(0), $field);
  562. if (!$ishtml) {
  563. foreach ($list as &$item) {
  564. $item = str_replace('&nbsp;', ' ', $item);
  565. }
  566. unset($item);
  567. }
  568. }
  569. }
  570. //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
  571. return json(['list' => $list, 'total' => $total]);
  572. }
  573. public function departadmintotal()
  574. {
  575. //设置过滤方法
  576. $this->request->filter(['strip_tags', 'trim']);
  577. if (false === $this->request->isAjax()) {
  578. return $this->view->fetch();
  579. }
  580. //如果发送的来源是 Selectpage,则转发到 Selectpage
  581. if ($this->request->request('keyField')) {
  582. return $this->selectpage();
  583. }
  584. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  585. $departmentModel = new DepartmentModel();
  586. $list = $departmentModel
  587. ->where($where)->where(['level'=>5])
  588. ->order($sort, $order)
  589. ->paginate($limit);
  590. foreach ($list as $k => $v){
  591. $v->department_name = '';
  592. if(!empty($v->parent_id)){
  593. $row = $departmentModel->where(['id'=>$v->parent_id])->find();
  594. if(!empty($row->parent_id)){
  595. $row2 = $departmentModel->where(['id'=>$row->parent_id])->find();
  596. if(!empty($row2->parent_id)){
  597. $row3 = $departmentModel->where(['id'=>$row2->parent_id])->find();
  598. if(!empty($row3->parent_id)) {
  599. $row4 = $departmentModel->where(['id' => $row3->parent_id])->find();
  600. $v->department_name = $row4['name'].">>".$row3['name'].">>".$row2['name'].">>".$row['name'].">>".$v['name'];
  601. }
  602. }
  603. }
  604. }
  605. $v->department_admin_count = Db::name('admin')->where(['depart_id'=>$v['id']])->count();
  606. }
  607. unset($v);
  608. $result = ['total' => $list->total(), 'rows' => $list->items()];
  609. return json($result);
  610. }
  611. }