Browse Source

文章修改

moshaorui 4 months ago
parent
commit
7d8249ef21

+ 1 - 1
.env.dev

@@ -4,7 +4,7 @@ APP_KEY=base64:bdomFa+r6S774rRyFNeb4pCBxVe4WbuzAzk+LyP86+k=
 APP_DEBUG=true
 APP_URL=http://localhost
 
-#网站主域名,不含http
+#网站主域名,不含http://www.
 TOP_DOMAIN=mietubl.com
 #分销商前端站点IP
 DIST_SITE_IP = 127.0.0.1

+ 14 - 0
app/Distributor/Controllers/ApiController.php

@@ -55,6 +55,20 @@ class ApiController extends Controller
         }
     }
 
+    public function generateSlug(Request $request)
+    {
+        $model = $request->get('model');
+        $title = $request->get('title');
+        $result = null;
+        switch ($model) {
+            case 'pages':
+                $obj = new SitePages();
+                $result = $obj->generateSlug($title);
+                break;
+        }
+        return ['slug' => $result];
+    }
+
 
     /*
      * 把数据转换成select需要的格式

+ 48 - 18
app/Distributor/Controllers/SitePagesController.php

@@ -6,6 +6,7 @@ use App\Distributor\Repositories\SitePages;
 use App\Models\SitePages as Model;
 use App\Distributor\Repositories\SitePagesTag;
 use App\Libraries\CommonHelper;
+use DateTime;
 use Dcat\Admin\Form;
 use Dcat\Admin\Grid;
 use Dcat\Admin\Models\Permission;
@@ -13,18 +14,24 @@ use Dcat\Admin\Show;
 use Dcat\Admin\Http\Controllers\AdminController;
 use Dcat\Admin\Layout\Content;
 use Dcat\Admin\Admin;
+use Dcat\Admin\Widgets\Form as widgetsForm;
 
 class SitePagesController extends AdminDistController
 {
+    public function title()
+    {
+        return admin_trans( 'admin.pages');
+    }
+
     /**
      * page index
      */
     public function index(Content $content)
     {
         return $content
-            ->header('列表')
-            ->description('全部')
-            ->breadcrumb(['text'=>'列表','url'=>''])
+            ->header(admin_trans( 'admin.pages'))
+            ->description('')
+            ->breadcrumb(['text'=>'','url'=>''])
             ->body($this->grid());
     }
 
@@ -35,13 +42,6 @@ class SitePagesController extends AdminDistController
      */
     protected function grid()
     {
-        // 检查是否支持 pcntl 扩展
-        if (!function_exists('pcntl_fork')) {
-            die('pcntl_fork() is not available on this system.');
-        } else {
-            die('pcntl_fork() supported on this system.');
-        }
-        exit;
         return Grid::make(SitePages::with(['pagesTag']), function (Grid $grid) {
             $grid->column('id')->sortable();
             $grid->column('title');
@@ -51,7 +51,13 @@ class SitePagesController extends AdminDistController
                 $dataImages = [$image];
                 return CommonHelper::displayImage($dataImages,100);
             });
-            $grid->column('post_date');
+            //发布日期
+            $grid->column('post_date')->display(function ($date) {
+                $dateTime = new DateTime($date);
+                $date = $dateTime->format('Y-m-d');
+                return $date;
+            });
+            //状态
             $grid->column('status')->using(config('dictionary.pages_status'));
             $grid->column('updated_at')->sortable();
             $grid->column('created_at');
@@ -82,7 +88,11 @@ class SitePagesController extends AdminDistController
                 return array_column($pagesTag,'name');
             })->label();
             $show->field('author');
