Last active
July 7, 2018 10:47
-
-
Save flxxyz/7ec4035838ec5d6dffac62f8bd1cdac2 to your computer and use it in GitHub Desktop.
自用简易控制器
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"php": ">=7.0", | |
"monolog/monolog": "1.2.*", | |
"php-curl-class/php-curl-class": "8.*" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
class Config | |
{ | |
private static $name = ''; | |
private static $arr = []; | |
/** | |
* @return array | |
*/ | |
public static function getArr() | |
{ | |
return self::$arr; | |
} | |
/** | |
* @return string | |
*/ | |
public static function getName() | |
{ | |
return self::$name; | |
} | |
/* | |
* 取 | |
*/ | |
public static function get($filename = '', $key = null) | |
{ | |
if ($filename === '') { | |
self::$name = ''; | |
return self::$arr; | |
} | |
if ( !is_file(CONF_PATH . "{$filename}.php")) { | |
return self::$arr; | |
} | |
$data = self::file($filename); | |
if (is_null($key)) { | |
return $data; | |
} | |
return self::key($key); | |
} | |
/* | |
* 获取键值 | |
*/ | |
private static function key($key) | |
{ | |
foreach (self::$arr as $k => $v) { | |
if ($key !== $k) | |
continue; | |
return self::$arr[$k]; | |
} | |
} | |
/* | |
* 缓存配置 | |
*/ | |
private static function file($filename = '') | |
{ | |
if (self::$name === $filename) { | |
return self::$arr; | |
} else { | |
self::$name = $filename; | |
self::$arr = require CONF_PATH . "{$filename}.php"; | |
return self::$arr; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
class Controller | |
{ | |
private $param = []; | |
public function assign($name, $value) | |
{ | |
$this->param[$name] = $value; | |
} | |
public function display() | |
{ | |
$filename = join('/', func_get_args()) . '.php'; | |
$path = VIEW_PATH . $filename; | |
if ( !is_file($path)) { | |
exit('视图文件不存在=' . $filename); | |
} | |
extract($this->param); | |
require_once $path; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Monolog\Logger; | |
use Monolog\Handler\StreamHandler; | |
class Log | |
{ | |
/** | |
* 输出info类型消息 | |
* @param string $channel | |
* @param string $message | |
* @param array $param | |
*/ | |
private static function info($channel, $message, $param) | |
{ | |
echo self::message('INFO', $message, $param); | |
$log = new Logger($channel); | |
$channel = self::is_log($channel); | |
$log->pushHandler(new StreamHandler($channel), Logger::INFO); | |
$log->info($message, $param); | |
} | |
/** | |
* 输出notice类型消息 | |
* @param string $channel | |
* @param string $message | |
* @param array $param | |
*/ | |
private static function notice($channel, $message, $param) | |
{ | |
echo self::message('NOTICE', $message, $param); | |
$log = new Logger($channel); | |
$channel = self::is_log($channel); | |
$log->pushHandler(new StreamHandler($channel), Logger::NOTICE); | |
$log->notice($message, $param); | |
} | |
/** | |
* 输出debug类型消息 | |
* @param string $channel | |
* @param string $message | |
* @param array $param | |
*/ | |
private static function debug($channel, $message, $param) | |
{ | |
echo self::message('DEBUG', $message, $param); | |
$log = new Logger($channel); | |
$channel = self::is_log($channel); | |
$log->pushHandler(new StreamHandler($channel), Logger::DEBUG); | |
$log->debug($message, $param); | |
} | |
/** | |
* 输出warn类型消息 | |
* @param string $channel | |
* @param string $message | |
* @param array $param | |
*/ | |
private static function warn($channel, $message, $param) | |
{ | |
echo self::message('WARNING', $message, $param); | |
$log = new Logger($channel); | |
$channel = self::is_log($channel); | |
$log->pushHandler(new StreamHandler($channel), Logger::WARNING); | |
$log->warn($message, $param); | |
} | |
/** | |
* 输出err类型消息 | |
* @param string $channel | |
* @param string $message | |
* @param array $param | |
*/ | |
private static function err($channel, $message, $param) | |
{ | |
echo self::message('ERROR', $message, $param); | |
$log = new Logger($channel); | |
$channel = self::is_log($channel); | |
$log->pushHandler(new StreamHandler($channel), Logger::ERROR); | |
$log->err($message, $param); | |
} | |
/** | |
* | |
* @param string $action | |
* @param string $channel | |
* @param string $message | |
* @param array $param | |
*/ | |
public static function handle(string $action = 'info', string $channel = '', string $message = '', array $param = []) { | |
if(!method_exists(self::class, $action)) { | |
echo self::message('ERROR', '方法不存在!'); | |
}else { | |
$action = strtolower($action); | |
self::$action($channel, $message, $param); | |
} | |
} | |
/** | |
* 命令行输出消息 | |
* @param $type | |
* @param string $message | |
* @param array $param | |
* @return false|string | |
*/ | |
private static function message($type, $message = '', $param = []) | |
{ | |
$str = date('Y-m-d H:i:s'); | |
$str .= " [{$type}] {$message} "; | |
$str .= json_encode($param); | |
$str .= PHP_EOL; | |
return $str; | |
} | |
private static function is_log(string $channel) { | |
if(!is_file('log/' . $channel . '.log')) { | |
if(!touch('log/' . $channel . '.log')) { | |
echo self::message('WARNING', '日志文件创建失败,请检查目录权限'); | |
} | |
} | |
return 'log/' . $channel . '.log'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Tools; | |
/* | |
* @link https://github.com/php-curl-class/php-curl-class | |
*/ | |
use Curl\Curl; | |
class Util | |
{ | |
/** | |
* 构造通用json返回 | |
* @param $data | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
static function json($data) | |
{ | |
return response()->json($data); | |
} | |
/** | |
* 获取客户端ip | |
* @return string | |
*/ | |
static function getClientIp() | |
{ | |
foreach (['HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR'] as $key) { | |
if (array_key_exists($key, $_SERVER) === true) { | |
foreach (explode(',', $_SERVER[$key]) as $ip) { | |
$ip = trim($ip); | |
return $ip; | |
// 过滤掉局域网地址 | |
/*if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) { | |
return $ip; | |
}*/ | |
} | |
} | |
} | |
} | |
/** | |
* 返回一天开始,当前,结束的三种时间戳 | |
* @param null $time | |
* @return array | |
*/ | |
static function diffDays($time = null) | |
{ | |
$time = $time ?? time(); | |
$day_seconds = 60 * 60 * 24 - 1; // 考虑在当天内,减去一秒钟 | |
$start = strtotime(date('Y-m-d 00:00:00', $time)); | |
$now = strtotime(date('Y-m-d H:i:s', $time)); | |
$end = $start + $day_seconds; | |
/** | |
* @return array | |
* @key now 当前时间戳 | |
* @key start 当天开始时间戳 | |
* @key end 当天结束时间戳 | |
*/ | |
return [ | |
'now' => $now, | |
'start' => $start, | |
'end' => $end, | |
]; | |
} | |
/** | |
* 构造指定位数随机数字 | |
* @param int $bit | |
* @return string | |
*/ | |
static function randPass($bit = 6) | |
{ | |
$pass = []; | |
$i = 0; | |
while ($i++ < $bit) { | |
$pass[] = mt_rand(0, 9); | |
} | |
return join('', $pass); | |
} | |
/** | |
* 发送http get请求 | |
* @param null $uri | |
* @param $data | |
* @param null $referer | |
* @return Curl|__anonymous@1717 | |
*/ | |
static function getHttp($uri = null, $data, $referer = null) | |
{ | |
if (is_null($uri)) { | |
return new Class{}; | |
} | |
$curl = new Curl(); | |
$curl->setUserAgent(Sms::HTTP_USERAGENT); | |
if ( !is_null($referer)) { | |
$curl->setReferer($referer); | |
} | |
$curl->get($uri, $data); | |
return $curl; | |
} | |
/** | |
* 发送http post请求 | |
* @param null $uri | |
* @param $data | |
* @param null $referer | |
* @return Curl|__anonymous@2234 | |
*/ | |
static function postHttp($uri = null, $data, $referer = null) | |
{ | |
if (is_null($uri)) { | |
return new Class{}; | |
} | |
$curl = new Curl(); | |
$curl->setUserAgent(Sms::HTTP_USERAGENT); | |
if ( !is_null($referer)) { | |
$curl->setReferer($referer); | |
} | |
$curl->post($uri, $data); | |
return $curl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment