Commit c9c90ab9 authored by luhongguang's avatar luhongguang
parents eeeeb314 f99797ca
...@@ -74,36 +74,6 @@ class CacheUtil ...@@ -74,36 +74,6 @@ class CacheUtil
return call_user_func_array([$this->handler, $method], $args); return call_user_func_array([$this->handler, $method], $args);
} }
/**
* 获取原始数据
* @param $key
* @return mixed
*/
public function getUnChange($key, $prefix = ''){
return $this->handler->get($this->getCacheKey($prefix, $key));
}
/**
* 保存原始数据
* @param $key
* @param $value
* @param $expire
* @return mixed
*/
public function setUnChange($key , $value , $expire = null, $prefix = ''){
if (is_null($expire)) {
// $expire = $this->expire;
return false;
}
$key = $this->getCacheKey($prefix, $key);
if (is_int($expire) && $expire) {
$result = $this->handler->setex($key, $expire, $value);
} else {
$result = $this->handler->set($key, $value);
}
return $result;
}
/** /**
* 读取缓存 * 读取缓存
* @access public * @access public
...@@ -117,9 +87,7 @@ class CacheUtil ...@@ -117,9 +87,7 @@ class CacheUtil
if (is_null($value) || false === $value) { if (is_null($value) || false === $value) {
return $default; return $default;
} }
$jsonData = json_decode($value, true); return $value;
// 检测是否为JSON数据 true 返回JSON解析数组, false返回源数据
return (null === $jsonData) ? $value : $jsonData;
} }
/** /**
...@@ -129,24 +97,13 @@ class CacheUtil ...@@ -129,24 +97,13 @@ class CacheUtil
* @param mixed $default 默认值 * @param mixed $default 默认值
* @return mixed * @return mixed
*/ */
public function gets($keys, $prefix = '', $default = false) public function gets($keys, $prefix = '')
{ {
if(empty($keys) || !is_array($keys)) { if(empty($keys) || !is_array($keys)) {
return $default; return false;
} }
$keys = array_values($keys); $keys = array_values($keys);
$values = $this->handler->mGet($this->getCacheKey($prefix, $keys)); return $this->handler->mGet($this->getCacheKey($prefix, $keys));
if (is_null($values) || false === $values) {
return $default;
}
$result = [];
foreach((array)$values as $i => $value) {
if($value !== false) {
$jsonData = json_decode($value, true);
$result[$keys[$i]] = (null === $jsonData) ? $value : $jsonData;
}
}
return $result;
} }
/** /**
...@@ -165,7 +122,6 @@ class CacheUtil ...@@ -165,7 +122,6 @@ class CacheUtil
} }
$key = $this->getCacheKey($prefix, $key); $key = $this->getCacheKey($prefix, $key);
//对数组/对象数据进行缓存处理,保证数据完整性 //对数组/对象数据进行缓存处理,保证数据完整性
$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
if (is_int($expire) && $expire) { if (is_int($expire) && $expire) {
$result = $this->handler->setex($key, $expire, $value); $result = $this->handler->setex($key, $expire, $value);
} else { } else {
...@@ -189,7 +145,6 @@ class CacheUtil ...@@ -189,7 +145,6 @@ class CacheUtil
$this->handler->multi(); $this->handler->multi();
foreach($items as $key => $value) { foreach($items as $key => $value) {
$key = $this->getCacheKey($prefix, $key); $key = $this->getCacheKey($prefix, $key);
$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
$this->handler->setex($key, $expire, $value); $this->handler->setex($key, $expire, $value);
} }
return $this->handler->exec(); return $this->handler->exec();
...@@ -210,7 +165,6 @@ class CacheUtil ...@@ -210,7 +165,6 @@ class CacheUtil
} }
$key = $this->getCacheKey($prefix, $key); $key = $this->getCacheKey($prefix, $key);
//对数组/对象数据进行缓存处理,保证数据完整性 //对数组/对象数据进行缓存处理,保证数据完整性
$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
if(is_int($expire) && $expire) { if(is_int($expire) && $expire) {
$result = $this->handler->setenx($key, $expire, $value); $result = $this->handler->setenx($key, $expire, $value);
} else { } else {
......
<?php
namespace Api\PhpUtils\Common;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
/**
* 根据给定数据生成excel文件,可选择生成本地临时文件或浏览器直接输出
*
* @author
* @since 2021-07-25
*/
class ExportExcel
{
/**
* 获取生成excel文件的绝对地址
*
* @param 必填项 :items {要生成excel的二维数组,array}
* @param 必填项 :keywords {生成excel使用的keywords数组(具体格式详见SendExportMail.class.php中keywords属性),array}
* @param 必填项 :filename {要生成excel的文件名,string}
* @return string {生成的excel文件的绝对地址}
*/
public static function getExcelFilePath($items, array $keywords, $filename = false)
{
$objPHPExcel = self::getPHPExcelObj($items, $keywords, $filename);
$writer = new Xlsx($objPHPExcel);
$filename = "/tmp/" . $filename;
if (file_exists($filename)) {
unlink($filename);
} else {
//$fp = fopen($filename, 'a');
}
//$objWriter->save($filename);
$writer->save($filename);
return $filename;
}
/**
* 生成PHPExcel object用于输出
*
* @param 必填项 :items {要生成excel的数组,array}
* @param 必填项 :keywords {生成excel文件使用的keywords数组(具体格式详见本类中keywords属性),array}
* @param 必填项 :filename {要生成excel的文件名,string}
* @return object {PHPExcel object}
*/
private static function getPHPExcelObj($items, array $keywords, $filename = false)
{
set_time_limit(0);
// Create new PHPExcel object
$objPHPExcel = new \PHPExcel();
// Set document properties
$objPHPExcel->getProperties()
->setCreator("yidian")
->setLastModifiedBy("yidian")
->setTitle("标题")
->setSubject("题目")
->setDescription("描述")
->setKeywords("关键字")
->setCategory("种类");
$ActiveSheet = $objPHPExcel->getActiveSheet();
$keywords = self::getKeywordsWithAlpha($keywords);
foreach ($keywords as $key => $value) {
// set the wdith of cloumn
$ActiveSheet->getColumnDimension($key)->setWidth($value['column_width']);
// set the value of first row
$ActiveSheet->getStyle($key)
->getAlignment()
->setHorizontal('center');
$ActiveSheet->setCellValue($key . '1', $value['column_name']);
}
$index = 2;
if (is_array($items)) {
foreach ($items as $item) {
foreach ($keywords as $key => $value) {
if (0 && is_numeric($item[$value['key']]) && ! is_int($item[$value['key']])) {
$ActiveSheet->setCellValue($key . $index, floatval($item[$value['key']]));
} elseif (0 && is_numeric($item[$value['key']])) {
$ActiveSheet->setCellValue($key . $index, intval($item[$value['key']]));
} else {
$ActiveSheet->setCellValue($key . $index, $item[$value['key']] . ' ');
}
$Alignment = $ActiveSheet->getStyle($key . $index)->getAlignment();
// 换行
$Alignment->setWrapText(true);
// 垂直居中
$Alignment->setVertical('center');
}
$index ++;
}
}
return $objPHPExcel;
}
/**
* 获取生成多sheet页excel文件的绝对地址
*
* @param 必填项 :data {要生成excel的三维数组,sheet页为第一维,array}
* @param 必填项 :filename {要生成excel的文件名,string}
* @return string {生成的excel文件的绝对地址}
*/
public static function getExcelFileForMultiSheet($data, $filename)
{
$objPHPExcel = self::getPHPExcelObjForMultiSheet($data);
$writer = new Xlsx($objPHPExcel);
//$objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
$filename = "/tmp/" . $filename;
//$filename = "/Users/work/" . $filename;
if (file_exists($filename)) {
unlink($filename);
} else {
//$fp = fopen($filename, 'a');
}
//$objWriter->save($filename);
$writer->save($filename);
return $filename;
}
/**
* 生成多sheet页 PHPExcel object用于输出
*
* @param 必填项 :data {要生成excel的三维数组,sheet页为第一维,array}
* @param 必填项 :keywords {生成excel文件使用的keywords数组(具体格式详见本类中keywords属性),array}
* @param 必填项 :filename {要生成excel的文件名,string}
* @return object {PHPExcel object}
*/
private static function getPHPExcelObjForMultiSheet($data)
{
set_time_limit(0);
// Create new PHPExcel object
$objPHPExcel = new Spreadsheet();
// Set document properties
$objPHPExcel->getProperties()
->setCreator("yidian")
->setLastModifiedBy("yidian")
->setTitle("标题")
->setSubject("题目")
->setDescription("描述")
->setKeywords("关键字")
->setCategory("种类");
$sheetIndex = 0;
foreach ($data as $sheet) {
$sheetName = !empty($sheet['sheetName']) ? $sheet['sheetName'] : 'sheet'.$sheetIndex;
$topHeader = !empty($sheet['topHeader']) ? $sheet['topHeader'] : '';
$keywords = !empty($sheet['keywords']) ? $sheet['keywords'] : '';
$items = !empty($sheet['items']) ? $sheet['items'] : '';
//创建一个新的工作空间(sheet)
($sheetIndex > 0) && $objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex($sheetIndex);
$activeSheet = $objPHPExcel->getActiveSheet();
$activeSheet->setTitle($sheetName);
$index = 1;
$keywords = self::getKeywordsWithAlpha($keywords);
if ($topHeader) {
$indexAlpha = array_keys($keywords);
$endAlpha = end($indexAlpha);
$activeSheet->setCellValue('A1', $topHeader);
$activeSheet->mergeCells('A1:' . $endAlpha . '1');
$activeSheet->getRowDimension(1)->setRowHeight(30);
$activeSheet->getStyle('A1:' . $endAlpha . '1')
->getAlignment()
->setHorizontal('center')
->setVertical('center');
$index++;
}
foreach ($keywords as $key => $value) {
// set the wdith of cloumn
$activeSheet->getColumnDimension($key)->setWidth($value['column_width']);
// set the value of first row
$activeSheet->getStyle($key)
->getAlignment()
->setHorizontal('center');
$activeSheet->setCellValue($key . $index, $value['column_name']);
}
$index++;
if (empty($items) || !is_array($items)) {
continue;
}
foreach ($items as $item) {
foreach ($keywords as $key => $value) {
if (0 && is_numeric($item[$value['key']]) && ! is_int($item[$value['key']])) {
$activeSheet->setCellValue($key . $index, floatval($item[$value['key']]));
} elseif (0 && is_numeric($item[$value['key']])) {
$activeSheet->setCellValue($key . $index, intval($item[$value['key']]));
} else {
$activeSheet->setCellValue($key . $index, $item[$value['key']] . ' ');
}
$alignment = $activeSheet->getStyle($key . $index)->getAlignment();
// 换行
$alignment->setWrapText(true);
// 垂直居中
$alignment->setVertical('center');
}
$index ++;
}
$sheetIndex++;
}
return $objPHPExcel;
}
/**
* 将keywords数组索引转为excel英文列名
*
* @param 必填项 :keywords {keywords数组中下标,array}
* @return array {英文下标的数组(下标类似A,AA,BA)}
*/
private static function getKeywordsWithAlpha($keywords)
{
$keywordsWithAlpha = array();
if (is_array($keywords)) {
foreach ($keywords as $key => $value) {
$keywordsWithAlpha[self::intToUpperAlpha($key)] = $value;
}
}
return $keywordsWithAlpha;
}
/**
* 将数组int索引转化为excel列的列名字符串(A,AA,BA)
*
* @param 必填项 :ord {keywords数组中下标,int}
* @return string {excel列的列名字符串}
*/
private static function intToUpperAlpha($ord)
{
$baseOrd = ord('A');
$mod = floor($ord/26);
if ($mod == 0) {
$retAphla = chr($baseOrd + $ord);
} else {
$retAphla = chr($baseOrd + $mod-1);
$retAphla .= chr($baseOrd + $ord%26);
}
return $retAphla;
}
}
\ No newline at end of file
...@@ -70,7 +70,7 @@ class FileLog ...@@ -70,7 +70,7 @@ class FileLog
foreach ($mail_to as $mail) { foreach ($mail_to as $mail) {
$key = md5(sprintf("%s,%s", $mail, md5($signature))); $key = md5(sprintf("%s,%s", $mail, md5($signature)));
if (self::shouldSendEmail($key) === true) { if (self::shouldSendEmail($key) === true) {
Email::sendMail('noreply@yidian-inc.com', $mail, $subject, $body); Email::sendMail('bp-noreply@yidian-inc.com', $mail, $subject, $body);
} }
} }
} }
......
...@@ -173,6 +173,10 @@ abstract class MysqlBase ...@@ -173,6 +173,10 @@ abstract class MysqlBase
*/ */
public static function getConnection($type, $flush = false) public static function getConnection($type, $flush = false)
{ {
if(PHP_SAPI == 'cli'){
// cli模式下,不走单例,gone away
//$flush = true;
}
if (!isset(self::$dbConnect[static::CONFIG_INDEX.':'.$type]) || $flush) { if (!isset(self::$dbConnect[static::CONFIG_INDEX.':'.$type]) || $flush) {
self::$dbConnect[static::CONFIG_INDEX.':'.$type] = new Medoo(self::getDbConf($type)); self::$dbConnect[static::CONFIG_INDEX.':'.$type] = new Medoo(self::getDbConf($type));
} }
...@@ -495,4 +499,20 @@ abstract class MysqlBase ...@@ -495,4 +499,20 @@ abstract class MysqlBase
self::$dbConnect[static::CONFIG_INDEX.':'.$type] = self::getConnection($type); self::$dbConnect[static::CONFIG_INDEX.':'.$type] = self::getConnection($type);
return self::$dbConnect[static::CONFIG_INDEX.':'.$type]->action($actions); return self::$dbConnect[static::CONFIG_INDEX.':'.$type]->action($actions);
} }
/**
* 销毁所有已存在的连接,守护进程需要手动调用
*
* @return void
*/
public static function destroyConnetion()
{
if(self::$dbConnect) {
foreach(self::$dbConnect as $conn) {
$conn->pdo = null;
unset($conn);
}
self::$dbConnect = [];
}
}
} }
...@@ -158,10 +158,7 @@ abstract class MysqlClusterBase ...@@ -158,10 +158,7 @@ abstract class MysqlClusterBase
} }
// 读取配置文件内容 // 读取配置文件内容
self::$iniConf = config('mysql', static::CONFIG_INDEX); self::loadCfg();
if (!self::$iniConf) {
throw new \Exception('mysql config not exist');
}
// 获取库分片索引 // 获取库分片索引
$dbShardingIndex = self::getDatabaseShardingIndex(); $dbShardingIndex = self::getDatabaseShardingIndex();
...@@ -198,7 +195,8 @@ abstract class MysqlClusterBase ...@@ -198,7 +195,8 @@ abstract class MysqlClusterBase
} }
} }
$end_time = microtime(true); $end_time = microtime(true);
MonUtil::dbMon(self::$dbCurrentConnect['server'].'_'.self::$dbCurrentConnect['port'] ?? 'unknow_mysql', $method, 'mysql', $end_time-$start_time); list($dbServer, $dbPort) = self::getDbServerPort($type, $dbShardingIndex);
MonUtil::dbMon($dbServer.'_'.$dbPort, $method, 'mysql', $end_time-$start_time);
return self::formatResult($method, $arguments); return self::formatResult($method, $arguments);
} }
...@@ -255,9 +253,11 @@ abstract class MysqlClusterBase ...@@ -255,9 +253,11 @@ abstract class MysqlClusterBase
* *
* @param string $type 读或写类型 * @param string $type 读或写类型
* @return void * @return void
* @throws \Exception
*/ */
private static function getDbServerPort($type, $dbShardingIndex) private static function getDbServerPort($type, $dbShardingIndex)
{ {
self::loadCfg();
$server = ''; $server = '';
$port = 0; $port = 0;
$host = self::$iniConf[$type]['host']; $host = self::$iniConf[$type]['host'];
...@@ -306,8 +306,12 @@ abstract class MysqlClusterBase ...@@ -306,8 +306,12 @@ abstract class MysqlClusterBase
* 获取数据库分片数量 * 获取数据库分片数量
* *
* @return integer * @return integer
* @throws \Exception
*/ */
private static function getDbShardingCount() { private static function getDbShardingCount() {
self::loadCfg();
$host = self::$iniConf[static::WRITE]['host']; $host = self::$iniConf[static::WRITE]['host'];
$hostList = explode(',', $host); $hostList = explode(',', $host);
self::$dbShardingCount = count($hostList); self::$dbShardingCount = count($hostList);
...@@ -318,13 +322,21 @@ abstract class MysqlClusterBase ...@@ -318,13 +322,21 @@ abstract class MysqlClusterBase
* *
* @return array * @return array
*/ */
private static function getDatabaseShardingIndex() protected static function getDatabaseShardingIndex($dbShardingColumnValue = 0, $dbShardingCompatColumnValue = 0)
{ {
// 未使用分库 // 未使用分库
if (!static::SHARDING_DATABASE) { if (!static::SHARDING_DATABASE) {
return 0; return 0;
} }
if(!empty($dbShardingColumnValue)) {
self::$dbShardingColumnValue = $dbShardingColumnValue;
}
if(!empty($dbShardingCompatColumnValue)) {
self::$dbShardingCompatColumnValue = $dbShardingCompatColumnValue;
}
self::getDbShardingCount(); self::getDbShardingCount();
$dbShardingIndex = 0; $dbShardingIndex = 0;
...@@ -384,9 +396,12 @@ abstract class MysqlClusterBase ...@@ -384,9 +396,12 @@ abstract class MysqlClusterBase
* 获取ini中的option配置,必须用\PDO::ATTR_STRINGIFY_FETCHES对应的int数值 * 获取ini中的option配置,必须用\PDO::ATTR_STRINGIFY_FETCHES对应的int数值
* *
* @return array * @return array
* @throws \Exception
*/ */
public static function getDbOptions() public static function getDbOptions()
{ {
self::loadCfg();
$options = []; $options = [];
if (isset(self::$iniConf['options'])) { if (isset(self::$iniConf['options'])) {
foreach (self::$iniConf['options'] as $item) { foreach (self::$iniConf['options'] as $item) {
...@@ -562,6 +577,21 @@ abstract class MysqlClusterBase ...@@ -562,6 +577,21 @@ abstract class MysqlClusterBase
return false; return false;
} }
/**
* @param bool $flush
* @throws \Exception
* 加载配置文件,支持强更新
*/
private static function loadCfg($flush = false) {
if($flush || empty(self::$iniConf)) {
//重新加载 ini
self::$iniConf = config('mysql', static::CONFIG_INDEX);
if (!self::$iniConf) {
throw new \Exception('mysql yaconf ini config not exist');
}
}
}
/** /**
* 格式化返回结果 * 格式化返回结果
* *
...@@ -618,6 +648,7 @@ abstract class MysqlClusterBase ...@@ -618,6 +648,7 @@ abstract class MysqlClusterBase
* *
* @param int $dbShardingColumnValue * @param int $dbShardingColumnValue
* @return bool * @return bool
* @throws \Exception
*/ */
public static function beginTransaction($dbShardingColumnValue = 0, $dbShardingCompatColumnValue = 0) public static function beginTransaction($dbShardingColumnValue = 0, $dbShardingCompatColumnValue = 0)
{ {
...@@ -625,10 +656,7 @@ abstract class MysqlClusterBase ...@@ -625,10 +656,7 @@ abstract class MysqlClusterBase
self::$dbShardingCompatColumnValue = $dbShardingCompatColumnValue; self::$dbShardingCompatColumnValue = $dbShardingCompatColumnValue;
// 读取配置文件内容 // 读取配置文件内容
self::$iniConf = config('mysql', static::CONFIG_INDEX); self::loadCfg();
if (!self::$iniConf) {
throw new \Exception('mysql config not exist');
}
// 获取库分片索引 // 获取库分片索引
self::$transationIndex = self::getDatabaseShardingIndex(); self::$transationIndex = self::getDatabaseShardingIndex();
...@@ -717,15 +745,10 @@ abstract class MysqlClusterBase ...@@ -717,15 +745,10 @@ abstract class MysqlClusterBase
* 获取所有分片连接 * 获取所有分片连接
* *
* @return array * @return array
* @throws \Exception
*/ */
public static function getWriteConnectionList() public static function getWriteConnectionList()
{ {
// 读取配置文件内容
self::$iniConf = config('mysql', static::CONFIG_INDEX);
if (!self::$iniConf) {
throw new \Exception('mysql config not exist');
}
// 获取库分片数量 // 获取库分片数量
self::getDbShardingCount(); self::getDbShardingCount();
...@@ -740,15 +763,10 @@ abstract class MysqlClusterBase ...@@ -740,15 +763,10 @@ abstract class MysqlClusterBase
* 获取所有分片连接 * 获取所有分片连接
* *
* @return array * @return array
* @throws \Exception
*/ */
public static function getReadConnectionList() public static function getReadConnectionList()
{ {
// 读取配置文件内容
self::$iniConf = config('mysql', static::CONFIG_INDEX);
if (!self::$iniConf) {
throw new \Exception('mysql config not exist');
}
// 获取库分片数量 // 获取库分片数量
self::getDbShardingCount(); self::getDbShardingCount();
......
...@@ -69,7 +69,7 @@ class RedisUtil ...@@ -69,7 +69,7 @@ class RedisUtil
* @param array $options 其他参数数组,优先级高于yaconf中的相同配置 * @param array $options 其他参数数组,优先级高于yaconf中的相同配置
* @param string $options['serverRegion'] 服务区域,不同数据中心 * @param string $options['serverRegion'] 服务区域,不同数据中心
* @param bool $options['master'] 哨兵模式/主从模式,默认true使用主库,若使用从库值为false * @param bool $options['master'] 哨兵模式/主从模式,默认true使用主库,若使用从库值为false
* @param string $options['serializer'] 序列化选项,包括 none|php|igbinary, msgpack自己实现msgpack_pack|msgpack_unpack, incr等方法只能使用none,set等方法使用none不能存Array或Object * @param string $options['serializer'] 序列化选项,包括 none|php|igbinary|json, msgpack自己实现msgpack_pack|msgpack_unpack, incr等方法只能使用none,set等方法使用none不能存Array或Object
* @param boolean $master * @param boolean $master
* @return mixed * @return mixed
*/ */
...@@ -368,10 +368,9 @@ class RedisUtil ...@@ -368,10 +368,9 @@ class RedisUtil
case 'igbinary': case 'igbinary':
$this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // Use igBinary serialize/unserialize $this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // Use igBinary serialize/unserialize
break; break;
case 'msgpack': case 'json':
// Undefined class constant 'SERIALIZER_MSGPACK' $this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_JSON); // Use json serialize/unserialize
// $this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_MSGPACK); // Use msgpack serialize/unserialize break;
// break;
default: default:
$this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // Don't serialize data $this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // Don't serialize data
break; break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment