mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-06 03:58:04 +08:00
115 lines
4.0 KiB
PHP
115 lines
4.0 KiB
PHP
<?php
|
||
|
||
// +----------------------------------------------------------------------
|
||
// | Think.Admin
|
||
// +----------------------------------------------------------------------
|
||
// | 版权所有 2016~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
|
||
// +----------------------------------------------------------------------
|
||
// | 官方网站: http://think.ctolog.com
|
||
// +----------------------------------------------------------------------
|
||
// | 开源协议 ( https://mit-license.org )
|
||
// +----------------------------------------------------------------------
|
||
// | github开源项目:https://github.com/zoujingli/Think.Admin
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace library;
|
||
|
||
/**
|
||
* 通用工具化辅助类
|
||
*
|
||
* @version 1.0
|
||
* @author Anyon <zoujingli@qq.com>
|
||
* @date 2016/10/20 16:21
|
||
*/
|
||
class Tools {
|
||
|
||
/**
|
||
* Cors Options 授权处理
|
||
*/
|
||
static public function corsOptionsHandler() {
|
||
if (request()->isOptions()) {
|
||
header('Access-Control-Allow-Origin:*');
|
||
header('Access-Control-Allow-Headers:Accept,Referer,Host,Keep-Alive,User-Agent,X-Requested-With,Cache-Control,Content-Type,token');
|
||
header('Access-Control-Allow-Credentials:true');
|
||
header('Access-Control-Allow-Methods:GET,POST,OPTIONS');
|
||
header('Access-Control-Max-Age:1728000');
|
||
header('Content-Type:text/plain charset=UTF-8');
|
||
header('Content-Length: 0', true);
|
||
header('status: 204');
|
||
header('HTTP/1.0 204 No Content');
|
||
exit;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 一维数据数组生成数据树
|
||
* @param array $list 数据列表
|
||
* @param string $id 父ID Key
|
||
* @param string $pid ID Key
|
||
* @param string $son 定义子数据Key
|
||
* @return array
|
||
*/
|
||
static public function arr2tree($list, $id = 'id', $pid = 'pid', $son = 'sub') {
|
||
$tree = $map = array();
|
||
foreach ($list as $item) {
|
||
$map[$item[$id]] = $item;
|
||
}
|
||
foreach ($list as $item) {
|
||
if (isset($item[$pid]) && isset($map[$item[$pid]])) {
|
||
$map[$item[$pid]][$son][] = &$map[$item[$id]];
|
||
} else {
|
||
$tree[] = &$map[$item[$id]];
|
||
}
|
||
}
|
||
unset($map);
|
||
return $tree;
|
||
}
|
||
|
||
/**
|
||
* 一维数据数组生成数据树
|
||
* @param array $list 数据列表
|
||
* @param string $id ID Key
|
||
* @param string $pid 父ID Key
|
||
* @param string $path
|
||
* @return array
|
||
*/
|
||
static public function arr2table($list, $id = 'id', $pid = 'pid', $path = 'path', $ppath = '') {
|
||
$_array_tree = self::arr2tree($list, $id, $pid);
|
||
$tree = array();
|
||
foreach ($_array_tree as $_tree) {
|
||
$_tree[$path] = $ppath . '-' . $_tree[$id];
|
||
$_tree['spl'] = str_repeat(" ├ ", substr_count($ppath, '-'));
|
||
if (!isset($_tree['sub'])) {
|
||
$_tree['sub'] = array();
|
||
}
|
||
$sub = $_tree['sub'];
|
||
unset($_tree['sub']);
|
||
$tree[] = $_tree;
|
||
if (!empty($sub)) {
|
||
$sub_array = self::arr2table($sub, $id, $pid, $path, $_tree[$path]);
|
||
$tree = array_merge($tree, (Array) $sub_array);
|
||
}
|
||
}
|
||
return $tree;
|
||
}
|
||
|
||
/**
|
||
* 获取数据树子ID
|
||
* @param array $list 数据列表
|
||
* @param int $id 起始ID
|
||
* @param string $key 子Key
|
||
* @param string $pkey 父Key
|
||
* @return array
|
||
*/
|
||
static public function getArrSubIds($list, $id = 0, $key = 'id', $pkey = 'pid') {
|
||
$ids = array(intval($id));
|
||
foreach ($list as $vo) {
|
||
if (intval($vo[$pkey]) > 0 && intval($vo[$pkey]) == intval($id)) {
|
||
$ids = array_merge($ids, self::getArrSubIds($list, intval($vo[$key]), $key, $pkey));
|
||
}
|
||
}
|
||
return $ids;
|
||
}
|
||
|
||
}
|