Index.php 3.9 KB
<?php

namespace App\Http\Controllers\V2;

use App\Fun;
use Illuminate\Support\Facades\View;
use function Swoole\Coroutine\Http\get;

/**
 * 第二版的控制器
 * @author:dc
 * @time 2022/12/28 9:26
 * Class Index
 * @package App\Http\Controllers\V2
 */
class Index extends Base
{


    /**
     * 首页
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     * @author:dc
     * @time 2022/11/16 9:06
     */
    public function home()
    {
        $web = [
            'title' => 'Shopk.com',
            'keywords' => 'Shopk.com',
            'description' => 'Shopk.com',
        ];
        return view('v2/home', compact('web'));
    }


    /**
     * 博客
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     * @author:dc
     * @time 2022/12/28 10:13
     */
    public function blog()
    {
        $web = [
            'title' => 'Blog - Shopk.com',
            'keywords' => 'blog',
            'description' => 'blog',
        ];

        $lists  =   $this->getData('blog');
        if(!empty($lists['links'])){
            foreach ($lists['links'] as &$link){
                preg_match("/page=(\d+)/i",$link['url'],$p);
                $link['page'] = $p[1]??0;
            }
        }
        return view('v2/blog/lists',[
            'lists' =>  $lists,
            'web' => $web
        ]);
    }

    /**
     * 博客详情
     * @param $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     * @author:dc
     * @time 2022/12/28 10:13
     */
    public function blog_info($id){

        $data  =   $this->getData('blog/'.$id,['prev'=>1,'next'=>1,'correlation'=>1]);

        if(!$data){
            Fun::abort();
        }


        $web = [
            'title' => ucfirst($data['blog_title']) . ' - Shopk.com',
            'keywords' => $data['blog_seo_keywords'] ?? $data['blog_title'],
            'description' => $data['blog_seo_description'] ?? $data['blog_title'],
        ];

        return view('v2/blog/info',[
            'data' =>  $data,
            'web' => $web
        ]);
    }



    /**
     * 单页
     * @param $key
     * @author:dc
     * @time 2022/12/28 16:59
     */
    public function page($key){
        $page = $this->getData('page',['urlkey'=>$key]);

        if(!$page){
            Fun::abort();
        }

        $web = [
            'title' => ucfirst($page['page_title']) . ' - Shopk.com',
            'keywords' => $page['page_seo_keywords'] ? implode(', ', $page['page_seo_keywords']) : $page['page_title'],
            'description' => $page['page_seo_description'] ?? $page['page_title'],
        ];

        return view('v2/page', [
            'data' => $page,
            'web' => $web
        ]);

    }


    /**
     * 设置模板,保存
     * @author:dc
     * @time 2022/12/30 16:44
     */
    public function template(){
        $name = request()->get('name');
        $data = $this->getData('template',['name'=>$name],false);

        if($data){
            // 保存文件
            $filename = resource_path('views'.$data['path'].'.blade.php');
            // 是否存在
            if(!is_dir(dirname($filename))){
                mkdir(dirname($filename),0775,true);
            }

            // 是否有第三方连接
            if (
            preg_match_all(
                "/<a[\s\t]*href[\s\t]*=[\s\t]*\"[\s\t]*(http[s]?:\/\/.*)[\s\t]*\"/Ui",
                $data['html'],
                $urls)
            ){
                foreach ($urls[1] as $k=>$url){
                    $_url = str_replace($url,'/redirect-'.urlencode($url),$urls[0][$k]);
                    $data['html'] = str_replace($urls[0][$k],$_url,$data['html']);
                }
            }

            @file_put_contents($filename,$data['html']);

        }


    }



}