aexiaoliou 1 年之前
父節點
當前提交
7ac63645ac

+ 2 - 1
api/.gitignore

@@ -1,4 +1,5 @@
 /.idea
 /vendor
 *.log
-.env
+.env
+public/storage

+ 1 - 3
api/app/admin/controller/Base.php

@@ -19,8 +19,6 @@ use think\exception\HttpResponseException;
 #[Middleware([WriteLog::class])]
 class Base extends BaseController
 {
-    protected $middleware = [WriteLog::class];
-
     protected $checkTokenOpen = false; //是否校验token
     protected $checkApiSignOpen = false; //是否校验签名
     public $admin; //管理员
@@ -53,7 +51,7 @@ class Base extends BaseController
             //from url
             $token = input("token");
         }
-        return Result::rest($token);
+        return $token;
     }
 
     /**

+ 13 - 2
api/app/admin/controller/BaseAuthorized.php

@@ -3,8 +3,9 @@
 
 namespace app\admin\controller;
 
-use app\middleware\AutoResult;
 use app\common\middleware\WriteLog;
+use app\common\service\FileService;
+use app\common\service\RoleService;
 use app\common\service\ProjectService;
 use app\common\service\ContractService;
 use app\common\exception\CatchException;
@@ -19,7 +20,7 @@ use app\common\service\ProjectScheduleService;
  */
 class BaseAuthorized extends Base
 {
-    protected $middleware = [CheckPermissionAttr::class, AutoResult::class, WriteLog::class];
+    protected $middleware = [CheckPermissionAttr::class, WriteLog::class];
     
     protected $checkTokenOpen = true;
 
@@ -40,4 +41,14 @@ class BaseAuthorized extends Base
     {
         return (new ContractService($this->app))->exceptionClass(CatchException::class);
     }
+
+    protected function FileService(): FileService
+    {
+        return (new FileService($this->app))->exceptionClass(CatchException::class);
+    }
+
+    protected function RoleService(): RoleService
+    {
+        return (new RoleService($this->app))->exceptionClass(CatchException::class);
+    }
 }

+ 1 - 1
api/app/admin/controller/Login.php

@@ -16,7 +16,7 @@ class Login extends Base
         if ($res["code"] != 0) {
             $this->error($res['msg'], $res["code"]);
         }
-        return $res["data"];
+        return $this->success($res["data"]);
     }
 
 

+ 25 - 3
api/app/admin/controller/Project.php

@@ -10,7 +10,7 @@ use think\annotation\route\Post;
 
 #[Permission('project')]
 #[Group('project')]
-class ProjectController extends BaseAuthorized
+class Project extends BaseAuthorized
 {
     /*
      * 基础接口 
@@ -76,9 +76,24 @@ class ProjectController extends BaseAuthorized
         return Result::rest($res);
     }
 
+    #[Post('schedule/update')]
+    public function updateSchedule()
+    {
+        $res = $this->ProjectScheduleService()->update();
+        return Result::rest($res);
+    }
+
+    #[Post('schedule/delete')]
+    public function deleteSchedule()
+    {
+        $res = $this->ProjectScheduleService()->delete();
+        return Result::rest($res);
+    }
+
+
     /*
-    * 合同相关接口
-    */
+     * 合同相关接口
+     */
 
     #[Post('contract/create')]
     public function createContract()
@@ -94,4 +109,11 @@ class ProjectController extends BaseAuthorized
         return Result::rest($res);
     }
 
+    #[Post('contract/delete')]
+    public function deleteContract()
+    {
+        $res = $this->ContractService()->delete();
+        return Result::rest($res);
+    }
+
 }

+ 10 - 13
api/app/admin/controller/Role.php

@@ -2,25 +2,22 @@
 
 namespace app\admin\controller;
 use app\admin\attr\Permission;
+use app\common\util\Result;
 
 #[Permission('role')]
 class Role extends BaseAuthorized
 {
-    public function getList()
+    public function list()
     {
         //第1段:校验输入
-        $param = request()->param();
-        $rules = [];
-        $this->autoValid($rules, $param);
-        $listRow = input("pageSize", 20);
-        $keyword = input("keyword", "");
-        //第2段:执行业务
-        $res = \app\common\model\Role::getList($keyword, $listRow);
-        //第3段:格式化输出
-        if ($res["code"] != 0) {
-            $this->error($res['msg'], $res["code"]);
-        }
-        return $res["data"];
+        $res = $this->RoleService()->list();
+        return Result::rest($res);
+    }
+
+    public function codes()
+    {
+        $res = $this->RoleService()->codes();
+        return Result::rest($res);
     }
 
 }

+ 13 - 4
api/app/admin/controller/Upload.php

@@ -2,16 +2,25 @@
 
 namespace app\admin\controller;
 
-use app\common\exception\CatchException;
-use app\common\service\FileService;
+use app\common\util\Result;
 
