Bladeren bron

新增书籍管理相关接口

lh 1 week geleden
bovenliggende
commit
2464c6ecfe

+ 1 - 8
README.md

@@ -1,8 +1 @@
-### 相关运维sql查询
-```
-查询书籍排序重复的问题
-select bid,count(*) from chapters WHERE sequence = 1 group by bid having count(bid)>1;
-
-查询书籍完本统计
-SELECT COUNT(*), `status` FROM books GROUP BY `status`;
-```
+多人有声后台

+ 1 - 1
app/Http/Controllers/Account/AccountController.php

@@ -37,7 +37,7 @@ class AccountController extends BaseController
         $all      = $request->all();
         $account  = trim(getProp($all, 'account'));
         $passwd = trim(getProp($all, 'passwd'));
-        if (strlen($account) < 1 || strlen($passwd) != 6) {
+        if (strlen($account) < 1 || strlen($passwd) < 1) {
             Utils::throwError(ErrorConst::PARAM_ERROR_CODE);
         }
 

+ 36 - 5
app/Http/Controllers/Book/BookController.php

@@ -8,31 +8,62 @@ use App\Exceptions\ApiException;
 use App\Libs\ApiResponse;
 use App\Libs\Utils;
 use App\Services\Book\BookService;
+use App\Transformer\Book\BookTransformer;
 use Illuminate\Http\Request;
 use Illuminate\Routing\Controller as BaseController;
 use Illuminate\Support\Facades\Redis;
 use Illuminate\Support\Facades\Validator;
 
-class DeepSeekController extends BaseController
+class BookController extends BaseController
 {
     use ApiResponse;
     protected $bookService;
 
     public function __construct(
-        BookService $BookService
+        BookService $bookService
     ) {
         $this->bookService = $bookService;
     }
 
     /**
-     * 可选供应商
+     * 书籍列表
      *
      * @param Request $request
      * @return mixed
      */
-    public function chatWithReasoner(Request $request) {
+    public function bookList(Request $request) {
         $data = $request->all();
-        $result = $this->bookService->chatWithReasoner($data);
+        $result = $this->bookService->getBookList($data);
+        return $this->success($result, [new BookTransformer(), 'newBuildBookList']);
+    }
+
+    public function allBooks(Request $request) {
+        $data = $request->all();
+        $result = $this->bookService->getAllBooks($data);
+        return $this->success($result);
+    }
+
+    public function bookVersion(Request $request) {
+        $data = $request->all();
+        $result = $this->bookService->getBookVersion($data);
+        return $this->success($result);
+    }
+
+    public function addBookVersion(Request $request) {
+        $data = $request->all();
+        $result = $this->bookService->addBookVersion($data);
+        return $this->success(['success'=>$result ? 1 : 0]);
+    }
+
+    public function chapterList(Request $request) {
+        $data = $request->all();
+        $result = $this->bookService->getChapterList($data);
+        return $this->success($result, [new BookTransformer(), 'newBuildChapterList']);
+    }
+
+    public function chapterContent(Request $request) {
+        $data = $request->all();
+        $result = $this->bookService->getChapterContent($data);
         return $this->success($result);
     }
 }

+ 1 - 1
app/Http/Middleware/CheckTokenTrait.php

@@ -13,7 +13,7 @@ trait CheckTokenTrait
     public function checkTokenTrait($token)
     {
         // 获取用户信息
-        $user = DB::table('bd_manage_users')->where('token', $token)->first();
+        $user = DB::table('mp_manage_users')->where('token', $token)->first();
         $uid  = (int)getProp($user, 'id');
         if (!$uid) {
             Utils::throwError(ErrorConst::NOT_LOGIN);

+ 2 - 0
app/Libs/Helpers.php

@@ -648,10 +648,12 @@ function chineseNum($figure, $capital = false, $mode = true)
     return mb_substr($result, 0, 2) == '一十' ? mb_substr($result, 1) : $result;
 }
 
+
 function addPrefix($str)
 {
     if (!$str) return '';
     if (mb_substr($str, 0, 4) == 'http') return $str;
+    if (mb_substr($str, 0, 5) == 'https') return $str;
     if (mb_substr($str, 0, 7) == '/books/') return 'http://zwcontent.oss-cn-hangzhou.aliyuncs.com' . $str;
     if (mb_substr($str, 0, 6) == '/card/') return 'http://zwcontent.oss-cn-hangzhou.aliyuncs.com' . $str;
     if (mb_substr($str, 0, 15) == 'uploader/idcard') return 'http://zwcontent.oss-cn-hangzhou.aliyuncs.com/' . $str;

+ 122 - 3
app/Services/Book/BookService.php

@@ -21,15 +21,22 @@ class BookService
     public function getBookList($data) {
         $bid = getProp($data, 'bid');
         $book_name = getProp($data, 'book_name');
+        $status = getProp($data, 'status');
 
-        $query = DB::table('books as b')->leftJoin('book_configs as bc', 'b.id', '=', 'bc.bid')->select('b.*', 'bc.*');
+        $query = DB::table('books as b')->leftJoin('book_configs as bc', 'b.id', '=', 'bc.bid')
+        ->whereIn('bc.cp_source', ['ycsd', 'yqsd'])->whereIn('bc.is_on_shelf', [1,2])->select('b.status', 'bc.*', 
+        DB::raw('(select count(id) from mp_book_version where bid = b.id) as version_count'));
 
         if ($bid) {
             $query->where('b.id', $bid);
         }
 
         if ($book_name) {
-            $query->where('b.name', 'like', "%{$book_name}%");
+            $query->where('bc.book_name', 'like', "%{$book_name}%");
+        }
+
+        if ($status !== '') {
+            $query->where('b.status', $status);
         }
 
         $result = $query->orderBy('b.id')->paginate();
@@ -37,7 +44,119 @@ class BookService
         return $result;
     }
 
-    public function getBook($data) {
+    public function getAllBooks($data) {
+        $bid = getProp($data, 'bid');
+        $book_name = getProp($data, 'book_name');
+
+        $query = DB::table('book_configs')->whereIn('is_on_shelf', [1,2])->select('bid', 'book_name');
+        if ($bid) {
+            $query->where('bid', $bid);
+        }
+
+        if ($book_name) {
+            $query->where('book_name', 'like', "%{$book_name}%");
+        }
+
+        return $query->orderBy('bid', 'desc')->get()->map(function($value) {
+            return (array)$value;
+        })->toArray();
+    }
+
+    public function getBookVersion($data) {
+        $bid = getProp($data, 'bid');
+        if (!$bid) Utils::throwError('20003:请选择书籍');
+
+        return DB::table('mp_book_version')->where('bid', $bid)->orderBy('id', 'desc')->select('bid', 'id as version_id', 'version_name')
+        ->get()->map(function($value) {
+            return (array)$value;
+        })->toArray();
+    }
+
+    public function addBookVersion($data) {
+        $bid = getProp($data, 'bid');
+        if (!$bid) Utils::throwError('20003:请选择书籍');
+
+        $version_name = trim(getProp($data, 'version_name'));
+        if (!$version_name) Utils::throwError('20003:请填写版本名');
+        $book_name = DB::table('book_configs')->where('bid', $bid)->value('book_name');
+        $version_name = $book_name."【{$version_name}】";
+        if (DB::table('mp_book_version')->where('bid', $bid)->where('version_name', $version_name)->exists()) Utils::throwError('20003:版本名已存在');
+        
+        $all_chapters = DB::table('chapters')->where('bid', $bid)->where('is_check', 1)->where('is_deleted', 0)->select('id', 'name', 'sequence', 'size', 'chapter_content_id')->get();
+        try {
+            DB::beginTransaction();
+
+            $version_id = DB::table('mp_book_version')->insertGetId([
+                'bid' => $bid,
+                'version_name' => $version_name,
+                'created_at' => date('Y-m-d H:i:s'),
+                'updated_at' => date('Y-m-d H:i:s'),
+            ]);
+            if (!$version_id) {
+                DB::rollBack();
+                Utils::throwError('20003:创建版本失败');
+            }
+
+            $insert_data = [];
+            foreach ($all_chapters as $chapter) {
+                $insert_data[] = [
+                    'bid'                   => $bid,
+                    'book_name'             => $book_name,
+                    'version_id'            => $version_id,
+                    'cid'                   => getProp($chapter, 'id'),
+                    'chapter_name'          => getProp($chapter, 'name'),
+                    'sequence'              => getProp($chapter, 'sequence'),
+                    'size'                  => getProp($chapter, 'size'),
+                    'generate_status'       => '待制作',
+                    'created_at'            => date('Y-m-d H:i:s'),
+                    'updated_at'            => date('Y-m-d H:i:s'),
+                ];
+            }
+
+            $boolen = DB::table('mp_chapter_audios')->insert($insert_data);
+            if (!$boolen) {
+                DB::rollBack();
+                Utils::throwError('20003:带入章节失败');
+            }
+
+        }catch (\Exception $e) {
+            DB::rollBack();
+            Utils::throwError('20003:'.$e->getMessage());
+
+        }
+
+        DB::commit();
+        return true;
+    }
+
+    public function getChapterList($data) {
+        $bid = getProp($data, 'bid');
+        $version_id = getProp($data, 'version_id');
+        if (!$bid) Utils::throwError('20003:请选择书籍');
+        if (!$version_id) Utils::throwError('20003:请选择版本');
+        $book = DB::table('books as b')->leftJoin('book_configs as bc', 'b.id', '=', 'bc.bid')->where('b.id', $bid)->whereIn('bc.is_on_shelf', [1,2])
+        ->select('bc.book_name', 'bc.cover', 'b.size', 'b.chapter_count', 'b.intro')->first();
+        if (!$book) Utils::throwError('20003:书籍不存在');
+        $header = (array)$book;
+        $header['cover'] = addPrefix($header['cover']);
+        $header['intro'] = filterIntro($header['intro']);
+        
+        $list = DB::table('mp_chapter_audios')->where('bid', $bid)->where('version_id', $version_id)->orderBy('sequence', 'asc')
+        ->select('cid', 'chapter_name', 'sequence', 'size', 'generate_status', 'audio_url', 'remark')
+        ->paginate();
+
+        return ['header' => $header, 'list' => $list];
+
+    }
+
+    public function getChapterContent($data) {
+        $cid = getProp($data, 'cid');
+
+        if (!$cid) Utils::throwError('20003:请选择章节');
+        $content = DB::table('chapters as c')->leftJoin('chapter_contents as cc', 'c.chapter_content_id', '=', 'cc.id')->where('c.id', $cid)->value('cc.content');
+        if (!$content) Utils::throwError('20003:章节内容不存在');
+        $content = filterContent($content);
 
+        return $content;
     }
 }

+ 69 - 0
app/Transformer/Book/BookTransformer.php

@@ -0,0 +1,69 @@
+<?php
+
+namespace App\Transformer\Book;
+
+use App\Cache\StatisticCache;
+use App\Consts\BaseConst;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Redis;
+use Vinkla\Hashids\Facades\Hashids;
+
+class BookTransformer
+{
+    // 用户列表
+    public function newBuildBookList($data): array
+    {
+        return [
+            'meta' => getMeta($data),
+            'list'  => $this->newEachBookList($data),
+        ];
+    }
+
+    private function newEachBookList($list): array
+    {
+        $result = [];
+        if (empty($list)) return $result;
+
+        foreach ($list as $item) {
+            $result[] = [
+                'bid'                       => getProp($item, 'bid'),
+                'book_name'                 => getProp($item, 'book_name'),
+                'origin_book_name'          => getProp($item, 'origin_book_name') ? getProp($item, 'origin_book_name') : getProp($item, 'book_name'),
+                'status'                    => getProp($item, 'status'),
+                'status_info'               => getProp($item, 'status') == 1 ? '已完结' : '连载中',
+                'version_count'             => getProp($item, 'version_count'),   
+            ];
+        }
+
+        return $result;
+    }
+
+    // 用户列表
+    public function newBuildChapterList($data): array
+    {
+        return [
+            'meta'      => getMeta($data['list']),
+            'header'    => $data['header'],
+            'list'      => $this->newEachChapterList($data['list']),
+        ];
+    }
+
+    private function newEachChapterList($list): array
+    {
+        $result = [];
+        if (empty($list)) return $result;
+
+        foreach ($list as $item) {
+            $result[] = [
+                'cid'               => getProp($item, 'cid'),
+                'chapter_name'      => getProp($item, 'chapter_name'),
+                'sequence'          => getProp($item, 'sequence'),
+                'size'              => getProp($item, 'size'),
+                'generate_status'   => getProp($item, 'generate_status'),
+                'audio_url'         => getProp($item, 'audio_url'),
+            ];
+        }
+
+        return $result;
+    }
+}

+ 11 - 5
routes/api.php

@@ -2,6 +2,7 @@
 
 use App\Http\Controllers\Account\AccountController;
 use App\Http\Controllers\DeepSeek\DeepSeekController;
+use App\Http\Controllers\Book\BookController;
 use Illuminate\Support\Facades\Route;
 
 /*
@@ -18,15 +19,20 @@ use Illuminate\Support\Facades\Route;
 Route::group(['middleware' => ['bindToken', 'bindExportToken', 'checkLogin']], function () {
 
     Route::group(['prefix' => 'book'], function () {
-        
+        Route::get('list', [BookController::class, 'bookList']);
+        Route::get('version', [BookController::class, 'bookVersion']);
+        Route::get('all', [BookController::class, 'allBooks']);
+        Route::get('addVersion', [BookController::class, 'addBookVersion']);
+        Route::get('chapterList', [BookController::class, 'chapterList']);
+        Route::get('chapterContent', [BookController::class, 'chapterContent']);
     });
 
-    
+    Route::group(['prefix' => 'deepseek'], function () {
+        Route::post('chatWithReasoner', [DeepSeekController::class, 'chatWithReasoner']);
+    });
     
 });
-Route::group(['prefix' => 'deepseek'], function () {
-    Route::post('chatWithReasoner', [DeepSeekController::class, 'chatWithReasoner']);
-});
+
 
 Route::get('login', [AccountController::class, 'login']); // 登录
 Route::get('logout', [AccountController::class, 'logout']); // 退出