| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 | <?php/** */namespace Modules\ContentManage\Services\Books;use Modules\ContentManage\Models\BookChapterContents;use Modules\ContentManage\Models\BookChapters;class ChapterService{    /**     * 解析导入的文件,可以从中间章节导入,但是比空虚连续,比如可以从第5章导入$start_sequence=5,可以支持覆盖导入     * 比如如果之前导入的第10-20章有问题,可以重新导入第10-20章,此时$start_sequence=10,会覆盖之前第10-20章的内容     * @param [int] $bid      * @param [itn] $start_sequence   开始序号,可以支持     * @param [int] $vip_start        vip起始章节     * @param [string] $path          文件目录       * @return void     */    public static function createChapterFromFile($bid,$start_sequence,$vip_start,$path){        if( !file_exists($path)){            return ;        }        $handle = fopen($path, "r");        $i = $start_sequence;        $content = '';        $last_name = '';        $last_chapter = '';        $size = 0;        $first_cid = 0;        $last_cid = 0;        //一行行读入文件,如果当前行是以###开头,则表示此行是章节标题,直到读到下一个标题,两个标题中间的内容就是正文        while (($buffer = fgets($handle)) !== false) {            $buffer = trim($buffer);            $buffer = trim($buffer,' ');            $buffer = preg_replace('/\s+/','',$buffer);            if (!$buffer) continue;            $temp =$buffer;            //解析标题            if($name = self::parseName($temp)){                if(!$last_name){                    //第一行直接过                    $last_name = $name;                }else{                    //第二行,中间的部分是内容,此时可以保存数据库                    $is_vip = $i >= $vip_start? 1:0;                    $size += mb_strlen($content);                    $cid = self::createOrUpdateContent($bid,$i,$last_name,$content,$is_vip);                    if($i  == $start_sequence){                        $first_cid = $cid;                    }                    $i++;$content = '';                    $last_name = $name;                }            }else{                $content .= $temp."\r\n";            }        }        //当前是最后一张        $is_vip = $i >= $vip_start? 1:0;        $last_cid = self::createOrUpdateContent($bid,$i,$last_name,$content,$is_vip);        $last_chapter = $last_name;        $size += mb_strlen($content);        fclose($handle);        return ['size'=>$size,'chapter_count'=>$i,'first_cid'=>$first_cid,'last_cid'=>$last_cid,'last_chapter'=>$last_chapter];    }    /**     * 保存章节,支持覆盖     *     * @param [type] $bid     * @param [type] $sequence     * @param [type] $name     * @param [type] $content     * @param [type] $is_vip     * @return void     */    private static function createOrUpdateContent($bid,$sequence,$name,$content,$is_vip){        $name = mb_convert_encoding($name,'UTF-8');        $content = mb_convert_encoding($content,'UTF-8');        $chapter_info = BookChapters::where('bid',$bid)->where('sequence',$sequence)->first();        if($chapter_info){            $content_info = BookChapterContents::where('id',$chapter_info->chapter_content_id)->first();            if($content_info){                $content_info->chapter_name = $name;                $content_info->content = $content;                $content_info->save();            }else{                $content_info = BookChapterContents::create(['bid'=>$bid,'chapter_name'=>$name,'content'=>$content]);            }            $chapter_info->name = $name;            $chapter_info->size = mb_strlen($content);            $chapter_info->chapter_content_id = $content_info->id;            $chapter_info->save();        }else{            $content_info =  BookChapterContents::create(['bid'=>$bid,'chapter_name'=>$name,'content'=>$content]);            $chapter_info = BookChapters::create([                'bid'=>$bid,'name'=>$name,'sequence'=>$sequence,'size'=>mb_strlen($content),                'is_vip'=>$is_vip,'prev_cid'=>0,'next_cid'=>0,'chapter_content_id'=>$content_info->id            ]);        }        return $chapter_info->id;    }    /**     * 解析章节标题     *     * @param [type] $content     * @return void     */    private static function parseName($content){        if(stripos($content,'###') !== false){            return str_replace('###','',$content);        }        /*        $status = preg_match('/^第.*?章/',$content);        if($status){            return  $content;        }*/        return '';    }}
 |