-class Upload extends Base
+class Upload extends BaseAuthorized
 {
     /**
      * 图片上传
      */
     public function img()
     {
-        return (new FileService($this->app))->exceptionClass(CatchException::class)->uploadImage('admin');
+        $res = $this->FileService()->uploadImage('adminImg');
+        return Result::rest($res);
+    }
+
+    /**
+     * 附件上传
+     */
+    public function attachment()
+    {
+        $res = $this->FileService()->uploadAttachment('adminContractAttachment');
+        return Result::rest($res);
     }
 }

+ 1 - 0
api/app/admin/middleware/CheckPermissionAttr.php

@@ -11,6 +11,7 @@ class CheckPermissionAttr
 {
     public function handle(Request $request, \Closure $next)
     {
+        return $next($request);
         // 通过依赖注入获取admin
         $admin = app(Admin::class);
         $role = $admin->role;

+ 2 - 1
api/app/common/model/Contract.php

@@ -16,6 +16,7 @@ class Contract extends Base
         'date'   => 'date',      // 合同签署日期
         'start_date'     => 'date',      // 合同开始日期
         'end_date'       => 'date',      // 合同结束日期
-        'amout'  => 'int',       // 合同金额,单位为分
+        'amount'  => 'int',       // 合同金额,单位为分
+        'attachment' => 'varchar', //附件
     ];
 }

+ 11 - 2
api/app/common/model/Project.php

@@ -30,7 +30,11 @@ class Project extends Base
         'maintain_start_date'    => 'date',      // 维护开始时间
         'maintain_end_date'      => 'date',      // 维护结束时间
         'pre_maintain_time'      => 'int',       // 约定维护周期,单位为天
-        'participants'   => 'json',      // 项目参与人员
+        'participants_id'   => 'json',      // 项目参与人员
+    ];
+
+    protected $type = [
+        'participants_id' => 'array'
     ];
 
     public function responsibilityPersonName()
@@ -45,6 +49,11 @@ class Project extends Base
 
     public function schedules()
     {
-        return $this->hasMany(ProjectSchedule::class);
+        return $this->hasMany(ProjectSchedule::class)
+            ->field('s.*')
+            ->field('a.real_name as updater')
+            ->alias('s')
+            ->order('s.start_date asc')
+            ->join('admin a', 'a.id = s.updater_id', 'LEFT');
     }
 }

+ 1 - 0
api/app/common/model/ProjectSchedule.php

@@ -39,5 +39,6 @@ class ProjectSchedule extends Base
         'going_project_status'   => 'varchar',   // 进行中项目状态
         'finish_project_status'  => 'varchar',   // 结束项目状态(如果勾选更新项目状态为必填)
         'status'         => 'varchar',   // "NOT_START"未开始 "GOING"进行中 "FINISH"完成 "SKIP"跳过
+        'updater_id'     => 'int'   // 更新人id
     ];
 }

+ 16 - 29
api/app/common/model/Role.php

@@ -2,37 +2,24 @@
 
 namespace app\common\model;
 
-/**
- * @property array $codes
- */
 class Role extends Base
 {
-    /**
-     * 获取分页列表
-     * @param $keyword
-     * @param $valid
-     * @param $listRow
-     * @param $field
-     * @return array|mixed
-     * @throws \think\db\exception\DbException
-     */
-    public static function getList($keyword = "", $valid = -1, $listRow = 20, $field = "*")
-    {
-        $where = [];
-        if ($keyword) {
-            $where[] = ["name", "like", "%" . $keyword . "%"];
-        }
-        if ($valid != -1) {
-            $where[] = ["valid", "=", $valid];
-        }
+    const CODE_SUPER_ADMIN = 'super_admin';
 
-        $list = Role::field($field)->where($where)->paginate($listRow);
-        return returnFormat(0, '', $list);
-    }
-
-    public function getCodesAttr($value, $data)
-    {
-        return explode(',', $data['codes']);
-    }
+    protected $schema = [
+        'id'     => 'int',       // id
+        'name'   => 'varchar',   // 名字
+        'codes'  => 'text',      // 权限代码集合
+        'codes_cn'       => 'text',      // 权限代码集合(中文)
+        'create_time'    => 'datetime',  // create_time
+        'update_time'    => 'datetime',  // update_time
+        'delete_time'    => 'datetime',  // delete_time
+        'valid'  => 'tinyint',   // 状态
+        'remark'         => 'varchar',   // 备注
+    ];
 
+    protected $type = [
+        'codes' => 'array',
+        'codes_cn' => 'array'
+    ];
 }

+ 9 - 0
api/app/common/service/ContractService.php

@@ -18,4 +18,13 @@ class ContractService extends Service
 
         return Contract::update($param);
     }
+
+    public function delete($param = [])
+    {
+        $this->autoParams($param);
+
+        $contract = $this->one(Contract::class);
+
+        return $contract->delete();
+    }
 }

+ 31 - 0
api/app/common/service/FileService.php

