Ver Fonte

青爻公众号

cqp há 10 meses atrás
pai
commit
03ee0522d8

+ 36 - 0
app/Console/Commands/WxArticle.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace App\Console\Commands;
+
+
+use App\Service\CustomerPondService;
+use App\Service\Weixin\WeixinService;
+use Illuminate\Console\Command;
+
+
+class WxArticle extends Command
+{
+
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+
+    protected $signature = 'command:';
+
+    protected $description = '';
+
+    public function __construct()
+    {
+        parent::__construct();
+
+    }
+
+    public function handle()
+    {
+        $service = new WeixinService();
+        $service->getArticle();
+    }
+}

+ 11 - 0
app/Http/Controllers/Api/WeixinController.php

@@ -94,4 +94,15 @@ class WeixinController extends BaseController
 
         return "";
     }
+
+    public function getArticle(Request $request){
+        $service = new WeixinService();
+        list($status,$data) = $service->getArticle();
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 12 - 0
app/Model/WxArticle.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace App\Model;
+
+class WxArticle extends UseScopeBaseModel
+{
+    protected $table = "wx_article"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+
+}

+ 12 - 0
app/Model/WxArticleDetail.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace App\Model;
+
+class WxArticleDetail extends UseScopeBaseModel
+{
+    protected $table = "wx_article_detail"; //指定表
+    const CREATED_AT = null;
+    const UPDATED_AT = null;
+    protected $dateFormat = 'U';
+
+}

+ 148 - 1
app/Service/Weixin/WeixinService.php

@@ -2,10 +2,14 @@
 
 namespace App\Service\Weixin;
 
+use App\Console\Commands\WxArticle;
 use App\Model\Settings;
+use App\Model\WxArticleDetail;
 use App\Service\Service;
+use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Redis;
+use Illuminate\Support\Facades\Storage;
 
 class WeixinService extends Service
 {
@@ -296,7 +300,9 @@ class WeixinService extends Service
             Log::channel('apiLog')->info('wxget', ["message" => $result]);
             if (isset($result['errmsg'])) return [false, $result['errmsg']];
 
-            $a = ['data' => $result['item'] ?? [], 'total' => $result['total_count'], 'data_count' => $result['item_count']];
+            //保存文章
+            list($status,$msg) = $this->saveWxArticle($result);
+            if(! $status) return [false, $msg];
 
             // 更新 offset
             $offset += 1;
@@ -310,4 +316,145 @@ class WeixinService extends Service
 
         return [true, ''];
     }
+
+    public function saveWxArticle($result){
+        if(! empty($result['item'])){
+            try {
+                DB::beginTransaction();
+
+                foreach ($result['item'] as $value){
+                    $model = WxArticle::where('del_time',0)
+                        ->where('article_id', $value['article_id'])
+                        ->first();
+                    if(empty($model)){
+                        $model = new WxArticle();
+                        $model->article_id = $value['article_id'];
+                        $model->upd_time = $value['update_time'];
+                        $model->save();
+                        $id = $model->id;
+                        if(! empty($value['content']['news_item'])){
+                            foreach ($value['content']['news_item'] as $val){
+                                // 获取当前时间的微秒级时间戳
+                                list($micro, $seconds) = explode(' ', microtime());
+                                // 从微秒部分截取全部6位作为唯一标识符
+                                $microSuffix = substr($micro, 2, 6);
+                                $content_name = date('YmdHis', $seconds) . $microSuffix;
+                                $this->saveContent($val['content'], $content_name);
+                                $this->get_helper_for_img($val['thumb_url']);
+
+                                $modelDetail = new WxArticleDetail();
+                                $modelDetail->wx_article_id = $id;
+                                $modelDetail->title = $val['title'];
+                                $modelDetail->author = $val['author'];
+                                $modelDetail->digest = $val['digest'];
+                                $modelDetail->content_name = $content_name;
+                                $modelDetail->content_source_url = $val['content_source_url'];
+                                $modelDetail->thumb_media_id = $val['thumb_media_id'];
+                                $modelDetail->show_cover_pic = $val['show_cover_pic'];
+                                $modelDetail->url = $val['url'];
+                                $modelDetail->thumb_url = $val['thumb_url'];
+                                $modelDetail->need_open_comment = $val['need_open_comment'];
+                                $modelDetail->only_fans_can_comment = $val['only_fans_can_comment'];
+                                $modelDetail->is_deleted = $val['is_deleted'];
+                                $modelDetail->upd_time = $value['content']['update_time'];
+                            }
+                        }
+                    }else{
+                        if($model->upd_time != $value['update_time']){
+                            $model->upd_time = $value['update_time'];
+                            $model->save();
+                            $id = $model->id;
+
+                            $detail = WxArticleDetail::where('del_time',0)
+                                ->where('wx_article_id',$id)
+                                ->select('content_name','thumb_url')
+                                ->get()->toArray();
+
+                            WxArticleDetail::where('del_time',0)
+                                ->where('wx_article_id',$id)
+                                ->update(['del_time' => time()]);
+                            if(! empty($value['content']['news_item'])){
+                                foreach ($value['content']['news_item'] as $val){
+                                    // 获取当前时间的微秒级时间戳
+                                    list($micro, $seconds) = explode(' ', microtime());
+                                    // 从微秒部分截取全部6位作为唯一标识符
+                                    $microSuffix = substr($micro, 2, 6);
+                                    $content_name = date('YmdHis', $seconds) . $microSuffix;
+                                    $this->saveContent($val['content'], $content_name);
+                                    $this->get_helper_for_img($val['thumb_url']);
+
+                                    $modelDetail = new WxArticleDetail();
+                                    $modelDetail->wx_article_id = $id;
+                                    $modelDetail->title = $val['title'];
+                                    $modelDetail->author = $val['author'];
+                                    $modelDetail->digest = $val['digest'];
+                                    $modelDetail->content_name = $content_name;
+                                    $modelDetail->content_source_url = $val['content_source_url'];
+                                    $modelDetail->thumb_media_id = $val['thumb_media_id'];
+                                    $modelDetail->show_cover_pic = $val['show_cover_pic'];
+                                    $modelDetail->url = $val['url'];
+                                    $modelDetail->thumb_url = $val['thumb_url'];
+                                    $modelDetail->need_open_comment = $val['need_open_comment'];
+                                    $modelDetail->only_fans_can_comment = $val['only_fans_can_comment'];
+                                    $modelDetail->is_deleted = $val['is_deleted'];
+                                    $modelDetail->upd_time = $value['content']['update_time'];
+                                }
+                            }
+
+                            $this->delFile($detail);
+                        }
+                    }
+                }
+
+                DB::commit();
+            }catch (\Throwable $exception){
+                DB::rollBack();
+                return [false, $exception->getMessage()];
+            }
+        }
+
+        return [false, '暂无数据'];
+    }
+
+    //生成临时文件
+    public function saveContent($content, $name)
+    {
+        // 定义本地文件路径
+        $name = $name . '.txt';
+        $localFilePath = storage_path(self::wx_img . $name);
+
+        // 写入文件前先检查文件是否存在
+        if (! file_exists($localFilePath)) {
+
+            // 检查目录是否存在,如果不存在则创建
+            $directoryPath = dirname($localFilePath);
+            if (!is_dir($directoryPath)) {
+                // 设置目录权限,可以根据需要更改
+                $mode = 0777;
+                // 使用递归选项创建目录
+                if (!mkdir($directoryPath, $mode, true)) {
+                    return [false, '目录创建失败'];
+                }
+            }
+
+            file_put_contents($localFilePath, $content);
+        }
+
+        return [true, $name];
+    }
+
+    public function delFile($detail){
+        if(empty($detail)) return;
+
+        $dir = 'wx_img/';
+        foreach ($detail as $value){
+            $dir_content = $dir . $value['content_name'] . '.txt';
+            if(Storage::disk('public')->exists($dir_content)) Storage::disk('public')->delete($dir_content);
+
+            if(! empty($value['thumb_url'])){
+                $dir_thumb = $dir . md5($value['thumb_url']) . '.jpg';
+                if(Storage::disk('public')->exists($dir_thumb)) Storage::disk('public')->delete($dir_thumb);
+            }
+        }
+    }
 }

+ 1 - 0
routes/weixin.php

@@ -18,6 +18,7 @@ Route::any('getPublicWxMaterial', 'Api\WeixinController@getPublicWxMaterial');
 Route::any('getPublicWxDraft', 'Api\WeixinController@getPublicWxDraft');
 Route::any('getWxFile', 'Api\WeixinController@getWxFile');
 Route::any('getWxFileLocal/{file_name}', 'Api\WeixinController@getWxFileLocal');
+Route::any('getArticle', 'Api\WeixinController@getArticle');
 
 Route::group(['middleware'=> ['checkWeixin']],function ($route){