AdminLogic.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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\logic\auth;
  15. use app\common\cache\AdminAuthCache;
  16. use app\common\enum\YesNoEnum;
  17. use app\common\logic\BaseLogic;
  18. use app\common\model\auth\Admin;
  19. use app\common\model\auth\AdminDept;
  20. use app\common\model\auth\AdminJobs;
  21. use app\common\model\auth\AdminRole;
  22. use app\common\model\auth\AdminSession;
  23. use app\common\cache\AdminTokenCache;
  24. use app\common\service\FileService;
  25. use think\facade\Config;
  26. use think\facade\Db;
  27. /**
  28. * 管理员逻辑
  29. * Class AdminLogic
  30. * @package app\adminapi\logic\auth
  31. */
  32. class AdminLogic extends BaseLogic
  33. {
  34. /**
  35. * @notes 添加管理员
  36. * @param array $params
  37. * @author 段誉
  38. * @date 2021/12/29 10:23
  39. */
  40. public static function add(array $params)
  41. {
  42. Db::startTrans();
  43. try {
  44. $passwordSalt = Config::get('project.unique_identification');
  45. $password = create_password($params['password'], $passwordSalt);
  46. $defaultAvatar = config('project.default_image.admin_avatar');
  47. $avatar = !empty($params['avatar']) ? FileService::setFileUrl($params['avatar']) : $defaultAvatar;
  48. $admin = Admin::create([
  49. 'name' => $params['name'],
  50. 'account' => $params['account'],
  51. 'avatar' => $avatar,
  52. 'password' => $password,
  53. 'create_time' => time(),
  54. 'disable' => $params['disable'],
  55. 'multipoint_login' => $params['multipoint_login'],
  56. ]);
  57. // 角色
  58. self::insertRole($admin['id'], $params['role_id'] ?? []);
  59. // 部门
  60. self::insertDept($admin['id'], $params['dept_id'] ?? []);
  61. // 岗位
  62. self::insertJobs($admin['id'], $params['jobs_id'] ?? []);
  63. Db::commit();
  64. return true;
  65. } catch (\Exception $e) {
  66. Db::rollback();
  67. self::setError($e->getMessage());
  68. return false;
  69. }
  70. }
  71. /**
  72. * @notes 编辑管理员
  73. * @param array $params
  74. * @return bool
  75. * @author 段誉
  76. * @date 2021/12/29 10:43
  77. */
  78. public static function edit(array $params): bool
  79. {
  80. Db::startTrans();
  81. try {
  82. // 基础信息
  83. $data = [
  84. 'id' => $params['id'],
  85. 'name' => $params['name'],
  86. 'account' => $params['account'],
  87. 'disable' => $params['disable'],
  88. 'multipoint_login' => $params['multipoint_login']
  89. ];
  90. // 头像
  91. $data['avatar'] = !empty($params['avatar']) ? FileService::setFileUrl($params['avatar']) : '';
  92. // 密码
  93. if (!empty($params['password'])) {
  94. $passwordSalt = Config::get('project.unique_identification');
  95. $data['password'] = create_password($params['password'], $passwordSalt);
  96. }
  97. // 禁用或更换角色后.设置token过期
  98. $roleId = AdminRole::where('admin_id', $params['id'])->column('role_id');
  99. $editRole = false;
  100. if (!empty(array_diff_assoc($roleId, $params['role_id']))) {
  101. $editRole = true;
  102. }
  103. if ($params['disable'] == 1 || $editRole) {
  104. $tokenArr = AdminSession::where('admin_id', $params['id'])->select()->toArray();
  105. foreach ($tokenArr as $token) {
  106. self::expireToken($token['token']);
  107. }
  108. }
  109. Admin::update($data);
  110. (new AdminAuthCache($params['id']))->clearAuthCache();
  111. // 删除旧的关联信息
  112. AdminRole::delByUserId($params['id']);
  113. AdminDept::delByUserId($params['id']);
  114. AdminJobs::delByUserId($params['id']);
  115. // 角色
  116. self::insertRole($params['id'], $params['role_id']);
  117. // 部门
  118. self::insertDept($params['id'], $params['dept_id'] ?? []);
  119. // 岗位
  120. self::insertJobs($params['id'], $params['jobs_id'] ?? []);
  121. Db::commit();
  122. return true;
  123. } catch (\Exception $e) {
  124. Db::rollback();
  125. self::setError($e->getMessage());
  126. return false;
  127. }
  128. }
  129. /**
  130. * @notes 删除管理员
  131. * @param array $params
  132. * @return bool
  133. * @author 段誉
  134. * @date 2021/12/29 10:45
  135. */
  136. public static function delete(array $params): bool
  137. {
  138. Db::startTrans();
  139. try {
  140. $admin = Admin::findOrEmpty($params['id']);
  141. if ($admin->root == YesNoEnum::YES) {
  142. throw new \Exception("超级管理员不允许被删除");
  143. }
  144. Admin::destroy($params['id']);
  145. //设置token过期
  146. $tokenArr = AdminSession::where('admin_id', $params['id'])->select()->toArray();
  147. foreach ($tokenArr as $token) {
  148. self::expireToken($token['token']);
  149. }
  150. (new AdminAuthCache($params['id']))->clearAuthCache();
  151. // 删除旧的关联信息
  152. AdminRole::delByUserId($params['id']);
  153. AdminDept::delByUserId($params['id']);
  154. AdminJobs::delByUserId($params['id']);
  155. Db::commit();
  156. return true;
  157. } catch (\Exception $e) {
  158. Db::rollback();
  159. self::setError($e->getMessage());
  160. return false;
  161. }
  162. }
  163. /**
  164. * @notes 过期token
  165. * @param $token
  166. * @return bool
  167. * @throws \think\db\exception\DataNotFoundException
  168. * @throws \think\db\exception\DbException
  169. * @throws \think\db\exception\ModelNotFoundException
  170. * @author 段誉
  171. * @date 2021/12/29 10:46
  172. */
  173. public static function expireToken($token): bool
  174. {
  175. $adminSession = AdminSession::where('token', '=', $token)
  176. ->with('admin')
  177. ->find();
  178. if (empty($adminSession)) {
  179. return false;
  180. }
  181. $time = time();
  182. $adminSession->expire_time = $time;
  183. $adminSession->update_time = $time;
  184. $adminSession->save();
  185. return (new AdminTokenCache())->deleteAdminInfo($token);
  186. }
  187. /**
  188. * @notes 查看管理员详情
  189. * @param $params
  190. * @return array
  191. * @author 段誉
  192. * @date 2021/12/29 11:07
  193. */
  194. public static function detail($params, $action = 'detail'): array
  195. {
  196. $admin = Admin::field([
  197. 'id', 'account', 'name', 'disable', 'root',
  198. 'multipoint_login', 'avatar',
  199. ])->findOrEmpty($params['id'])->toArray();
  200. if ($action == 'detail') {
  201. return $admin;
  202. }
  203. $result['user'] = $admin;
  204. // 当前管理员角色拥有的菜单
  205. $result['menu'] = MenuLogic::getMenuByAdminId($params['id']);
  206. // 当前管理员橘色拥有的按钮权限
  207. $result['permissions'] = AuthLogic::getBtnAuthByRoleId($admin);
  208. return $result;
  209. }
  210. /**
  211. * @notes 编辑超级管理员
  212. * @param $params
  213. * @return Admin
  214. * @author 段誉
  215. * @date 2022/4/8 17:54
  216. */
  217. public static function editSelf($params)
  218. {
  219. $data = [
  220. 'id' => $params['admin_id'],
  221. 'name' => $params['name'],
  222. 'avatar' => FileService::setFileUrl($params['avatar']),
  223. ];
  224. if (!empty($params['password'])) {
  225. $passwordSalt = Config::get('project.unique_identification');
  226. $data['password'] = create_password($params['password'], $passwordSalt);
  227. }
  228. return Admin::update($data);
  229. }
  230. /**
  231. * @notes 新增角色
  232. * @param $adminId
  233. * @param $roleIds
  234. * @throws \Exception
  235. * @author 段誉
  236. * @date 2022/11/25 14:23
  237. */
  238. public static function insertRole($adminId, $roleIds)
  239. {
  240. if (!empty($roleIds)) {
  241. // 角色
  242. $roleData = [];
  243. foreach ($roleIds as $roleId) {
  244. $roleData[] = [
  245. 'admin_id' => $adminId,
  246. 'role_id' => $roleId,
  247. ];
  248. }
  249. (new AdminRole())->saveAll($roleData);
  250. }
  251. }
  252. /**
  253. * @notes 新增部门
  254. * @param $adminId
  255. * @param $deptIds
  256. * @throws \Exception
  257. * @author 段誉
  258. * @date 2022/11/25 14:22
  259. */
  260. public static function insertDept($adminId, $deptIds)
  261. {
  262. // 部门
  263. if (!empty($deptIds)) {
  264. $deptData = [];
  265. foreach ($deptIds as $deptId) {
  266. $deptData[] = [
  267. 'admin_id' => $adminId,
  268. 'dept_id' => $deptId
  269. ];
  270. }
  271. (new AdminDept())->saveAll($deptData);
  272. }
  273. }
  274. /**
  275. * @notes 新增岗位
  276. * @param $adminId
  277. * @param $jobsIds
  278. * @throws \Exception
  279. * @author 段誉
  280. * @date 2022/11/25 14:22
  281. */
  282. public static function insertJobs($adminId, $jobsIds)
  283. {
  284. // 岗位
  285. if (!empty($jobsIds)) {
  286. $jobsData = [];
  287. foreach ($jobsIds as $jobsId) {
  288. $jobsData[] = [
  289. 'admin_id' => $adminId,
  290. 'jobs_id' => $jobsId
  291. ];
  292. }
  293. (new AdminJobs())->saveAll($jobsData);
  294. }
  295. }
  296. }