闫增涛
2025-04-14 729e47e5aa77d73914de4353e95f56350611f820
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
<?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\api\controller;
 
use app\api\BaseController;
use app\api\model\EditLog;
use think\facade\Db;
 
class Index extends BaseController
{
    //上传文件
    public function upload()
    {
        if (request()->isPost()) {
            $param = get_params();
            $sourse = 'file';
            if(isset($param['sourse'])){
                $sourse = $param['sourse'];
            }
            if($sourse == 'file' || $sourse == 'tinymce'){
                if(request()->file('file')){
                    $file = request()->file('file');
                }
                else{
                    return to_assign(1, '没有选择上传文件');
                }
            }
            else{
                if (request()->file('editormd-image-file')) {
                    $file = request()->file('editormd-image-file');
                } else {
                    return to_assign(1, '没有选择上传文件');
                }
            }
            // 获取上传文件的hash散列值
            $sha1 = $file->hash('sha1');
            $md5 = $file->hash('md5');
            $rule = [
                'image' => 'jpg,png,jpeg,gif',
                'doc' => 'txt,doc,docx,ppt,pptx,xls,xlsx,pdf',
                'file' => 'zip,gz,7z,rar,tar',
                'video' => 'mpg,mp4,mpeg,avi,wmv,mov,flv,m4v',
                'audio' => 'mp3,wav,wma,flac,midi',
            ];
            $fileExt = $rule['image'] . ',' . $rule['doc'] . ',' . $rule['file'] . ',' . $rule['video'] . ',' . $rule['audio'];
            //1M=1024*1024=1048576字节
            $file_size = get_system_config('system','upload_max_filesize');
            if(!isset($file_size)){
                $file_size=50;
            }
            $fileSize = $file_size * 1024 * 1024;
            if (isset($param['type']) && $param['type']) {
                $fileExt = $rule[$param['type']];
            }
            if (isset($param['size']) && $param['size']) {
                $fileSize = $param['size'];
            }
            $validate = \think\facade\Validate::rule([
                'image' => 'require|fileSize:' . $fileSize . '|fileExt:' . $fileExt,
            ]);
            $file_check['image'] = $file;
            if (!$validate->check($file_check)) {
                return to_assign(1, $validate->getError());
            }
            // 日期前綴
            $dataPath = date('Ym');
            $use = 'thumb';
            $filename = \think\facade\Filesystem::disk('public')->putFile($dataPath, $file, function () use ($md5) {
                return set_salt(5).'_'.$md5;
            });
            if ($filename) {
                //写入到附件表
                $data = [];
                $path = get_config('filesystem.disks.public.url');
                $data['filepath'] = $path . '/' . $filename;
                $data['name'] = $file->getOriginalName();
                $data['mimetype'] = $file->getOriginalMime();
                $data['fileext'] = $file->extension();
                $data['filesize'] = $file->getSize();
                $data['filename'] = $filename;
                $data['sha1'] = $sha1;
                $data['md5'] = $md5;
                $data['module'] = \think\facade\App::initialize()->http->getName();
                $data['action'] = app('request')->action();
                $data['uploadip'] = app('request')->ip();
                $data['create_time'] = time();
                $data['user_id'] = $this->uid;
                if ($data['module'] = 'admin') {
                    //通过后台上传的文件直接审核通过
                    $data['status'] = 1;
                    $data['admin_id'] = $data['user_id'];
                    $data['audit_time'] = time();
                }
                $data['use'] = request()->has('use') ? request()->param('use') : $use; //附件用处
                $res['id'] = Db::name('file')->insertGetId($data);
                $res['filepath'] = $data['filepath'];
                $res['name'] = $data['name'];
                $res['uid'] = $this->uid;
                $res['filename'] = $data['filename'];
                $res['filesize'] = $data['filesize'];
                $res['fileext'] = $data['fileext'];
                add_log('upload', $data['user_id'], $data,'文件');
                if($sourse == 'editormd'){
                    //editormd编辑器上传返回
                    return json(['success'=>1,'message'=>'上传成功','url'=>$data['filepath']]);
                }
                else if($sourse == 'tinymce'){
                    //tinymce编辑器上传返回
                    return json(['success'=>1,'message'=>'上传成功','location'=>$data['filepath']]);
                }
                else{
                    //普通上传返回
                    return to_assign(0, '上传成功', $res);
                }            
             }
             else {
                return to_assign(1, '上传失败,请重试');
             }
        }
        else{
            return to_assign(1, '非法请求');
        } 
    }
    
