版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yqwwj001/article/details/83307041
/**
* 构建返回头信息
*/
private function buildHeader($retCode, $retMessage)
{
$retNode = new stdClass();
$retNode->retCode = $retCode;
$retNode->retMessage = $retMessage;
return $retNode;
}
/**
* 成功返回信息
*/
function success($body)
{
$head = $this->buildHeader(200, '成功');
$ret = array(
'Head'=>$head,
'Body'=>$body,
);
return json_encode($ret);
}
/**
* 失败返回信息
*/
function failed($retCode, $retMessage)
{
$head = $this->buildHeader($retCode, $retMessage);
$ret = array(
'Head'=>$head,
'Body'=>array(),
);
return json_encode($ret);
}
/**
* 构建成功提示信息
*/
function buildFakeStatus($Message)
{
$retNode = new stdClass();
$retNode->Checked=1;
$retNode->Message=$Message;
return $retNode;
}
/**
* post 接值
* @param string post名称
* @must bool true 不能为空值
*/
function getPostString($param, $must=false)
{
$value = $_POST[$param];
if($must == true)
{
if($value=="")
{
throw new Exception("$param 参数不能为空");
}
}
return $value;
}
/**
* 封包输出,确保输出之前,将输出格式设定为json格式
*/
function printJson($packageData)
{
header('Content-type: application/json');
print $packageData;
}
/**
* 通用适配器
*/
function adapter($params, $node)
{
$ret = new StdClass();
foreach($params as $key=>$value)
{
$part = $node;
$sections = explode('/', $key);
foreach($sections as $section)
{
$part = $part[$section];
}
if(isset($part))
{
$ret->$value = $part;
}
else
{
$ret->$value = NULL;
}
}
return $ret;
}
/*
* 去除null 变成 ''
* */
function removeNull($str){
return $string=$str==null?'':$str;
}
/**
* 根据数组的某个字段排序
* */
function arrSort($data,$field,$direction='DESC'){
if($direction=='asc'||$direction=='ASC'){
$direction='SORT_ASC';
}else{
$direction='SORT_DESC';
}
$sort = array(
'direction' => $direction, //排序顺序标志 SORT_DESC 降序;SORT_ASC 升序
'field' => $field, //排序字段
);
$arrSort = array();
foreach($data AS $uniqid => $row){
foreach($row AS $key=>$value){
$arrSort[$key][$uniqid] = $value;
}
}
if($sort['direction']){
array_multisort($arrSort[$sort['field']], constant($sort['direction']), $data);
}
return $data;
}