Commit 2f50fd8b authored by 梁俊杰's avatar 梁俊杰

新增公共函数,和说明文档

parent 79fdedbe
......@@ -59,4 +59,97 @@ function getUser {}
*/
function getUser {}
```
### 3、对集合获取值、然后调用rpc方法获取数据后,重新赋值
#### 1)、获取值,设置值
```
$list = Task::with(['c'=>function($q){
$q->select(['d','y']);
}])->select(['c','v'])->->paginate(2);
/*
* 假设拿到的数据是这个
* $list = [['c' => ['d' => 4, 'y' => 9], 'v' => '5'], ['c'=>'','v'=>'8'], ['c' => ['d' => 6, 'y' => 10], 'v' => '7']];
*/
$user_ids = get_collection_values($list, 'c.d');
// 去RPC拿用户数据
$users = $this->userService->getByIdList($hello);
//重新赋值给列表
put_collection_values($list, $users, 'c.d', 'user', 'id');
/*
* 则新的数据如下
* $list = [['c' => ['d' => 4,'user'=>['id'=>4,'name'=>'张三'], 'y' => 9], 'v' => '5'],
['c'=>'','v'=>'8'],
['c' => ['d' => 6, ,'user'=>['id'=>6,'name'=>'王五']'y' => 10], 'v' => '7']];
*/
```
#### 2)、判断各种值和更新各种值
```
// 使用第一步获取的列表
// askModel文件 新增获取值,和设置值方法
* 动态判断
* @param $currentUserId
*/
public function getCanDelete($currentUserId)
{
$this->attributes['can_delete'] = $this->user_id == $currentUserId ? true : false;
}
/**新增time_show属性
* @return string 返回友好时间值
*/
public function getTimeShowAttribute()
{
if ($this->status == 2) {
return '已完成';
}
$time = time();
if ($this->status === 0) {
$time = $time - strtotime($this->start_time);
return human_time($time) . "后开始";
}
if ($this->status === 1) {
$time = $time - strtotime($this->end_time);
if ($time > 0) {
return "超时" . human_time($time);
}
return human_time($time) . "后结束";
}
}
/**
* 设置结束时间
* @param string $time
*/
public function setFinishAttribute($time = '')
{
$this->attributes['finish'] = $time ? $time : today();
}
// TaskRepositoryEloquent 文件
// 获取列表方法
.. .....
$currentUserId = Auth::id();
foreach ($list->items() as $item) {
$item->getCanDelete($currentUserId);//结果会新增 can_delete属性
$item->time_show;//自动调用getTimeShowAttribute方法,并设置值
$item->finish=today();//自动调用setFinishAttribute方法,并设置值
}
return $list;//集合自动转数组,会带上新的三个属性,和分页数据
```
### 4、新方法说明
#### 1)、human_time 友好的显示时间
用法:
```
human_time(time()-strtotime('2020-06-06 12:12'));
//12分钟等等根据计算值自动显示时分秒,不足一分钟显示秒,不足一小时显示分
//不足一天显示小时
//可以自行测试
```
#### 2)、info 输出数据到控制台,支持任何数据,自动换行 方便测试
用法:
```
//支持多参数
info('aaa',[1,2,3],new stdClass(){$a=1;},collect([1,23,4]));
info(1);
```
......@@ -435,14 +435,14 @@ if (!function_exists('putLog')) {
// 创建一个 Channel,参数 log 即为 Channel 的名字
$log = new Logger('');
// 创建两个 Handler,对应变量 $stream 和 $fire
!$filename && $filename = date('Y-m-d',time()).'.log';
$stream = new StreamHandler($dir.$filename, Logger::WARNING);
!$filename && $filename = date('Y-m-d', time()) . '.log';
$stream = new StreamHandler($dir . $filename, Logger::WARNING);
$fire = new FirePHPHandler();
if (is_array($output)) {
$output = var_export($output, true);
}
$output = '[ ' . date('H:i:s', time()) . ' ] --- ' . $output;
$formatter = new LineFormatter($output."\r\n");
$formatter = new LineFormatter($output . "\r\n");
$stream->setFormatter($formatter);
$log->pushHandler($stream);
$log->pushHandler($fire);
......@@ -474,6 +474,138 @@ if (!function_exists('empty_string_2_null')) {
}
}
if (!function_exists('get_collection_values')) {
/**
* 从集合中提取深层数据
* @param $collection 数据集合
* @param string $key 集合中键 支持多层提取,例如"a.b.c"
* @return array | collection
*/
function get_collection_values($collection, string $key)
{
$values = [];
if (!empty($collection) && (is_object($collection) || is_array($collection))) {
$itemKeys = explode(".", $key);
$keyCount = count($itemKeys) - 1;
foreach ($collection as $value) {
foreach ($itemKeys as $k => $ik) {
if (isset($value[$ik])) {
$value = $value[$ik];
if ($k == $keyCount) {
$values[] = $value;
}
} else {
break;
}
}
}
}
return $values;
}
}
if (!function_exists('put_collection_values')) {
/**
* 对集合中设置深层数据
* @param array | collection $collection 需要设置的数据集合
* @param array | collection $valueList 需要设置值集合
* @param string $collectionKey 集合中键 支持多层提取,例如"user.info"
* @param string $collectionNewKey 数据集合的新键,例如"infomation",同名会覆盖
* @param string $valueKey $valueList中的值字段,例如"user_id"
* @return array | collection
*/
function put_collection_values($collection, $valueList, string $collectionKey, string $collectionNewKey, $valueKey)
{
if (!empty($collection) && (is_object($collection) || is_array($collection))) {
$itemKeys = explode(".", $collectionKey);
if (!empty($valueList)) {
if (!is_object($valueList)) {
$valueList = collect($valueList);
}
$valueList = $valueList->keyBy($valueKey);
}
foreach ($collection as $index => $value) {
$collection[$index] = private_put_collection_values($value, $itemKeys, $valueList, $collectionNewKey);
}
}
return $collection;
}
}
if (!function_exists('private_put_collection_values')) {
/**
* 对集合设置值,不对外公开
* @param $collection
* @param $itemKeys
* @param $valueList
* @param $collectionNewKey
* @return mixed
*/
function private_put_collection_values(&$collection, $itemKeys, $valueList, $collectionNewKey)
{
if (isset($collection[$itemKeys[0]])) {
if (count($itemKeys) != 1) {
$t = $itemKeys[0];
unset($itemKeys[0]);
$itemKeys = array_values($itemKeys);
$collection[$t] = private_put_collection_values($collection[$t], $itemKeys, $valueList, $collectionNewKey);
} else {
if (isset($valueList[$collection[$itemKeys[0]]])) {
$collection[$collectionNewKey] = $valueList[$collection[$itemKeys[0]]];
} else {
$collection[$collectionNewKey] = null;
}
}
}
return $collection;
}
}
if (!function_exists('human_time')) {
/**
* 计算时间
* @param $time 时间戳
* @return string
*/
function human_time($time)
{
if (is_numeric($time)) {
$time = strtotime($time);
}
$time = abs($time);
//计算天数
$days = intval($time / 86400);
if ($days != 0) {
return $days . "天";
}
//计算小时数
$remain = $time % 86400;
$hours = intval($remain / 3600);
if ($hours != 0) {
return $hours . "小时";
}
//计算分钟数
$remain = $time % 3600;
$mins = intval($remain / 60);
if ($mins != 0) {
return $mins . "分钟";
}
//计算秒数
$secs = $time % 60;
return $secs . "秒";
}
}
if (!function_exists('info')) {
/**
* 输出数据到控制台
* @param mixed ...$arguments
*/
function info(...$arguments)
{
var_dump(...$arguments);
}
}
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