    //执行分块上传的控制器方法
    public function chunkUpload() {
        if ($this->request->isPost()) {
            //执行分块上传流程
            $data = $this->request->post();
            //判断是否是分块上传
            if ($data['type'] === 'chunk') {
                $file = request()->file('file');
                
                $rule = [
                    'image' => 'jpg,png,jpeg,gif',
                    'doc' => 'txt,doc,docx,ppt,pptx,xls,xlsx,pdf',
                    'file' => 'zip,gz,7z,rar,tar',
                    'video' => 'mpg,mp4,mpeg,avi,wmv,mov,flv,m4v',
                    'audio' => 'mp3,wav,wma,flac,midi',
                ];
                $fileExt = $rule['image'] . ',' . $rule['doc'] . ',' . $rule['file'] . ',' . $rule['video'] . ',' . $rule['audio'];
                //1M=1024*1024=1048576字节
                $file_size = get_system_config('system','upload_max_filesize');
                if(!isset($file_size)){
                    $file_size=50;
                }
                $fileSize = $file_size * 1024 * 1024;
                $validate = \think\facade\Validate::rule([
                    'image' => 'require|fileSize:' . $fileSize . '|fileExt:' . $fileExt,
                ]);
                $file_check['image'] = $file;
                if (!$validate->check($file_check)) {
                    return to_assign(1, $validate->getError());
                }
                
                //获取对应的上传配置
                $fs = \think\facade\Filesystem::disk('public');
                $ext = $file->extension();
                $chunkPath = $data['file_id'].'/'.$file->md5().($ext ? '.'.$ext : '');
                //存储分片文件到指定路径
                $savename = $fs->putFileAs( 'chunk', $file,$chunkPath);
                if (!$savename) {
                    return json([
                        'code' => 1,
                        'msg' => '上传失败',
                        'data' => [],
                    ]);
                }
                if (!$data['is_end']) {
                    $filepath = '';
                } else {
                    //合并块文件
                    $fileUrl = '';
                    $chunkSaveDir = \think\facade\Filesystem::getDiskConfig('public');
                    $smallChunkDir = $chunkSaveDir['root'].'/chunk/'.$data['file_id'];
                    //获取已存储的属于源文件的所有分块文件 进行合并
                    if ($handle = opendir($smallChunkDir)) {
                        $chunkList = [];
                        $modifyTime = [];
                        while (false !== ($file = readdir($handle))) {
                            if ($file != "." && $file != "..") {
                                $temp['path'] = $smallChunkDir.'/'.$file;
                                $temp['modify'] = filemtime($smallChunkDir.'/'.$file);
                                $chunkList[] = $temp;
                                $modifyTime[] = $temp['modify'];
                            }
                        }
                        //对分块文件进行排序
                        array_multisort($modifyTime,SORT_ASC,$chunkList);
                        $saveDir = \think\facade\Filesystem::getDiskConfig('public');
                        $saveName = md5($data['file_id'].$data['file_name']).'.'.$data['file_extension'];
                        $newPath = $saveDir['root'].'/'.date('Ym').'/'.$saveName;
                        if (!file_exists($saveDir['root'].'/'.date('Ym'))) {
                            mkdir($saveDir['root'].'/'.date('Ym'),0777,true);
                        }
                        $newFileHandle = fopen($newPath,'a+b');
                        foreach ($chunkList as $item) {
                            fwrite($newFileHandle,file_get_contents($item['path']));
                            unlink($item['path']);
                        }
                        rmdir($smallChunkDir);
                        //将合并后的文件存储到指定路径
                        $fileUrl = $saveDir['url'].'/'.date('Ym').'/'.$saveName;
                        fclose($newFileHandle);
                        closedir($handle);
                    } else {
                        return json([
                            'code' => 1,
                            'msg' => '目录:'.$chunkSaveDir['root'].'/chunk/'.$data['file_id'].'不存在',
                            'data' => [],
                        ]);
                    }
                    $filepath = $fileUrl;
                }
                $res=[];
                //合并流程结束
                if ($filepath!='') {
                    $fileinfo = [];
                    $fileinfo['filepath'] = $filepath;
                    $fileinfo['name'] = $data['file_name'];
                    $fileinfo['fileext'] = $data['file_extension'];
                    $fileinfo['filesize'] = $data['file_size'];
                    $fileinfo['filename'] = date('Ym').'/'.$saveName;
                    $fileinfo['sha1'] = $data['file_id'];
                    $fileinfo['md5'] = $data['file_id'];
                    $fileinfo['module'] = \think\facade\App::initialize()->http->getName();
                    $fileinfo['action'] = app('request')->action();
                    $fileinfo['uploadip'] = app('request')->ip();
                    $fileinfo['create_time'] = time();
                    $fileinfo['user_id'] = get_login_admin('id') ? get_login_admin('id') : 0;
                    if ($fileinfo['module'] = 'admin') {
                        //通过后台上传的文件直接审核通过
                        $fileinfo['status'] = 1;
                        $fileinfo['admin_id'] = $fileinfo['user_id'];
                        $fileinfo['audit_time'] = time();
                    }
                    $fileinfo['use'] = 'big';
                    $res['id'] = Db::name('file')->insertGetId($fileinfo);
                    $res['filepath'] = $fileinfo['filepath'];
                    $res['name'] = $fileinfo['name'];
                    $res['filename'] = $fileinfo['filename'];
                    $res['filesize'] = $fileinfo['filesize'];
                    $res['fileext'] = $fileinfo['fileext'];
                    add_log('upload', $fileinfo['user_id'], $fileinfo);
                } 
                return to_assign(0, '上传成功', $res);
            }
        }
        else{
            return to_assign(1, '非法请求', $res);
        }
    }
 
    //取消上传,删除临时文件
    public function clearChunk() {
        if ($this->request->isPost()) {
            $param = get_params();
            $saveDir = \think\facade\Filesystem::getDiskConfig('public');
            $smallChunkDir = $saveDir['root'].'/chunk/'.$param['file_id'];
            if(!is_dir($smallChunkDir)){
                return to_assign(0, '上传的临时文件已删除');
            }
            //获取已存储的属于源文件的所有分块文件
            if ($handle = opendir($smallChunkDir)) {
                while (false !== ($file = readdir($handle))) {
                    if ($file != "." && $file != "..") {
                        $temp['path'] = $smallChunkDir.'/'.$file;
                        unlink($temp['path']);
                    }
                }
                rmdir($smallChunkDir);
                closedir($handle);
                return to_assign(0, '已取消上传');
            }
        }
    }
 
    //附件重命名
    public function file_edit()
    {
        $param = get_params();
        if (Db::name('File')->where('id',$param['id'])->update(['name'=>$param['title']]) !== false) {
            add_log('edit', $param['id'], $param,'文件名称');
            return to_assign(0, "操作成功");
        } else {
            return to_assign(1, "操作失败");
        }
    }
 
    //获取编辑记录
    public function load_log()
    {
        $param = get_params();
        $log = new EditLog();
        $list = $log->datalist($param);
        return to_assign(0, '', $list);
    }
 
 
    //清空缓存
    public function cache_clear()
    {
        \think\facade\Cache::clear();
        return to_assign(0, '系统缓存已清空');
    }
 
    // 测试邮件发送
    public function email_test()
    {
        $sender = get_params('email');
        //检查是否邮箱格式
        if (!is_email($sender)) {
            return to_assign(1, '测试邮箱码格式有误');
        }
        $email_config = \think\facade\Db::name('config')->where('name', 'email')->find();
        $config = unserialize($email_config['content']);
        $content = $config['template'];
        //所有项目必须填写
        if (empty($config['smtp']) || empty($config['smtp_port']) || empty($config['smtp_user']) || empty($config['smtp_pwd'])) {
            return to_assign(1, '请完善邮件配置信息');
        }
 
        $send = send_email($sender, '测试邮件', $content);
        if ($send) {
            return to_assign(0, '邮件发送成功');
        } else {
            return to_assign(1, '邮件发送失败');
        }
    }
    
