123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- <?php
- namespace App\Http\Controllers\Manage\Book;
- use App\Modules\Book\Models\Book;
- use App\Http\Controllers\Manage\Book\Transformers\ChapterListTransformer;
- use App\Http\Controllers\Manage\Book\Transformers\ChapterTransformer;
- use App\Modules\Book\Services\BookConfigService;
- use App\Modules\Book\Services\ChapterService;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Storage;
- use Hashids;
- use DB;
- class ChapterController extends Controller
- {
-
-
- public function getChapterLists(Request $request,$bid){
- $page_size = $request->input('page_size',15);
- $lists = ChapterService::getChapterListsPage($bid,$page_size);
- $book = BookConfigService::getBookById($bid);
- foreach ($lists as &$item){
- $item->is_recommend = 0;
- $item->recommend_text = '';
- if($item->sequence>= ($book->force_subscribe_chapter_seq)){
- $item->is_need_subscirbe = 1;
- }else{
- $item->is_need_subscirbe = 0;
- }
- if($item->id == $book->recommend_cid ){
- $item->is_recommend = 1;
- if( Storage::exists('RecommendChapterPositionWord.txt')){
- $item->recommend_text = Storage::get('RecommendChapterPositionWord.txt');
- }else{
- $item->recommend_text = '建议此章节生成推广文案(原文转化率好)';
- }
-
- }
- }
- return response()->pagination(new ChapterListTransformer,$lists);
- }
-
- public function getTopFiveChapter($bid){
- $res = ChapterService::getTopFiveChapter($bid);
- return response()->collection(new ChapterTransformer,$res);
- }
-
- function getCatalogPagination(Request $request, $bid)
- {
- if(!is_numeric($bid)){
- $bid = Hashids::decode($bid)[0];
- }
- $page_size = $request->input('page_size',15);
- $lists = ChapterService::getChapterPage($bid,$page_size);
- $book = BookConfigService::getBookById($bid);
- foreach ($lists as &$item){
- $item->is_recommend = '';
- $item->recommend_text = '';
- if($item->sequence>= ($book->force_subscribe_chapter_seq)){
- $item->is_need_subscirbe = 1;
- }else{
- $item->is_need_subscirbe = 0;
- }
- if($item->id == $book->recommend_cid ){
- $item->is_recommend = 1;
- if( Storage::exists('RecommendChapterPositionWord.txt')){
- $item->recommend_text = Storage::get('RecommendChapterPositionWord.txt');
- }else{
- $item->recommend_text = '建议此章节生成推广文案(原文转化率好)';
- }
-
- }
- }
- return response()->pagination(new ChapterListTransformer,$lists);
- }
-
- public function editChapterVip(Request $request){
- $bid = $request->get('bid');
- $vip_num = $request->get('vip');
- if(empty($bid) || $vip_num == ''){
- return response()->error('PARAM_EMPTY');
- }
- ChapterService::editVip($bid,$vip_num);
- BookConfigService::editVipSeq($bid,$vip_num);
- return response()->success();
- }
-
- public function editChapterContent(Request $request){
- $cid = $request->input('cid');
- $bid = $request->input('bid');
- $content = $request->input('content');
- if(empty($cid) || empty($bid) || empty($content)){
- return response()->error('PARAM_EMPTY');
- }
- $chapter = ChapterService::getChapterFromDb($bid,$cid);
- $chapter->content = $content;
- $chapter->size = ceil(strlen($content)/3);
- ChapterService::editChapter($chapter);
- return response()->success();
- }
-
- public function editChapterName(Request $request){
- $cid = $request->get('cid');
- $chaper_name = $request->get('chaper_name');
- if(empty($cid) || empty($chaper_name)){
- return response()->error('PARAM_EMPTY');
- }
- ChapterService::updateChapterName($cid,$chaper_name);
- return response()->success();
- }
-
- public function deleteChapter(Request $request){
- $cid = $request->get('cid');
- $bid = $request->get('bid');
- if(empty($cid) || empty($bid)){
- return response()->error('PARAM_EMPTY');
- }
- $chapter = ChapterService::getChapterById($cid);
- if(!$chapter || $chapter->bid< 0){
- return response()->error('PARAM_EMPTY');
- }
- $book = Book::where('id',$bid)->first();
- $chapter->bid = -$chapter->bid;
- $chapter->save();
- ChapterService::updateSequence($bid,$chapter->sequence);
- $this->adjustSequentOne($bid,$chapter->sequence-1,$chapter->sequence+1);
- $book->size -= $chapter->size;
- if($chapter->sequence == 1){
- $next = ChapterService::getChapterInfoByBidAndSeq($bid,1);
- ChapterService::updateOne($chapter->next_cid,['prev_cid'=>0]);
- if($next){
- $book->first_cid = $next->id;
- }
- }
- if($chapter->sequence == $book->chapter_count){
- $prev = ChapterService::getChapterInfoByBidAndSeq($bid,$chapter->sequence-1);
- ChapterService::updateOne($chapter->prev_cid,['next_cid'=>0]);
- if($prev){
- $book->last_cid = $prev->id;
- $book->last_chapter = $prev->name;
- }
- }
- $book->chapter_count -= 1;
- $book->save();
- return response()->success();
- }
-
- public function addChapter(Request $request){
- $param = $request->except('_url');
- $res = checkParam($param,['name','content','sequence','is_vip','bid']);
- if($res){
- return response()->error('PARAM_EMPTY',['msg'=>$res]);
- }
- if(!is_numeric($param['sequence'])){
- return response()->error('PARAM_ERROR',['msg'=>'sequence']);
- }
- $book = Book::where('id',$param['bid'])->first();
- if(!$book){
- return response()->error('PARAM_ERROR',['msg'=>'bid not exist']);
- }
- DB::beginTransaction();
- try{
- ChapterService::updateSequenceIncr($param['bid'],$param['sequence']);
- $param['size'] = mb_strlen($param['content']);
- $param['recent_update_at'] = date('Y-m-d H:i:s');
- $param['prev_cid'] = 0;
- $param['next_cid'] = 0;
- $param['ly_chapter_id'] = 0;
- $result = ChapterService::createChapter($param);
- if($param['sequence'] == 1){
- $book->first_cid = $result->id;
- }
- if($param['sequence'] == $book->chapter_count+1){
- $book->last_cid = $result->id;
- $book->last_chapter = $param['name'];
- }
- $this->adjustSequentOne($param['bid'],$param['sequence']-1,$param['sequence']+1);
- $book->chapter_count++;
- $book->size += $param['size'] ;
- $book->save();
- DB::commit();
- return response()->success();
- }catch (\Exception $e){
- DB::rollBack();
- }
- return response()->error('UNKNOWN_ERROR');
- }
- private function adjustSequentOne($bid,$startsequence,$endsequence){
- $chapter_list = DB::table('chapters')
- ->whereBetween('sequence',[$startsequence,$endsequence])->orderBy('sequence')->where('bid',$bid)->get();
- $prev = 0;
- foreach ($chapter_list as $chapter){
- if($prev){
- DB::table('chapters')->where('id',$chapter->id)->update(['prev_cid'=>$prev]);
- DB::table('chapters')->where('id',$prev)->update(['next_cid'=>$chapter->id]);
- }
- $prev = $chapter->id;
- }
- }
- }
|