<?php
namespace app\common\func;
use think\Db;
class CommonFunc
{
/*
* get方式curl
*/
public static function get_curl_g($url, $data = [])
{
$ch = curl_init();
if (!empty($data)) {
$url .= "?" . http_build_query($data);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}
/**
* 获取时间精确到毫秒
* @param string $format
* @param null $utimestamp
* @return false|string
*/
function microtime_float(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
/*
* post方式url
*/
public static function get_curl_p($url, $data)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$data = curl_exec($curl);
curl_close($curl);
return json_decode($data, true);
}
public static function api_success($data)
{
header('Content-type: application/json');
$res['success'] = true;
$res['data'] = $data;
echo json_encode($res, JSON_UNESCAPED_UNICODE);
exit;
}
public static function api_error($errorCode, $errorMsg)
{
$data['error_msg'] = $errorMsg;
$data['error_code'] = $errorCode;
echo json_encode($data, JSON_UNESCAPED_UNICODE);
exit();
}
public static function get_real_ip()
{
$ip = false;
if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode(", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip) {
array_unshift($ips, $ip);
$ip = FALSE;
}
for ($i = 0; $i < count($ips); $i++) {
if (!eregi("^(10|172\.16|192\.168)\.", $ips[$i])) {
$ip = $ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
/*
* 验证数组字段是否为空
*/
public static function arrEmptyVerify($data, $arr)
{
foreach ($arr as $k => $v) {
if (!isset($data[$v])) {
CommonFunc::api_error("20000", "param {$v} is not set.");
}
if ($data[$v] === '' || $data[$v] === null) {
CommonFunc::api_error("20000", "param {$v} is empty.");
}
}
}
/*
* 验证数组字段是否为空
*/
public static function arrIssetVerify($data, $arr)
{
foreach ($arr as $k => $v) {
if (!isset($data[$v])) {
CommonFunc::api_error("20000", "param {$v} is not set.");
}
}
}
/*
* 设置为空字段
*/
public static function arrEmptyReplace(&$data, $arr, $default = '')
{
foreach ($arr as $k => $v) {
if (empty($data[$v])) {
$data[$v] = $default;
}
}
return $data;
}
/*
* 设置为空字段
*/
public static function arrIssetReplace(&$data, $arr, $default = '')
{
foreach ($arr as $k => $v) {
if (!isset($data[$v])) {
$data[$v] = $default;
}
}
return $data;
}
/*
* 设置为空字段
*/
public static function arrReplace1DKey(&$data, $arr)
{
foreach ($data as $k => $v) {
foreach ($arr as $key => $value) {
if($k == $key){
unset($data[$k]);
$data[$value] = $v;
}
}
}
return $data;
}
/*
* 删除数组中冗余字段
*/
public static function arrRemove($arr, $remove = [])
{
foreach ($arr as $k => $v) {
if (in_array($k, $remove)) {
unset($arr[$k]);
}
}
return $arr;
}
/*
* 删除多为数组中二维级别的冗余字段
*/
public static function multiArrRemove(&$arr, $remove = [])
{
foreach ($arr as $key => $value) {
foreach ($value as $k => $v) {
if (in_array($k, $remove)) {
unset($arr[$key][$k]);
}
}
}
return $arr;
}
/*
* 批量替换数组指定数据 0=>否
*/
public static function arrReplace(&$arr, $replace)
{
foreach ($arr as $k => &$v) {
foreach ($replace as $k1 => $v1) {
if ($k1 == $k) {
foreach ($v1 as $k2 => $v2) {
if ($v == $k2) {
$v = $v2;
}
}
}
}
}
}
/*
* 事务处理
*/
public static function transaction($func,$data = ['res'=>1],$errorCode = 20000, $errorMsg = "操作失败")
{
Db::startTrans();
try {
$data['res'] = $func();
Db::commit();
CommonFunc::api_success($data);
} catch (Exception $e) {
Db::rollback();
CommonFunc::api_error($errorCode, $errorMsg.':'.$e->getMessage());
}
}
}
我的公共函数
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 在Laravel 5.1 项目中有时我们会需要一些不依赖于类的辅助函数,通常我们会将这些辅助函数定义在一个单独文件...