mirror of
https://gitee.com/apiadmin/ApiAdmin.git
synced 2025-04-06 03:58:00 +08:00
89 lines
1.9 KiB
PHP
89 lines
1.9 KiB
PHP
<?php
|
|
/**
|
|
* 工程基类
|
|
* @since 2017/02/28 创建
|
|
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
|
*/
|
|
|
|
namespace app\admin\controller;
|
|
use app\util\ReturnCode;
|
|
use think\Controller;
|
|
|
|
class Base extends Controller {
|
|
|
|
private $debug = [];
|
|
|
|
public function _initialize() {
|
|
|
|
}
|
|
|
|
public function buildSuccess($data, $msg = '操作成功', $code = ReturnCode::SUCCESS) {
|
|
$return = [
|
|
'code' => $code,
|
|
'msg' => $msg,
|
|
'data' => $data
|
|
];
|
|
if ($this->debug) {
|
|
$return['debug'] = $this->debug;
|
|
}
|
|
|
|
return $return;
|
|
}
|
|
|
|
public function buildFailed($code, $msg, $data = []) {
|
|
$return = [
|
|
'code' => $code,
|
|
'msg' => $msg,
|
|
'data' => $data
|
|
];
|
|
if ($this->debug) {
|
|
$return['debug'] = $this->debug;
|
|
}
|
|
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* 将二维数组变成指定key
|
|
* @param $array
|
|
* @param $keyName
|
|
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
|
* @return array
|
|
*/
|
|
protected function buildArrByNewKey($array, $keyName = 'id') {
|
|
$list = array();
|
|
foreach ($array as $item) {
|
|
$list[$item[$keyName]] = $item;
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* 将查询的二维对象转换成二维数组
|
|
* @param array $res
|
|
* @param string $key
|
|
* @return array
|
|
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
|
*/
|
|
protected function buildArrFromObj($res, $key = '') {
|
|
$arr = [];
|
|
foreach ($res as $value) {
|
|
$value = $value->toArray();
|
|
if ($key) {
|
|
$arr[$value[$key]] = $value;
|
|
} else {
|
|
$arr[] = $value;
|
|
}
|
|
}
|
|
|
|
return $arr;
|
|
}
|
|
|
|
protected function debug($data) {
|
|
if ($data) {
|
|
$this->debug[] = $data;
|
|
}
|
|
}
|
|
|
|
}
|