179 lines
4.9 KiB
PHP
179 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace common\core;
|
|
|
|
use Yii;
|
|
use yii\base\Arrayable;
|
|
use yii\base\DynamicModel;
|
|
use yii\base\InvalidArgumentException;
|
|
use yii\base\Model;
|
|
use yii\data\ActiveDataProvider;
|
|
use yii\data\DataProviderInterface;
|
|
use yii\helpers\ArrayHelper;
|
|
use yii\base\Controller;
|
|
use yii\web\Response;
|
|
|
|
class BaseController extends Controller
|
|
{
|
|
public $field=[];//重定义格式
|
|
public $listkey = 'list';
|
|
public $pagekey = 'pagination';
|
|
public $extend_result = [];
|
|
public $enableCsrfValidation = false;
|
|
// 不需进行token权限认证的方法
|
|
public $optional = [];
|
|
public $user;
|
|
|
|
public function create($query, $post)
|
|
{
|
|
$pagination = [];
|
|
$defaultPageSize = 10;
|
|
if (isset($post['page'])) {
|
|
$pagination['page'] = (int)$post['page'];
|
|
}
|
|
if (isset($post['page_size'])) {
|
|
$defaultPageSize = (int)$post['page_size'];
|
|
}
|
|
return new ActiveDataProvider([
|
|
'query' => $query,
|
|
'pagination' => [
|
|
'defaultPageSize' => $defaultPageSize,
|
|
'params' => $pagination
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 当前登陆用户ID
|
|
* @return int|string
|
|
*/
|
|
public function getCurrentUserId()
|
|
{
|
|
return Yii::$app->user->identity->getId();
|
|
}
|
|
|
|
/**
|
|
* 取post参数
|
|
* @param $name
|
|
* @param $defaultValue
|
|
* @return array|mixed
|
|
*/
|
|
public function post($name = null, $defaultValue = null)
|
|
{
|
|
return Yii::$app->request->post($name, $defaultValue);
|
|
}
|
|
|
|
/**
|
|
* 取get参数
|
|
* @param $name
|
|
* @param $defaultValue
|
|
* @return array|mixed
|
|
*/
|
|
public function get($name = null, $defaultValue = null)
|
|
{
|
|
return Yii::$app->request->get($name, $defaultValue);
|
|
}
|
|
|
|
/**
|
|
* 参数验证
|
|
* @param $data
|
|
* @param $rules
|
|
* @return DynamicModel|\stdClass
|
|
* @throws
|
|
*/
|
|
public function requestValidate($data, $rules)
|
|
{
|
|
foreach ($rules as $rule) {
|
|
if (is_array($rule) && isset($rule[0], $rule[1])) { // attributes, validator type
|
|
foreach ((array)$rule[0] as $r) {
|
|
if (!isset($data[$r])) {
|
|
$data[$r] = null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$validate = DynamicModel::validateData($data, $rules);
|
|
if ($validate->hasErrors()) {
|
|
throw new InvalidArgumentException(current($validate->getFirstErrors()));
|
|
}
|
|
return $validate;
|
|
}
|
|
|
|
public function serializeData($data)
|
|
{
|
|
if(Yii::$app->response->format== Response::FORMAT_RAW){
|
|
return $data;
|
|
}
|
|
if ($data instanceof Model && $data->hasErrors()) {
|
|
throw new InvalidArgumentException('参数错误。'.implode('',$data->getFirstErrors()));
|
|
// $data = $this->serializeModelErrors($data);
|
|
} elseif ($data instanceof Arrayable) {
|
|
$data = $this->serializeModel($data);
|
|
} elseif ($data instanceof DataProviderInterface) {
|
|
$data = $this->serializeDataProvider($data);
|
|
}elseif($data ===null){
|
|
$data = [];
|
|
}
|
|
return array_merge($this->extend_result,$data);
|
|
}
|
|
|
|
protected function serializeDataProvider($dataProvider)
|
|
{
|
|
if(!empty($this->field)){
|
|
$models = ArrayHelper::toArray($dataProvider->getModels(),$this->field);
|
|
}else{
|
|
$models = array_values($dataProvider->getModels());
|
|
$models = $this->serializeModels($models);
|
|
}
|
|
$pagination = $dataProvider->getPagination();
|
|
$result = [
|
|
$this->listkey => $models,
|
|
];
|
|
if ($pagination !== false) {
|
|
return array_merge($result, $this->serializePagination($pagination));
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Serializes a model object.
|
|
* @param Arrayable $model
|
|
* @return array the array representation of the model
|
|
*/
|
|
protected function serializeModel($model)
|
|
{
|
|
return $model->toArray();
|
|
}
|
|
|
|
protected function serializeModels(array $models)
|
|
{
|
|
foreach ($models as $i => $model) {
|
|
if ($model instanceof Arrayable) {
|
|
$models[$i] = $model->toArray();
|
|
} elseif (is_array($model)) {
|
|
$models[$i] = ArrayHelper::toArray($model);
|
|
}
|
|
}
|
|
|
|
return $models;
|
|
}
|
|
|
|
protected function serializePagination($pagination)
|
|
{
|
|
return [
|
|
$this->pagekey => [
|
|
'total' => $pagination->totalCount,
|
|
'totalPage' => $pagination->getPageCount(),
|
|
'pageSize' => $pagination->getPageSize(),
|
|
],
|
|
];
|
|
}
|
|
|
|
final public function afterAction($action, $result)
|
|
{
|
|
$result = parent::afterAction($action, $result);
|
|
return $this->serializeData($result);
|
|
}
|
|
}
|