Prechádzať zdrojové kódy

修改b端接口:添加资讯、修改资讯、删除资讯、更新资讯状态

FengR 3 mesiacov pred
rodič
commit
70a1f3dc42
2 zmenil súbory, kde vykonal 307 pridanie a 32 odobranie
  1. 280 32
      app/JsonRpc/NewsService.php
  2. 27 0
      app/Model/ArticleIgnore.php

+ 280 - 32
app/JsonRpc/NewsService.php

@@ -79,6 +79,7 @@ use App\Model\GroupMemberImp;
 use App\Model\GroupImp;
 use App\Model\GroupTalkImp;
 use App\Model\GroupTalkMessagerImp;
+use App\Model\ArticleIgnore;
 
 #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class NewsService implements NewsServiceInterface
@@ -465,14 +466,15 @@ class NewsService implements NewsServiceInterface
    */
   public function addArticle(array $data): array
   {
-    var_dump($data, '----------12-----------1');
-    if(isset($data['is_collect'])){
-      $is_collect = $data['is_collect'];
-      unset($data['is_collect']);
-    }
-    
+    var_dump($data, '----------12-----------1');    
+   
+	// ------c_show_time********add***1****
+	$is_collect = $data['is_collect'] ?? 0;
+    $user_type = $data['user_type'];
+	unset($data['is_collect']);
     unset($data['user_type']);
-    unset($data['nav_add_pool_id']);
+	// ------c_show_time********add***1****
+	unset($data['nav_add_pool_id']);
     // unset($data['commend_id']);
     // $data['cat_arr_id'] = is_string($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
     Db::beginTransaction();
@@ -485,8 +487,8 @@ class NewsService implements NewsServiceInterface
       unset($data['is_survey']);
       unset($data['survey_name']);
       unset($data['suvey_array']);
-      // unset($data['website_id']);
-      // unset($data['web_site_id']);
+      unset($data['website_id']);
+      unset($data['web_site_id']);
       // $data['web_site_id'] = is_array($data['web_site_id']) ? json_encode($data['web_site_id']) : ($data['web_site_id']);
       if ($data['hits'] == '') {
         $data['hits'] = 0;
@@ -505,12 +507,14 @@ class NewsService implements NewsServiceInterface
         $levelArr = json_decode($articleData['level'], true);
         var_dump($levelArr, '----------levelArr-----------1');
         //content中提取图片第一个图,正则提取
-        // 优化正则表达式,仅匹配 src 属性值,避免匹配到多余内容
-        if(isset($is_collect) && $is_collect == 1){
+        //  -------采集器相关优化-----
+		//    优化正则表达式,仅匹配 src 属性值,避免匹配到多余内容
+        if($is_collect == 1){
           $reg = '/<img.*?src=[\'"]?((?!.*\.gif)[^\'" >]+)[\'"]?.*?>/i';
         }else{
           $reg = '/<img.*?src=[\'"]?([^\'" >]+)[\'"]?.*?>/i';
         }
+		//  -------采集器相关优化-----
         preg_match_all($reg, $data['content'], $matches);
         if (isset($matches[1][0])) {
           //截取varchar240
@@ -563,12 +567,98 @@ class NewsService implements NewsServiceInterface
         ]);
       }
       $id = Article::insertGetId($articleData);
+      if (!$id) {
+        throw new \Exception('文章插入失败');
+      }
       $articleDataContent = [
         'article_id' => $id,
         'content' => $data['content'],
       ];
-      ArticleData::insertGetId($articleDataContent);
+      $contentInsertId = ArticleData::insertGetId($articleDataContent);
+      if (!$contentInsertId) {
+        throw new \Exception('文章内容插入失败');
+      }
+
+
+	//   ------c_show_time********add***2****
+      // 直接使用当前系统时间,不再强制添加8小时
+      $now = date('Y-m-d H:i:s');
+      if($user_type == 10000 || $is_collect == 1){
+        // 管理员添加文章时,核心站c端显示时间就是创建时间;非核心站c端显示时间就是此创建时间往后推二十分钟;
+        // 非管理员添加文章时,不做操作只有在审核通过时获取c端显示时间
+        if (!isset($data['catid'])) {
+          throw new \Exception('缺少必要字段:catid');
+        }
+        // 查找网站,若是核心站则返回字段is_core=1,否则返回is_core=0,若是不存在此网站则为is_core=2
+        $web = WebsiteCategory::where('category_id', $data['catid'])
+        ->leftJoin('website', 'website_category.website_id', '=', 'website.id')
+        ->select('website_id', 
+            Db::raw('CASE WHEN website.id IS NULL THEN 2 WHEN JSON_CONTAINS(website.`website_column_arr_id`, \'3\') THEN 1 ELSE 0 END as is_core'))
+        ->get();
+        // $id = 225;   -----测试开启
+        // 采集来的文章,错开时间在两小时之内发布
+        if($user_type == 10000 && $is_collect == 1){
+          $web_num = $web->where('is_core', '!=', 2)->count();
+          $waiting_interval = 10;
+          if($web_num > 12){
+            // 若网站数量大于12个,则每个网站间隔时间取均值
+            // 防止除数为 0,虽然这里 $web_num 是 count 结果不会为 0,但做健壮性检查
+            $waiting_interval = 120 / max(1, $web_num);
+          }
+          $time = 0;
+        }else{
+          // 非核心站与核心站----显示时间-----间隔
+          $time = 20;
+          // 若不是采集来的文章,则非核心站之间的间隔时间为5分钟;初始时间与当前时间差二十分钟
+          $waiting_interval = 5;
 
+        }
+        // 将 Hyperf\Database\Model\Collection 转换为数组
+        $webArray = $web->toArray();
+        $article_ignore = array_values(array_filter(array_map(function ($index, $item) use (&$time, $now, $id, $waiting_interval, $is_collect) {
+          // 检查 $item 对象是否有 is_core 属性
+          if (!isset($item['is_core'])) {
+            throw new \Exception('$item 对象缺少 is_core 属性');
+          }
+          // 当 is_core 为 2 时,则表示此网站不存在
+          if ($item['is_core'] == 2) {
+              return null;
+          }
+          if($is_collect == 1){
+            // 采集来的文章,错开时间在两小时之内发布
+            // $time = 0;
+            $c_show_time = date('Y-m-d H:i:s', strtotime($now)+$time*60);
+            $time += $waiting_interval;
+          }else if($item['is_core'] == 1){
+            // 当 is_core 为 1 时,则表示此网站是核心站
+            $c_show_time = date('Y-m-d H:i:s', strtotime($now));
+          }else{
+             // 若是is_core为0,则表示此网站不是核心站,需要往后推时间
+            $c_show_time = date('Y-m-d H:i:s', strtotime($now)+$time*60);
+            if ($item['is_core'] == 0) {
+                $time += $waiting_interval;
+            }
+           
+          }
+          
+          return [
+              'article_id' => $id,
+              'website_id' => $item['website_id'],
+              'c_show_time' => $c_show_time,
+			//   'is_core' => $item['is_core'],
+			//   'time' => $time
+          ];
+        }, array_keys($webArray), $webArray)));
+		// Db::commit();
+		// return Result::success($article_ignore);
+        if(!empty($article_ignore)){
+          $article_ignores = ArticleIgnore::insert($article_ignore);
+          if(!$article_ignores){
+            throw new \Exception('c端展示时间插入失败');
+          }
+        }
+      }
+	// ------c_show_time********add***2****
       //处理投票
       if ($is_survey == 1) {
         //生成年月日时分秒+8位随机数
@@ -630,22 +720,40 @@ class NewsService implements NewsServiceInterface
       return Result::error("创建失败", 0);
     }
   }
