小东西 还挺别致

	//laravel无限级分类
	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;
	}
	//thinkphp5无限级分类
	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){
	    //将每条数据中的id值作为其下标
	        $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();
	//使用封装的get_cate_list递归函数重新排序 无限级分类列表
	$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;
    }

	//curl请求
    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, //suppress_row: 去掉会多出一行数据
            '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';
    }
    /**
     * @param $table
     * @param $id
     * @param bool $includeMyself
     * @return array
     * 查出某个父级ID下的所有子分类id
     */
    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;
    }

    /**
     * @param $table
     * @param $id
     * @return array
     * 查出某个分类id上的所有子父级ID
     */
    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');
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值