-            $show->field('post_date');
+            $show->field('post_date')->as(function ($date) {
+                $dateTime = new DateTime($date);
+                $date = $dateTime->format('Y-m-d');
+                return $date;
+            });
             $show->field('cover_image')->as(function ($image) {
                 // 开始生成 HTML
                 $dataImages = [$image];
@@ -122,14 +132,34 @@ class SitePagesController extends AdminDistController
                 ->dir('images/pages/'.date("Ymd"));
             $form->editor('content')->required();
             $form->switch('status',admin_trans_label('publish'))->default(1);
-            $form->hidden('slug');
-            $form->hidden('dist_id');
-            //保存前,强制写死dist_id
+            //SEO
+            $form->radio('visibility')->when(1,function (Form $form) {
+                $form->text('seo_title');
+                $form->text('seo_keywords');
+                $form->textarea('seo_description');
+                $form->text('slug',admin_trans_label('url_and_handle'))->width(4, 2)->prepend(getSiteDomain().config('dictionary.site_url.pages'));
+            })->options(config('dictionary.visibility'))->default(1)->value(1);
+            //隐藏字段
+            $form->hidden('dist_id',);
+            $form->ignore(['visibility']);
+            //新建时插入JS
+            $form->creating(function (Form $form) {
+                CommonHelper::seoReplace();
+            });
+            //保存前
             $form->saving(function (Form $form) {
-                //$form->pagesTag = [1,2];
+                //强制写死dist_id
                 $form->dist_id = getDistributorId();
-                //生成slug
-                if ($form->isCreating()) {
+                //slug配置是否重复
+                $id = $form->getKey();
+                if ($form->slug != '') {
+                    if (SitePages::isRepeatedSlug($id, $form->slug)) {
+                        //slug重复
+                        return $form->response()->error(admin_trans_label('url_repeated'));
+                    }
+                }
+                //当slug为空时生成slug
+                if ($form->slug == '') {
                     $form->slug = SitePages::generateSlug($form->title);
                 }
             });

+ 2 - 2
app/Distributor/Repositories/SitePages.php

@@ -3,12 +3,12 @@
 namespace App\Distributor\Repositories;
 
 use App\Models\SitePages as Model;
-use App\Traits\DistCommonTrait;
+use App\Traits\DistSlugTrait;
 use Dcat\Admin\Repositories\EloquentRepository;
 
 class SitePages extends EloquentRepository
 {
-    use DistCommonTrait;
+    use DistSlugTrait;
     /**
      * Model.
      *

+ 2 - 2
app/Distributor/Repositories/SitePagesTag.php

@@ -3,12 +3,12 @@
 namespace App\Distributor\Repositories;
 
 use App\Models\SitePagesTag as Model;
-use App\Traits\DistCommonTrait;
+use App\Traits\DistSlugTrait;
 use Dcat\Admin\Repositories\EloquentRepository;
 
 class SitePagesTag extends EloquentRepository
 {
-    use DistCommonTrait;
+    use DistSlugTrait;
     /**
      * Model.
      *

+ 1 - 0
app/Distributor/routes.php

@@ -53,6 +53,7 @@ Route::group([
     $router->get('api/products', 'ApiController@products');
     $router->get('api/pages', 'ApiController@pages');
     $router->get('api/tag', 'ApiController@tag');
+    $router->get('api/generate-slug', 'ApiController@generateSlug');
 });
 
 

+ 29 - 0
app/Libraries/CommonHelper.php

@@ -94,5 +94,34 @@ JS
         );
     }
 
+
+    public static function seoReplace($titleName = 'title',$modelName = 'pages')
+    {
+        Admin::script(
+<<<JS
+    $('input[name="title"]').on('input', function() {
+        // 将 title 的值赋给 seo_title
+        $('input[name="seo_title"]').val($(this).val());
+    });
+    $('input[name="title"]').on('blur', function() {
+        //通过ajax请求修改slug
+        title = $(this).val();
+        $.ajax({
+            url: '/dist/api/generate-slug',
+            type: 'GET',
+            data: {
+                model:'{$modelName}',
+                title: title,
+            },
+            success: function(response) {
+                $('input[name="slug"]').val(response.slug);
+            }
+        });
+    });
+JS
+        );
+
+    }
+
 }
 

+ 17 - 5
app/Traits/DistCommonTrait.php → app/Traits/DistSlugTrait.php

@@ -11,19 +11,19 @@ use Spatie\EloquentSortable\EloquentModelSortedEvent;
 use Spatie\EloquentSortable\Sortable;
 
 /*
- * 列表排序is_pinned desc,order desc时用这个trait
+ * slug
  */
-trait DistCommonTrait
+trait DistSlugTrait
 {
     /*
      * 生成slug
      */
-    public static function generateSlug($title) {
+    public static function generateSlug($slug) {
         $self = new self();
         $model = $self->model();
-        $slug = generateSlug($title);
+        $slug = generateSlug($slug);
         for ($i=0;$i<5;$i++) {
-            $count = $model->where('slug', $title)->where('dist_id', getDistributorId())->count();
+            $count = $model->where('slug', $slug)->where('dist_id', getDistributorId())->count();
             if ($count == 0) {
                 return $slug;
             } else {
@@ -32,4 +32,16 @@ trait DistCommonTrait
         }
         return $slug . '-' . generateRandomString(5);
     }
+
+    public static function isRepeatedSlug($id,$slug) {
+        $id = intval($id);
+        $self = new self();
+        $model = $self->model();
+        $count = $model->where('id','!=',$id)->where('slug', $slug)->where('dist_id', getDistributorId())->count();
+        if ($count > 0) {
+            return true;
+        } else {
+            return false;
+        }
+    }
 }

+ 12 - 0
app/helpers.php

@@ -59,6 +59,18 @@ if (!function_exists('getDistributorId')) {
     }
 }
 
+if (!function_exists('getSiteDomain')) {
+    //得到分销商域名
+    function getSiteDomain($hasHttp = true) {
+        $distributor = Session::get('distributor');
+        $domain = $distributor['domain_type'] == 0 ?  $distributor['secondary_domain'] : $domain = $distributor['custom_domain'];
+        if ($hasHttp) {
+            $domain = 'https://'.$domain;
+        }
+        return $domain;
+    }
+}
+
 
 
 //通过parent_id构建树形结构

+ 12 - 0
config/dictionary.php

@@ -52,5 +52,17 @@ return [
         '0' => 'Draft',
         '1' => 'Published',
     ],
+    //
+    'visibility' => [
+        '1' => 'Visible',
+        '0' => 'Hidden',
+    ],
+    //站点URL配置
+    'site_url' => [
+        'pages' => '/pages',
+        'tags' => '/collections',
+        'products' => '/products',
+        'product_category' => '/product-category',
+    ],
 
 ];

+ 2 - 0
config/distributor.php

@@ -356,4 +356,6 @@ return [
     | Whether enable default breadcrumb for every page content.
     */
     'enable_default_breadcrumb' => true,
+
+
 ];

+ 1 - 0
lang/zh_CN/admin.php

@@ -245,4 +245,5 @@ return [
     'domain'  => '域名',
     'top_menu'  => '顶部菜单',
     'footer_menu'  => '底部菜单',
+    'pages'  => '文章',
 ];

+ 6 - 1
lang/zh_CN/global.php

@@ -78,7 +78,10 @@ return [
         'pages_tag'             => '文章标签',
         'post_date'             => '发布日期',
         'author'                => '作者',
-
+        'visibility'            => '可见性',
+        'seo_title'            => 'SEO标题',
+        'seo_description'      => 'SEO描述',
+        'seo_keywords'         => 'SEO关健字',
     ],
     'labels' => [
         'list'         => '列表',
@@ -120,6 +123,8 @@ return [
         'select_category'       => '选择分类',
         'publish'              => '发报',
         'tags'                  => '标签',
+        'url_and_handle'        => 'URL地址',
+        'url_repeated'          => 'URL地址重复了,请检查',
     ],
     'options' => [
         //