    //获取未读消息
    public function get_msg()
    {
        $msg_map[] = ['to_uid', '=', $this->uid];
        $msg_map[] = ['read_time', '=', 0];
        $msg_map[] = ['delete_time', '=', 0];
        $msg_count = Db::name('Msg')->where($msg_map)->count();
        return to_assign(0, 'ok', $msg_count);
    }
 
    //获取部门
    public function get_department()
    {
        $department = get_department();
        return to_assign(0, '', $department);
    }
 
    //获取部门树形节点列表,用于tree前端组件
    public function get_department_tree()
    {
        $department = get_department();
        $list = get_tree($department);
        $data['trees'] = $list;
        return json($data);
    }
    
    //获取下属部门树形节点列表,用于tree前端组件
    public function get_department_tree_sub()
    {
        if($this->uid==1){
            $department = get_department();
        }
        else{
            $dids = get_leader_departments($this->uid);
            $department = Db::name('Department')->order('sort desc,id asc')->where([['status','=',1],['id','in',$dids]])->select()->toArray();
        }
        $list = get_tree($department,$department[0]['pid']);
        $data['trees'] = $list;
        return json($data);
    }
    
    //获取部门树形节点列表,用于X-select前端组件
    public function get_department_select()
    {
        $keyword = get_params('keyword');
        $selected = [];
        if(!empty($keyword)){
            $selected = explode(",",$keyword);
        }        
        $department = get_department();
        $list = get_select_tree($department, 0,0,$selected);
        return to_assign(0, '',$list);
    }
 
    //获取所有员工,did>0时时获取部门员工,用于picker签单组件
    public function get_employee($did = 0)
    {
        $where=[];
        $whereOr=[];
        if (!empty($did)) {
            $admin_array = Db::name('DepartmentAdmin')->where('department_id',$did)->column('admin_id');
            $map1=[
                ['a.id','in',$admin_array],
            ];
            $map2=[
                ['a.did', '=', $did],
            ];
            $whereOr =[$map1,$map2];
        }        
        $where[] = ['a.id', '>', 1];
        $where[] = ['a.status', '=', 1];
        $employee = Db::name('admin')
            ->field('a.id,a.did,a.position_id,a.mobile,a.name,a.nickname,a.sex,a.status,a.thumb,a.username,d.title as department')
            ->alias('a')
            ->join('Position p', 'p.id = a.position_id','left')
            ->join('Department d', 'a.did = d.id','left')
            ->where($where)
            ->where(function ($query) use($whereOr) {
                if (!empty($whereOr)){
                    $query->whereOr($whereOr);
                }
            })
            ->group('a.id')
            ->order('a.id desc')
            ->select();
        return to_assign(0, '', $employee);
    }
    
   //获取所有下属员工,did>0时时获取部门员工,用于picker签单组件
    public function get_employee_sub($did = 0)
    {
        $where=[];
        $whereOr=[];
        if (!empty($did)) {
            $admin_array = Db::name('DepartmentAdmin')->where('department_id',$did)->column('admin_id');
            $map1=[
                ['a.id','in',$admin_array],
            ];
            $map2=[
                ['a.did', '=', $did],
            ];
            $whereOr =[$map1,$map2];
        }
        else{
            if($this->uid>1){
                $dids = get_leader_departments($this->uid);
                $where[] = ['a.did', 'in', $dids];
            }
        }
        $where[] = ['a.id', '>', 1];
        $where[] = ['a.status', '=', 1];
        $employee = Db::name('admin')
            ->field('a.id,a.did,a.position_id,a.mobile,a.name,a.nickname,a.sex,a.status,a.thumb,a.username,d.title as department')
            ->alias('a')
            ->join('Position p', 'p.id = a.position_id','left')
            ->join('Department d', 'a.did = d.id','left')
            ->where($where)
            ->where(function ($query) use($whereOr) {
                if (!empty($whereOr)){
                    $query->whereOr($whereOr);
                }
            })
            ->group('a.id')
            ->order('a.id desc')
            ->select();
        return to_assign(0, '', $employee);
    }
    
