first commit
This commit is contained in:
@@ -0,0 +1,432 @@
|
||||
<?php
|
||||
/*
|
||||
* Material-Design-Avatars
|
||||
* https://github.com/lincanbin/Material-Design-Avatars
|
||||
*
|
||||
* Copyright 2015 Canbin Lin (lincanbin@hotmail.com)
|
||||
* http://www.94cb.com/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0:
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Create material deisgn avatars for users just like Gmail or Messager in Android.
|
||||
*/
|
||||
declare (strict_types = 1);
|
||||
namespace avatars;
|
||||
|
||||
class MDAvatars
|
||||
{
|
||||
public $Char;
|
||||
public $AvatarSize;
|
||||
public $Shape;
|
||||
public $Padding;
|
||||
public $Avatar;
|
||||
public $FontFile;
|
||||
public $IsNotLetter;
|
||||
public $LetterFont;
|
||||
public $AsianFont;
|
||||
public $EnableAsianChar;
|
||||
|
||||
|
||||
function __construct($Char, $AvatarSize = 256 ,$Shape = 1)
|
||||
{
|
||||
$this->Char = mb_strtoupper(mb_substr($Char, 0, 1, "UTF-8"));
|
||||
$this->AvatarSize = $AvatarSize;
|
||||
$this->Shape = $Shape;
|
||||
$this->Padding = 30 * ($this->AvatarSize / 256);
|
||||
$this->LetterFont = './static/font/MiSans-Regular.ttf';
|
||||
$this->AsianFont = './static/font/MiSans-Regular.ttf';
|
||||
$this->EnableAsianChar = is_file($this->AsianFont);
|
||||
$path='./storage/avatars/';
|
||||
if(!is_dir($path)){
|
||||
mkdir($path, 0755, true);
|
||||
}
|
||||
$CNChar = ord($this->Char);
|
||||
if (!$this->EnableAsianChar &&
|
||||
preg_match("/^[\x7f-\xff]/", $this->Char) &&
|
||||
!($CNChar >= ord("A") && $CNChar <= ord("z"))
|
||||
) {
|
||||
//如果是中文,并且没有中文字体包,则按拼音首字母对其进行转换
|
||||
$CNByte = iconv("UTF-8", "gb2312", $this->Char);
|
||||
$Code = ord($CNByte[0]) * 256 + ord($CNByte[1]) - 65536;//求其偏移量
|
||||
if ($Code >= -20319 and $Code <= -20284) $this->Char = "A";
|
||||
if ($Code >= -20283 and $Code <= -19776) $this->Char = "B";
|
||||
if ($Code >= -19775 and $Code <= -19219) $this->Char = "C";
|
||||
if ($Code >= -19218 and $Code <= -18711) $this->Char = "D";
|
||||
if ($Code >= -18710 and $Code <= -18527) $this->Char = "E";
|
||||
if ($Code >= -18526 and $Code <= -18240) $this->Char = "F";
|
||||
if ($Code >= -18239 and $Code <= -17923) $this->Char = "G";
|
||||
if ($Code >= -17922 and $Code <= -17418) $this->Char = "H";
|
||||
if ($Code >= -17417 and $Code <= -16475) $this->Char = "J";
|
||||
if ($Code >= -16474 and $Code <= -16213) $this->Char = "K";
|
||||
if ($Code >= -16212 and $Code <= -15641) $this->Char = "L";
|
||||
if ($Code >= -15640 and $Code <= -15166) $this->Char = "M";
|
||||
if ($Code >= -15165 and $Code <= -14923) $this->Char = "N";
|
||||
if ($Code >= -14922 and $Code <= -14915) $this->Char = "O";
|
||||
if ($Code >= -14914 and $Code <= -14631) $this->Char = "P";
|
||||
if ($Code >= -14630 and $Code <= -14150) $this->Char = "Q";
|
||||
if ($Code >= -14149 and $Code <= -14091) $this->Char = "R";
|
||||
if ($Code >= -14090 and $Code <= -13319) $this->Char = "S";
|
||||
if ($Code >= -13318 and $Code <= -12839) $this->Char = "T";
|
||||
if ($Code >= -12838 and $Code <= -12557) $this->Char = "W";
|
||||
if ($Code >= -12556 and $Code <= -11848) $this->Char = "X";
|
||||
if ($Code >= -11847 and $Code <= -11056) $this->Char = "Y";
|
||||
if ($Code >= -11055 and $Code <= -10247) $this->Char = "Z";
|
||||
}
|
||||
if (in_array($this->Char, str_split('QWERTYUIOPASDFGHJKLZXCVBNM0123456789', 1))) {
|
||||
$this->IsNotLetter = false;
|
||||
$this->FontFile = $this->LetterFont;
|
||||
} else {
|
||||
$this->IsNotLetter = true;
|
||||
$this->FontFile = $this->AsianFont;
|
||||
}
|
||||
$this->Initialize();
|
||||
}
|
||||
|
||||
private function Initialize()
|
||||
{
|
||||
//extension_loaded('gd')
|
||||
$Width = $this->AvatarSize;//Width of avatar
|
||||
$Height = $this->AvatarSize;//Height of avatar
|
||||
$Padding = $this->Padding;
|
||||
$this->Avatar = imagecreatetruecolor($Width, $Height);
|
||||
//全透明背景
|
||||
imageSaveAlpha($this->Avatar, true);
|
||||
$BackgroundAlpha = imagecolorallocatealpha($this->Avatar, 255, 255, 255, 127);
|
||||
imagefill($this->Avatar, 0, 0, $BackgroundAlpha);
|
||||
//抗锯齿
|
||||
if (function_exists('imageantialias')) {
|
||||
imageantialias($this->Avatar, true);
|
||||
}
|
||||
//Material Design参考颜色
|
||||
//http://www.google.com/design/spec/style/color.html#color-color-palette
|
||||
$MaterialDesignColor = array(
|
||||
array(239, 154, 154),
|
||||
array(229, 115, 115),
|
||||
array(239, 83, 80),
|
||||
array(244, 67, 54),
|
||||
array(229, 57, 53),
|
||||
array(211, 47, 47),
|
||||
array(198, 40, 40),
|
||||
array(183, 28, 28),
|
||||
array(255, 138, 128),
|
||||
array(255, 82, 82),
|
||||
array(255, 23, 68),
|
||||
array(213, 0, 0),
|
||||
array(248, 187, 208),
|
||||
array(244, 143, 177),
|
||||
array(240, 98, 146),
|
||||
array(236, 64, 122),
|
||||
array(233, 30, 99),
|
||||
array(216, 27, 96),
|
||||
array(194, 24, 91),
|
||||
array(173, 20, 87),
|
||||
array(136, 14, 79),
|
||||
array(255, 128, 171),
|
||||
array(255, 64, 129),
|
||||
array(245, 0, 87),
|
||||
array(197, 17, 98),
|
||||
array(206, 147, 216),
|
||||
array(186, 104, 200),
|
||||
array(171, 71, 188),
|
||||
array(156, 39, 176),
|
||||
array(142, 36, 170),
|
||||
array(123, 31, 162),
|
||||
array(106, 27, 154),
|
||||
array(74, 20, 140),
|
||||
array(234, 128, 252),
|
||||
array(224, 64, 251),
|
||||
array(213, 0, 249),
|
||||
array(170, 0, 255),
|
||||
array(179, 157, 219),
|
||||
array(149, 117, 205),
|
||||
array(126, 87, 194),
|
||||
array(103, 58, 183),
|
||||
array(94, 53, 177),
|
||||
array(81, 45, 168),
|
||||
array(69, 39, 160),
|
||||
array(49, 27, 146),
|
||||
array(179, 136, 255),
|
||||
array(124, 77, 255),
|
||||
array(101, 31, 255),
|
||||
array(98, 0, 234),
|
||||
array(197, 202, 233),
|
||||
array(159, 168, 218),
|
||||
array(121, 134, 203),
|
||||
array(92, 107, 192),
|
||||
array(63, 81, 181),
|
||||
array(57, 73, 171),
|
||||
array(48, 63, 159),
|
||||
array(40, 53, 147),
|
||||
array(26, 35, 126),
|
||||
array(140, 158, 255),
|
||||
array(83, 109, 254),
|
||||
array(61, 90, 254),
|
||||
array(48, 79, 254),
|
||||
array(227, 242, 253),
|
||||
array(187, 222, 251),
|
||||
array(144, 202, 249),
|
||||
array(100, 181, 246),
|
||||
array(66, 165, 245),
|
||||
array(33, 150, 243),
|
||||
array(30, 136, 229),
|
||||
array(25, 118, 210),
|
||||
array(21, 101, 192),
|
||||
array(13, 71, 161),
|
||||
array(130, 177, 255),
|
||||
array(68, 138, 255),
|
||||
array(41, 121, 255),
|
||||
array(41, 98, 255),
|
||||
array(179, 229, 252),
|
||||
array(129, 212, 250),
|
||||
array(79, 195, 247),
|
||||
array(41, 182, 252),
|
||||
array(3, 169, 244),
|
||||
array(3, 155, 229),
|
||||
array(2, 136, 209),
|
||||
array(2, 119, 189),
|
||||
array(1, 87, 155),
|
||||
array(128, 216, 255),
|
||||
array(64, 196, 255),
|
||||
array(0, 176, 255),
|
||||
array(0, 145, 234),
|
||||
array(178, 235, 242),
|
||||
array(128, 222, 234),
|
||||
array(77, 208, 225),
|
||||
array(38, 198, 218),
|
||||
array(0, 188, 212),
|
||||
array(0, 172, 193),
|
||||
array(0, 151, 167),
|
||||
array(0, 131, 143),
|
||||
array(0, 96, 100),
|
||||
array(132, 255, 255),
|
||||
array(24, 255, 255),
|
||||
array(0, 229, 255),
|
||||
array(0, 184, 212),
|
||||
array(178, 223, 219),
|
||||
array(128, 203, 196),
|
||||
array(77, 182, 172),
|
||||
array(38, 166, 154),
|
||||
array(0, 150, 136),
|
||||
array(0, 137, 123),
|
||||
array(0, 121, 107),
|
||||
array(0, 105, 92),
|
||||
array(0, 77, 64),
|
||||
array(167, 255, 235),
|
||||
array(100, 255, 218),
|
||||
array(29, 233, 182),
|
||||
array(0, 191, 165),
|
||||
array(165, 214, 167),
|
||||
array(129, 199, 132),
|
||||
array(102, 187, 106),
|
||||
array(76, 175, 80),
|
||||
array(67, 160, 71),
|
||||
array(56, 142, 60),
|
||||
array(46, 125, 50),
|
||||
array(27, 94, 32),
|
||||
array(185, 246, 202),
|
||||
array(105, 240, 174),
|
||||
array(0, 230, 118),
|
||||
array(0, 200, 83),
|
||||
array(197, 225, 165),
|
||||
array(174, 213, 129),
|
||||
array(156, 204, 101),
|
||||
array(139, 195, 74),
|
||||
array(124, 179, 66),
|
||||
array(104, 159, 56),
|
||||
array(85, 139, 47),
|
||||
array(51, 105, 30),
|
||||
array(178, 255, 89),
|
||||
array(118, 255, 3),
|
||||
array(100, 221, 23),
|
||||
array(249, 251, 231),
|
||||
array(240, 244, 195),
|
||||
array(230, 238, 156),
|
||||
array(220, 231, 117),
|
||||
array(212, 225, 87),
|
||||
array(205, 220, 57),
|
||||
array(192, 202, 51),
|
||||
array(164, 180, 43),
|
||||
array(158, 157, 36),
|
||||
array(130, 119, 23),
|
||||
array(198, 255, 0),
|
||||
array(174, 234, 0),
|
||||
array(251, 192, 45),
|
||||
array(249, 168, 37),
|
||||
array(245, 127, 23),
|
||||
array(255, 179, 0),
|
||||
array(255, 160, 0),
|
||||
array(255, 143, 0),
|
||||
array(255, 111, 0),
|
||||
array(255, 171, 0),
|
||||
array(255, 183, 77),
|
||||
array(255, 167, 38),
|
||||
array(255, 152, 0),
|
||||
array(251, 140, 0),
|
||||
array(245, 124, 0),
|
||||
array(239, 108, 0),
|
||||
array(230, 81, 0),
|
||||
array(255, 171, 64),
|
||||
array(255, 145, 0),
|
||||
array(255, 109, 0),
|
||||
array(255, 171, 145),
|
||||
array(255, 138, 101),
|
||||
array(255, 112, 67),
|
||||
array(255, 87, 34),
|
||||
array(244, 81, 30),
|
||||
array(230, 74, 25),
|
||||
array(216, 67, 21),
|
||||
array(191, 54, 12),
|
||||
array(255, 158, 128),
|
||||
array(255, 110, 64),
|
||||
array(255, 61, 0),
|
||||
array(221, 38, 0),
|
||||
array(188, 170, 164),
|
||||
array(161, 136, 127),
|
||||
array(141, 110, 99),
|
||||
array(121, 85, 72),
|
||||
array(109, 76, 65),
|
||||
array(93, 64, 55),
|
||||
array(78, 52, 46),
|
||||
array(62, 39, 35),
|
||||
array(189, 189, 189),
|
||||
array(158, 158, 158),
|
||||
array(117, 117, 117),
|
||||
array(97, 97, 97),
|
||||
array(66, 66, 66),
|
||||
array(33, 33, 33),
|
||||
array(236, 239, 241),
|
||||
array(176, 187, 197),
|
||||
array(144, 164, 174),
|
||||
array(120, 144, 156),
|
||||
array(96, 125, 139),
|
||||
array(84, 110, 122),
|
||||
array(69, 90, 100),
|
||||
array(55, 71, 79),
|
||||
array(38, 50, 56)
|
||||
);
|
||||
$BackgroundColorIndex = mt_rand(0, count($MaterialDesignColor) - 1);
|
||||
$BackgroundColor = imagecolorallocate($this->Avatar,
|
||||
$MaterialDesignColor[$BackgroundColorIndex][0],
|
||||
$MaterialDesignColor[$BackgroundColorIndex][1],
|
||||
$MaterialDesignColor[$BackgroundColorIndex][2]
|
||||
);
|
||||
if($this->Shape == 1){
|
||||
//画一个矩形
|
||||
imagerectangle($this->Avatar,
|
||||
0,
|
||||
0,
|
||||
$Width,
|
||||
$Height,
|
||||
$BackgroundColor
|
||||
);
|
||||
imagefilledrectangle($this->Avatar,
|
||||
0,
|
||||
0,
|
||||
$Width,
|
||||
$Height,
|
||||
$BackgroundColor
|
||||
);
|
||||
}
|
||||
else{
|
||||
//画一个居中圆形
|
||||
imagefilledellipse($this->Avatar,
|
||||
$Width / 2,
|
||||
$Height / 2,
|
||||
$Width,
|
||||
$Height,
|
||||
$BackgroundColor
|
||||
);
|
||||
}
|
||||
//字体
|
||||
$FontColor = imagecolorallocate($this->Avatar, 255, 255, 255);
|
||||
if ($this->IsNotLetter) {
|
||||
//中文字符偏移
|
||||
$FontSize = $Width - $Padding * 3.5;
|
||||
$X = $Padding + (-2 / 166) * $FontSize;
|
||||
$Y = $Height - $Padding - (23.5 / 166) * $FontSize;
|
||||
} else {
|
||||
$FontSize = $Width - $Padding * 2;
|
||||
$X = $Padding + (20 / 196) * $FontSize;
|
||||
$Y = $Height - $Padding - (13 / 196) * $FontSize;
|
||||
}
|
||||
// 在圆正中央填入字符
|
||||
imagettftext($this->Avatar, $FontSize,0,intval($X), intval($Y),$FontColor,$this->FontFile, $this->Char);
|
||||
}
|
||||
|
||||
private function Resize($TargetSize)
|
||||
{
|
||||
if (isset($this->Avatar)) {
|
||||
if ($this->AvatarSize > $TargetSize) {
|
||||
$Percent = $TargetSize / $this->AvatarSize;
|
||||
$TargetWidth = round($this->AvatarSize * $Percent);
|
||||
$TargetHeight = round($this->AvatarSize * $Percent);
|
||||
$TargetImageData = imagecreatetruecolor($TargetWidth, $TargetHeight);
|
||||
//全透明背景
|
||||
imageSaveAlpha($TargetImageData, true);
|
||||
$BackgroundAlpha = imagecolorallocatealpha($TargetImageData, 255, 255, 255, 127);
|
||||
imagefill($TargetImageData, 0, 0, $BackgroundAlpha);
|
||||
imagecopyresampled($TargetImageData, $this->Avatar, 0, 0, 0, 0, $TargetWidth, $TargetHeight, $this->AvatarSize, $this->AvatarSize);
|
||||
return $TargetImageData;
|
||||
} else {
|
||||
return $this->Avatar;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function Free()
|
||||
{
|
||||
imagedestroy($this->Avatar);
|
||||
}
|
||||
|
||||
public function Output2Browser($AvatarSize = 0)
|
||||
{
|
||||
if (!$AvatarSize) {
|
||||
$AvatarSize = $this->AvatarSize;
|
||||
}
|
||||
header('Content-Type: image/png');
|
||||
return imagepng($this->Resize($AvatarSize));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get image resource identifier
|
||||
* @param int $AvatarSize
|
||||
* @return resource
|
||||
*/
|
||||
public function Output2ImageResource($AvatarSize = 0)
|
||||
{
|
||||
if (!$AvatarSize) {
|
||||
$AvatarSize = $this->AvatarSize;
|
||||
}
|
||||
return $this->Resize($AvatarSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output Base64 encoded image data
|
||||
* @param int $AvatarSize
|
||||
* @return string
|
||||
*/
|
||||
public function Output2Base64($AvatarSize = 0)
|
||||
{
|
||||
if (!$AvatarSize) {
|
||||
$AvatarSize = $this->AvatarSize;
|
||||
}
|
||||
|
||||
ob_start();
|
||||
imagepng($this->Resize($AvatarSize));
|
||||
$content = ob_get_contents();
|
||||
ob_end_clean();
|
||||
|
||||
return 'data:image/png;base64,' . base64_encode($content);
|
||||
}
|
||||
|
||||
public function Save($Path, $AvatarSize = 0)
|
||||
{
|
||||
if (!$AvatarSize) {
|
||||
$AvatarSize = $this->AvatarSize;
|
||||
}
|
||||
return imagepng($this->Resize($AvatarSize), $Path);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,524 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
namespace backup;
|
||||
use think\facade\Config;
|
||||
class Backup
|
||||
{
|
||||
private $fp;
|
||||
/**
|
||||
* 备份文件信息 part - 卷号,name - 文件名
|
||||
* @var array
|
||||
*/
|
||||
private $file;
|
||||
/**
|
||||
* 当前打开文件大小
|
||||
* @var integer
|
||||
*/
|
||||
private $size = 0;
|
||||
|
||||
/**
|
||||
* 数据库配置
|
||||
* @var integer
|
||||
*/
|
||||
private $dbconfig = array();
|
||||
/**
|
||||
* 备份配置
|
||||
* @var integer
|
||||
*/
|
||||
private $config = array(
|
||||
//数据库备份路径
|
||||
'path' => './backup/',
|
||||
//数据库备份卷大小,默认10MB
|
||||
'part' => 10485760,
|
||||
//数据库备份文件是否启用压缩 0不压缩 1 压缩
|
||||
'compress' => 0,
|
||||
//数压缩级别
|
||||
'level' => 9,
|
||||
);
|
||||
/**
|
||||
* 数据库备份构造方法
|
||||
* @param array $file 备份或还原的文件信息
|
||||
* @param array $config 备份配置信息
|
||||
*/
|
||||
public function __construct($config = [])
|
||||
{
|
||||
$this->config = array_merge($this->config, $config);
|
||||
//初始化文件名
|
||||
$this->setFile();
|
||||
//初始化数据库连接参数
|
||||
$this->setDbConn();
|
||||
//检查文件是否可写
|
||||
if (!$this->checkPath($this->config['path'])) {
|
||||
throw new \think\Exception("The current directory is not writable");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 设置脚本运行超时时间
|
||||
* 0表示不限制,支持连贯操作
|
||||
*/
|
||||
public function setTimeout($time=null)
|
||||
{
|
||||
if (!is_null($time)) {
|
||||
set_time_limit($time)||ini_set("max_execution_time", $time);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* 设置数据库连接必备参数
|
||||
* @param array $dbconfig 数据库连接配置信息
|
||||
* @return object
|
||||
*/
|
||||
public function setDbConn($dbconfig = [])
|
||||
{
|
||||
if (empty($dbconfig)) {
|
||||
$this->dbconfig = Config::get('database');
|
||||
} else {
|
||||
$this->dbconfig = $dbconfig;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* 设置备份文件名
|
||||
* @param Array $file 文件名字
|
||||
* @return object
|
||||
*/
|
||||
public function setFile($file = null)
|
||||
{
|
||||
if (is_null($file)) {
|
||||
$this->file = ['name' => date('Ymd-His'), 'part' => 1];
|
||||
} else {
|
||||
if (!array_key_exists("name", $file) && !array_key_exists("part", $file)) {
|
||||
$this->file = $file['1'];
|
||||
} else {
|
||||
$this->file = $file;
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
//数据类连接
|
||||
public static function connect()
|
||||
{
|
||||
return \think\facade\Db::connect();
|
||||
}
|
||||
//数据库表列表
|
||||
public function dataList($table = null,$type=1)
|
||||
{
|
||||
$db = self::connect();
|
||||
if (is_null($table)) {
|
||||
$list = $db->query("SHOW TABLE STATUS");
|
||||
} else {
|
||||
if ($type) {
|
||||
$list = $db->query("SHOW FULL COLUMNS FROM {$table}");
|
||||
}else{
|
||||
$list = $db->query("show columns from {$table}");
|
||||
}
|
||||
}
|
||||
return array_map('array_change_key_case', $list);
|
||||
//$list;
|
||||
}
|
||||
//数据库备份文件列表
|
||||
public function fileList()
|
||||
{
|
||||
if (!is_dir($this->config['path'])) {
|
||||
mkdir($this->config['path'], 0755, true);
|
||||
}
|
||||
$path = realpath($this->config['path']);
|
||||
$flag = \FilesystemIterator::KEY_AS_FILENAME;
|
||||
$glob = new \FilesystemIterator($path, $flag);
|
||||
$list = array();
|
||||
foreach ($glob as $name => $file) {
|
||||
if (preg_match('/^\\d{8,8}-\\d{6,6}-\\d+\\.sql(?:\\.gz)?$/', $name)) {
|
||||
$name1= $name;
|
||||
$name = sscanf($name, '%4s%2s%2s-%2s%2s%2s-%d');
|
||||
$date = "{$name[0]}-{$name[1]}-{$name[2]}";
|
||||
$time = "{$name[3]}:{$name[4]}:{$name[5]}";
|
||||
$part = $name[6];
|
||||
if (isset($list["{$date} {$time}"])) {
|
||||
$info = $list["{$date} {$time}"];
|
||||
$info['part'] = max($info['part'], $part);
|
||||
$info['size'] = $info['size'] + $file->getSize();
|
||||
} else {
|
||||
$info['part'] = $part;
|
||||
$info['size'] = $file->getSize();
|
||||
}
|
||||
$extension = strtoupper(pathinfo($file->getFilename(), PATHINFO_EXTENSION));
|
||||
$info['name']=$name1;
|
||||
$info['compress'] = $extension === 'SQL' ? '-' : $extension;
|
||||
$info['time'] = strtotime("{$date} {$time}");
|
||||
$list["{$date} {$time}"] = $info;
|
||||
}
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
public function getFile($type = '', $time = 0)
|
||||
{
|
||||
//
|
||||
if (!is_numeric($time)) {
|
||||
throw new \think\Exception("{$time} Illegal data type");
|
||||
}
|
||||
switch ($type) {
|
||||
case 'time':
|
||||
$name = date('Ymd-His', $time) . '-*.sql*';
|
||||
$path = realpath($this->config['path']) . DIRECTORY_SEPARATOR . $name;
|
||||
return glob($path);
|
||||
break;
|
||||
case 'timeverif':
|
||||
$name = date('Ymd-His', $time) . '-*.sql*';
|
||||
$path = realpath($this->config['path']) . DIRECTORY_SEPARATOR . $name;
|
||||
$files = glob($path);
|
||||
$list = array();
|
||||
foreach ($files as $name) {
|
||||
$basename = basename($name);
|
||||
$match = sscanf($basename, '%4s%2s%2s-%2s%2s%2s-%d');
|
||||
$gz = preg_match('/^\\d{8,8}-\\d{6,6}-\\d+\\.sql.gz$/', $basename);
|
||||
$list[$match[6]] = array($match[6], $name, $gz);
|
||||
}
|
||||
$last = end($list);
|
||||
if (count($list) === $last[0]) {
|
||||
return $list;
|
||||
} else {
|
||||
throw new \think\Exception("File {$files['0']} may be damaged, please check again");
|
||||
}
|
||||
break;
|
||||
case 'pathname':
|
||||
return "{$this->config['path']}{$this->file['name']}-{$this->file['part']}.sql";
|
||||
break;
|
||||
case 'filename':
|
||||
return "{$this->file['name']}-{$this->file['part']}.sql";
|
||||
break;
|
||||
case 'filepath':
|
||||
return $this->config['path'];
|
||||
break;
|
||||
default:
|
||||
$arr = array('pathname' => "{$this->config['path']}{$this->file['name']}-{$this->file['part']}.sql", 'filename' => "{$this->file['name']}-{$this->file['part']}.sql", 'filepath' => $this->config['path'], 'file' => $this->file);
|
||||
return $arr;
|
||||
}
|
||||
}
|
||||
//删除备份文件
|
||||
public function delFile($time)
|
||||
{
|
||||
if ($time) {
|
||||
$file = $this->getFile('time', $time);
|
||||
array_map("unlink", $this->getFile('time', $time));
|
||||
if (count($this->getFile('time', $time))) {
|
||||
throw new \think\Exception("File {$path} deleted failed");
|
||||
} else {
|
||||
return $time;
|
||||
}
|
||||
} else {
|
||||
throw new \think\Exception("{$time} Time parameter is incorrect");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 下载备份
|
||||
* @param string $time
|
||||
* @param integer $part
|
||||
* @return array|mixed|string
|
||||
*/
|
||||
public function downloadFile($time, $part = 0)
|
||||
{
|
||||
$file = $this->getFile('time', $time);
|
||||
$fileName = $file[$part];
|
||||
if (file_exists($fileName)) {
|
||||
ob_end_clean();
|
||||
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
|
||||
header('Content-Description: File Transfer');
|
||||
header('Content-Type: application/octet-stream');
|
||||
header('Content-Length: ' . filesize($fileName));
|
||||
header('Content-Disposition: attachment; filename=' . basename($fileName));
|
||||
readfile($fileName);
|
||||
} else {
|
||||
throw new \think\Exception("{$time} File is abnormal");
|
||||
}
|
||||
}
|
||||
public function import($start,$time,$part)
|
||||
{
|
||||
//还原数据
|
||||
$db = self::connect();
|
||||
//$this->file=$this->getFile('time',$time);
|
||||
$file = $this->getFile('time', $time);
|
||||
$fileName = $file[$part-1];
|
||||
if (!file_exists($fileName)) {
|
||||
return false;
|
||||
}
|
||||
if ($this->config['compress']) {
|
||||
$gz = gzopen($fileName, 'r');
|
||||
$size = 0;
|
||||
} else {
|
||||
$size = filesize($fileName);
|
||||
$gz = fopen($fileName, 'r');
|
||||
}
|
||||
$sql = '';
|
||||
if ($start) {
|
||||
$this->config['compress'] ? gzseek($gz, $start) : fseek($gz, $start);
|
||||
}
|
||||
for ($i = 0; $i < 1000; $i++) {
|
||||
$sql .= $this->config['compress'] ? gzgets($gz) : fgets($gz);
|
||||
if (preg_match('/.*;$/', trim($sql))) {
|
||||
if (false !== $db->execute($sql)) {
|
||||
$start += strlen($sql);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
$sql = '';
|
||||
} elseif ($this->config['compress'] ? gzeof($gz) : feof($gz)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return array($start, $size);
|
||||
}
|
||||
/**
|
||||
* 写入初始数据
|
||||
* @return boolean true - 写入成功,false - 写入失败
|
||||
*/
|
||||
public function Backup_Init()
|
||||
{
|
||||
$sql = "-- -----------------------------\n";
|
||||
$sql .= "-- Think MySQL Data Transfer \n";
|
||||
$sql .= "-- \n";
|
||||
$sql .= "-- \n";
|
||||
$sql .= "-- Part : #{$this->file['part']}\n";
|
||||
$sql .= "-- Date : " . date("Y-m-d H:i:s") . "\n";
|
||||
$sql .= "-- -----------------------------\n\n";
|
||||
$sql .= "SET FOREIGN_KEY_CHECKS = 0;\n\n";
|
||||
return $this->write($sql);
|
||||
}
|
||||
/**
|
||||
* 备份表结构
|
||||
* @param string $table 表名
|
||||
* @param integer $start 起始行数
|
||||
* @return boolean false - 备份失败
|
||||
*/
|
||||
public function backup($table, $start)
|
||||
{
|
||||
$db = self::connect();
|
||||
// 备份表结构
|
||||
if (0 == $start) {
|
||||
$result = $db->query("SHOW CREATE TABLE `{$table}`");
|
||||
$sql = "\n";
|
||||
$sql .= "-- -----------------------------\n";
|
||||
$sql .= "-- Table structure for `{$table}`\n";
|
||||
$sql .= "-- -----------------------------\n";
|
||||
$sql .= "DROP TABLE IF EXISTS `{$table}`;\n";
|
||||
$sql .= trim($result[0]['Create Table']) . ";\n\n";
|
||||
if (false === $this->write($sql)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//数据总数
|
||||
$result = $db->query("SELECT COUNT(*) AS count FROM `{$table}`");
|
||||
$count = $result['0']['count'];
|
||||
//备份表数据
|
||||
if ($count) {
|
||||
//写入数据注释
|
||||
if (0 == $start) {
|
||||
$sql = "-- -----------------------------\n";
|
||||
$sql .= "-- Records of `{$table}`\n";
|
||||
$sql .= "-- -----------------------------\n";
|
||||
$this->write($sql);
|
||||
}
|
||||
//备份数据记录
|
||||
$result = $db->query("SELECT * FROM `{$table}` LIMIT {$start}, 1000");
|
||||
foreach ($result as $row) {
|
||||
$sql = "INSERT INTO `{$table}` VALUES ('" . str_replace(array("\r", "\n"), array('\\r', '\\n'), implode("', '", $row)) . "');\n";
|
||||
if (false === $this->write($sql)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//还有更多数据
|
||||
if ($count > $start + 1000) {
|
||||
return $this->backup($table, $start + 1000);
|
||||
}
|
||||
}
|
||||
//备份下一表
|
||||
return 0;
|
||||
}
|
||||
/**
|
||||
* 优化表
|
||||
* @param String $tables 表名
|
||||
* @return String $tables
|
||||
*/
|
||||
public function optimize($tables = null)
|
||||
{
|
||||
if ($tables) {
|
||||
$db = self::connect();
|
||||
if (is_array($tables)) {
|
||||
$tables = implode('`,`', $tables);
|
||||
$list = $db->query("OPTIMIZE TABLE `{$tables}`");
|
||||
} else {
|
||||
$list = $db->query("OPTIMIZE TABLE `{$tables}`");
|
||||
}
|
||||
if ($list) {
|
||||
return $tables;
|
||||
} else {
|
||||
throw new \think\Exception("data sheet'{$tables}'Repair mistakes please try again!");
|
||||
}
|
||||
} else {
|
||||
throw new \think\Exception("Please specify the table to be repaired!");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 修复表
|
||||
* @param String $tables 表名
|
||||
* @return String $tables
|
||||
*/
|
||||
public function repair($tables = null)
|
||||
{
|
||||
if ($tables) {
|
||||
$db = self::connect();
|
||||
if (is_array($tables)) {
|
||||
$tables = implode('`,`', $tables);
|
||||
$list = $db->query("REPAIR TABLE `{$tables}`");
|
||||
} else {
|
||||
$list = $db->query("REPAIR TABLE `{$tables}`");
|
||||
}
|
||||
if ($list) {
|
||||
return $list;
|
||||
} else {
|
||||
throw new \think\Exception("data sheet'{$tables}'Repair mistakes please try again!");
|
||||
}
|
||||
} else {
|
||||
throw new \think\Exception("Please specify the table to be repaired!");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 写入SQL语句
|
||||
* @param string $sql 要写入的SQL语句
|
||||
* @return boolean true - 写入成功,false - 写入失败!
|
||||
*/
|
||||
private function write($sql)
|
||||
{
|
||||
$size = strlen($sql);
|
||||
//由于压缩原因,无法计算出压缩后的长度,这里假设压缩率为50%,
|
||||
//一般情况压缩率都会高于50%;
|
||||
$size = $this->config['compress'] ? $size / 2 : $size;
|
||||
$this->open($size);
|
||||
return $this->config['compress'] ? @gzwrite($this->fp, $sql) : @fwrite($this->fp, $sql);
|
||||
}
|
||||
/**
|
||||
* 打开一个卷,用于写入数据
|
||||
* @param integer $size 写入数据的大小
|
||||
*/
|
||||
private function open($size)
|
||||
{
|
||||
if ($this->fp) {
|
||||
$this->size += $size;
|
||||
if ($this->size > $this->config['part']) {
|
||||
$this->config['compress'] ? @gzclose($this->fp) : @fclose($this->fp);
|
||||
$this->fp = null;
|
||||
$this->file['part']++;
|
||||
session('backup_file', $this->file);
|
||||
$this->Backup_Init();
|
||||
}
|
||||
} else {
|
||||
$backuppath = $this->config['path'];
|
||||
$filename = "{$backuppath}{$this->file['name']}-{$this->file['part']}.sql";
|
||||
if ($this->config['compress']) {
|
||||
$filename = "{$filename}.gz";
|
||||
$this->fp = @gzopen($filename, "a{$this->config['level']}");
|
||||
} else {
|
||||
$this->fp = @fopen($filename, 'a');
|
||||
}
|
||||
$this->size = filesize($filename) + $size;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 检查目录是否可写
|
||||
* @param string $path 目录
|
||||
* @return boolean
|
||||
*/
|
||||
protected function checkPath($path)
|
||||
{
|
||||
if (is_dir($path)) {
|
||||
return true;
|
||||
}
|
||||
if (mkdir($path, 0755, true)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 析构方法,用于关闭文件资源
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if($this->fp){
|
||||
$this->config['compress'] ? @gzclose($this->fp) : @fclose($this->fp);
|
||||
}
|
||||
}
|
||||
|
||||
//判断某数据表是否存在
|
||||
public function check_table($table){
|
||||
$prefix = config('database.connections.mysql.prefix');
|
||||
$db = self::connect();
|
||||
$res = $db->query('SHOW TABLES LIKE '."'".$prefix.$table."'");
|
||||
if($res){
|
||||
return 1;
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
//判断某数据表中某字段是否存在
|
||||
public function check_column($table,$column){
|
||||
$prefix = config('database.mysql.prefix');
|
||||
$db = self::connect();
|
||||
$res = $db->query('select count(*) from information_schema.columns where table_name = '."'".$table."' ". 'and column_name ='."'".$column."'");
|
||||
if($res[0]['count(*)'] != 0){
|
||||
return 1;
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
//添加字段
|
||||
public function add_column($table,$column,$type,$condition,$after){
|
||||
$db = self::connect();
|
||||
$res = $db->execute('alter table'." `".$table."` ".'add'." `".$column."` ".$type." ".$condition." ".'after'." `".$after."`");
|
||||
if($res){
|
||||
return 1;
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
//删除字段
|
||||
public function del_column($table,$column){
|
||||
$db = self::connect();
|
||||
$res = $db->execute('alter table '."`".$table."`".' drop column'."`".$column."`");
|
||||
if($res){
|
||||
return 1;
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
//修改字段
|
||||
public function update_column($table,$old_column,$column,$type){
|
||||
//字段名和类型同时修改才会返回1不然返回0
|
||||
$db = self::connect();
|
||||
$res = $db->execute('alter table' ." `".$table."` ". 'change' ." `".$old_column."` " ."`".$column."`" .$type);
|
||||
if($res){
|
||||
return 1;
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
//执行sql
|
||||
public function run_sql($sql){
|
||||
$prefix = config('database.connections.mysql.prefix');
|
||||
$db = self::connect();
|
||||
$sql_array = preg_split("/;[\r\n]+/", str_replace("oa_", $prefix, $sql));
|
||||
foreach ($sql_array as $k => $v) {
|
||||
if (!empty($v)) {
|
||||
try {
|
||||
$res = $db->query($v);
|
||||
} catch (\Exception $e) {
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,545 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace dateset;
|
||||
|
||||
/**
|
||||
* 日期时间处理类
|
||||
*/
|
||||
class Dateset
|
||||
{
|
||||
const YEAR = 31536000;
|
||||
const MONTH = 2592000;
|
||||
const WEEK = 604800;
|
||||
const DAY = 86400;
|
||||
const HOUR = 3600;
|
||||
const MINUTE = 60;
|
||||
|
||||
/**
|
||||
* 间隔时间段格式化
|
||||
* @param int $time 时间戳
|
||||
* @param string $format 格式 【d:显示到天 i显示到分钟 s显示到秒】
|
||||
* @return string
|
||||
*/
|
||||
function time_trans($time, $format = 'd')
|
||||
{
|
||||
$now = time();
|
||||
$diff = $now - $time;
|
||||
if ($diff < self::MINUTE) {
|
||||
return '1分钟前';
|
||||
} else if ($diff < self::HOUR) {
|
||||
return floor($diff / self::MINUTE) . '分钟前';
|
||||
} else if ($diff < self::DAY) {
|
||||
return floor($diff / self::HOUR) . '小时前';
|
||||
}
|
||||
$yes_start_time = strtotime(date('Y-m-d 00:00:00', strtotime('-1 days'))); //昨天开始时间
|
||||
$yes_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-1 days'))); //昨天结束时间
|
||||
$two_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-2 days'))); //2天前结束时间
|
||||
$three_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-3 days'))); //3天前结束时间
|
||||
$four_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-4 days'))); //4天前结束时间
|
||||
$five_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-5 days'))); //5天前结束时间
|
||||
$six_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-6 days'))); //6天前结束时间
|
||||
$seven_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-7 days'))); //7天前结束时间
|
||||
|
||||
if ($time > $yes_start_time && $time < $yes_end_time) {
|
||||
return '昨天';
|
||||
}
|
||||
|
||||
if ($time > $yes_start_time && $time < $two_end_time) {
|
||||
return '1天前';
|
||||
}
|
||||
|
||||
if ($time > $yes_start_time && $time < $three_end_time) {
|
||||
return '2天前';
|
||||
}
|
||||
|
||||
if ($time > $yes_start_time && $time < $four_end_time) {
|
||||
return '3天前';
|
||||
}
|
||||
|
||||
if ($time > $yes_start_time && $time < $five_end_time) {
|
||||
return '4天前';
|
||||
}
|
||||
|
||||
if ($time > $yes_start_time && $time < $six_end_time) {
|
||||
return '5天前';
|
||||
}
|
||||
|
||||
if ($time > $yes_start_time && $time < $seven_end_time) {
|
||||
return '6天前';
|
||||
}
|
||||
|
||||
switch ($format) {
|
||||
case 'd':
|
||||
$show_time = date('Y-m-d', $time);
|
||||
break;
|
||||
case 'i':
|
||||
$show_time = date('Y-m-d H:i', $time);
|
||||
break;
|
||||
case 's':
|
||||
$show_time = date('Y-m-d H:i:s', $time);
|
||||
break;
|
||||
}
|
||||
return $show_time;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 计算两个时间戳之间相差的时间
|
||||
*
|
||||
* $differ = self::differ(60, 182, 'minutes,seconds'); // array('minutes' => 2, 'seconds' => 2)
|
||||
* $differ = self::differ(60, 182, 'minutes'); // 2
|
||||
*
|
||||
* @param int $remote timestamp to find the span of
|
||||
* @param int $local timestamp to use as the baseline
|
||||
* @param string $output formatting string
|
||||
* @return string when only a single output is requested
|
||||
* @return array associative list of all outputs requested
|
||||
* @from https://github.com/kohana/ohanzee-helpers/blob/master/src/Date.php
|
||||
*/
|
||||
public static function differ($remote, $local = null, $output = 'years,months,weeks,days,hours,minutes,seconds')
|
||||
{
|
||||
// Normalize output
|
||||
$output = trim(strtolower((string)$output));
|
||||
if (!$output) {
|
||||
// Invalid output
|
||||
return false;
|
||||
}
|
||||
// Array with the output formats
|
||||
$output = preg_split('/[^a-z]+/', $output);
|
||||
// Convert the list of outputs to an associative array
|
||||
$output = array_combine($output, array_fill(0, count($output), 0));
|
||||
// Make the output values into keys
|
||||
extract(array_flip($output), EXTR_SKIP);
|
||||
if ($local === null) {
|
||||
// Calculate the span from the current time
|
||||
$local = time();
|
||||
}
|
||||
// Calculate timespan (seconds)
|
||||
$timespan = abs($remote - $local);
|
||||
if (isset($output['years'])) {
|
||||
$timespan -= self::YEAR * ($output['years'] = (int)floor($timespan / self::YEAR));
|
||||
}
|
||||
if (isset($output['months'])) {
|
||||
$timespan -= self::MONTH * ($output['months'] = (int)floor($timespan / self::MONTH));
|
||||
}
|
||||
if (isset($output['weeks'])) {
|
||||
$timespan -= self::WEEK * ($output['weeks'] = (int)floor($timespan / self::WEEK));
|
||||
}
|
||||
if (isset($output['days'])) {
|
||||
$timespan -= self::DAY * ($output['days'] = (int)floor($timespan / self::DAY));
|
||||
}
|
||||
if (isset($output['hours'])) {
|
||||
$timespan -= self::HOUR * ($output['hours'] = (int)floor($timespan / self::HOUR));
|
||||
}
|
||||
if (isset($output['minutes'])) {
|
||||
$timespan -= self::MINUTE * ($output['minutes'] = (int)floor($timespan / self::MINUTE));
|
||||
}
|
||||
// Seconds ago, 1
|
||||
if (isset($output['seconds'])) {
|
||||
$output['seconds'] = $timespan;
|
||||
}
|
||||
if (count($output) === 1) {
|
||||
// Only a single output was requested, return it
|
||||
return array_pop($output);
|
||||
}
|
||||
// Return array
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定年月拥有的天数
|
||||
* @param int $month
|
||||
* @param int $year
|
||||
* @return false|int|string
|
||||
*/
|
||||
public static function days_in_month($month, $year)
|
||||
{
|
||||
if (function_exists("cal_days_in_month")) {
|
||||
return cal_days_in_month(CAL_GREGORIAN, $month, $year);
|
||||
} else {
|
||||
return date('t', mktime(0, 0, 0, $month, 1, $year));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将秒数转换为时间 (小时、分、秒)
|
||||
* @param
|
||||
*/
|
||||
function getTimeBySec($time,$second=true)
|
||||
{
|
||||
if (is_numeric($time)) {
|
||||
$value = array(
|
||||
"hours" => 0,
|
||||
"minutes" => 0, "seconds" => 0,
|
||||
);
|
||||
$t='';
|
||||
if ($time >= 3600) {
|
||||
$value["hours"] = floor($time / 3600);
|
||||
$time = ($time % 3600);
|
||||
$t .= $value["hours"] . "小时";
|
||||
}
|
||||
if ($time >= 60) {
|
||||
$value["minutes"] = floor($time / 60);
|
||||
$time = ($time % 60);
|
||||
$t .= $value["minutes"] . "分钟";
|
||||
}
|
||||
if ($time > 0 && $time < 60 && $second==true) {
|
||||
$value["seconds"] = floor($time);
|
||||
$t .= $value["seconds"] . "秒";
|
||||
}
|
||||
return $t;
|
||||
} else {
|
||||
return (bool)FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将秒数转换为时间 (年、天、小时、分、秒)
|
||||
* @param
|
||||
*/
|
||||
function getDateBySec($time,$second=false)
|
||||
{
|
||||
if (is_numeric($time)) {
|
||||
$value = array(
|
||||
"years" => 0, "days" => 0, "hours" => 0,
|
||||
"minutes" => 0, "seconds" => 0,
|
||||
);
|
||||
$t='';
|
||||
if ($time >= 31556926) {
|
||||
$value["years"] = floor($time / 31556926);
|
||||
$time = ($time % 31556926);
|
||||
$t .= $value["years"] . "年";
|
||||
}
|
||||
if ($time >= 86400) {
|
||||
$value["days"] = floor($time / 86400);
|
||||
$time = ($time % 86400);
|
||||
$t .= $value["days"] . "天";
|
||||
}
|
||||
if ($time >= 3600) {
|
||||
$value["hours"] = floor($time / 3600);
|
||||
$time = ($time % 3600);
|
||||
$t .= $value["hours"] . "小时";
|
||||
}
|
||||
if ($time >= 60) {
|
||||
$value["minutes"] = floor($time / 60);
|
||||
$time = ($time % 60);
|
||||
$t .= $value["minutes"] . "分钟";
|
||||
}
|
||||
if ($time < 60 && $second==true) {
|
||||
$value["seconds"] = floor($time);
|
||||
$t .= $value["seconds"] . "秒";
|
||||
}
|
||||
return $t;
|
||||
} else {
|
||||
return (bool)FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*根据年月计算有几天
|
||||
*/
|
||||
function getmonthByYM($param)
|
||||
{
|
||||
$month = $param['month'] ? $param['month'] : date('m', time());
|
||||
$year = $param['year'] ? $param['year'] : date('Y', time());
|
||||
if (in_array($month, array('1', '3', '5', '7', '8', '01', '03', '05', '07', '08', '10', '12'))) {
|
||||
$days = '31';
|
||||
} elseif ($month == 2) {
|
||||
if ($year % 400 == 0 || ($year % 4 == 0 && $year % 100 !== 0)) {
|
||||
//判断是否是闰年
|
||||
$days = '29';
|
||||
} else {
|
||||
$days = '28';
|
||||
}
|
||||
} else {
|
||||
$days = '30';
|
||||
}
|
||||
return $days;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据时间戳计算当月天数
|
||||
* @param
|
||||
*/
|
||||
function getmonthdays($time)
|
||||
{
|
||||
$month = date('m', $time);
|
||||
$year = date('Y', $time);
|
||||
if (in_array($month, array('1', '3', '5', '7', '8', '01', '03', '05', '07', '08', '10', '12'))) {
|
||||
$days = '31';
|
||||
} elseif ($month == 2) {
|
||||
if ($year % 400 == 0 || ($year % 4 == 0 && $year % 100 !== 0)) {
|
||||
//判断是否是闰年
|
||||
$days = '29';
|
||||
} else {
|
||||
$days = '28';
|
||||
}
|
||||
} else {
|
||||
$days = '30';
|
||||
}
|
||||
return $days;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成从开始时间到结束时间的日期数组
|
||||
* @param type,默认时间戳格式
|
||||
* @param type = 1 时,date格式
|
||||
* @param type = 2 时,获取每日开始、结束时间
|
||||
*/
|
||||
function dateList($start, $end, $type = 0)
|
||||
{
|
||||
if (!is_numeric($start) || !is_numeric($end) || ($end <= $start)) return '';
|
||||
$i = 0;
|
||||
//从开始日期到结束日期的每日时间戳数组
|
||||
$d = array();
|
||||
if ($type == 1) {
|
||||
while ($start <= $end) {
|
||||
$d[$i] = date('Y-m-d', $start);
|
||||
$start = $start + 86400;
|
||||
$i++;
|
||||
}
|
||||
} else {
|
||||
while ($start <= $end) {
|
||||
$d[$i] = $start;
|
||||
$start = $start + 86400;
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
if ($type == 2) {
|
||||
$list = array();
|
||||
foreach ($d as $k => $v) {
|
||||
$list[$k] = $this->getDateRange($v);
|
||||
}
|
||||
return $list;
|
||||
} else {
|
||||
return $d;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定日期开始时间与结束时间
|
||||
*/
|
||||
function getDateRange($timestamp)
|
||||
{
|
||||
$ret = array();
|
||||
$ret['sdate'] = strtotime(date('Y-m-d', $timestamp));
|
||||
$ret['edate'] = strtotime(date('Y-m-d', $timestamp)) + 86400;
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成从开始月份到结束月份的月份数组
|
||||
* @param int $start 开始时间戳
|
||||
* @param int $end 结束时间戳
|
||||
*/
|
||||
function monthList($start, $end)
|
||||
{
|
||||
if (!is_numeric($start) || !is_numeric($end) || ($end <= $start)) return '';
|
||||
$start = date('Y-m', $start);
|
||||
$end = date('Y-m', $end);
|
||||
//转为时间戳
|
||||
$start = strtotime($start . '-01');
|
||||
$end = strtotime($end . '-01');
|
||||
$i = 0;
|
||||
$d = array();
|
||||
while ($start <= $end) {
|
||||
//这里累加每个月的的总秒数 计算公式:上一月1号的时间戳秒数减去当前月的时间戳秒数
|
||||
$d[$i] = $start;
|
||||
$start += strtotime('+1 month', $start) - $start;
|
||||
$i++;
|
||||
}
|
||||
return $d;
|
||||
}
|
||||
|
||||
/**
|
||||
* 等于(时间段)数据处理
|
||||
*
|
||||
* @param $type
|
||||
* @return array
|
||||
* @since 2021-06-11
|
||||
* @author fanqi
|
||||
*/
|
||||
function advancedDate($type)
|
||||
{
|
||||
// 本年度
|
||||
if ($type == 'year') {
|
||||
$arrTime = DataTime::year();
|
||||
$start_time = date('Y-m-d 00:00:00', $arrTime[0]);
|
||||
$end_time = date('Y-m-d 23:59:59', $arrTime[1]);
|
||||
}
|
||||
|
||||
// 上一年度
|
||||
if ($type == 'lastYear') {
|
||||
$start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-1 year'));
|
||||
$end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 year'));
|
||||
}
|
||||
|
||||
// 下一年度
|
||||
if ($type == 'nextYear') {
|
||||
$start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 year'));
|
||||
$end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+1 year'));
|
||||
}
|
||||
|
||||
// 上半年
|
||||
if ($type == 'firstHalfYear') {
|
||||
$start_time = date('Y-01-01 00:00:00');
|
||||
$end_time = date('Y-06-30 23:59:59');
|
||||
}
|
||||
|
||||
// 下半年
|
||||
if ($type == 'nextHalfYear') {
|
||||
$start_time = date('Y-07-01 00:00:00');
|
||||
$end_time = date('Y-12-31 23:59:59');
|
||||
}
|
||||
|
||||
// 本季度
|
||||
if ($type == 'quarter') {
|
||||
$season = ceil((date('n')) / 3);
|
||||
$start_time = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y')));
|
||||
$end_time = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')));
|
||||
}
|
||||
|
||||
// 上一季度
|
||||
if ($type == 'lastQuarter') {
|
||||
$season = ceil((date('n')) / 3) - 1;
|
||||
$start_time = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y')));
|
||||
$end_time = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')));
|
||||
}
|
||||
|
||||
// 下一季度
|
||||
if ($type == 'nextQuarter') {
|
||||
$season = ceil((date('n')) / 3);
|
||||
$start_time = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 + 1, 1, date('Y')));
|
||||
$end_time = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3 + 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')));
|
||||
}
|
||||
|
||||
// 本月
|
||||
if ($type == 'month') {
|
||||
$start_time = date('Y-m-01 00:00:00');
|
||||
$end_time = date('Y-m-31 23:59:59');
|
||||
}
|
||||
|
||||
// 上月
|
||||
if ($type == 'lastMonth') {
|
||||
$start_time = date('Y-m-01 00:00:00', strtotime(date('Y-m-d') . '-1 month'));
|
||||
$end_time = date('Y-m-31 23:59:59', strtotime(date('Y-m-d') . '-1 month'));
|
||||
}
|
||||
|
||||
// 下月
|
||||
if ($type == 'nextMonth') {
|
||||
$start_time = date('Y-m-01 00:00:00', strtotime(date('Y-m-d') . '+1 month'));
|
||||
$end_time = date('Y-m-31 23:59:59', strtotime(date('Y-m-d') . '+1 month'));
|
||||
}
|
||||
|
||||
// 本周
|
||||
if ($type == 'week') {
|
||||
$start_time = date('Y-m-d 00:00:00', mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y')));
|
||||
$end_time = date('Y-m-d 23:59:59', mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('Y')));
|
||||
}
|
||||
|
||||
// 上周
|
||||
if ($type == 'lastWeek') {
|
||||
$date = date("Y-m-d");
|
||||
$w = date("w", strtotime($date));
|
||||
$d = $w ? $w - 1 : 6;
|
||||
$start = date("Y-m-d", strtotime($date . " - " . $d . " days"));
|
||||
$start_time = date('Y-m-d', strtotime($start . " - 7 days"));
|
||||
$end_time = date('Y-m-d', strtotime($start . " - 1 days"));
|
||||
}
|
||||
|
||||
// 下周
|
||||
if ($type == 'nextWeek') {
|
||||
$date = date("Y-m-d");
|
||||
$w = date("w", strtotime($date));
|
||||
$d = $w ? $w - 1 : 6;
|
||||
$start = date("Y-m-d", strtotime($date . " - " . $d . " days"));
|
||||
$start_time = date('Y-m-d', strtotime($start . " + 7 days"));
|
||||
$end_time = date('Y-m-d', strtotime($start . " + 13 days"));
|
||||
}
|
||||
|
||||
// 今天
|
||||
if ($type == 'today') {
|
||||
$start_time = date('Y-m-d 00:00:00');
|
||||
$end_time = date('Y-m-d 23:59:59');
|
||||
}
|
||||
|
||||
// 昨天
|
||||
if ($type == 'yesterday') {
|
||||
$start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-1 day'));
|
||||
$end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
|
||||
}
|
||||
|
||||
// 明天
|
||||
if ($type == 'tomorrow') {
|
||||
$start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
|
||||
$end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+1 day'));
|
||||
}
|
||||
|
||||
// 过去3天
|
||||
if ($type == 'previous3day') {
|
||||
$start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-3 day'));
|
||||
$end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
|
||||
}
|
||||
|
||||
// 过去5天
|
||||
if ($type == 'previous5day') {
|
||||
$start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-5 day'));
|
||||
$end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
|
||||
}
|
||||
|
||||
// 过去7天
|
||||
if ($type == 'previous7day') {
|
||||
$start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-7 day'));
|
||||
$end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
|
||||
}
|
||||
// 过去10天
|
||||
if ($type == 'previous10day') {
|
||||
$start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-10 day'));
|
||||
$end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
|
||||
}
|
||||
// 过去30天
|
||||
if ($type == 'previous30day') {
|
||||
$start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-30 day'));
|
||||
$end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
|
||||
}
|
||||
// 未来3天
|
||||
if ($type == 'future3day') {
|
||||
$start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
|
||||
$end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+3 day'));
|
||||
}
|
||||
// 未来5天
|
||||
if ($type == 'future5day') {
|
||||
$start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
|
||||
$end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+5 day'));
|
||||
}
|
||||
// 未来7天
|
||||
if ($type == 'future7day') {
|
||||
$start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
|
||||
$end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+7 day'));
|
||||
}
|
||||
// 未来10天
|
||||
if ($type == 'future10day') {
|
||||
$start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
|
||||
$end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+10 day'));
|
||||
}
|
||||
// 未来30天
|
||||
if ($type == 'future30day') {
|
||||
$start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
|
||||
$end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+30 day'));
|
||||
}
|
||||
return [$start_time,$end_time];
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据时间戳获取星期几
|
||||
* @param $time 要转换的时间戳
|
||||
*/
|
||||
function getTimeWeek($time, $i = 0)
|
||||
{
|
||||
$weekarray = array("日", "一", "二", "三", "四", "五", "六");
|
||||
$oneD = 24 * 60 * 60;
|
||||
return "星期" . $weekarray[date("w", $time + $oneD * $i)];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
+-----------------------------------------------------------------------------------------------
|
||||
* GouGuOPEN [ 左手研发,右手开源,未来可期!]
|
||||
+-----------------------------------------------------------------------------------------------
|
||||
* @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
|
||||
+-----------------------------------------------------------------------------------------------
|
||||
* @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
|
||||
+-----------------------------------------------------------------------------------------------
|
||||
* @Author 勾股工作室 <hdm58@qq.com>
|
||||
+-----------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
namespace Schedule;
|
||||
|
||||
class Schedule {
|
||||
// Tests whether the given ISO8601 string has a time-of-day or not
|
||||
const ALL_DAY_REGEX = '/^\d{4}-\d\d-\d\d$/'; // matches strings like "2013-12-29"
|
||||
|
||||
public $title;
|
||||
public $allDay; // a boolean
|
||||
public $start; // a DateTime
|
||||
public $end; // a DateTime, or null
|
||||
public $properties = array(); // an array of other misc properties
|
||||
|
||||
// Constructs an Event object from the given array of key=>values.
|
||||
// You can optionally force the timeZone of the parsed dates.
|
||||
public function __construct($array, $timeZone=null) {
|
||||
|
||||
$this->title = $array['title'];
|
||||
|
||||
if (isset($array['allDay'])) {
|
||||
// allDay has been explicitly specified
|
||||
$this->allDay = (bool)$array['allDay'];
|
||||
}
|
||||
else {
|
||||
// Guess allDay based off of ISO8601 date strings
|
||||
$this->allDay = preg_match(self::ALL_DAY_REGEX, $array['start']) &&
|
||||
(!isset($array['end']) || preg_match(self::ALL_DAY_REGEX, $array['end']));
|
||||
}
|
||||
|
||||
if ($this->allDay) {
|
||||
// If dates are allDay, we want to parse them in UTC to avoid DST issues.
|
||||
$timeZone = null;
|
||||
}
|
||||
|
||||
// Parse dates
|
||||
$this->start = parseDateTime($array['start'], $timeZone);
|
||||
$this->end = isset($array['end']) ? parseDateTime($array['end'], $timeZone) : null;
|
||||
|
||||
// Record misc properties
|
||||
foreach ($array as $name => $value) {
|
||||
if (!in_array($name, array('title', 'allDay', 'start', 'end'))) {
|
||||
$this->properties[$name] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Returns whether the date range of our event intersects with the given all-day range.
|
||||
// $rangeStart and $rangeEnd are assumed to be dates in UTC with 00:00:00 time.
|
||||
public function isWithinDayRange($rangeStart, $rangeEnd) {
|
||||
|
||||
// Normalize our event's dates for comparison with the all-day range.
|
||||
$eventStart = stripTime($this->start);
|
||||
|
||||
if (isset($this->end)) {
|
||||
$eventEnd = stripTime($this->end); // normalize
|
||||
}
|
||||
else {
|
||||
$eventEnd = $eventStart; // consider this a zero-duration event
|
||||
}
|
||||
|
||||
// Check if the two whole-day ranges intersect.
|
||||
return $eventStart < $rangeEnd && $eventEnd >= $rangeStart;
|
||||
}
|
||||
|
||||
// Converts this Event object back to a plain data array, to be used for generating JSON
|
||||
public function toArray() {
|
||||
|
||||
// Start with the misc properties (don't worry, PHP won't affect the original array)
|
||||
$array = $this->properties;
|
||||
|
||||
$array['title'] = $this->title;
|
||||
|
||||
// Figure out the date format. This essentially encodes allDay into the date string.
|
||||
if ($this->allDay) {
|
||||
$format = 'Y-m-d'; // output like "2013-12-29"
|
||||
}
|
||||
else {
|
||||
$format = 'Y-m-d H:i:s';
|
||||
// $format = 'c'; // full ISO8601 output, like "2013-12-29T09:00:00+08:00"
|
||||
}
|
||||
|
||||
// Serialize dates into strings
|
||||
$array['start'] = $this->start->format($format);
|
||||
if (isset($this->end)) {
|
||||
$array['end'] = $this->end->format($format);
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
+-----------------------------------------------------------------------------------------------
|
||||
* GouGuOPEN [ 左手研发,右手开源,未来可期!]
|
||||
+-----------------------------------------------------------------------------------------------
|
||||
* @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
|
||||
+-----------------------------------------------------------------------------------------------
|
||||
* @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
|
||||
+-----------------------------------------------------------------------------------------------
|
||||
* @Author 勾股工作室 <hdm58@qq.com>
|
||||
+-----------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
declare (strict_types = 1);
|
||||
namespace smsservice;
|
||||
|
||||
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
|
||||
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
|
||||
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
|
||||
use AlibabaCloud\Tea\Exception\TeaError;
|
||||
use think\facade\Config;
|
||||
use think\facade\Log;
|
||||
|
||||
class Smsservice
|
||||
{
|
||||
protected $client;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$config = config('aliyun');
|
||||
|
||||
$configData = new \Darabonba\OpenApi\Models\Config([
|
||||
'accessKeyId' => $config['access_key_id'],
|
||||
'accessKeySecret' => $config['access_key_secret'],
|
||||
'endpoint' => 'dysmsapi.aliyuncs.com',
|
||||
'protocol' => 'HTTP',
|
||||
'regionId' => $config['region_id']
|
||||
]);
|
||||
|
||||
$this->client = new Dysmsapi($configData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
*
|
||||
* @param string $phone 手机号
|
||||
* @param string $templateCode 模板CODE(如:SMS_123456789)
|
||||
* @param array $params 模板变量,如 ['code' => '1234']
|
||||
* @return array
|
||||
*/
|
||||
public function sendSms(string $phone, string $templateCode, array $params = [])
|
||||
{
|
||||
$request = new SendSmsRequest([
|
||||
"signName" => config('aliyun.sign_name'),
|
||||
"templateCode" => $templateCode,
|
||||
"phoneNumbers" => $phone,
|
||||
"templateParam" => json_encode($params, JSON_UNESCAPED_UNICODE)
|
||||
]);
|
||||
|
||||
try {
|
||||
$response = $this->client->sendSms($request);
|
||||
$body = $response->body;
|
||||
return json_decode(json_encode($body),true);
|
||||
} catch (TeaUnableRetryError $e) {
|
||||
return ['code' => 1, 'msg' => '网络错误或请求失败', 'error' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
+-----------------------------------------------------------------------------------------------
|
||||
* GouGuOPEN [ 左手研发,右手开源,未来可期!]
|
||||
+-----------------------------------------------------------------------------------------------
|
||||
* @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
|
||||
+-----------------------------------------------------------------------------------------------
|
||||
* @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
|
||||
+-----------------------------------------------------------------------------------------------
|
||||
* @Author 勾股工作室 <hdm58@qq.com>
|
||||
+-----------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
declare (strict_types = 1);
|
||||
namespace systematic;
|
||||
use think\facade\Config;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Db;
|
||||
/**
|
||||
* 系统类
|
||||
*/
|
||||
class Systematic
|
||||
{
|
||||
public function auth($uid)
|
||||
{
|
||||
if (!Cache::get('RulesSrc' . $uid) || !Cache::get('RulesSrc0')) {
|
||||
//用户所在权限组及所拥有的权限
|
||||
$groups = [];
|
||||
$position_id = Db::name('Admin')->where('id', $uid)->value('position_id');
|
||||
$groups = Db::name('PositionGroup')
|
||||
->alias('a')
|
||||
->join("AdminGroup g", "a.group_id=g.id", 'LEFT')
|
||||
->where([['a.pid', '=', $position_id], ['g.status', '=', 1]])
|
||||
->select()->toArray();
|
||||
//保存用户所属用户组设置的所有权限规则id
|
||||
$ids = [];
|
||||
foreach ($groups as $g) {
|
||||
$ids = array_merge($ids, explode(',', trim($g['rules'], ',')));
|
||||
}
|
||||
$ids = array_unique($ids);
|
||||
//读取所有权限规则
|
||||
$rules_all = Db::name('AdminRule')->field('src')->select()->toArray();
|
||||
//读取用户组所有权限规则
|
||||
$rules = Db::name('AdminRule')->where('id', 'in', $ids)->field('src')->select()->toArray();
|
||||
//循环规则,判断结果。
|
||||
$auth_list_all = [];
|
||||
$auth_list = [];
|
||||
foreach ($rules_all as $rule_all) {
|
||||
$auth_list_all[] = strtolower($rule_all['src']);
|
||||
}
|
||||
foreach ($rules as $rule) {
|
||||
$auth_list[] = strtolower($rule['src']);
|
||||
}
|
||||
//规则列表结果保存到Cache
|
||||
Cache::tag('adminRules')->set('RulesSrc0', $auth_list_all, 36000);
|
||||
Cache::tag('adminRules')->set('RulesSrc' . $uid, $auth_list, 36000);
|
||||
}
|
||||
}
|
||||
|
||||
//读取文件配置
|
||||
public function getConfig($key)
|
||||
{
|
||||
return Config::get($key);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user