-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
添加 restful Response Handler && Migration
- Loading branch information
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* author : forecho <[email protected]> | ||
* createTime : 2017/7/28 15:31 | ||
* description: | ||
*/ | ||
namespace yiier\helpers; | ||
|
||
class Migration extends \yii\db\Migration | ||
{ | ||
/** | ||
* 创建表选项 | ||
* @var string | ||
*/ | ||
public $tableOptions = null; | ||
|
||
/** | ||
* 是否事务性存储表, 则创建为事务性表. 默认使用 | ||
* @var bool | ||
*/ | ||
public $useTransaction = true; | ||
|
||
|
||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
if ($this->db->driverName === 'mysql') { //Mysql 表选项 | ||
$engine = $this->useTransaction ? 'InnoDB' : 'MyISAM'; | ||
$this->tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=' . $engine; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* author : forecho <[email protected]> | ||
* createTime : 2017/7/28 15:31 | ||
* description: | ||
*/ | ||
|
||
namespace yiier\helpers; | ||
|
||
class ResponseHandler | ||
{ | ||
public $event; | ||
|
||
/** | ||
* 返回数据统一处理 | ||
*/ | ||
public function formatResponse() | ||
{ | ||
$response = $this->event->sender; | ||
if ($response->data !== null) { | ||
if (isset($response->data['code']) && isset($response->data['message'])) { | ||
$response->data = [ | ||
'code' => $response->data['code'] ?: $response->statusCode, | ||
'data' => isset($response->data['data']) ? $response->data['data'] : null, | ||
'message' => $response->data['message'], | ||
]; | ||
} elseif ($response->format != 'html' && !isset($response->data['message'])) { | ||
$response->data = [ | ||
'code' => 0, | ||
'data' => $response->data, | ||
'message' => '成功', | ||
]; | ||
} elseif (isset($response->data['message']) && $response->data['message'] != "" && !isset($response->data['code'])) { | ||
$message = $response->data['message']; | ||
unset($response->data['message']); | ||
$response->data = [ | ||
'code' => 0, | ||
'data' => isset($response->data[0]) ? $response->data[0] : $response->data, | ||
'message' => $message, | ||
]; | ||
} | ||
} | ||
} | ||
} |