visuddhinanda 2 سال پیش
والد
کامیت
bafd533cbc

+ 83 - 0
app/Console/Commands/TestMarkdownToTpl.php

@@ -0,0 +1,83 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Http\Controllers\ArticleController;
+use Illuminate\Support\Facades\Log;
+
+class TestMarkdownToTpl extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'test:markdown.tpl {item?}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        if(\App\Tools\Tools::isStop()){
+            return 0;
+        }
+        Log::info('md render start item='.$this->argument('item'));
+        $data = array();
+        $data['basic'] = <<<md
+        # 去除烦恼的五种方法(一种分类)
+
+        1 为了自己的利益而从别人处听法;
+        2 为了自己的利益而开示自己听闻过的法;
+        3 念诵听闻过、学习过的法;
+        4 一次又一次地用心思维听闻过、学习过的法;
+        5 在心中忆念自己适合的禅修业处,如十遍、十不净等。
+
+        (无碍解道,义注,1,63)
+        md;
+
+        $data['tpl'] = <<<md
+        为了自己的利益而从别人处听法;
+
+        {{168-916-2-9}}
+        md;
+
+        $article = new ArticleController;
+
+        foreach ($data as $key => $value) {
+            $_item = $this->argument('item');
+            if(!empty($_item) && $key !==$_item){
+                continue;
+            }
+            $tpl = $article->toTpl($value,
+                        'eb9e3f7f-b942-4ca4-bd6f-b7876b59a523',
+                        [
+                            'user_uid'=>'ba5463f3-72d1-4410-858e-eadd10884713',
+                            'user_id'=>4,
+                        ]
+                    );
+            var_dump($tpl);
+        }
+        return 0;
+    }
+}

+ 90 - 0
app/Http/Api/SentenceApi.php

@@ -0,0 +1,90 @@
+<?php
+
+namespace App\Http\Api;
+
+use Illuminate\Support\Str;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Redis;
+
+use App\Models\Sentence;
+use App\Models\SentHistory;
+use App\Models\Channel;
+
+use App\Http\Api\ShareApi;
+
+class SentenceApi{
+    protected $auth = false;
+    protected $channel = null;
+
+    public function auth($channelId,$userId){
+        $channel = Channel::where('uid',$channelId)->first();
+        if(!$channel){
+            return false;
+        }
+        if($channel->owner_uid !== $userId){
+            //判断是否为协作
+            $power = ShareApi::getResPower($userId,$channel->uid,2);
+            if($power < 20){
+                return false;
+            }
+        }
+        $this->channel = $channel;
+        $this->auth = true;
+        return true;
+    }
+    public function store($sent,$user,$copy=false){
+        $row = Sentence::firstOrNew([
+            "book_id"=>$sent['book_id'],
+            "paragraph"=>$sent['paragraph'],
+            "word_start"=>$sent['word_start'],
+            "word_end"=>$sent['word_end'],
+            "channel_uid"=>$this->channel->uid,
+        ],[
+            "id"=>app('snowflake')->id(),
+            "uid"=>Str::uuid(),
+        ]);
+        $row->content = $sent['content'];
+        $row->strlen = mb_strlen($sent['content'],"UTF-8");
+        $row->language = $this->channel->lang;
+        $row->status = $this->channel->status;
+        if($copy){
+            //复制句子,保留原作者信息
+            $row->editor_uid = $sent["editor_uid"];
+            $row->acceptor_uid = $user["user_uid"];
+            $row->pr_edit_at = $sent["updated_at"];
+        }else{
+            $row->editor_uid = $user["user_uid"];
+            $row->acceptor_uid = null;
+            $row->pr_edit_at = null;
+        }
+        $row->create_time = time()*1000;
+        $row->modify_time = time()*1000;
+        $row->save();
+
+        //保存历史记录
+        if($copy){
+            $this->saveHistory($row->uid,$sent["editor_uid"],$sent['content']);
+        }else{
+            $this->saveHistory($row->uid,$user["user_uid"],$sent['content']);
+        }
+        //清除缓存
+        $sentId = "{$sent['book_id']}-{$sent['paragraph']}-{$sent['word_start']}-{$sent['word_end']}";
+        $hKey = "/sentence/res-count/{$sentId}/";
+        Redis::del($hKey);
+    }
+
+    private function saveHistory($uid,$editor,$content){
+        $newHis = new SentHistory;
+        $newHis->id = app('snowflake')->id();
+        $newHis->sent_uid = $uid;
+        $newHis->user_uid = $editor;
+        if(empty($content)){
+            $newHis->content = "";
+        }else{
+            $newHis->content = $content;
+        }
+
+        $newHis->create_time = time()*1000;
+        $newHis->save();
+    }
+}

+ 33 - 0
database/migrations/2023_11_11_145019_add_book_id_in_collections.php

@@ -0,0 +1,33 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddBookIdInCollections extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('collections', function (Blueprint $table) {
+            $table->integer('book_id')->nullable()->index();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('collections', function (Blueprint $table) {
+            $table->dropColumn('book_id');
+
+        });
+    }
+}