AdminLists.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\lists\auth;
  15. use app\adminapi\lists\BaseAdminDataLists;
  16. use app\common\lists\ListsExcelInterface;
  17. use app\common\lists\ListsExtendInterface;
  18. use app\common\lists\ListsSearchInterface;
  19. use app\common\lists\ListsSortInterface;
  20. use app\common\model\auth\Admin;
  21. use app\common\model\auth\AdminRole;
  22. use app\common\model\auth\SystemRole;
  23. use app\common\model\dept\Dept;
  24. use app\common\model\dept\Jobs;
  25. /**
  26. * 管理员列表
  27. * Class AdminLists
  28. * @package app\adminapi\lists\auth
  29. */
  30. class AdminLists extends BaseAdminDataLists implements ListsExtendInterface, ListsSearchInterface, ListsSortInterface,ListsExcelInterface
  31. {
  32. /**
  33. * @notes 设置导出字段
  34. * @return string[]
  35. * @author 段誉
  36. * @date 2021/12/29 10:08
  37. */
  38. public function setExcelFields(): array
  39. {
  40. return [
  41. 'account' => '账号',
  42. 'name' => '名称',
  43. 'role_name' => '角色',
  44. 'dept_name' => '部门',
  45. 'create_time' => '创建时间',
  46. 'login_time' => '最近登录时间',
  47. 'login_ip' => '最近登录IP',
  48. 'disable_desc' => '状态',
  49. ];
  50. }
  51. /**
  52. * @notes 设置导出文件名
  53. * @return string
  54. * @author 段誉
  55. * @date 2021/12/29 10:08
  56. */
  57. public function setFileName(): string
  58. {
  59. return '管理员列表';
  60. }
  61. /**
  62. * @notes 设置搜索条件
  63. * @return \string[][]
  64. * @author 段誉
  65. * @date 2021/12/29 10:07
  66. */
  67. public function setSearch(): array
  68. {
  69. return [
  70. '%like%' => ['name', 'account'],
  71. ];
  72. }
  73. /**
  74. * @notes 设置支持排序字段
  75. * @return string[]
  76. * @author 段誉
  77. * @date 2021/12/29 10:07
  78. * @remark 格式: ['前端传过来的字段名' => '数据库中的字段名'];
  79. */
  80. public function setSortFields(): array
  81. {
  82. return ['create_time' => 'create_time', 'id' => 'id'];
  83. }
  84. /**
  85. * @notes 设置默认排序
  86. * @return string[]
  87. * @author 段誉
  88. * @date 2021/12/29 10:06
  89. */
  90. public function setDefaultOrder(): array
  91. {
  92. return ['id' => 'desc'];
  93. }
  94. /**
  95. * @notes 查询条件
  96. * @return array
  97. * @author 段誉
  98. * @date 2022/11/29 11:33
  99. */
  100. public function queryWhere()
  101. {
  102. $where = [];
  103. if (isset($this->params['role_id']) && $this->params['role_id'] != '') {
  104. $adminIds = AdminRole::where('role_id', $this->params['role_id'])->column('admin_id');
  105. if (!empty($adminIds)) {
  106. $where[] = ['id', 'in', $adminIds];
  107. }
  108. }
  109. return $where;
  110. }
  111. /**
  112. * @notes 获取管理列表
  113. * @return array
  114. * @throws \think\db\exception\DataNotFoundException
  115. * @throws \think\db\exception\DbException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. * @author 段誉
  118. * @date 2021/12/29 10:05
  119. */
  120. public function lists(): array
  121. {
  122. $field = [
  123. 'id', 'name', 'account', 'create_time', 'disable', 'root',
  124. 'login_time', 'login_ip', 'multipoint_login', 'avatar'
  125. ];
  126. $adminLists = Admin::field($field)
  127. ->where($this->searchWhere)
  128. ->where($this->queryWhere())
  129. ->limit($this->limitOffset, $this->limitLength)
  130. ->order($this->sortOrder)
  131. ->append(['role_id', 'dept_id', 'jobs_id', 'disable_desc'])
  132. ->select()
  133. ->toArray();
  134. // 角色数组('角色id'=>'角色名称')
  135. $roleLists = SystemRole::column('name', 'id');
  136. // 部门列表
  137. $deptLists = Dept::column('name', 'id');
  138. // 岗位列表
  139. $jobsLists = Jobs::column('name', 'id');
  140. //管理员列表增加角色名称
  141. foreach ($adminLists as $k => $v) {
  142. $roleName = '';
  143. if ($v['root'] == 1) {
  144. $roleName = '系统管理员';
  145. } else {
  146. foreach ($v['role_id'] as $roleId) {
  147. $roleName .= $roleLists[$roleId] ?? '';
  148. $roleName .= '/';
  149. }
  150. }
  151. $deptName = '';
  152. foreach ($v['dept_id'] as $deptId) {
  153. $deptName .= $deptLists[$deptId] ?? '';
  154. $deptName .= '/';
  155. }
  156. $jobsName = '';
  157. foreach ($v['jobs_id'] as $jobsId) {
  158. $jobsName .= $jobsLists[$jobsId] ?? '';
  159. $jobsName .= '/';
  160. }
  161. $adminLists[$k]['role_name'] = trim($roleName, '/');
  162. $adminLists[$k]['dept_name'] = trim($deptName, '/');
  163. $adminLists[$k]['jobs_name'] = trim($jobsName, '/');
  164. }
  165. return $adminLists;
  166. }
  167. /**
  168. * @notes 获取数量
  169. * @return int
  170. * @author 令狐冲
  171. * @date 2021/7/13 00:52
  172. */
  173. public function count(): int
  174. {
  175. return Admin::where($this->searchWhere)
  176. ->where($this->queryWhere())
  177. ->count();
  178. }
  179. public function extend()
  180. {
  181. return [];
  182. }
  183. }