TenantAdminLogic.php 11 KB

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