    //获取所有员工
    public function get_personnel()
    {       
        $param = get_params();
        $where[] = ['a.status', '=', 1];
        $where[] = ['a.id', '>', 1];
        if (!empty($param['keywords'])) {
            $where[] = ['a.name', 'like', '%' . $param['keywords'] . '%'];
        }
        if(!empty($param['ids'])){
            //排除某些员工
            $where[] = ['a.id', 'notin', $param['ids']];
        }
        $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
        $list = Db::name('admin')
            ->field('a.id,a.did,a.position_id,a.mobile,a.name,a.nickname,a.sex,a.status,a.thumb,a.username,d.title as department')
            ->alias('a')
            ->join('Department d', 'a.did = d.id')
            ->where($where)
            ->order('a.id desc')
            ->paginate(['list_rows'=> $rows]);
        return table_assign(0, '', $list);
    }    
    
    //获取所有员工,用于X-select前端组件,did>0时时获取部门员工
    public function get_employee_select($did=0)
    {
        $keyword = get_params('keyword');
        $selected = [];
        if(!empty($keyword)){
            $selected = explode(",",$keyword);
        }
        if($did == 0){
            $employee = Db::name('admin')->field('id as value,name')->where(['status' => 1])->select()->toArray();        
        }
        else{
            $employee = get_department_employee($did);
        }
        $list=[];
        foreach($employee as $k => $v){
            $select = '';
            if(in_array($v['id'],$selected)){
                $select = 'selected';
            }
            $list[]=[
                'value'=>$v['id'],
                'name'=>$v['name'],
                'selected'=>$select
            ];
        }
        return to_assign(0, '', $list);
    }    
 
 
    //获取某部门的负责人
    public function get_department_leader($uid=0,$pid=0)
    {
        $leaders = get_department_leader($uid,$pid);
        return to_assign(0, '', $leaders);
    }
 
    //获取职位
    public function get_position()
    {
        $position = Db::name('Position')->field('id,title')->where([['status', '=', 1], ['id', '>', 1]])->select();
        return to_assign(0, '', $position);
    }
    
    //获取消息模板
    public function get_template()
    {
        $param = get_params();
        if (!empty($param['keywords'])) {
            $where[] = ['title', 'like', '%' . $param['keywords'] . '%'];
        }
        $where[] = ['status', '=', 1];
        $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];        
        $list = Db::name('Template')->field('id,title')->where($where)->paginate(['list_rows'=> $rows]);;
        return table_assign(0, '', $list);
    }
    
    //读取报销类型
    function get_expense_cate()
    {
        $cate = get_base_data('ExpenseCate');
        return to_assign(0, '', $cate);
    }
 
    //读取费用类型
    function get_cost_cate()
    {
        $cate = get_base_data('CostCate');
        return to_assign(0, '', $cate);
    }
 
    //读取印章类型
    function get_seal_cate()
    {
        $cate = get_base_data('SealCate');
        return to_assign(0, '', $cate);
    }
 
    //读取车辆类型
    function get_car_cate()
    {
        $cate = get_base_data('CarCate');
        return to_assign(0, '', $cate);
    }
 
    //读取企业主体
    function get_subject()
    {
        $subject = get_base_data('Subject');
        return to_assign(0, '', $subject);
    }
 
    //读取行业类型
    function get_industry()
    {
        $industry = get_base_data('Industry');
        return to_assign(0, '', $industry);
    }
 
    //读取服务类型
    function get_services()
    {
        $services = get_base_data('Services');
        return to_assign(0, '', $services);
    }
    
    //获取工作类型列表
    public function get_work_cate()
    {
        $cate = get_base_data('WorkCate');
        return to_assign(0, '', $cate);
    }
}