public function tree()
{
$res = DB::table('user_china_address')->get()->map(function ($value) {
return (array)$value;
})->toArray();
$res = $this->getTree($res);
return json_encode(['code' => 200, 'msg' => 'ok', 'data' => $res]);
}
protected function getTree($array)
{
$items = array();
foreach ($array as $value) {
$items[$value['id']] = $value;
}
$tree = array();
foreach ($items as $key => $item) {
if (isset($items[$item['pid']])) {
$items[$item['pid']]['son'][] = &$items[$key];
} else {
$tree[] = &$items[$key];
}
}
return $tree;
}
if (!function_exists('get_cate_list')) {
function get_cate_list($list,$pid=0,$level=0) {
static $tree = array();
foreach($list as $row) {
if($row['pid']==$pid) {
$row['level'] = $level;
$tree[] = $row;
get_cate_list($list, $row['id'], $level + 1);
}
}
return $tree;
}
}
if(!function_exists('get_tree_list')){
function get_tree_list($list){
$temp = [];
foreach($list as $v){
$v['son'] = [];
$temp[$v['id']] = $v;
}
foreach($temp as $k=>$v){
$temp[$v['pid']]['son'][] = &$temp[$v['id']];
}
return isset($temp[0]['son']) ? $temp[0]['son'] : [];
}
}
$list = (new \think\Collection($list))->toArray();
$list = get_cate_list($list);
public static function curlPostHeader($url, $post_data = array(), $headers = array())
{
$curl = curl_init();
if (count($headers) >= 1) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($post_data)) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
public static function httpRequest($url, $data = '', $method = 'GET')
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
if ($method == 'POST') {
curl_setopt($curl, CURLOPT_POST, 1);
if ($data != '') {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
}
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
public function quickExportXls($fileName, &$data, $header): string
{
require_once env('APP_DIR_PATH') . '/vendor/XLSXWriter/xlsxwriter.class.php';
ini_set('memory_limit', '-1');
set_time_limit(0);
$header = array_values($header);
$filePath = env('APP_DIR') . 'temp';
$widths = $col_style = [];
foreach ($header as $item) {
$col_style[] = 'string';
$widths[] = 20;
}
$headerOptions = array(
'suppress_row' => true,
'widths' => $widths
);
$writer = new \XLSXWriter();
$writer->writeSheetHeader('Sheet1', $col_style, $headerOptions);
$header = array_values($header);
array_unshift($data, $header);
foreach ($data as $key => $value) {
$value = array_values($value);
$writer->writeSheetRow('Sheet1', $value);
}
$writer->writeToFile($filePath . '/' . $fileName . '.xlsx');
return env('APP_URL') . 'temp/' . $fileName . '.xlsx';
}
public static function getAllIdUnderById($table, $id, bool $includeMyself = true): array
{
$sql = "SELECT id FROM
(
SELECT t1.id,
IF
( FIND_IN_SET( pid, @pids ) > 0, @pids := CONCAT( @pids, ',', id ), 0 )
AS ischild
FROM
( SELECT id, pid FROM {$table} t ORDER BY pid, id ) t1,
( SELECT @pids := {$id} ) t2
) t3
WHERE
ischild != 0;";
$ids = array_column(DB::select($sql), 'id');
if ($includeMyself) {
return array_values(array_merge([$id], $ids));
}
return $ids;
}
public static function getAllParentById($table, $id): array
{
$sql = "SELECT t.id FROM
(
SELECT @id idlist,
( SELECT @id := GROUP_CONCAT( pid SEPARATOR ',' ) FROM {$table} WHERE FIND_IN_SET( id, @id ) )
sub FROM {$table},
( SELECT @id := {$id} )
vars WHERE @id IS NOT NULL
) tl,{$table} t
WHERE
FIND_IN_SET( t.id, tl.idlist );";
return array_column(DB::select($sql), 'id');
}