Просмотр исходного кода

Merge pull request #1787 from visuddhinanda/laravel

目录树支持lazy load
visuddhinanda 2 лет назад
Родитель
Сommit
31e43f288e

+ 1 - 1
app/Console/Commands/ImportArticle.php

@@ -119,7 +119,7 @@ class ImportArticle extends Command
                 $response = Http::withToken($token)->put($url.'/'.$articleId,
                 $response = Http::withToken($token)->put($url.'/'.$articleId,
                                     [
                                     [
                                         'title'=> $realTitle,
                                         'title'=> $realTitle,
-                                        'subtitle'=> $realTitle,
+                                        'summary'=> $title.'#'.$id,
                                         'lang'=> 'my',
                                         'lang'=> 'my',
                                         'content'=> $contentCombine,
                                         'content'=> $contentCombine,
                                         'anthology_id'=>$anthologyId,
                                         'anthology_id'=>$anthologyId,

+ 137 - 52
app/Console/Commands/ImportArticleMap.php

@@ -3,15 +3,21 @@
 namespace App\Console\Commands;
 namespace App\Console\Commands;
 
 
 use Illuminate\Console\Command;
 use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Http;
+use Illuminate\Support\Facades\Log;
+use App\Http\Api\StudioApi;
+use App\Models\Article;
+use App\Models\Collection;
 
 
 class ImportArticleMap extends Command
 class ImportArticleMap extends Command
 {
 {
     /**
     /**
      * The name and signature of the console command.
      * The name and signature of the console command.
+     * php artisan import:article.map visuddhinanda --studio=visuddhinanda --size=30000 --anthology=4c6b661b-fd68-44c5-8918-2e327c870b9a --token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJuYmYiOjE2OTc3Mjg2ODUsImV4cCI6MTcyOTI2NDY4NSwidWlkIjoiYmE1NDYzZjMtNzJkMS00NDEwLTg1OGUtZWFkZDEwODg0NzEzIiwiaWQiOjR9.fiXhnY2LczZ9kKVHV0FfD3AJPZt-uqM5wrDe4EhToVexdd007ebPFYssZefmchfL0mx9nF0rgHSqjNhx4P0yDA
      *
      *
      * @var string
      * @var string
      */
      */
-    protected $signature = 'import:article.map {--token=} {--studio=} {--anthology=}';
+    protected $signature = 'import:article.map {src_studio} {--token=} {--studio=} {--anthology=} {--size=}';
 
 
     /**
     /**
      * The console command description.
      * The console command description.
@@ -37,30 +43,27 @@ class ImportArticleMap extends Command
      */
      */
     public function handle()
     public function handle()
     {
     {
-        if (!$this->confirm('Do you wish to continue?')) {
-            return 0;
-        }
         $token = $this->option('token');
         $token = $this->option('token');
         $studioName = $this->option('studio');
         $studioName = $this->option('studio');
         $anthologyId = $this->option('anthology');
         $anthologyId = $this->option('anthology');
-
-        //先获取文章列表,建立全部目录
-        $url = config('app.url').'/api/v2/article-map';
-        $response = Http::get($url,[
-            'view'=>'anthology',
-            'id'=>$anthologyId,
-        ]);
-        if($response->failed()){
-            $this->error('获取文章列表 fail');
+        $srcStudio = $this->argument('src_studio');
+        if (!$this->confirm('Do you wish to continue?')) {
+            return 0;
+        }
+        $studioId = StudioApi::getIdByName($studioName);
+        if(!$studioId){
+            $this->error("can not found studio name {$studioName}");
             return 0;
             return 0;
-        }else{
-            $this->info('获取文章列表 ok');
         }
         }
-        if(!$response->json('ok')){
-            $this->error('http request error'.$response->json('message'));
+        $srcStudioId = StudioApi::getIdByName($srcStudio);
+        if(!$srcStudioId){
+            $this->error("can not found src studio name {$srcStudio}");
             return 0;
             return 0;
         }
         }
-        $articles = $response->json('data.rows');
+
+        //先获取文章列表,建立全部目录
+        $url = config('app.url').'/api/v2/article-map';
+
         $this->info('打开csv文件并读取数据');
         $this->info('打开csv文件并读取数据');
         $head = array();
         $head = array();
         $strFileName = __DIR__."/tipitaka-sarupa.csv";
         $strFileName = __DIR__."/tipitaka-sarupa.csv";
@@ -70,54 +73,136 @@ class ImportArticleMap extends Command
         }
         }
 
 
         if (($fp = fopen($strFileName, "r")) === false) {
         if (($fp = fopen($strFileName, "r")) === false) {
-            $this->error("can not open csv $strFileName");
+            $this->error("can not open csv {$strFileName}");
+            return 0;
+        }
+        //查询文集语言
+        $lang = Collection::where('uid',$anthologyId)->value('lang');
+        if(empty($lang)){
+            $this->error("文集语言不能为空 anthologyId=".$anthologyId);
             return 0;
             return 0;
         }
         }
         $inputRow = 0;
         $inputRow = 0;
+        $currSize=0;
+        $currBlock=1;
+        $currDir='';
+        $success = 0;
+        $fail = 0;
+        $articleMap = array();
         while (($data = fgetcsv($fp, 0, ',')) !== false) {
         while (($data = fgetcsv($fp, 0, ',')) !== false) {
             if($inputRow>0){
             if($inputRow>0){
-                if(!isset($head[$data[1]])){
-                    $head[$data[1]] = 1;
+                $id = $data[0];
+                $dir = $data[1];
+                $title = $data[2];
+                $realTitle = "[{$id}]{$title}";
+                $realTitle = mb_substr($realTitle,0,128,'UTF-8');
+                $reference = $data[5];
+
+                $percent = (int)($inputRow*100/6984);
+                $this->info("[{$percent}%] doing ".$realTitle);
+
+                if($this->option('size')){
+                    $currDir = 'tipitaka-sarupa-'.$currBlock;
+                    if($currSize > $this->option('size')){
+                        $currBlock++;
+                        $currSize=0;
+                    }
+                }else{
+                    $currDir = $dir;
+                }
+                //查找目录文章是否存在
+                $dirArticle = Article::where('owner',$studioId)
+                              ->where('title',$currDir)
+                              ->first();
+                if($dirArticle){
+                    $dirId = $dirArticle->uid;
+                }else{
+                    $this->info('不存在目录'.$currDir.'新建');
+                    $url = config('app.url').'/api/v2/article';
+                    sleep(2);
+                    $response = Http::withToken($token)->post($url,
+                    [
+                        'title'=> $currDir,
+                        'lang'=> $lang,
+                        'studio'=> $studioName,
+                        'anthologyId'=> $anthologyId,
+                    ]);
+                    if($response->ok()){
+                        $this->info('dir create ok title='.$currDir);
+                        $dirId = $response->json('data.uid');
+                    }else{
+                        $this->error('create dir fail.'.$currDir);
+                        Log::error('create dir fail title='.$currDir);
+                        $fail++;
+                        continue;
+                    }
+                }
+                //创建目录结束
+                if(!isset($articleMap[$dirId])){
+                    $articleMap[$dirId] = ['name'=>$currDir,'children'=>[]];
+                }
+                //查找文章
+                $article = Article::where('owner',$srcStudioId)
+                              ->where('title',$realTitle)
+                              ->first();
+                if(!$article){
+                    $this->error('文章没找到.'.$realTitle);
+                    Log::error('文章没找到 title='.$realTitle);
+                    $fail++;
+                    continue;
                 }
                 }
+                $articleMap[$dirId]['children'][] = [
+                    'id'=>$article->uid,
+                    'title'=>$article->title,
+                ];
+                if($this->option('size')){
+                    $currSize += mb_strlen($title,'UTF-8') +
+                                mb_strlen($data[4],'UTF-8') +
+                                mb_strlen($reference,'UTF-8');
+                }
+                $success++;
             }
             }
             $inputRow++;
             $inputRow++;
         }
         }
+        $this->info("找到文章=" .$success);
+        $this->info("目录=" .count($articleMap));
 
 
-        $this->info("csv head=" .count($head));
+        $this->info('正在准备map数据');
 
 
-        $titles = array();
-        $allTitles = array();
-        foreach ($articles as $key => $article) {
-            $allTitles[$article['title']] = $article['article_id'];
-            if($article['level']===1 && isset($head[$article['title']])){
-                $titles[$article['title']] = $article['article_id'];
-                $this->info('已有='.$article['title']);
+        $data = array();
+        foreach ($articleMap as $dirId => $dir) {
+            $data[] = [
+                    'article_id'=> $dirId,
+                    'level'=> 1,
+                    'title'=> $dir['name'],
+                    'children'=> count($dir['children']),
+                    'deleted_at'=> null,
+            ];
+            foreach ($dir['children'] as $key => $child) {
+                $data[] = [
+                        'article_id'=> $child['id'],
+                        'level'=> 2,
+                        'title'=> $child['title'],
+                        'children'=> 0,
+                        'deleted_at'=> null,
+                ];
             }
             }
         }
         }
-        $this->info("article map head=" .count($titles));
-        //新建目录
-        foreach ($head as $key => $value) {
-            if(!isset($titles[$key])){
-                $this->info('没有key'.$key.'新建');
-                $url = config('app.url').'/api/v2/article';
-                $response = Http::withToken($token)->post($url,
-                                [
-                                    'title'=> $key,
-                                    'lang'=> 'my',
-                                    'studio'=> $studioName,
-                                    'anthologyId'=> $anthologyId,
-                                ]);
-                if($response->ok()){
-                    $this->info('append ok title='.$key);
-                    $articleId = $response->json('data')['uid'];
-                    $titles[$key] = $articleId;
-                }else{
-                    $this->error('append fail');
-                    Log::error('append fail');
-                }
-            }
+        $this->info('map data='.count($data));
+
+        //目录写入db
+        $url = config('app.url').'/api/v2/article-map/'.$anthologyId;
+        $response = Http::withToken($token)->put($url,
+        [
+            'data'=> $data,
+            'operation' => "anthology",
+        ]);
+        if($response->ok()){
+            $this->info('map update ok ');
+        }else{
+            $this->error('map update  fail.');
+            Log::error('map update  fail ');
         }
         }
-        print_r($titles);
         return 0;
         return 0;
     }
     }
 }
 }

+ 29 - 2
app/Http/Controllers/ArticleMapController.php

@@ -29,9 +29,36 @@ class ArticleMapController extends Controller
                 $table = ArticleCollection::where('article_id',$request->get('id'));
                 $table = ArticleCollection::where('article_id',$request->get('id'));
                 break;
                 break;
         }
         }
