From 8f6bf9800e878ea4c3d2e593a517684b1e96efb8 Mon Sep 17 00:00:00 2001 From: zhujinyong Date: Wed, 21 Jul 2021 06:44:41 +0800 Subject: [PATCH] * Format fields of response data. --- api/v1/entries/bug.php | 4 +- api/v1/entries/bugs.php | 4 +- api/v1/entries/execution.php | 4 +- api/v1/entries/executions.php | 11 +++- api/v1/entries/product.php | 5 +- api/v1/entries/products.php | 17 +++--- api/v1/entries/programs.php | 2 +- api/v1/entries/project.php | 4 +- api/v1/entries/projects.php | 9 ++- api/v1/entries/stories.php | 4 +- api/v1/entries/story.php | 5 +- api/v1/entries/storychange.php | 2 +- api/v1/entries/task.php | 4 +- api/v1/entries/taskassignto.php | 2 +- api/v1/entries/taskfinish.php | 2 +- api/v1/entries/tasks.php | 6 +- api/v1/entries/user.php | 4 +- api/v1/entries/users.php | 4 +- framework/api/entry.class.php | 99 +++++++++++++++++++++++++++++++++ framework/base/router.class.php | 2 + 20 files changed, 155 insertions(+), 39 deletions(-) diff --git a/api/v1/entries/bug.php b/api/v1/entries/bug.php index 13d872ddf4..fb4b676b6b 100644 --- a/api/v1/entries/bug.php +++ b/api/v1/entries/bug.php @@ -15,7 +15,7 @@ class bugEntry extends entry $data = $this->getData(); $bug = $data->data->bug; - $this->send(200, $bug); + $this->send(200, $this->format($bug, 'activatedDate:time,openedDate:time,assignedDate:time,resolvedDate:time,closedDate:time,lastEditedDate:time')); } public function put($bugID) @@ -35,7 +35,7 @@ class bugEntry extends entry if(!isset($data->status)) return $this->sendError(400, 'error'); $bug = $this->bug->getByID($bugID); - $this->send(200, $bug); + $this->send(200, $this->format($bug, 'activatedDate:time,openedDate:time,assignedDate:time,resolvedDate:time,closedDate:time,lastEditedDate:time')); } public function delete($bugID) diff --git a/api/v1/entries/bugs.php b/api/v1/entries/bugs.php index aa3d101122..70a31c67b2 100644 --- a/api/v1/entries/bugs.php +++ b/api/v1/entries/bugs.php @@ -21,7 +21,7 @@ class bugsEntry extends entry $result = array(); foreach($bugs as $bug) { - $result[] = $bug; + $result[] = $this->format($bug, 'activatedDate:time,openedDate:time,assignedDate:time,resolvedDate:time,closedDate:time,lastEditedDate:time'); } return $this->send(200, array('page' => $pager->pageID, 'total' => $pager->recTotal, 'limit' => $pager->recPerPage, 'bugs' => $result)); } @@ -51,6 +51,6 @@ class bugsEntry extends entry $bug = $this->loadModel('bug')->getByID($data->id); - $this->send(200, $bug); + $this->send(200, $this->format($bug, 'activatedDate:time,openedDate:time,assignedDate:time,resolvedDate:time,closedDate:time,lastEditedDate:time')); } } diff --git a/api/v1/entries/execution.php b/api/v1/entries/execution.php index a382de3ad1..cbbf55da09 100644 --- a/api/v1/entries/execution.php +++ b/api/v1/entries/execution.php @@ -15,7 +15,7 @@ class executionEntry extends Entry $data = $this->getData(); $execution = $data->data->execution; - $this->send(200, $execution); + $this->send(200, $this->format($execution, 'openedDate:time,lastEditedDate:time,closedDate:time,canceledDate:time')); } public function put($executionID) @@ -36,7 +36,7 @@ class executionEntry extends Entry if(!isset($data->result)) return $this->sendError(400, 'error'); $execution = $this->execution->getByID($executionID); - $this->sendSuccess(200, $execution); + $this->send(200, $this->format($execution, 'openedDate:time,lastEditedDate:time,closedDate:time,canceledDate:time')); } public function delete($executionID) diff --git a/api/v1/entries/executions.php b/api/v1/entries/executions.php index d5a26aac08..03660142bb 100644 --- a/api/v1/entries/executions.php +++ b/api/v1/entries/executions.php @@ -16,8 +16,13 @@ class executionsEntry extends entry if(isset($data->status) and $data->status == 'success') { - $pager = $data->data->pager; - return $this->send(200, array('page' => $pager->pageID, 'total' => $pager->recTotal, 'limit' => $pager->recPerPage, 'executions' => $data->data->executionStats)); + $pager = $data->data->pager; + $result = array(); + foreach($data->data->executionStats as $execution) + { + $result[] = $this->format($execution, 'openedDate:time,lastEditedDate:time,closedDate:time,canceledDate:time'); + } + return $this->send(200, array('page' => $pager->pageID, 'total' => $pager->recTotal, 'limit' => $pager->recPerPage, 'executions' => $result)); } if(isset($data->status) and $data->status == 'fail') return $this->sendError(400, $data->message); @@ -46,6 +51,6 @@ class executionsEntry extends entry $execution = $this->loadModel('execution')->getByID($data->id); - $this->send(200, $execution); + $this->send(201, $this->format($execution, 'openedDate:time,lastEditedDate:time,closedDate:time,canceledDate:time')); } } diff --git a/api/v1/entries/product.php b/api/v1/entries/product.php index 578f033eb3..69e8704fda 100644 --- a/api/v1/entries/product.php +++ b/api/v1/entries/product.php @@ -14,7 +14,7 @@ class productEntry extends Entry $control->view($productID); $data = $this->getData(); - if(isset($data->status) and $data->status == 'success') return $this->send(200, $data->data->product); + if(isset($data->status) and $data->status == 'success') return $this->send(200, $this->format($data->data->product, 'createdDate:time')); if(isset($data->status) and $data->status == 'fail') return $this->sendError(400, $data->message); $this->sendError(400, 'error'); @@ -34,7 +34,8 @@ class productEntry extends Entry $data = $this->getData(); if(isset($data->result) and $data->result == 'fail') return $this->sendError(400, $data->message); - $this->sendSuccess(200, 'success'); + $product = $this->product->getByID($productID); + $this->sendSuccess(200, $this->format($product, 'createdDate:time')); } public function delete($productID) diff --git a/api/v1/entries/products.php b/api/v1/entries/products.php index de4aa0d01d..bbfc563586 100644 --- a/api/v1/entries/products.php +++ b/api/v1/entries/products.php @@ -12,17 +12,18 @@ class productsEntry extends entry { $control = $this->loadController('product', 'all'); $control->all(); - $data = $this->getData(); - $pager = $data->data->pager; + /* Response */ + $data = $this->getData(); if(isset($data->status) and $data->status == 'success') { - return $this->send(200, array('products' => $data->data->productStats)); - } - if(isset($data->status) and $data->status == 'fail') - { - return $this->sendError(400, $data->message); + $result = array(); + $products = $data->data->productStats; + foreach($products as $product) $result[] = $this->format($product, 'createdDate:time'); + + return $this->send(200, array('products' => $result)); } + if(isset($data->status) and $data->status == 'fail') return $this->sendError(400, $data->message); return $this->sendError(400, 'error'); } @@ -43,7 +44,9 @@ class productsEntry extends entry $data = $this->getData(); if(isset($data->result) and $data->result == 'fail') return $this->sendError(400, $data->message); + /* Response */ $product = $this->loadModel('product')->getByID($data->id); + $product = $this->format($product, 'createdDate:time,whitelist:[]string'); $this->send(200, $product); } diff --git a/api/v1/entries/programs.php b/api/v1/entries/programs.php index ea9948d803..abd75ad87e 100644 --- a/api/v1/entries/programs.php +++ b/api/v1/entries/programs.php @@ -20,7 +20,7 @@ class ProgramsEntry extends Entry $result = array(); foreach($programs as $program) { - $result[] = $program; + $result[] = $this->format($program, 'openedDate:time,lastEditedDate:time,closedDate:time,canceledDate:time'); } return $this->send(200, array('programs' => $result)); } diff --git a/api/v1/entries/project.php b/api/v1/entries/project.php index 05334b9455..1ce4d04db8 100644 --- a/api/v1/entries/project.php +++ b/api/v1/entries/project.php @@ -14,7 +14,7 @@ class projectEntry extends entry $control->view($projectID); $data = $this->getData(); - if(isset($data->status) and $data->status == 'success') return $this->send(200, $data->data->project); + if(isset($data->status) and $data->status == 'success') return $this->send(200, $this->format($data->data->project, 'openedDate:time,lastEditedDate:time,closedDate:time,canceledDate:time')); if(isset($data->status) and $data->status == 'fail') return $this->sendError(400, $data->message); $this->sendError(400, 'error'); @@ -47,7 +47,7 @@ class projectEntry extends entry if(!isset($data->result)) return $this->sendError(400, 'error'); $project = $this->project->getByID($projectID); - $this->send(200, $project); + $this->send(200, $this->format($project, 'openedDate:time,lastEditedDate:time,closedDate:time,canceledDate:time')); } public function delete($projectID) diff --git a/api/v1/entries/projects.php b/api/v1/entries/projects.php index aedd9c0467..9bf4cdce21 100644 --- a/api/v1/entries/projects.php +++ b/api/v1/entries/projects.php @@ -17,7 +17,12 @@ class projectsEntry extends entry if(isset($data->status) and $data->status == 'success') { $pager = $data->data->pager; - return $this->send(200, array('page' => $pager->pageID, 'total' => $pager->recTotal, 'limit' => $pager->recPerPage, 'projects' => $data->data->projectStats)); + $result = array(); + foreach($data->data->projectStats as $project) + { + $result[] = $this->format($project, 'openedDate:time,lastEditedDate:time,closedDate:time,canceledDate:time'); + } + return $this->send(200, array('page' => $pager->pageID, 'total' => $pager->recTotal, 'limit' => $pager->recPerPage, 'projects' => $result)); } if(isset($data->status) and $data->status == 'fail') { @@ -49,6 +54,6 @@ class projectsEntry extends entry $project = $this->loadModel('project')->getByID($data->id); - $this->send(201, $project); + $this->send(201, $this->format($project, 'openedDate:time,lastEditedDate:time,closedDate:time,canceledDate:time')); } } diff --git a/api/v1/entries/stories.php b/api/v1/entries/stories.php index c4d3eea3f7..fecf7c28a9 100644 --- a/api/v1/entries/stories.php +++ b/api/v1/entries/stories.php @@ -21,7 +21,7 @@ class storiesEntry extends entry $result = array(); foreach($stories as $story) { - $result[] = $story; + $result[] = $this->format($story, 'openedDate:time,assignedDate:time,reviewedDate:time,lastEditedDate:time,closedDate:time'); } return $this->send(200, array('page' => $pager->pageID, 'total' => $pager->recTotal, 'limit' => $pager->recPerPage, 'stories' => $result)); } @@ -54,6 +54,6 @@ class storiesEntry extends entry $story = $this->loadModel('story')->getByID($data->id); - $this->send(200, $story); + $this->send(200, $this->format($story, 'openedDate:time,assignedDate:time,reviewedDate:time,lastEditedDate:time,closedDate:time')); } } diff --git a/api/v1/entries/story.php b/api/v1/entries/story.php index ff7dc0874e..7e668e0d73 100644 --- a/api/v1/entries/story.php +++ b/api/v1/entries/story.php @@ -15,7 +15,7 @@ class storyEntry extends Entry $data = $this->getData(); $story = $data->data->story; - $this->send(200, $story); + $this->send(200, $this->format($story, 'openedDate:time,assignedDate:time,reviewedDate:time,lastEditedDate:time,closedDate:time')); } public function put($storyID) @@ -31,7 +31,8 @@ class storyEntry extends Entry $control->edit($storyID); $this->getData(); - $this->sendSuccess(200, 'success'); + $story = $this->story->getByID($storyID); + $this->sendSuccess(200, $this->format($story, 'openedDate:time,assignedDate:time,reviewedDate:time,lastEditedDate:time,closedDate:time')); } public function delete($storyID) diff --git a/api/v1/entries/storychange.php b/api/v1/entries/storychange.php index 50568500e1..04da596b2b 100644 --- a/api/v1/entries/storychange.php +++ b/api/v1/entries/storychange.php @@ -34,6 +34,6 @@ class storyChangeEntry extends Entry $story = $this->loadModel('story')->getByID($storyID); - $this->send(200, $story); + $this->send(200, $this->format($story, 'openedDate:time,assignedDate:time,reviewedDate:time,lastEditedDate:time,closedDate:time')); } } diff --git a/api/v1/entries/task.php b/api/v1/entries/task.php index 1ee0f8daf9..a7c65625db 100644 --- a/api/v1/entries/task.php +++ b/api/v1/entries/task.php @@ -15,7 +15,7 @@ class taskEntry extends Entry $data = $this->getData(); $task = $data->data->task; - $this->send(200, $task); + $this->send(200, $this->format($task, 'openedDate:time,assignedDate:time,realStarted:time,finishedDate:time,canceledDate:time,closedDate:time,lastEditedDate:time')); } public function put($taskID) @@ -31,7 +31,7 @@ class taskEntry extends Entry $this->getData(); $task = $this->task->getByID($taskID); - $this->send(200, $task); + $this->send(200, $this->format($task, 'openedDate:time,assignedDate:time,realStarted:time,finishedDate:time,canceledDate:time,closedDate:time,lastEditedDate:time')); } public function delete($taskID) diff --git a/api/v1/entries/taskassignto.php b/api/v1/entries/taskassignto.php index 42452a2fd6..009b1e31a1 100644 --- a/api/v1/entries/taskassignto.php +++ b/api/v1/entries/taskassignto.php @@ -25,6 +25,6 @@ class taskAssignToEntry extends Entry $task = $this->loadModel('task')->getByID($taskID); - $this->send(200, $task); + $this->send(200, $this->format($task, 'openedDate:time,assignedDate:time,realStarted:time,finishedDate:time,canceledDate:time,closedDate:time,lastEditedDate:time')); } } diff --git a/api/v1/entries/taskfinish.php b/api/v1/entries/taskfinish.php index 5f23f8e70a..bb6bb1d312 100644 --- a/api/v1/entries/taskfinish.php +++ b/api/v1/entries/taskfinish.php @@ -30,6 +30,6 @@ class taskFinishEntry extends Entry $task = $this->loadModel('task')->getByID($taskID); - $this->send(200, $task); + $this->send(200, $this->format($task, 'openedDate:time,assignedDate:time,realStarted:time,finishedDate:time,canceledDate:time,closedDate:time,lastEditedDate:time')); } } diff --git a/api/v1/entries/tasks.php b/api/v1/entries/tasks.php index 27fdd83d13..62b9121996 100644 --- a/api/v1/entries/tasks.php +++ b/api/v1/entries/tasks.php @@ -11,7 +11,7 @@ class tasksEntry extends entry public function get($executionID) { $control = $this->loadController('execution', 'task'); - $control->task($executionID); + $control->task($executionID, 'all'); $data = $this->getData(); if(isset($data->status) and $data->status == 'success') @@ -21,7 +21,7 @@ class tasksEntry extends entry $result = array(); foreach($tasks as $task) { - $result[] = $task; + $result[] = $this->format($task, 'openedDate:time,assignedDate:time,realStarted:time,finishedDate:time,canceledDate:time,closedDate:time,lastEditedDate:time'); } return $this->send(200, array('page' => $pager->pageID, 'total' => $pager->recTotal, 'limit' => $pager->recPerPage, 'tasks' => $result)); } @@ -46,6 +46,6 @@ class tasksEntry extends entry $task = $this->loadModel('task')->getByID($data->id); - $this->send(201, $task); + $this->send(201, $this->format($task, 'openedDate:time,assignedDate:time,realStarted:time,finishedDate:time,canceledDate:time,closedDate:time,lastEditedDate:time')); } } diff --git a/api/v1/entries/user.php b/api/v1/entries/user.php index 84a0081960..ecc5faa910 100644 --- a/api/v1/entries/user.php +++ b/api/v1/entries/user.php @@ -17,7 +17,7 @@ class userEntry extends Entry $user = $data->data->user; unset($user->password); - $this->send(200, $user); + $this->send(200, $this->format($user, 'last:time,locked:time')); } public function put($userID) @@ -39,7 +39,7 @@ class userEntry extends Entry $user = $this->user->getByID($userID, 'id'); unset($user->password); - $this->send(200, $user); + $this->send(200, $this->format($user, 'last:time,locked:time')); } public function delete($userID) diff --git a/api/v1/entries/users.php b/api/v1/entries/users.php index 3bf25eae58..e87c9539a5 100644 --- a/api/v1/entries/users.php +++ b/api/v1/entries/users.php @@ -19,7 +19,7 @@ class usersEntry extends entry $users = $data->data->users; $pager = $data->data->pager; $result = array(); - foreach($users as $user) $result[] = $user; + foreach($users as $user) $result[] = $this->format($user, 'locked:time'); return $this->send(200, array('page' => $pager->pageID, 'total' => $pager->recTotal, 'limit' => $pager->recPerPage, 'users' => $result)); } @@ -50,6 +50,6 @@ class usersEntry extends entry $user = $this->loadModel('user')->getByID($data->id, 'id'); unset($user->password); - $this->send(201, $user); + $this->send(201, $this->format($user, 'last:time,locked:time')); } } diff --git a/framework/api/entry.class.php b/framework/api/entry.class.php index f742ccb185..fbd16704a2 100644 --- a/framework/api/entry.class.php +++ b/framework/api/entry.class.php @@ -381,4 +381,103 @@ class baseEntry } } } + + /** + * 格式化数据的字段类型. + * Format fields of response data. + * + * @param object|array $data + * @param string $fields + * @access public + * @return object|array + */ + public function format($data, $fields) + { + if(is_array($data)) + { + foreach($data as $object) $this->formatFields($object, $fields); + } + $this->formatFields($data, $fields); + + return $data; + } + + /** + * 格式化对象的字段类型. + * Format fields of object. + * + * @param object $object + * @param string $fields + * @access public + * @return object + */ + private function formatFields(&$object, $fields) + { + $fields = explode(',', $fields); + + foreach($fields as $field) + { + $field = explode(':', $field); + $key = $field[0]; + $type = $field[1]; + $isArray = false; + + $pos = strpos($type, ']'); + if($pos !== FALSE) + { + $is_array = true; + $type = substr($type, $pos + 1); + } + + /* Format value. */ + if(!$isArray) + { + $object->$key = $this->cast($object->$key, $type); + continue; + } + + /* Format array. */ + $value = array(); + if(is_array($object->$key)) + { + foreach($object->$key as $v) $value[] = $this->cast($v, $type); + } + else + { + $vs = implode(',', $object->$key); + foreach($vs as $v) + { + if($v === '') continue; + $value[] = $this->cast($v, $type); + } + } + $object->$key = $value; + } + } + + /** + * 类型转换. + * Typecasting. + * + * @param mixed $vaule + * @param string $type + * @access public + * @return mixed + */ + private function cast($value, $type) + { + switch($type) + { + case 'time': + $timeFormat = $this->param('timeFormat', ''); + if($timeFormat == 'utc') + { + if(!$value or $value == '0000-00-00 00:00:00') return null; + return gmdate("Y-m-d\TH:i:s\Z", strtotime($value)); + } + return $value; + default: + return $value; + } + } } diff --git a/framework/base/router.class.php b/framework/base/router.class.php index 13c8bbd635..ded80fbd7d 100644 --- a/framework/base/router.class.php +++ b/framework/base/router.class.php @@ -2207,6 +2207,8 @@ class baseRouter if(!isset($params->emulatePrepare) and PHP_OS == 'Linux') $params->emulatePrepare = true; if(!isset($params->bufferQuery) and PHP_OS == 'Linux') $params->bufferQuery = true; + if(defined('RUN_MODE') and RUN_MODE == 'api') $params->emulatePrepare = false; + $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); if(isset($params->strictMode) and $params->strictMode == false) $dbh->exec("SET @@sql_mode= ''");