Quellcode durchsuchen

feat: EK-2086: Member Portal : Dashboard Article and Ad only show self country.

kevin vor 4 Monaten
Ursprung
Commit
1717f2ff9b

+ 41 - 7
backendApi/modules/v1/controllers/AdController.php

@@ -8,12 +8,14 @@
 namespace backendApi\modules\v1\controllers;
 
 use backendApi\modules\v1\models\Admin;
+use backendApi\modules\v1\models\AdminCountry;
 use common\helpers\Cache;
 use common\helpers\Form;
 use common\helpers\Log;
 use common\models\Ad;
 use common\models\AdLocation;
 use common\models\Article;
+use common\models\Countries;
 use common\models\forms\AdForm;
 use common\models\forms\UploadForm;
 use Yii;
@@ -52,21 +54,53 @@ class AdController extends BaseController
      * @return mixed
      * @throws \yii\web\HttpException
      */
-    public function actionList(){
+    public function actionList()
+    {
         $lid = Yii::$app->request->get('lid');
-        $condition = ' AND AD.LID=:LID';
+
+        // 如果admin不是超管,只允许查询自己关联的国家
+        $admin = Admin::findOne(Yii::$app->user->id);
+        $roleId = $admin->ROLE_ID;
+        if ($roleId == \Yii::$app->params['superAdminRoleId']) {
+            $countries = Countries::find()->asArray()->all();
+        } else {
+            // 关联国家
+            $countries = Countries::find()
+                ->select('COU.ID, COU.CODE, COU.NAME')
+                ->from(['COU' => Countries::tableName()])
+                ->join('INNER JOIN', AdminCountry::tableName() . ' AS ADL', 'countries.COUNTRY_ID = ADL.ID')
+                ->where(['ADL.ADMIN_ID' => $admin->ID])
+                ->asArray()
+                ->all();
+        }
+
+        $filter = $this->filterCondition([
+            'TITLE' => 'TITLE',
+            'COUNTRY_ID' => 'COUNTRY_ID',
+        ]);
+        $condition = $filter['condition'];
+        $params = $filter['params'];
+
+        $condition .= ' AND AD.LID=:LID';
         $params[':LID']=$lid;
         $data = Ad::lists($condition, $params, [
-            'select' => 'AD.*,ADMC.ADMIN_NAME CREATE_ADMIN_NAME,ADMU.ADMIN_NAME UPDATE_ADMIN_NAME',
+            'select' => 'AD.*,ADC.COUNTRY_NAME',
             'from' => Ad::tableName().' AS AD',
             'join' => [
-                ['LEFT JOIN', Admin::tableName() . ' AS ADMC', 'ADMC.ID=AD.CREATE_ADMIN'],
-                ['LEFT JOIN', Admin::tableName() . ' AS ADMU', 'ADMU.ID=AD.UPDATE_ADMIN'],
+                ['INNER JOIN', Countries::tableName() . ' AS ADC', 'ADC.ID=AD.COUNTRY_ID'],
             ],
-            'orderBy' => 'AD.SORT DESC,AD.CREATED_AT ASC',
+            'orderBy' => 'AD.STATUS DESC,AD.SORT DESC,AD.CREATED_AT ASC',
         ]);
+
         $data['allLocation'] = AdLocation::getAllLocation();
-        $data['allArticle'] = Article::find()->select('ID,TITLE')->asArray()->all();
+        $data['countries'] = $countries;
+        $data['allArticle'] = Article::find()
+            ->from(['ART' => Article::tableName()])
+            ->select('ART.ID,ART.TITLE')
+            ->where(['ART.STATUS' => 1])
+            ->andFilterWhere(['ART.COUNTRY_ID' => array_column($countries, 'ID')])
+            ->asArray()
+            ->all();
 
         return static::notice($data);
     }

+ 28 - 9
backendApi/modules/v1/controllers/ArticleController.php

@@ -11,6 +11,7 @@ use common\helpers\Cache;
 use common\helpers\Form;
 use common\models\Article;
 use common\models\ArticleCategory;
+use common\models\Countries;
 use common\models\forms\ArticleCategoryForm;
 use common\models\forms\ArticleForm;
 use common\models\forms\UploadForm;
@@ -43,8 +44,9 @@ class ArticleController extends BaseController
      * @return mixed
      * @throws \yii\web\HttpException
      */
-    public function actionCategoryAdd(){
-        if(Yii::$app->request->isPost) {
+    public function actionCategoryAdd()
+    {
+        if (Yii::$app->request->isPost) {
             return parent::edit(ArticleCategoryForm::class, Yii::t('ctx', 'successfully'));
         }
     }
@@ -82,13 +84,23 @@ class ArticleController extends BaseController
      * @return mixed
      * @throws \yii\web\HttpException
      */
-    public function actionIndex(){
-        $data = Article::lists('', [], [
-            'select' => 'ID,TITLE,CID,STATUS,SORT,CREATED_AT',
+    public function actionIndex()
+    {
+        $filter = $this->filterCondition([
+            'TITLE' => 'TITLE',
+            'COUNTRY_ID' => 'COUNTRY_ID',
+        ]);
+        $condition = $filter['condition'];
+        $params = $filter['params'];
+
+        $data = Article::lists($condition, $params, [
+            'select' => 'ID,TITLE,CID,COUNTRY_ID,STATUS,SORT,CREATED_AT',
             'orderBy' => 'SORT ASC,CREATED_AT DESC',
         ]);
+
         // 全部分类
         $data['allCategory'] = ArticleCategory::getAllCategory();
+
         return static::notice($data);
     }
 
@@ -97,12 +109,15 @@ class ArticleController extends BaseController
      * @return mixed
      * @throws \yii\web\HttpException
      */
-    public function actionAdd(){
-        if(Yii::$app->request->isPost) {
+    public function actionAdd()
+    {
+        if (Yii::$app->request->isPost) {
             return parent::edit(ArticleForm::class, Yii::t('ctx', 'successfully'));
         }
+
         // 获取全部分类
         $allCategory = ArticleCategory::find()->where('STATUS=1')->asArray()->all();
+
         return static::notice(['allCategory'=>$allCategory]);
     }
 
@@ -119,6 +134,8 @@ class ArticleController extends BaseController
         }
         $oneData = Article::findOneAsArray(['ID'=>$id]);
         $oneData['CONTENT'] = is_resource($oneData['CONTENT']) ? stream_get_contents($oneData['CONTENT']) : '';
+        // 国家
+        $oneData['COUNTRY'] = Countries::getCountry($oneData['COUNTRY_ID']);
         // 暂时先从文件中取内容
         $path = \Yii::getAlias('@common/runtime/articleContent/').$oneData['ID'];
         if(!file_exists($path)){
@@ -180,11 +197,13 @@ class ArticleController extends BaseController
         $id = \Yii::$app->request->get('id');
         $data = null;
         if($id){
-            $data = Article::findOneAsArray('ID=:ID AND STATUS=1', [':ID'=>$id], 'ID,TITLE,CID,SORT,CREATED_AT');
+            $data = Article::findOneAsArray('ID=:ID AND STATUS=1', [':ID'=>$id], 'ID,COUNTRY_ID,TITLE,CID,SORT,CREATED_AT');
+            // 国家
+            $data['COUNTRY'] = Countries::getCountry($data['COUNTRY_ID']);
         }
         if($data){
             // 暂时先从文件中取内容
-            $path = \Yii::getAlias('@common/runtime/articleContent/').$data['ID'];
+            $path = \Yii::getAlias('@common/runtime/articleContent/') . $data['ID'];
             if(!file_exists($path)){
                 $data['CONTENT'] = '';
             } else {

+ 49 - 0
backendApi/modules/v1/models/AdminCountry.php

@@ -0,0 +1,49 @@
+<?php
+
+namespace backendApi\modules\v1\models;
+
+/**
+ * This is the model class for table "{{%ADMIN_COUNTRY}}".
+ *
+ * @property string $ID
+ * @property string $ADMIN_ID 管理ID
+ * @property string $COUNTRY_ID 国家ID
+ * @property int $CREATED_AT 创建时间
+ * @property int $UPDATED_AT 更新时间
+ */
+class AdminCountry extends \common\components\ActiveRecord
+{
+    /**
+     * @inheritdoc
+     */
+    public static function tableName()
+    {
+        return '{{%ADMIN_COUNTRY}}';
+    }
+
+    /**
+     * @inheritdoc
+     */
+    public function rules()
+    {
+        return [
+            [['ADMIN_ID', 'COUNTRY_ID'], 'required'],
+            [['ID', 'ADMIN_ID', 'COUNTRY_ID'], 'string', 'max' => 50],
+            [['ID'], 'unique'],
+        ];
+    }
+
+    /**
+     * @inheritdoc
+     */
+    public function attributeLabels()
+    {
+        return [
+            'ID' => 'ID',
+            'ADMIN_ID' => '管理ID',
+            'COUNTRY_ID' => '国家ID',
+            'CREATED_AT' => '创建时间',
+            'UPDATED_AT' => '更新时间',
+        ];
+    }
+}

+ 2 - 1
common/models/Ad.php

@@ -41,7 +41,7 @@ class Ad extends \common\components\ActiveRecord
     public function rules()
     {
         return [
-            [['TITLE', 'IMAGE', 'TYPE', 'LID', 'CREATE_ADMIN', 'CREATED_AT'], 'required'],
+            [['TITLE', 'IMAGE', 'TYPE', 'LID', 'CREATE_ADMIN', 'CREATED_AT', 'COUNTRY_ID'], 'required'],
             [['TYPE', 'SORT', 'STATUS', 'CREATED_AT', 'UPDATED_AT'], 'integer'],
             [['ID', 'LID', 'CREATE_ADMIN', 'UPDATE_ADMIN'], 'string', 'max' => 32],
             [['TITLE'], 'string', 'max' => 48],
@@ -59,6 +59,7 @@ class Ad extends \common\components\ActiveRecord
         return [
             'ID' => 'ID',
             'TITLE' => '标题',
+            'COUNTRY_ID' => '国家ID',
             'IMAGE' => '图片地址',
             'TYPE' => '类型',
             'LID' => '广告位ID',

+ 4 - 1
common/models/Article.php

@@ -8,6 +8,7 @@ use Yii;
  * This is the model class for table "{{%ARTICLE}}".
  *
  * @property string $ID
+ * @property string $COUNTRY_ID 国家ID
  * @property string $TITLE 标题
  * @property string $CID 分类ID
  * @property string $CONTENT 内容
@@ -31,7 +32,8 @@ class Article extends \common\components\ActiveRecord
     public function rules()
     {
         return [
-            [['CID', 'CREATED_AT'], 'required'],
+            [['CID', 'CREATED_AT', 'COUNTRY_ID'], 'required'],
+            [['COUNTRY_ID'], 'string'],
             [['STATUS', 'CREATED_AT', 'SORT'], 'integer'],
             [['ID', 'CID'], 'string', 'max' => 32],
             [['TITLE'], 'string', 'max' => 255],
@@ -49,6 +51,7 @@ class Article extends \common\components\ActiveRecord
         return [
             'ID' => 'ID',
             'TITLE' => Yii::t('app', 'title'),
+            'COUNTRY_ID' => Yii::t('app', 'country'),
             'CID' => Yii::t('app', 'category'),
             'CONTENT' => Yii::t('app', 'content'),
             'STATUS' => Yii::t('app', 'state'),

+ 6 - 0
common/models/Countries.php

@@ -113,4 +113,10 @@ class Countries extends \common\components\ActiveRecord
         $record = self::findOneAsArray('ID=:ID', [':ID' => $id]);
         return $record['LOCAL_CURRENCY_ID'] ?? 0;
     }
+
+    public static function getCountry(string $id)
+    {
+        $record = self::findOneAsArray('ID=:ID', [':ID' => $id]);
+        return $record['NAME'] ?? '';
+    }
 }

+ 11 - 5
common/models/forms/ArticleForm.php

@@ -18,6 +18,7 @@ class ArticleForm extends Model
     public $cid;
     public $sort;
     public $content;
+    public $countryId;
 
     /**
      * @inheritdoc
@@ -26,8 +27,8 @@ class ArticleForm extends Model
     {
         return [
             [['id', 'title', 'cid', 'content'], 'trim'],
-            [['id', 'title', 'cid', 'content'], 'required'],
-            //[['title'], 'unique', 'targetClass'=>Article::class, 'targetAttribute'=>'TITLE'],
+            [['id', 'title', 'cid', 'content', 'countryId'], 'required'],
+            [['countryId'], 'string'],
             [['id'], 'exist', 'targetClass'=>Article::class, 'targetAttribute'=>'ID'],
             [['cid'], 'exist', 'targetClass'=>ArticleCategory::class, 'targetAttribute'=>'ID'],
         ];
@@ -41,6 +42,7 @@ class ArticleForm extends Model
             'cid' => 'Type', // 分类
             'content' => 'Content', // 内容
             'sort' => 'Sort', // 排序
+            'countryId' => 'Country', // 国家
         ];
     }
 
@@ -52,8 +54,8 @@ class ArticleForm extends Model
     {
         $parentScenarios =  parent::scenarios();
         $customScenarios = [
-            'add' => ['title', 'cid', 'content', 'sort'],
-            'edit' => ['id','title', 'cid', 'content', 'sort'],
+            'add' => ['title', 'cid', 'content', 'sort', 'countryId'],
+            'edit' => ['id','title', 'cid', 'content', 'sort', 'countryId'],
             'sort' => ['id', 'sort'],
         ];
         return array_merge($parentScenarios, $customScenarios);
@@ -64,10 +66,12 @@ class ArticleForm extends Model
      * @return Article|null
      * @throws \yii\db\Exception
      */
-    public function edit(){
+    public function edit()
+    {
         if(!$this->validate()){
             return null;
         }
+
         $db = \Yii::$app->db;
         $transaction = $db->beginTransaction();
         try {
@@ -87,9 +91,11 @@ class ArticleForm extends Model
             $model->CONTENT = '';
             $model->STATUS = 1;
             $model->SORT = $this->sort;
+            $model->COUNTRY_ID = $this->countryId;
             if(!$model->save()){
                 throw new Exception(Form::formatErrorsForApi($model->getErrors()));
             }
+
             // 暂时把内容写入到文件中
             $path = \Yii::getAlias('@common/runtime/articleContent/').$model->ID;
             file_put_contents($path, $this->content);