Admin.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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'];
  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['salt'] = Random::alnum();
  160. $params['password'] = md5(md5($params['password']) . $params['salt']);
  161. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  162. $result = $adminModel->validate('Admin.add')->save($params);
  163. if ($result === false) {
  164. exception($adminModel->getError());
  165. }
  166. if (!$this->auth->isSuperAdmin()&&isset($params['data_scope'])){
  167. unset($params['data_scope']);//超级管理人才能赋予全部数据权限
  168. }
  169. $admin_id = $adminModel->id;
  170. $dadmin = array();
  171. //添加部门信息
  172. foreach ($d_list as $d_row) {
  173. $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];
  174. }
  175. $this->dadminModel->saveAll($dadmin);
  176. $group = $this->request->post("group/a");
  177. //过滤不允许的组别,避免越权
  178. $group = [8];//array_intersect($this->childrenGroupIds, $group);
  179. //添加权限默认
  180. if (!$group) {
  181. exception(__('The parent group exceeds permission limit'));
  182. }
  183. $dataset = [];
  184. foreach ($group as $value) {
  185. $dataset[] = ['uid' => $admin_id, 'group_id' => $value];
  186. }
  187. model('AuthGroupAccess')->saveAll($dataset);
  188. Db::commit();
  189. } catch (\Exception $e) {
  190. Db::rollback();
  191. $this->error($e->getMessage());
  192. }
  193. $this->success();
  194. }
  195. $this->error(__('Parameter %s can not be empty', ''));
  196. }
  197. $this->view->assign('groupdata', $groupdata);
  198. return $this->view->fetch();
  199. }
  200. /**
  201. * 修改
  202. * @param null $ids
  203. * @return string|\think\response\Json
  204. * @throws \think\Exception
  205. * @throws \think\db\exception\DataNotFoundException
  206. * @throws \think\db\exception\ModelNotFoundException
  207. * @throws \think\exception\DbException
  208. */
  209. public function edit($ids = null)
  210. {
  211. $this->model = new AdminModel();
  212. $departmentModel = new DepartmentModel();
  213. $row = $this->model->get($ids);
  214. if (!$row) {
  215. $this->error(__('No Results were found'));
  216. }
  217. // if (!\app\admin\model\department\Admin::checkDataAuth($this->auth,$row,'id')){
  218. // $this->error(__('You have no permission'));
  219. // }
  220. if ($this->request->isPost()) {
  221. //判断是不是超级管理员编辑
  222. // if (!$this->auth->isSuperAdmin() && DepartmentAdminModel::isSuperAdmin($row->id)) {
  223. // $this->error("您无权操作超级管理员");
  224. // }
  225. $params = $this->request->post("row/a");
  226. if ($params) {
  227. Db::startTrans();
  228. try {
  229. $department_id = $this->request->post("department_id/a");
  230. //获取部门信息
  231. if (!$department_id) {
  232. exception(__("Department can't null"));
  233. }
  234. $d_list = $departmentModel->where('id', 'in', $department_id)->select();
  235. if (!$d_list) {
  236. exception(__("Department can't null"));
  237. }
  238. foreach ($d_list as $d_row){
  239. if ($d_row['level']!=5){
  240. exception(__("学员请选择到区队"));
  241. }
  242. }
  243. if ($params['password']) {
  244. if (!Validate::is($params['password'], '\S{6,16}')) {
  245. exception(__("Please input correct password"));
  246. }
  247. $params['salt'] = Random::alnum();
  248. $params['password'] = md5(md5($params['password']) . $params['salt']);
  249. } else {
  250. unset($params['password'], $params['salt']);
  251. }
  252. if (!$this->auth->isSuperAdmin()&&isset($params['data_scope'])){
  253. unset($params['data_scope']);//超级管理人才能赋予全部数据权限
  254. }
  255. //这里需要针对username和email做唯一验证
  256. $adminValidate = \think\Loader::validate('Admin');
  257. $adminValidate->rule([
  258. 'username' => 'require|regex:\w{3,20}|unique:admin,username,' . $row->id,
  259. // 'email' => 'require|email|unique:admin,email,' . $row->id,
  260. // 'mobile' => 'regex:1[3-9]\d{9}|unique:admin,mobile,' . $row->id,
  261. 'password' => 'regex:\S{32}',
  262. ]);
  263. $result = $row->validate('Admin.edit')->save($params);
  264. if ($result === false) {
  265. exception($row->getError());
  266. }
  267. $exist_departmentids = $this->dadminModel->where('admin_id', $row->id)->column('department_id');
  268. $dadmin = array();
  269. $deleteids = array_diff($exist_departmentids, $department_id);
  270. //添加部门信息
  271. foreach ($d_list as $d_row) {
  272. if (!in_array($d_row->id, $exist_departmentids)) {
  273. $dadmin[] = ['department_id' => $d_row->id, 'organise_id' => $d_row->organise_id ? $d_row->organise_id : $d_row->id, 'admin_id' => $row->id];
  274. }
  275. }
  276. if ($deleteids) {
  277. $this->dadminModel->where('admin_id', $row->id)->where('department_id', 'in', $deleteids)->delete();
  278. }
  279. if (count($dadmin) > 0) {
  280. $this->dadminModel->saveAll($dadmin);
  281. }
  282. // 先移除所有权限
  283. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  284. $group = $this->request->post("group/a");
  285. // 过滤不允许的组别,避免越权
  286. $group = [8];//array_intersect($this->childrenGroupIds, $group);
  287. if (!$group) {
  288. exception(__('The parent group exceeds permission limit'));
  289. }
  290. $dataset = [];
  291. foreach ($group as $value) {
  292. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  293. }
  294. model('AuthGroupAccess')->saveAll($dataset);
  295. Db::commit();
  296. } catch (\Exception $e) {
  297. Db::rollback();
  298. $this->error($e->getMessage());
  299. }
  300. $this->success();
  301. }
  302. $this->error(__('Parameter %s can not be empty', ''));
  303. }
  304. $groupdata = DepartmentModel::getGroupdata(
  305. $this->childrenGroupIds,
  306. $this->auth->isSuperAdmin() ? null : $this->auth->getGroups()
  307. );
  308. $grouplist = $this->auth->getGroups($row['id']);
  309. $groupids = [];
  310. foreach ($grouplist as $k => $v) {
  311. $groupids[] = $v['id'];
  312. }
  313. $this->view->assign("groupids", $groupids);
  314. $this->view->assign('row', $row);
  315. $this->view->assign('department_ids', $this->dadminModel->getDepartmentIds($ids));
  316. $this->view->assign('groupdata', $groupdata);
  317. return $this->view->fetch();
  318. }
  319. /**
  320. * 删除
  321. */
  322. public function del($ids = "")
  323. {
  324. if (!$this->request->isPost()) {
  325. $this->error(__("Invalid parameters"));
  326. }
  327. $this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isSuperAdmin());
  328. $this->model = new AdminModel();
  329. $ids = $ids ? $ids : $this->request->post("ids");
  330. if ($ids) {
  331. $ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $ids)));
  332. // 避免越权删除管理员
  333. $childrenGroupIds = $this->childrenGroupIds;
  334. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
  335. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  336. })->select();
  337. if ($adminList) {
  338. $deleteIds = [];
  339. foreach ($adminList as $k => $v) {
  340. $deleteIds[] = $v->id;
  341. }
  342. $deleteIds = array_values(array_diff($deleteIds, [$this->auth->id]));
  343. if ($deleteIds) {
  344. Db::startTrans();
  345. try {
  346. $this->model->destroy($deleteIds);
  347. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  348. //删除部门员工信息
  349. $this->dadminModel->where('admin_id', 'in', $deleteIds)->delete();;
  350. Db::commit();
  351. } catch (\Exception $e) {
  352. Db::rollback();
  353. $this->error($e->getMessage());
  354. }
  355. $this->success();
  356. }
  357. $this->error(__('No rows were deleted'));
  358. }
  359. }
  360. $this->error(__('You have no permission'));
  361. }
  362. /**
  363. * 批量更新
  364. */
  365. public function multi($ids = "")
  366. {
  367. $this->model = new AdminModel();
  368. return parent::multi($ids);
  369. }
  370. /**
  371. * 设置部门负责人
  372. */
  373. public function principal($ids = "")
  374. {
  375. $this->model = new AdminModel();
  376. $departmentModel = new DepartmentModel();
  377. $row = $this->model->get($ids);
  378. if ($this->request->isPost()) {
  379. //判断是不是超级管理员编辑
  380. if (!DepartmentAdminModel::isSuperAdmin($this->auth->id) && DepartmentAdminModel::isSuperAdmin($row->id)) {
  381. $this->error("您无权操作超级管理员");
  382. }
  383. $department_id = $this->request->post("department_id/a");
  384. if ($department_id && $department_id[0]) {
  385. Db::startTrans();
  386. try {
  387. $d_list = $departmentModel->where('id', 'in', $department_id)->select();
  388. if (!$d_list) {
  389. exception(__("Department can't null"));
  390. }
  391. //先移除他所有负责的部门
  392. $this->dadminModel->where('admin_id', $row->id)->update(['is_principal' => 0]);
  393. //判断选择的部门是否存在,不存在就先把他加入
  394. $p_departmentids = array_column($d_list, 'id');
  395. $exist_departmentids = $this->dadminModel->where('admin_id', $row->id)->column('department_id');
  396. $dadmin = array();
  397. //添加部门信息
  398. foreach ($d_list as $d_row) {
  399. if (!in_array($d_row->id, $exist_departmentids)) {
  400. $dadmin[] = ['department_id' => $d_row->id, 'organise_id' => $d_row->organise_id ? $d_row->organise_id : $d_row->id, 'admin_id' => $row->id];
  401. }
  402. }
  403. if (count($dadmin) > 0) {
  404. $this->dadminModel->saveAll($dadmin);
  405. }
  406. //更改为负责人
  407. $this->dadminModel->where('admin_id', $row->id)->where('department_id', 'in', $p_departmentids)->update(['is_principal' => 1]);
  408. Db::commit();
  409. } catch (\Exception $e) {
  410. Db::rollback();
  411. $this->error($e->getMessage());
  412. }
  413. } else {
  414. //移除他所有负责的部门
  415. $this->dadminModel->where('admin_id', $row->id)->update(['is_principal' => 0]);
  416. }
  417. $this->success();
  418. }
  419. $this->view->assign('row', $row);
  420. $this->view->assign('department_ids', $this->dadminModel::getPrincipalIds($ids));
  421. return $this->view->fetch();
  422. }
  423. /**
  424. * 获取员工的部门IDs
  425. */
  426. public function getDepartmentIds($id = ""){
  427. $departmentIds=DepartmentAdminModel::getDepartmentIds($id);
  428. //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
  429. $this->success("",'',['departmentIds' => $departmentIds]);
  430. }
  431. /**
  432. * 下拉选择
  433. * @Internal
  434. */
  435. public function selectpage()
  436. {
  437. $type=$this->request->request('type');//all代表选择所有
  438. $custom = ['status' => 'normal'];
  439. if ($type != "all") {
  440. $childrenAdminIds = \app\admin\model\department\Admin::getChildrenAdminIds($this->auth->id, true);
  441. $custom['id'] = ['in', $childrenAdminIds];
  442. }
  443. $this->selectpageFields = ['id', 'concat(nickname,"(",id,")") as nickname'];
  444. $this->request->request(['custom' => $custom]);
  445. //搜索字段
  446. $searchfield = (array)$this->request->request("searchField/a");
  447. $searchfield[]='id';
  448. $this->request->request(['searchField'=>$searchfield]);
  449. $this->model = model('admin');
  450. //设置过滤方法
  451. $this->request->filter(['trim', 'strip_tags', 'htmlspecialchars']);
  452. //搜索关键词,客户端输入以空格分开,这里接收为数组
  453. $word = (array)$this->request->request("q_word/a");
  454. //当前页
  455. $page = $this->request->request("pageNumber");
  456. //分页大小
  457. $pagesize = $this->request->request("pageSize");
  458. //搜索条件
  459. $andor = $this->request->request("andOr", "and", "strtoupper");
  460. //排序方式
  461. $orderby = (array)$this->request->request("orderBy/a");
  462. //显示的字段
  463. $field = $this->request->request("showField");
  464. //主键
  465. $primarykey = $this->request->request("keyField");
  466. //主键值
  467. $primaryvalue = $this->request->request("keyValue");
  468. //搜索字段
  469. $searchfield = (array)$this->request->request("searchField/a");
  470. //自定义搜索条件
  471. $custom = (array)$this->request->request("custom/a");
  472. //是否返回树形结构
  473. $istree = $this->request->request("isTree", 0);
  474. $ishtml = $this->request->request("isHtml", 0);
  475. if ($istree) {
  476. $word = [];
  477. $pagesize = 999999;
  478. }
  479. $order = [];
  480. foreach ($orderby as $k => $v) {
  481. $order[$v[0]] = $v[1];
  482. }
  483. $field = $field ? $field : 'name';
  484. //如果有primaryvalue,说明当前是初始化传值
  485. if ($primaryvalue !== null) {
  486. $where = [$primarykey => ['in', $primaryvalue]];
  487. $pagesize = 999999;
  488. } else {
  489. $where = function ($query) use ($word, $andor, $field, $searchfield, $custom) {
  490. $logic = $andor == 'AND' ? '&' : '|';
  491. $searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;
  492. $searchfield = str_replace(',', $logic, $searchfield);
  493. $word = array_filter(array_unique($word));
  494. if (count($word) == 1) {
  495. $query->where($searchfield, "like", "%" . reset($word) . "%");
  496. } else {
  497. $query->where(function ($query) use ($word, $searchfield) {
  498. foreach ($word as $index => $item) {
  499. $query->whereOr(function ($query) use ($item, $searchfield) {
  500. $query->where($searchfield, "like", "%{$item}%");
  501. });
  502. }
  503. });
  504. }
  505. if ($custom && is_array($custom)) {
  506. foreach ($custom as $k => $v) {
  507. if (is_array($v) && 2 == count($v)) {
  508. $query->where($k, trim($v[0]), $v[1]);
  509. } else {
  510. $query->where($k, '=', $v);
  511. }
  512. }
  513. }
  514. };
  515. }
  516. $adminIds = $this->getDataLimitAdminIds();
  517. if (is_array($adminIds)) {
  518. $this->model->where($this->dataLimitField, 'in', $adminIds);
  519. }
  520. $list = [];
  521. $total = $this->model->where($where)->count();
  522. if ($total > 0) {
  523. if (is_array($adminIds)) {
  524. $this->model->where($this->dataLimitField, 'in', $adminIds);
  525. }
  526. $fields = is_array($this->selectpageFields) ? $this->selectpageFields : ($this->selectpageFields && $this->selectpageFields != '*' ? explode(',', $this->selectpageFields) : []);
  527. //如果有primaryvalue,说明当前是初始化传值,按照选择顺序排序
  528. if ($primaryvalue !== null && preg_match("/^[a-z0-9_\-]+$/i", $primarykey)) {
  529. $primaryvalue = array_unique(is_array($primaryvalue) ? $primaryvalue : explode(',', $primaryvalue));
  530. //修复自定义data-primary-key为字符串内容时,给排序字段添加上引号
  531. $primaryvalue = array_map(function ($value) {
  532. return '\'' . $value . '\'';
  533. }, $primaryvalue);
  534. $primaryvalue = implode(',', $primaryvalue);
  535. $this->model->orderRaw("FIELD(`{$primarykey}`, {$primaryvalue})");
  536. } else {
  537. $this->model->order($order);
  538. }
  539. $datalist = $this->model->where($where)
  540. ->field($this->selectpageFields)
  541. ->page($page, $pagesize)
  542. ->select();
  543. foreach ($datalist as $index => $item) {
  544. unset($item['password'], $item['salt']);
  545. if ($this->selectpageFields == '*') {
  546. $result = [
  547. $primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '',
  548. $field => isset($item[$field]) ? $item[$field] : '',
  549. ];
  550. } else {
  551. $result = (($item instanceof Model ? $item->toArray() : (array)$item));
  552. }
  553. $result['pid'] = isset($item['pid']) ? $item['pid'] : (isset($item['parent_id']) ? $item['parent_id'] : 0);
  554. $list[] = $result;
  555. }
  556. if ($istree && !$primaryvalue) {
  557. $tree = Tree::instance();
  558. $tree->init(collection($list)->toArray(), 'pid');
  559. $list = $tree->getTreeList($tree->getTreeArray(0), $field);
  560. if (!$ishtml) {
  561. foreach ($list as &$item) {
  562. $item = str_replace('&nbsp;', ' ', $item);
  563. }
  564. unset($item);
  565. }
  566. }
  567. }
  568. //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
  569. return json(['list' => $list, 'total' => $total]);
  570. }
  571. }