-
   /**
    * @param array $data
    * @return array
    */
   public function delArticle(array $data): array
   {
-    $result = Article::where($data)->delete();
-    //survey投票删除
-    articleSurvey::where(['art_id' => $data['id']])->delete();
-    if (!$result) {
+    // ------c_show_time********del*******
+    Db::beginTransaction();
+    try{
+       $article = Article::where(['id' => $data['id']])->delete();
+       // 若文章删除失败,抛出异常,后续 SQL 语句不会执行
+       if (!$article) {
+           throw new \Exception("删除失败,文章删除失败");
+       }
+      //survey投票删除
+      ArticleSurvey::where(['art_id' => $data['id']])->delete();  // 修正类名大小写
+      $articleIgnore = ArticleIgnore::where(['article_id' => $data['id']])->delete();
+      $article_data = ArticleData::where(['article_id' => $data['id']])->delete();
+      // 若文章详情删除失败,抛出异常
+      if (!$article_data) {
+        throw new \Exception("删除失败,文章详情删除失败");
+      }
+      Db::commit();  // 所有操作成功,提交事务
+    }catch (\Throwable $ex) {
+      Db::rollBack();
+      var_dump($ex->getMessage());
+      return Result::error("删除失败: " . $ex->getMessage(), 0);
+    }
+   // ------c_show_time********del*******
+    if (!$article) {
       return Result::error("删除失败");
     }
-    return Result::success($result);
+    return Result::success($article);
   }
-
   /**
    * @param array $data
    * @return array
@@ -654,6 +762,82 @@ class NewsService implements NewsServiceInterface
   {
     var_dump($data, '----------12-----------1');
     Db::beginTransaction();
+	// ------c_show_time********up*******
+    $user_type = isset($data['user_type']) ? $data['user_type'] : 0;
+	// 无论是否改变了c端显示时间或者栏目,都需要将未屏蔽的网站显示时间删除记录
+	// 若是存在交集,则进行修改,若无,则合并(此表中的记录应该包含被屏蔽网站及涉及所有网站的展示时间)
+    $web = WebsiteCategory::where('category_id', $data['catid'])
+      ->leftJoin('website', 'website_category.website_id', '=', 'website.id')
+      ->select('website_id', 
+          Db::raw('CASE WHEN website.id IS NULL THEN 2 WHEN JSON_CONTAINS(website.`website_column_arr_id`, \'3\') THEN 1 ELSE 0 END as is_core'))
+      ->get();
+	// 根据错误信息,问题在于 $web 可能是一个包含对象的集合,不能直接用于 whereNotIn 方法
+	// 先从 $web 集合中提取 website_id 字段的值,再进行删除操作
+	$websiteIds = $web->pluck('website_id')->toArray();
+	$del_article_ignore = ArticleIgnore::where('article_id', $data['id'])->whereNotIn('website_id', $websiteIds)->delete();
+    if($user_type == 0){
+      	throw new \Exception("用户类型不能为空");
+    }
+    if($user_type == 10000 ){
+		// 查找网站,若是核心站则返回字段is_core=1,否则返回is_core=0,若是不存在此网站则为is_core=2
+		$web = WebsiteCategory::where('category_id', $data['catid'])
+		->leftJoin('website', 'website_category.website_id', '=', 'website.id')
+		->select('website_id', 
+			Db::raw('CASE WHEN website.id IS NULL THEN 2 WHEN JSON_CONTAINS(website.`website_column_arr_id`, \'3\') THEN 1 ELSE 0 END as is_core'))
+		->get();
+		// 非核心站与核心站----显示时间-----间隔
+		$time = 20;
+		// 若不是采集来的文章,则非核心站之间的间隔时间为5分钟;初始时间与当前时间差二十分钟
+		$waiting_interval = 5;
+		$webArray = $web->toArray();
+		if(empty($webArray)){
+			throw new \Exception('网站分类不存在');
+		}
+     	// 直接使用当前系统时间,不再强制添加8小时
+		$now = date('Y-m-d H:i:s');
+      	$article_ignore = array_values(array_filter(array_map(function ($index, $item) use (&$time, $now, $data, $waiting_interval) {
+			// 检查 $item 对象是否有 is_core 属性
+			if (!isset($item['is_core'])) {
+			throw new \Exception('$item 对象缺少 is_core 属性');
+			}
+			// 当 is_core 为 2 时,则表示此网站不存在
+			if ($item['is_core'] == 2) {
+				return null;
+			}else if($item['is_core'] == 1){
+			// 当 is_core 为 1 时,则表示此网站是核心站
+			$c_show_time = date('Y-m-d H:i:s', strtotime($now));
+			}else{
+				// 若是is_core为0,则表示此网站不是核心站,需要往后推时间
+			$c_show_time = date('Y-m-d H:i:s', strtotime($now)+$time*60);
+			if ($item['is_core'] == 0) {
+				$time += $waiting_interval;
+			}
+			
+			}
+			
+			return [
+				'article_id' => $data['id'],
+				'website_id' => $item['website_id'],
+				'c_show_time' => $c_show_time,
+			];
+      	}, array_keys($webArray), $webArray)));
+
+		if(!empty($article_ignore)){
+			// 使用 upsert 方法,当 article_id 和 website_id 组合存在时更新 c_show_time,不存在时插入新记录
+			// 使用 upsert 方法,根据 article_id 和 website_id 作为唯一键判断记录是否存在,存在则更新 c_show_time 字段,不存在则新增
+			$article_ignores = ArticleIgnore::upsert(
+				$article_ignore,
+				['article_id', 'website_id'], // 用于判断记录是否存在的唯一键
+				['c_show_time'] // 需要更新的字段
+			);
+			if(!$article_ignores){
+				throw new \Exception('c端展示时间插入失败');
+			}
+		}
+		// Db::commit();
+	  	// return Result::success($article_ignore);
+    }
+	// ------c_show_time********up*******
     unset($data['user_type']);
     // unset($data['web_site_id']);
     unset($data['nav_add_pool_id']);
@@ -873,22 +1057,86 @@ class NewsService implements NewsServiceInterface
 
 
     $result = Article::where(['id' => $data['id']])->update($data);
+	
     if (isset($data['ignore_ids']) && !empty($data['ignore_ids'])) {
       //article_ignore  表插入数据  db操作
       //转成数组
       //删掉数据重新写入
-      DB::table('article_ignore')->where('article_id', $data['id'])->delete();
+    //   DB::table('article_ignore')->where('article_id', $data['id'])->delete();
 
       $website_ids = is_array($data['ignore_ids']) ? $data['ignore_ids'] : json_decode($data['ignore_ids'], true);
-      $insert_data = [];
-      foreach ($website_ids as $key => $value) {
-        $insert_data[] = [
-          'article_id' => $data['id'],
-          'website_id' => $value,
-        ];
-      }
-      DB::table('article_ignore')->insert($insert_data);
-    }
+    //   $insert_data = [];
+    //   foreach ($website_ids as $key => $value) {
+    //     $insert_data[] = [
+    //       'article_id' => $data['id'],
+    //       'website_id' => $value,
+	// 	  'is_ignore' => 1
+    //     ];
+    //   }
+      DB::table('article_ignore_copy2')->where('article_id', $data['id'])->whereIn('website_id',$website_ids)->update(['is_ignore' => 1]);
+    }else{
+		// ------c_show_time********up_status*******
+		// 若是审核通过  则更新c_show_time
+		if($data['status'] == 1){
+			// 查找网站,若是核心站则返回字段is_core=1,否则返回is_core=0,若是不存在此网站则为is_core=2
+			$web = Article::where('article.id',$data['id'])
+			->leftJoin('website_category','article.catid','website_category.category_id')
+			->leftJoin('website', 'website_category.website_id', '=', 'website.id')
+			->select('website_id', 
+				Db::raw('CASE WHEN website.id IS NULL THEN 2 WHEN JSON_CONTAINS(website.`website_column_arr_id`, \'3\') THEN 1 ELSE 0 END as is_core'))
+			->get();
+			// 非核心站与核心站----显示时间-----间隔
+			$time = 20;
+			// 若不是采集来的文章,则非核心站之间的间隔时间为5分钟;初始时间与当前时间差二十分钟
+			$waiting_interval = 5;
+			$webArray = $web->toArray();
+			if(empty($webArray)){
+				throw new \Exception('网站分类不存在');
+			}
+			// 直接使用当前系统时间,不再强制添加8小时
+			$now = date('Y-m-d H:i:s');
+			$article_ignore = array_values(array_filter(array_map(function ($index, $item) use (&$time, $now, $data, $waiting_interval) {
+				// 检查 $item 对象是否有 is_core 属性
+				if (!isset($item['is_core'])) {
+				throw new \Exception('$item 对象缺少 is_core 属性');
+				}
+				// 当 is_core 为 2 时,则表示此网站不存在
+				if ($item['is_core'] == 2) {
+					return null;
+				}else if($item['is_core'] == 1){
+				// 当 is_core 为 1 时,则表示此网站是核心站
+				$c_show_time = date('Y-m-d H:i:s', strtotime($now));
+				}else{
+					// 若是is_core为0,则表示此网站不是核心站,需要往后推时间
+				$c_show_time = date('Y-m-d H:i:s', strtotime($now)+$time*60);
+				if ($item['is_core'] == 0) {
+					$time += $waiting_interval;
+				}
+				
+				}
+				
+				return [
+					'article_id' => $data['id'],
+					'website_id' => $item['website_id'],
+					'c_show_time' => $c_show_time,
+				];
+			}, array_keys($webArray), $webArray)));
+
+			if(!empty($article_ignore)){
+				// 使用 upsert 方法,当 article_id 和 website_id 组合存在时更新 c_show_time,不存在时插入新记录
+				// 使用 upsert 方法,根据 article_id 和 website_id 作为唯一键判断记录是否存在,存在则更新 c_show_time 字段,不存在则新增
+				$article_ignores = ArticleIgnore::upsert(
+					$article_ignore,
+					['article_id', 'website_id'], // 用于判断记录是否存在的唯一键
+					['c_show_time'] // 需要更新的字段
+				);
+				if(!$article_ignores){
+					throw new \Exception('c端展示时间插入失败');
+				}
+			}
+		}
+		// ------c_show_time********up_status*******
+	}
 
     if ($result) {
       return Result::success();
@@ -1610,13 +1858,13 @@ class NewsService implements NewsServiceInterface
     //     ->get()->all();
     // return Result::success($query);
     if (isset($data['cityid']) && !empty($data['cityid'])) {
-      $query->whereRaw("JSON_CONTAINS(city_arr_id, '" . intval($data['cityid']) . "')");
+      	$query->whereRaw("JSON_CONTAINS(city_arr_id, '" . intval($data['cityid']) . "')");
     }
     if (isset($data['department_id']) && !empty($data['department_id'])) {
-      $query->whereRaw("JSON_CONTAINS(department_arr_id, '" . intval($data['department_id']) . "')");
+      	$query->whereRaw("JSON_CONTAINS(department_arr_id, '" . intval($data['department_id']) . "')");
     }
     if (isset($data['keyword']) && !empty($data['keyword'])) {
-      $query->where('title', 'like', '%' . $data['keyword'] . '%');
+      	$query->where('title', 'like', '%' . $data['keyword'] . '%');
     }
     // 计算总数
     $count = $query->count();

+ 27 - 0
app/Model/ArticleIgnore.php

@@ -0,0 +1,27 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Model;
+
+use Hyperf\DbConnection\Model\Model;
+
+/**
+ */
+class ArticleIgnore extends Model
+{
+    /**
+     * The table associated with the model.
+     */
+    protected ?string $table = 'article_ignore';
+
+    /**
+     * The attributes that are mass assignable.
+     */
+    protected array $fillable = [];
+
+    /**
+     * The attributes that should be cast to native types.
+     */
+    protected array $casts = [];
+}