TenantSystemMenuLogic.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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\model\auth\TenantSystemMenu;
  16. /**
  17. * 租户菜单逻辑
  18. * Class TenantSystemMenuLogic
  19. * @package app\adminapi\logic\tenant
  20. */
  21. class TenantSystemMenuLogic
  22. {
  23. /**
  24. * @notes 初始化租户菜单信息
  25. * @param $tenant_id
  26. * @author yfdong
  27. * @date 2024/09/05 22:15
  28. */
  29. public static function initialization($tenant_id)
  30. {
  31. //初始化租户菜单字段
  32. $field = "id,pid,type,name,icon,sort,perms,paths,component,selected,params,is_cache,is_show,is_disable,tenant_id";
  33. //查询模板菜单配置文件 此处默认为租户号为0的模板数据
  34. $tenantSystemMenuList = TenantSystemMenu::where(['tenant_id' => 0])->field($field)->order('pid')->select()->toArray();
  35. //记录对应的关系
  36. foreach ($tenantSystemMenuList as $item) {
  37. $tenantSystemMenu[$item['id']] = $item;
  38. }
  39. //创建菜单数据
  40. foreach ($tenantSystemMenuList as $item) {
  41. $item['tenant_id'] = $tenant_id;
  42. //创建新的菜单并保存原本id对应现在的哪个信息
  43. $oldId = $item['id'];
  44. unset($item['id']);
  45. $newMenu = TenantSystemMenu::create($item);
  46. $tenantSystemMenu[$oldId] = $newMenu;
  47. }
  48. //获取当前租户的初始化菜单关系
  49. $tenantSystemMenuNewList = TenantSystemMenu::where(['tenant_id' => $tenant_id])->field($field)->order('pid')->select()->toArray();
  50. //更新对应的主菜单关系
  51. foreach ($tenantSystemMenuNewList as $item) {
  52. if ($item['pid'] != 0)
  53. $item['pid'] = $tenantSystemMenu[$item['pid']]['id'];
  54. $where = array('id' => intval($item['id']));
  55. TenantSystemMenu::update($item, $where);
  56. }
  57. }
  58. }