浏览代码

feat: policy add sort logic

王亚超 1 年之前
父节点
当前提交
e172b0ea4e

+ 1 - 1
server/api/modules/v1/controllers/PolicyController.php

@@ -65,7 +65,7 @@ class PolicyController extends Controller {
             ],
             'sort' => [
                 'defaultOrder' => [
-                    'created_at' => SORT_ASC,
+                    'sort' => SORT_ASC,
                 ]
             ]
         ]);

+ 1 - 1
server/api/modules/v1/controllers/SurveyController.php

@@ -441,7 +441,7 @@ class SurveyController extends Controller {
 
 
             foreach($list as $k => $v){
-                $zclist = Policy::find()->where(['type' => Policy::TYPE_SURVEY, 'estate' => $type])->andWhere(['like','cengci',"{$v['level']}"])->all();
+                $zclist = Policy::find()->where(['type' => Policy::TYPE_SURVEY, 'estate' => $type])->andWhere(['like','cengci',"{$v['level']}"])->orderBy(['sort' => SORT_ASC])->all();
                 $list[$k]['policy'] = $zclist;
             }
 

+ 11 - 0
server/backend/controllers/PolicyController.php

@@ -31,6 +31,17 @@ class PolicyController extends Controller
             ],
         ];
     }
+
+    public function actions()
+    {
+        return [
+            'position' => [
+                'class' => 'backend\\actions\\Position',
+                'returnUrl' => Url::current()
+            ]
+        ];
+    }
+
     /**
      * Lists all Policy models.
      *

+ 5 - 5
server/backend/models/search/PolicySearch.php

@@ -44,11 +44,11 @@ class PolicySearch extends Policy
 
         $dataProvider = new ActiveDataProvider([
             'query' => $query,
-//            'sort' => [
-//                'defaultOrder' => [
-//                    'id' => SORT_ASC
-//                ]
-//            ]
+            'sort' => [
+                'defaultOrder' => [
+                    'sort' => SORT_ASC
+                ]
+            ]
         ]);
 
         $this->load($params);

+ 1 - 0
server/backend/views/policy/_form.php

@@ -18,6 +18,7 @@ use backend\widgets\ActiveForm;
         <?= $form->field($model, 'author')->textInput(['maxlength' => true]) ?>
         <?= $form->field($model, 'type')->checkboxList(\common\models\Policy::getTypeList()) ?>
         <?= $form->field($model, 'estate')->radioList(\common\enums\PolicyEnum::$list) ?>
+        <?= $form->field($model, 'sort')->textInput() ?>
 		<?php
 
         foreach ($configModels as $index => $configModel) {

+ 4 - 0
server/backend/views/policy/index.php

@@ -42,6 +42,10 @@ $this->params['breadcrumbs'][] = $this->title;
             ],
             'cengci',
             'author',
+            [
+                'class' => 'backend\widgets\grid\PositionColumn',
+                'attribute' => 'sort'
+            ],
             'created_at:datetime',
             ['class' => 'common\helpers\DiyActionColumn'],
         ],

+ 24 - 12
server/common/models/Policy.php

@@ -1,8 +1,10 @@
 <?php
 
 namespace common\models;
+
 use common\behaviors\CommentBehavior;
 use common\behaviors\MetaBehavior;
+use common\behaviors\PositionBehavior;
 use yii\behaviors\TimestampBehavior;
 use yii\helpers\HtmlPurifier;
 use yii\helpers\StringHelper;
@@ -19,6 +21,7 @@ use yii\helpers\StringHelper;
  * @property integer $markdown
  * @property string $type
  * @property string $estate
+ * @property int $sort
  */
 class Policy extends \yii\db\ActiveRecord
 {
@@ -49,11 +52,12 @@ class Policy extends \yii\db\ActiveRecord
     public function rules()
     {
         return [
-            [['content', 'title', 'summary', 'type','cengci', 'estate'], 'required'],
-            [['content'], 'string'],
-			[['filter','cengci'], 'string'],
+            [['content', 'title', 'summary', 'type', 'cengci', 'estate'], 'required'],
+            [['sort'], 'integer'],
+            [['content', 'filter', 'cengci'], 'string'],
             ['markdown', 'default', 'value' => $this->getIsMarkdown()],
             [['use_layout'], 'in', 'range' => [0, 1]],
+            [['sort'], 'default', 'value' => 0],
             [['title'], 'string', 'max' => 50],
             [['author'], 'string', 'max' => 50],
 //            ['content', 'filterHtml']
@@ -94,11 +98,12 @@ class Policy extends \yii\db\ActiveRecord
             'summary' => '摘要',
             'content' => '详情',
             'title' => '标题',
-            'author'=>'作者',
-            'type'=>'类型',//类型:0 匹配,1 查询
-            'cengci'=>'层次',
-            'estate'=>'产业',
-            'created_at'=>'发布时间'
+            'author' => '作者',
+            'type' => '类型',//类型:0 匹配,1 查询
+            'cengci' => '层次',
+            'estate' => '产业',
+            'created_at' => '发布时间',
+            'sort' => '排序',
         ];
     }
 
@@ -118,7 +123,14 @@ class Policy extends \yii\db\ActiveRecord
             ],
             [
                 'class' => CommentBehavior::className()
-            ]
+            ],
+            'positionBehavior' => [
+                'class' => PositionBehavior::className(),
+                'positionAttribute' => 'sort',
+                'groupAttributes' => [
+                    'estate'
+                ],
+            ],
         ];
     }
 
@@ -126,9 +138,9 @@ class Policy extends \yii\db\ActiveRecord
     {
         $model = $this->getMetaModel();
 
-        $title = $model->title ?  : $this->title;
-        $author = $model->author ?  : $this->author;
-        $description = $model->description ?  : StringHelper::truncate(strip_tags($this->content), 150);
+        $title = $model->title ?: $this->title;
+        $author = $model->author ?: $this->author;
+        $description = $model->description ?: StringHelper::truncate(strip_tags($this->content), 150);
 
         return [$title, $author, $description, $model->keywords];
     }

+ 0 - 0
server/database/202310291848 → server/database/202310291848.sql


+ 1 - 0
server/database/202311020009.sql

@@ -0,0 +1 @@
+ALTER TABLE `dd_policy` ADD `sort` INT(11) NOT NULL DEFAULT '0' COMMENT '排序' AFTER `estate`;