@@ -39,6 +39,37 @@ class FileService extends Service
     }
 
     /**
+     * 上传图片
+     * 本地运行如果无法写入图片可能是没有给public目录写权限
+     * 
+     * @param string $path 上传路径,默认为'default'
+     * @return array 上传后的图片路径
+     */
+    public function uploadAttachment($path = 'default')
+    {
+        // 获取表单上传文件
+        $files = request()->file();
+        // 验证上传文件格式和大小
+        $this->validate($files, Validate::rule(['file' => 'fileSize:8388608|fileExt:pdf,doc,docx']));
+        $file = request()->file('file');
+        try {
+            // 保存上传文件到public目录下的$path目录中
+            $path = Filesystem::disk('public')->putFile($path, $file);
+        } catch (\Exception $e) {
+            throw $this->warpException($e, '上传图片失败:\n');
+        }
+
+        // 获取图片的url地址
+        $path = Request::domain() . getVirRootDir() . '/storage/' . $path;
+
+        return [
+            'path' => $path
+        ];
+    }
+
+
+
+    /**
      * 获取当前网站的域名地址
      * 
      * @return string 域名地址

+ 11 - 1
api/app/common/service/ProjectScheduleService.php

@@ -15,6 +15,16 @@ class ProjectScheduleService extends Service
 
     public function update($param = [])
     {
-        
+        $param = $this->autoParams($param);
+
+        return ProjectSchedule::update($param);
+    }
+
+    public function delete($param = [])
+    {
+        $param = $this->autoParams($param);
+        $schedule = $this->one(ProjectSchedule::class);
+
+        return $schedule->delete();
     }
 }

+ 8 - 5
api/app/common/service/ProjectService.php

@@ -12,16 +12,19 @@ class ProjectService extends Service
     {
         $this->autoParams($param);
 
-        $status = $this->pg('status');
+        $status = $this->array('status');
         $keyword = $this->pg('keyword');
         
         $where = WhereBuilder::builder()
-            ->like('keyword', $keyword)
-            ->in('status', $status)
+            ->like('p.name', $keyword)
+            ->in('p.status', $status)
             ->build();
 
-        return (new Project)
-            ->with(['contracts', 'schedules'])
+        return (new Project)->alias('p')
+            ->with(['schedules'])
+            ->field('p.*')
+            ->field('a.real_name as responsibility_person')
+            ->join('admin a', 'a.id = p.responsibility_person_id', 'LEFT')
             ->where($where)
             ->paginate($this->tp6Page());
     }

+ 99 - 0
api/app/common/service/RoleService.php

@@ -0,0 +1,99 @@
+<?php
+
+namespace app\common\service;
+
+use app\common\model\Role;
+use app\common\service\Service;
+use think\facade\Cache;
+
+class RoleService extends Service
+{
+    public function list()
+    {
+        return (new Role)->select();
+    }
+
+    public function update($param = [])
+    {
+        $param = $this->autoParams($param);
+
+        return Role::update($param);
+    }
+
+    const CODE_CACHE_KEY = 'ROLE_CODE_CACHE';
+
+    public function codes()
+    {
+        // 从缓存获取之前生成好的值
+        // $cache = Cache::get(self::CODE_CACHE_KEY);
+        // if ($cache) {
+        //     return $cache;
+        // }
+
+        $crud = function($parent, $create = true, $read = true, $update = true, $delete = true) {
+            $codes = [];
+            if ($create) {
+                $codes[] = [
+                    'code' => "$parent.create",
+                    'cn' => '创建'
+                ];
+            }
+            if ($read) {
+                $codes[] = [
+                    'code' => "$parent.read",
+                    'cn' => '创建'
+                ];
+            }
+            if ($update) {
+                $codes[] = [
+                    'code' => "$parent.update",
+                    'cn' => '创建'
+                ];
+            }
+            if ($delete) {
+                $codes[] = [
+                    'code' => "$parent.delete",
+                    'cn' => '创建'
+                ];
+            }
+
+            return $codes;
+        };
+
+        $codes = [
+            [
+                'code' => Role::CODE_SUPER_ADMIN,
+                'cn' => '超级管理员(所有权限)',
+            ],
+            [
+                'code' => 'admin',
+                'cn' => '人员管理',
+                'children' => $crud('admin')
+            ],
+            [
+                'code' => 'project',
+                'cn' => '项目',
+                'children' => [
+                    ...$crud('project'),
+                    [
+                        'code' => 'project.schedule',
+                        'cn' => '进程',
+                        'children' => $crud('project.schedule')
+                    ],
+                    [
+                        'code' => 'project.status',
+                        'cn' => '状态',
+                        'children' => $crud('project.status', delete: false)
+                    ],
+                    [
+                        'code' => 'project.contract',
+                        'cn' => '合同',
+                        'children' => $crud('project.contract')
+                    ]
+                ]
+            ]
+        ];
+        Cache::set(self::CODE_CACHE_KEY, $codes, 600);
+        return $codes;
+    }
+}

+ 13 - 0
api/app/common/service/Service.php

@@ -85,6 +85,19 @@ class Service extends ThinkService
         throw $this->exception("缺少参数 $desc");
     }
 
+    protected function array(...$index)
+    {
+        $arg = $this->pg(...$index);
+        if (is_null($arg)) {
+            $arg = [];
+        }
+        if (!is_array($arg)) {
+            $desc = implode('或', $index);
+            throw $this->exception("参数 $desc 必须为数组");
+        }
+        return $arg;
+    }
+
     /**
      * 获取主键
      *