/**
* 获取post请求的参数
*/
function getPostParam($paramKey, $nullAble = false, $checkArray = false, $checkType = false)
{
$errorMsg = "";
$data = @$_POST[$paramKey];
if (is_null($data)) {
if (!$nullAble) {
$errorMsg = '缺少参数:' . $paramKey;
}
} else if ($checkArray) {
if (!in_array($data, $checkArray)) {
$errorMsg = '参数:' . $paramKey . '取值范围有误';
}
} else if ($checkType) {
switch ($checkType) {
case 'numeric':
$errorMsg = is_numeric($data) ? "" : "参数类型错误numeric";
break;
case 'bool':
$errorMsg = is_bool($data) ? "" : "参数类型错误bool";
break;
case 'float':
$errorMsg = is_float($data) ? "" : "参数类型错误float";
break;
case 'int':
$errorMsg = is_int($data) ? "" : "参数类型错误int";
break;
case 'string':
$errorMsg = is_string($data) ? "" : "参数类型错误string";
break;
case 'object':
$errorMsg = is_object($data) ? "" : "参数类型错误object";
break;
case 'array':
$errorMsg = is_array($data) ? "" : "参数类型错误array";
break;
default:
# code...
break;
}
}
if (!empty($errorMsg)) {
ob_start();
errJson($errorMsg, 1002);
ob_end_flush();
exit;
}
return $data;
}
/**
* 返回错误信息
*/
function errJson($errorMsg, $errorCode = '0')
{
header('Content-type: application/json');
$outPutArray = array('status' => 'error', 'msg' => $errorMsg, 'errorCode' => $errorCode, 'timeStamp' => time());
echo json_encode($outPutArray, JSON_UNESCAPED_UNICODE);
exit();
}
/**
* Notes: 传入数组返回成功的json格式
*/
function reJson($jsonArray)
{
header('Content-type: application/json');
$outPutArray = array('status' => 'OK', 'data' => $jsonArray, 'code' => 200, 'timeStamp' => time());
echo json_encode($outPutArray, JSON_UNESCAPED_UNICODE);
exit();
}
/**
* 对数据进行脱敏
* @param string $data
* @param int $start 保留数据的前几位
* @param int $end 保留数据的后几位
* @param string $char 替换的字符
*/
function filter_repeat($data, $start = 4 , $end = 4, $char = '*')
{
$len = mb_strlen($data);
if(!$data || $len < $start + $end ){
return $data;
}
$startStr = mb_substr($data, 0, $start);
if(($start + $end) == $len){
// 放弃尾部数据
return $startStr . str_repeat($char, $len - $start);
}
$endStr = mb_substr($data,0 - $end);
return $startStr . str_repeat($char, $len - $start - $end) . $endStr;
}
// string(30) "各种身份******敏感信息"
var_dump(filter_repeat('各种身份证姓名电话等敏感信息', 4, 4));
/**
* 生成一个随机的字符串
*/
function random_string($type = 'alnum', $len = 8)
{
switch ($type)
{
case 'basic':
return mt_rand();
case 'alnum':
case 'numeric':
case 'nozero':
case 'alpha':
switch ($type)
{
case 'alpha':
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'alnum':
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'numeric':
$pool = '0123456789';
break;
case 'nozero':
$pool = '123456789';
break;
}
return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len);
case 'unique': // todo: remove in 3.1+
case 'md5':
return md5(uniqid(mt_rand()));
case 'encrypt': // todo: remove in 3.1+
case 'sha1':
return sha1(uniqid(mt_rand(), TRUE));
}
}
/**
* [write_log 写入日志]
* @param [type] $data [写入的数据]
* @return [type] [description]
*/
/**
* [write_log 写入日志]
* @param [type] $data [写入的数据]
* @return [type] [description]
*/
function write_log($data){
$years = date('Y-m');
//设置路径目录信息
$url = './public/log/txlog/'.$years.'/'.date('Ymd').'_request_log.txt';
$dir_name=dirname($url);
//目录不存在就创建
if(!file_exists($dir_name))
{
//iconv防止中文名乱码
$res = mkdir(iconv("UTF-8", "GBK", $dir_name),0777,true);
}
$fp = fopen($url,"a");//打开文件资源通道 不存在则自动创建
fwrite($fp,date("Y-m-d H:i:s").var_export($data,true)."\r\n");//写入文件
fclose($fp);//关闭资源通道
}
$data = ['status'=>2, 'message'=>'ssfs'];
write_log($data);
php 常用方法封装
最新推荐文章于 2021-05-27 17:02:42 发布