1
YM
2025-04-10 c4782840b8893dcc0bcf262153374237f40f6b9e
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
+-----------------------------------------------------------------------------------------------
* GouGuOPEN [ 左手研发,右手开源,未来可期!]
+-----------------------------------------------------------------------------------------------
* @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
+-----------------------------------------------------------------------------------------------
* @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
+-----------------------------------------------------------------------------------------------
* @Author 勾股工作室 <hdm58@qq.com>
+-----------------------------------------------------------------------------------------------
*/
 
declare (strict_types = 1);
 
namespace app\home\controller;
 
use app\base\BaseController;
use app\home\validate\ModuleCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
 
class Module extends BaseController
{
    public function index()
    {
        if (request()->isAjax()) {
            $module = Db::name('AdminModule')->select();
            return to_assign(0, '', $module);
        } else {
            $sys_module = Db::name('AdminModule')->select()->toArray();
            View::assign('sys_module', $sys_module);
            return view();
        }
    }
    
    //新增/编辑模块
    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            if($this->uid!=1){
                return to_assign(1,'只有系统超级管理员才有权限新增或编辑模块!');
            }
            if (!empty($param['id']) && $param['id'] > 0) {
                try {
                    validate(ModuleCheck::class)->scene('edit')->check($param);
                } catch (ValidateException $e) {
                    // 验证失败 输出错误信息
                    return to_assign(1, $e->getError());
                }
                Db::name('AdminModule')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);
                add_log('edit', $param['id'], $param);
            } else {
                try {
                    validate(ModuleCheck::class)->scene('add')->check($param);
                } catch (ValidateException $e) {
                    // 验证失败 输出错误信息
                    return to_assign(1, $e->getError());
                }
                $mid = Db::name('AdminModule')->strict(false)->field(true)->insertGetId($param);
                add_log('add', $mid, $param);
            }
            return to_assign();
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            $module=[];
            if ($id > 0) {
                $module = Db::name('AdminModule')->where(['id' => $id])->find();
            }
            View::assign('id', $id);
            View::assign('module', $module);
            return view();
        }
    }
    
    //安装模块
    public function install()
    {
        if($this->uid!=1){
            return to_assign(1,'只有系统超级管理员才有权限安装模块!');
        }
        $param = get_params();
        $name = $param['name'];
        $data = curl_post('https://www.gougucms.com/home/get_module/module',['name'=>$name]);
        $json_data = json_decode($data, true);
        if($json_data['code'] == 1){
            return to_assign(1,$json_data['msg']);
        }
        $detail = $json_data['data'];
        $rule = unserialize($detail['rule']);
        if(empty($rule)){
            return to_assign(1,'找不到该模块的信息');
        }
        $prefix = get_config('database.connections.mysql.prefix');
        
        $insert = [];
        $insert['title'] = $detail['title'];
        $insert['name'] = $detail['name'];
        $insert['type'] = $detail['type'];
        $insert['sourse'] = $detail['sourse'];
        $insert['create_time'] = time();
        try {
            validate(ModuleCheck::class)->scene('add')->check($insert);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return to_assign(1, $e->getError());
        }
        //sql语句
        $sql_file = CMS_ROOT . '/app/'.$name.'/config/install.sql';
        $sql_array = [];
        if(file_exists($sql_file)){
            $sql = file_get_contents($sql_file);
            $sql_array = preg_split("/;[\r\n]+/", str_replace("oa_", $prefix, $sql));
        }    
        //var_dump($sql_array);exit;
        Db::startTrans();
        try {            
            // 导入sql数据并创建表
            if(!empty($sql_array)){
                foreach ($sql_array as $k => $v) {
                    if (!empty($v)) {
                        Db::execute($v);
                    }
                }
            }            
            //如果安装过该模块,删除原来的菜单信息
            Db::name('AdminRule')->where('module',$name)->delete();
            $sort = Db::name('AdminRule')->where('pid',0)->max('sort');
            $this->add_rule($rule,0,$sort+1);            
            $mid = Db::name('AdminModule')->strict(false)->field(true)->insertGetId($insert);
            
            Db::commit();
        }
        catch (\Exception $e) {
            //回滚事务
            Db::rollback();
            return to_assign(1,'捕获到异常'.$e->getMessage());
        }
        
        //更新超级管理员的权限节点
        $rules = Db::name('AdminRule')->column('id');
        $admin_rules = implode(',',$rules);
        $res = Db::name('AdminGroup')->strict(false)->where('id',1)->update(['rules'=>$admin_rules,'update_time'=>time()]);        
        if($res!==false){
            // 删除后台节点缓存
            clear_cache('adminRules');
            add_log('install', $mid, $insert);
            return to_assign();
        }
        else{
            return to_assign(1,'操作失败');
        }
    }
    
    //递归插入菜单数据
    protected function add_rule($data, $pid=0,$sort=0)
    {
        foreach($data as $k => $v)
        {
            $rule=[
                'title'  => $v['title'],
                'name'   => $v['name'],
                'src'    => $v['src'],
                'module' => $v['module'],
                'menu'   => $v['menu'],
                'icon'   => $v['icon'],
                'pid'    => $pid,
                'sort'   => $sort,
                'create_time' => time()
            ];
            $new_id = Db::name('AdminRule')->strict(false)->field(true)->insertGetId($rule);
            if(!empty($v['son'] && $new_id)){
                $this->add_rule($v['son'],$new_id);            
            }
        }
    }
 
    //删除
    public function del()
    {
        if($this->uid!=1){
            return to_assign(1,'只有系统超级管理员才有权限删除模块!');
        }
        $param = get_params();
        $module = Db::name('AdminModule')->where('id',$param['id'])->find();
        if($module['type'] == 1){
            return to_assign(1,'系统模块不能删除');
        }
        $param['update_time']= time();
        $res = Db::name('AdminModule')->where('id',$param['id'])->delete();
        if($res!==false){
            add_log('delete', $module['id'], $param);
            return to_assign();
        }
        else{
            return to_assign(1,'操作失败');
        }
    }
 
    //卸载
    public function uninstall()
    {
        if($this->uid!=1){
            return to_assign(1,'只有系统超级管理员才有权限卸载模块!');
        }
        $param = get_params();
        $module = Db::name('AdminModule')->where('name',$param['name'])->find();
        if($module['type'] == 1){
            return to_assign(1,'系统模块不能卸载');
        }
        $param['update_time']= time();
        $res = Db::name('AdminModule')->where('name',$param['name'])->update(['status'=>0]);
        if($res!==false){
            Db::name('AdminRule')->strict(false)->where('module',$module['name'])->update(['status'=>0]);
            // 删除后台节点缓存
            clear_cache('adminRules');
            add_log('uninstall', $module['id'], $param);
            return to_assign();
        }
        else{
            return to_assign(1,'操作失败');
        }
    }
    
    //恢复
    public function recovery()
    {
        if($this->uid!=1){
            return to_assign(1,'只有系统超级管理员才有权限恢复模块!');
        }
        $param = get_params();
        $module = Db::name('AdminModule')->where('name',$param['name'])->find();
        $param['update_time']= time();
        $res = Db::name('AdminModule')->where('name',$param['name'])->update(['status'=>1]);
        if($res!==false){
            Db::name('AdminRule')->strict(false)->where('module',$module['name'])->update(['status'=>1]);
            // 删除后台节点缓存
            clear_cache('adminRules');
            add_log('recovery', $module['id'], $param);
            return to_assign();
        }
        else{
            return to_assign(1,'操作失败');
        }
    }
    
    
    //数据权限列表
    public function data_auth()
    {
        if (request()->isAjax()) {
            $auth = Db::name('DataAuth')->select();
            return to_assign(0, '', $auth);
        } else {
            return view();
        }
    }
    
    //数据权限详情
    public function data_auth_detail()
    {
        $param = get_params();
        if (request()->isPost()) {
            $param['update_time'] = time();
            $res = Db::name('DataAuth')->strict(false)->field(true)->update($param);
            return to_assign();
        } else {
            $detail = Db::name('DataAuth')->where('name',$param['name'])->find();
            View::assign('detail', $detail);
            return view();
        }
    }
    
}