Qingdongams.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace addons\qingdongams;
  3. use app\admin\model\AuthRule;
  4. use app\common\library\Menu;
  5. use think\Addons;
  6. use think\Exception;
  7. use think\exception\PDOException;
  8. /**
  9. * 插件
  10. */
  11. class Qingdongams extends Addons
  12. {
  13. /**
  14. * 插件安装方法
  15. * @return bool
  16. */
  17. public function install()
  18. {
  19. $menu = self::getMenu();
  20. Menu::create($menu['new']);
  21. return true;
  22. }
  23. /**
  24. * 插件卸载方法
  25. * @return bool
  26. */
  27. public function uninstall()
  28. {
  29. Menu::delete('qingdongams');
  30. return true;
  31. }
  32. /**
  33. * 插件启用方法
  34. * @return bool
  35. */
  36. public function enable()
  37. {
  38. Menu::enable('qingdongams');
  39. return true;
  40. }
  41. /**
  42. * 插件禁用方法
  43. * @return bool
  44. */
  45. public function disable()
  46. {
  47. Menu::disable('qingdongams');
  48. return true;
  49. }
  50. /**
  51. * 插件更新方法
  52. */
  53. public function upgrade()
  54. {
  55. $menu = self::getMenu();
  56. if (method_exists(Menu::class, 'upgrade')) {
  57. Menu::upgrade('qingdongams', $menu['new']);
  58. } else {
  59. self::menuCreateOrUpdate($menu['new'], $menu['old']);
  60. }
  61. return true;
  62. }
  63. private static function getMenu()
  64. {
  65. $newMenu = [];
  66. $config_file = ADDON_PATH . "qingdongams" . DS . 'config' . DS . "menu.php";
  67. if (is_file($config_file)) {
  68. $newMenu = include $config_file;
  69. }
  70. $oldMenu = AuthRule::where('name', 'like', "qingdongams%")->select();
  71. $oldMenu = array_column($oldMenu, null, 'name');
  72. return ['new' => $newMenu, 'old' => $oldMenu];
  73. }
  74. private static function menuCreateOrUpdate($newMenu, $oldMenu, $parent = 0)
  75. {
  76. if (!is_numeric($parent)) {
  77. $parentRule = AuthRule::getByName($parent);
  78. $pid = $parentRule ? $parentRule['id'] : 0;
  79. } else {
  80. $pid = $parent;
  81. }
  82. $allow = array_flip(['file', 'name', 'title', 'icon', 'condition', 'remark', 'ismenu', 'weigh']);
  83. foreach ($newMenu as $k => $v) {
  84. $hasChild = isset($v['sublist']) && $v['sublist'] ? true : false;
  85. $data = array_intersect_key($v, $allow);
  86. $data['ismenu'] = isset($data['ismenu']) ? $data['ismenu'] : ($hasChild ? 1 : 0);
  87. $data['icon'] = isset($data['icon']) ? $data['icon'] : ($hasChild ? 'fa fa-list' : 'fa fa-circle-o');
  88. $data['pid'] = $pid;
  89. $data['status'] = 'normal';
  90. try {
  91. if (!isset($oldMenu[$data['name']])) {
  92. $menu = AuthRule::create($data);
  93. } else {
  94. $menu = $oldMenu[$data['name']];
  95. }
  96. if ($hasChild) {
  97. self::menuCreateOrUpdate($v['sublist'], $oldMenu, $menu['id']);
  98. }
  99. } catch (PDOException $e) {
  100. throw new Exception($e->getMessage());
  101. }
  102. }
  103. }
  104. /**
  105. * 应用初始化
  106. */
  107. public function appInit()
  108. {
  109. // 公共方法
  110. require_once __DIR__ . '/helper.php';
  111. if(!class_exists("\dingding\TopSdk")){
  112. \think\Loader::addNamespace('dingding', ADDON_PATH . 'qingdongams' . DS . 'library' . DS . 'dingding' . DS);
  113. }
  114. if(!class_exists("\WeWork\App")){
  115. \think\Loader::addNamespace('WeWork', ADDON_PATH . 'qingdongams' . DS . 'library' . DS . 'wework' . DS);
  116. }
  117. if(!class_exists("\PhpOffice\PhpWord\PhpWord")){
  118. \think\Loader::addNamespace('PhpOffice\PhpWord', ADDON_PATH . 'qingdongams' . DS . 'library' . DS . 'PhpWord' . DS);
  119. }
  120. }
  121. }