From 0969fb4aafa06b2873156f348dd66aecac1aef7f Mon Sep 17 00:00:00 2001 From: liu <1873441552@qq.com> Date: Wed, 10 Apr 2024 10:26:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8F=90=E4=BA=A4=E5=AE=A1?= =?UTF-8?q?=E6=A0=B8=E6=9F=A5=E8=AF=A2=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/ApiBaseController.php | 186 ++++++++++++++++++++++++++ app/api/controller/User.php | 218 +++++++++++++++++++++++++++++++ app/project/model/ProjectLog.php | 2 +- 3 files changed, 405 insertions(+), 1 deletion(-) create mode 100644 app/api/ApiBaseController.php create mode 100644 app/api/controller/User.php diff --git a/app/api/ApiBaseController.php b/app/api/ApiBaseController.php new file mode 100644 index 0000000..2903c95 --- /dev/null +++ b/app/api/ApiBaseController.php @@ -0,0 +1,186 @@ + 'gouguoa', + 'iss' => 'www.gougucms.com', //签发者 可选 + 'aud' => 'gouguoa', //接收该JWT的一方,可选 + 'exptime' => 7200, //过期时间,这里设置2个小时 + ]; + + /** + * 构造方法 + * @access public + * @param App $app 应用对象 + */ + public function __construct(App $app) + { + $this->app = $app; + $this->request = $this->app->request; + $this->module = strtolower(app('http')->getName()); + $this->controller = strtolower($this->request->controller()); + $this->action = strtolower($this->request->action()); + $this->uid = 0; + $this->did = 0; + $this->jwt_conf = get_system_config('token'); + // 控制器初始化 + $this->initialize(); + } + + // 初始化 + protected function initialize() + { + // 检测权限 +// $this->checkLogin(); + //每页显示数据量 + $this->pageSize = Request::param('page_size', \think\facade\Config::get('app.page_size')); + } + + /** + *验证用户登录 + */ + protected function checkLogin() + { + $session_admin = get_config('app.session_admin'); + if (!Session::has($session_admin)) { + $this->apiError('请先登录'); + } + else{ + $this->uid = Session::get($session_admin); + $login_admin = Db::name('Admin')->where(['id' => $this->uid])->find(); + $this->did = $login_admin['did']; + View::assign('login_admin', $login_admin); + } + } + /** + * Api处理成功结果返回方法 + * @param $message + * @param null $redirect + * @param null $extra + * @return mixed + * @throws ReturnException + */ + protected function apiSuccess($msg = 'success', $data = []) + { + return $this->apiReturn($data, 0, $msg); + } + + + protected function apiOk($data = [],$msg ='success' ): Response + { + return $this->apiReturn($data, 200, $msg); + } + + /** + * Api处理结果失败返回方法 + * @param $error_code + * @param $message + * @param null $redirect + * @param null $extra + * @return mixed + * @throws ReturnException + */ + protected function apiError($msg = 'fail', $data = [], $code = 1) + { + return $this->apiReturn($data, $code, $msg); + } + + /** + * 返回封装后的API数据到客户端 + * @param mixed $data 要返回的数据 + * @param integer $code 返回的code + * @param mixed $msg 提示信息 + * @param string $type 返回数据格式 + * @param array $header 发送的Header信息 + * @return Response + */ + protected function apiReturn($data, int $code = 0, $msg = '', string $type = '', array $header = []): Response + { + $result = [ + 'code' => $code, + 'msg' => $msg, + 'time' => time(), + 'data' => $data, + ]; + + $type = $type ?: 'json'; + $response = Response::create($result, $type)->header($header); + + throw new HttpResponseException($response); + } + + + public function getToken($user_id){ + $time = time(); //当前时间 + $conf = $this->jwt_conf; + $token = [ + 'iss' => $conf['iss'], //签发者 可选 + 'aud' => $conf['aud'], //接收该JWT的一方,可选 + 'iat' => $time, //签发时间 + 'nbf' => $time-1 , //(Not Before):某个时间点后才能访问,比如设置time+30,表示当前时间30秒后才能使用 + 'exp' => $time+$conf['exptime'], //过期时间,这里设置2个小时 + 'data' => [ + //自定义信息,不要定义敏感信息 + 'userid' =>$user_id, + ] + ]; + return JWT::encode($token, $conf['secrect'], 'HS256'); //输出Token 默认'HS256' + } + +} diff --git a/app/api/controller/User.php b/app/api/controller/User.php new file mode 100644 index 0000000..311f8b7 --- /dev/null +++ b/app/api/controller/User.php @@ -0,0 +1,218 @@ + ['except' => ['index','login'] ] + ]; + + /** + * 移动端登录 + * @return void + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function login() + { + $param = get_params(); + if (empty($param['username']) || empty($param['password'])) { + $this->apiError('参数错误'); + } + // 校验用户名密码 + $user = Db::name('Admin')->where(['username' => $param['username']])->find(); + if (empty($user)) { + $this->apiError('帐号或密码错误'); + } + $param['pwd'] = set_password($param['password'], $user['salt']); + if ($param['pwd'] !== $user['pwd']) { + $this->apiError('帐号或密码错误'); + } + if ($user['status'] == -1) { + $this->apiError('该用户禁止登录,请于平台联系'); + } + $data = [ + 'last_login_time' => time(), + 'last_login_ip' => request()->ip(), + 'login_num' => $user['login_num'] + 1, + ]; + $res = Db::name('Admin')->where(['id' => $user['id']])->update($data); + if ($res) { + $token = self::getToken($user['id']); + $this->apiSuccess('登录成功', ['token' => $token,'uid'=>$user['id']]); + } + } + + + /** + * 用户信息 + * @return void + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function userInfo() + { + $uid = JWT_UID; + $data = Db::name('admin') ->alias('a') + ->join('Department d', 'a.did = d.id') + ->where(['a.id' => $uid]) + ->field('a.id,a.did,a.name,a.email,a.mobile,a.sex,a.nickname,a.thumb,a.desc,d.title as department') + ->find(); + $this->apiOk($data); + + } + + + + public function info() + { + $uid = JWT_UID; + $data = Db::name('admin') ->alias('a') + ->join('Department d', 'a.did = d.id') + ->where(['a.id' => $uid]) + ->field('a.id,a.did,a.name,a.email,a.mobile,a.sex,a.nickname,a.thumb,a.desc,d.title as department') + ->find(); + $this->apiOk($data); + + } + + //添加任务 + public function add() + { + $param = get_params(); + if (request()->isPost()) { + if (isset($param['end_time'])) { + $param['end_time'] = strtotime(urldecode($param['end_time'])); + } + if (!empty($param['id']) && $param['id'] > 0) { + $task = (new TaskList())->detail($param['id']); + try { + validate(TaskCheck::class)->scene('edit')->check($param); + } catch (ValidateException $e) { + // 验证失败 输出错误信息 + $this->apiError( $e->getError()); + } + if (isset($param['flow_status'])) { + if ($param['flow_status'] == 3) { + $param['over_time'] = time(); + $param['done_ratio'] = 100; + if($task['before_task']>0){ + $flow_status = Db::name('ProjectTask')->where(['id' => $task['before_task']])->value('flow_status'); + if($flow_status !=3){ + $this->apiError('前置任务未完成,不能设置已完成'); + } + } + } else { + $param['over_time'] = 0; + $param['done_ratio'] = 0; + } + } + if(isset($param['before_task'])){ + $after_task_array = admin_after_task_son($param['id']); + //包括自己在内 + $after_task_array[] = $param['id']; + if (in_array($param['before_task'], $after_task_array)) { + $this->apiError('前置任务不能是该任务本身或其后置任务'); + } + } + $param['update_time'] = time(); + $res = TaskList::where('id', $param['id'])->strict(false)->save($param); + if ($res) { + add_log('edit', $param['id'], $param); + add_project_log(JWT_UID,'task',$param, $task); + } + $this->apiOk(); + } else { + try { + validate(TaskCheck::class)->scene('add')->check($param); + } catch (ValidateException $e) { + // 验证失败 输出错误信息 + $this->apiError( $e->getError()); + } + $param['create_time'] = time(); + $param['admin_id'] = JWT_UID; + $sid = TaskList::strict(false)->field(true)->insertGetId($param); + if ($sid) { + add_log('add', $sid, $param); + $log_data = array( + 'module' => 'task', + 'task_id' => $sid, + 'new_content' => $param['title'], + 'field' => 'new', + 'action' => 'add', + 'admin_id' => JWT_UID, + 'create_time' => time(), + ); + Db::name('ProjectLog')->strict(false)->field(true)->insert($log_data); + //发消息 + //$users = $param['director_uid']; + //sendMessage($users, 21, ['title' => $param['title'],'from_uid' => $this->uid, 'create_time'=>date('Y-m-d H:i:s',time()), 'action_id' => $sid]); + } + $this->apiOk(); + } + } + + + + } + + + + //任务列表 + public function list() + { + $param = get_params(); + $param['uid'] = JWT_UID; + $list = (new TaskList())->list($param); + $data = $list; + if (is_object($data)) { + $data = $data->toArray(); + } + if (!empty($data['total'])) { + $res['count'] = $data['total']; + } else { + $res['count'] = 0; + } + $res['data'] = $data['data']; + $this->apiOk($res); + } + + + + + //详情--->流程? + public function detail() + { + $param = get_params(); + $where = array(); + $where['a.tid'] = $param['tid']; + $where['a.delete_time'] = 0; + $list = Db::name('Schedule') + ->field('a.*,u.name') + ->alias('a') + ->join('Admin u', 'u.id = a.admin_id') + ->order('a.create_time desc') + ->where($where) + ->select()->toArray(); + foreach ($list as $k => $v) { + $list[$k]['start_time'] = empty($v['start_time']) ? '' : date('Y-m-d H:i', $v['start_time']); + $list[$k]['end_time'] = empty($v['end_time']) ? '' : date('H:i', $v['end_time']); + } + + $this->apiOk($list); + } + +} \ No newline at end of file diff --git a/app/project/model/ProjectLog.php b/app/project/model/ProjectLog.php index bb3554b..b5f5b72 100644 --- a/app/project/model/ProjectLog.php +++ b/app/project/model/ProjectLog.php @@ -34,7 +34,7 @@ class ProjectLog extends Model ]], 'task' => [ 'priority' => ['', '低', '中', '高', '紧急'], - 'flow_status' => ['', '未开始', '进行中', '已完成', '已拒绝', '已关闭'], + 'flow_status' => ['', '未开始', '进行中', '已完成', '已拒绝', '已关闭','已提交审核'], 'type' => ['', '需求', '设计', '研发', '缺陷'], 'field_array' => [ 'director_uid' => array('icon' => 'icon-xueshengzhuce', 'title' => '负责人'),