1
0

TenantAdminLogic.php 11 KB

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