Blame view

addons/antis/Qingdong.php 3.71 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  <?php

  

  namespace addons\qingdong;

  

  use app\admin\model\AuthRule;

  use app\common\library\Menu;

  use think\Addons;

  use think\Exception;

  use think\exception\PDOException;

  

  

  /**

   * 插件

   */

  class Qingdong extends Addons

  {

  

      /**

       * 插件安装方法

       * @return bool

       */

      public function install()

      {

          $menu = self::getMenu();

          Menu::create($menu['new']);

          return true;

      }

  

      /**

       * 插件卸载方法

       * @return bool

       */

      public function uninstall()

      {

          Menu::delete('antis');

          return true;

      }

  

      /**

       * 插件启用方法

       * @return bool

       */

      public function enable()

      {

          Menu::enable('antis');

          return true;

      }

  

      /**

       * 插件禁用方法

       * @return bool

       */

      public function disable()

      {

          Menu::disable('antis');

          return true;

      }

  

      /**

       * 插件更新方法

       */

      public function upgrade()

      {

          $menu = self::getMenu();

          if (method_exists(Menu::class, 'upgrade')) {

              Menu::upgrade('antis', $menu['new']);

          } else {

              self::menuCreateOrUpdate($menu['new'], $menu['old']);

          }

  

          return true;

      }

  

      private static function getMenu()

      {

          $newMenu = [];

          $config_file = ADDON_PATH . "antis" . DS . 'config' . DS . "menu.php";

          if (is_file($config_file)) {

              $newMenu = include $config_file;

          }

          $oldMenu = AuthRule::where('name', 'like', "antis%")->select();

          $oldMenu = array_column($oldMenu, null, 'name');

          return ['new' => $newMenu, 'old' => $oldMenu];

      }

  

      private static function menuCreateOrUpdate($newMenu, $oldMenu, $parent = 0)

      {

          if (!is_numeric($parent)) {

              $parentRule = AuthRule::getByName($parent);

              $pid = $parentRule ? $parentRule['id'] : 0;

          } else {

              $pid = $parent;

          }

          $allow = array_flip(['file', 'name', 'title', 'icon', 'condition', 'remark', 'ismenu', 'weigh']);

          foreach ($newMenu as $k => $v) {

              $hasChild = isset($v['sublist']) && $v['sublist'] ? true : false;

              $data = array_intersect_key($v, $allow);

              $data['ismenu'] = isset($data['ismenu']) ? $data['ismenu'] : ($hasChild ? 1 : 0);

              $data['icon'] = isset($data['icon']) ? $data['icon'] : ($hasChild ? 'fa fa-list' : 'fa fa-circle-o');

              $data['pid'] = $pid;

              $data['status'] = 'normal';

              try {

                  if (!isset($oldMenu[$data['name']])) {

                      $menu = AuthRule::create($data);

                  } else {

                      $menu = $oldMenu[$data['name']];

                  }

                  if ($hasChild) {

                      self::menuCreateOrUpdate($v['sublist'], $oldMenu, $menu['id']);

                  }

              } catch (PDOException $e) {

                  throw new Exception($e->getMessage());

              }

          }

      }

  

      /**

       * 应用初始化

       */

      public function appInit()

      {

          // 公共方法

          require_once __DIR__ . '/helper.php';

          if(!class_exists("\dingding\TopSdk")){

              \think\Loader::addNamespace('dingding', ADDON_PATH . 'antis' . DS . 'library' . DS . 'dingding' . DS);

          }

          if(!class_exists("\WeWork\App")){

              \think\Loader::addNamespace('WeWork', ADDON_PATH . 'antis' . DS . 'library' . DS . 'wework' . DS);

          }

          if(!class_exists("\PhpOffice\PhpWord\PhpWord")){

              \think\Loader::addNamespace('PhpOffice\PhpWord', ADDON_PATH . 'antis' . DS . 'library' . DS . 'PhpWord' . DS);

          }

      }

  }