-        $result = $table->select(['id','collect_id','article_id','level','title','children','editor_id','deleted_at'])
+        $count = $table->count();
+        $result = [];
+        if(!empty($request->get('parent'))){
+            //输出某节点的子节点
+            $node = $table->where('article_id',$request->get('parent'))->first();
+            if($node){
+                $nodeList = ArticleCollection::where('collect_id',$request->get('id'))
+                                            ->where('id','>',(int)$node->id)->orderBy('id')->get();
+                foreach ($nodeList as $key => $curr) {
+                    if($curr->level <= $node->level){
+                        break;
+                    }
+                    if($request->has('lazy')){
+                        if($curr->level === $node->level+1){
+                            $result[] = $curr;
+                        }
+                    }else{
+                        $result[] = $curr;
+                    }
+                }
+            }
+        }else{
+            if($request->has('lazy') && $count > 300){
+                $table = $table->where('level',1);
+            }
+            $result = $table->select(['id','collect_id','article_id','level','title','children','editor_id','deleted_at'])
                         ->orderBy('id')->get();
                         ->orderBy('id')->get();
-        return $this->ok(["rows"=>ArticleMapResource::collection($result),"count"=>count($result)]);
+        }
+
+        return $this->ok(["rows"=>ArticleMapResource::collection($result),"count"=>$count]);
     }
     }
 
 
     /**
     /**

+ 1 - 6
app/Http/Controllers/CollectionController.php

@@ -12,9 +12,6 @@ use App\Http\Api\ShareApi;
 use App\Http\Resources\CollectionResource;
 use App\Http\Resources\CollectionResource;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\DB;
 
 
-require_once __DIR__.'/../../../public/app/ucenter/function.php';
-
-
 class CollectionController extends Controller
 class CollectionController extends Controller
 {
 {
     /**
     /**
@@ -24,9 +21,7 @@ class CollectionController extends Controller
      */
      */
     public function index(Request $request)
     public function index(Request $request)
     {
     {
-        //
-                //
-        $userinfo = new \UserInfo();
+
 		$result=false;
 		$result=false;
 		$indexCol = ['uid','title','subtitle','summary',
 		$indexCol = ['uid','title','subtitle','summary',
                       'article_list','owner','status',
                       'article_list','owner','status',