ChapterService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/12/4
  6. * Time: 下午1:57
  7. */
  8. namespace App\Modules\Book\Services;
  9. use App\Modules\Book\Models\Book;
  10. use App\Modules\Book\Models\Chapter;
  11. use OSS\Core\OssException;
  12. use OSS\OssClient;
  13. use Redis;
  14. use Storage;
  15. use Log;
  16. class ChapterService
  17. {
  18. //private static $bucket = 'yueduyun';
  19. private static $bucket = 'zhuishuyun';
  20. /**
  21. * 获取章节内容
  22. * @param $bid
  23. * @param $chapter_id
  24. * @return array|bool|mixed|null|string
  25. */
  26. public static function getChapter($bid,$chapter_id){
  27. if(empty($bid) || empty($chapter_id))
  28. return false;
  29. //Redis::incr('book_'.$bid.'_click_count');
  30. //从redis获取
  31. try{
  32. $res = self::getChapterFromRedis($bid,$chapter_id);
  33. if($res) {
  34. return json_decode($res);
  35. }
  36. }catch (\Exception $e){
  37. Log::info($e);
  38. }
  39. //从oss获取
  40. try{
  41. $res = self::getChapterFromOss($bid,$chapter_id);
  42. if($res) {
  43. try{
  44. self::saveChapterToRedis($bid,json_decode($res));
  45. }catch (\Exception $redis){
  46. Log::info($redis);
  47. }
  48. return json_decode($res);
  49. }
  50. }catch (\Exception $e){
  51. Log::info($e);
  52. }
  53. $res = self::getChapterFromDb($bid,$chapter_id);
  54. if(empty($res)) return false;
  55. try{
  56. self::saveChapterToRedis($bid,$res);
  57. self::saveChapterToOss($bid,$res);
  58. }catch (\Exception $e){
  59. Log::info($e);
  60. }
  61. return $res;
  62. }
  63. /**
  64. * 根据cid获取章节内容
  65. * @param $chapter_id
  66. * @return mixed
  67. */
  68. public static function getChapterById($chapter_id){
  69. return Chapter::getChapterById($chapter_id);
  70. }
  71. /**
  72. * 获取目录不分页
  73. * @param $bid
  74. * @return mixed
  75. */
  76. public static function getChapterLists($bid)
  77. {
  78. $lists = Chapter::getChapterLists($bid);
  79. return $lists;
  80. }
  81. /**
  82. * 获取目录分页
  83. * @param $bid
  84. * @param int $page_size
  85. * @return mixed
  86. */
  87. public static function getChapterListsPage($bid,$page_size=15){
  88. $lists = Chapter::getChapterListsPage($bid,$page_size);
  89. return $lists;
  90. }
  91. /**
  92. * 获取章节信息没有内容
  93. * @param $chapter_id
  94. * @return mixed
  95. */
  96. public static function getChapterNameById($chapter_id,$bid)
  97. {
  98. $chapter = Chapter::getChapterNameById($chapter_id);
  99. if ($chapter){
  100. if ($chapter->bid == $bid){
  101. return $chapter;
  102. }else if (abs($chapter->bid) == $bid && $chapter->bid != $bid){
  103. return self::realChapterNameById($chapter,$bid);
  104. }
  105. }
  106. return false;
  107. }
  108. /**
  109. * 获取章节信息没有内容 不做bid的验证
  110. * @param $chapter_id
  111. * @return mixed
  112. */
  113. public static function getChapterNameByIdNoCheck($chapter_id){
  114. return Chapter::getChapterNameById($chapter_id);
  115. }
  116. /**
  117. * 获取前五章内容(推广需要)
  118. * @param $bid
  119. * @return mixed
  120. */
  121. public static function getTopFiveChapter($bid)
  122. {
  123. $res = Chapter::getTopFiveChapter($bid);
  124. return $res;
  125. }
  126. /**
  127. * 从oss获取章节内容
  128. * @param $chapter_id
  129. * @param $bid
  130. */
  131. public static function getChapterFromOss($bid, $chapter_id)
  132. {
  133. $bucket = env('OSS_BUCKET','zhuishuyun');
  134. $oss = self::ossObject();
  135. $path_format = 'book/chapters/%s/%s.json';
  136. $path = sprintf($path_format, $bid, $chapter_id);
  137. if ($oss->doesObjectExist($bucket, $path)) {
  138. return $oss->getObject($bucket, $path);
  139. }
  140. return null;
  141. }
  142. /**
  143. * 从redis获取章节内容
  144. * @param $chapter_id
  145. * @param $bid
  146. */
  147. public static function getChapterFromRedis($bid, $chapter_id)
  148. {
  149. $key = sprintf('book_chapter_%s_%s', $bid, $chapter_id);
  150. //Redis::connection('chapter')
  151. return Redis::connection('chapter')->get($key);
  152. }
  153. /**
  154. * 从db获取章节内容
  155. * @param $chapter_id
  156. * @param $bid
  157. */
  158. public static function getChapterFromDb($bid, $chapter_id)
  159. {
  160. $chapter = self::getChapterById($chapter_id);
  161. if ($chapter->bid != $bid) {
  162. return [];
  163. }
  164. return $chapter;
  165. }
  166. /**
  167. * 章节内容保存到oss
  168. * @param $chapter
  169. * @param $bid
  170. */
  171. public static function saveChapterToOss($bid, $chapter)
  172. {
  173. $bucket = env('OSS_BUCKET','zhuishuyun');
  174. $file = ('book/' . $bid . '_' . $chapter->id . '.json');
  175. Storage::put($file, json_encode($chapter));
  176. $path_format = 'book/chapters/%s/%s.json';
  177. self::ossObject()->uploadFile($bucket, sprintf($path_format, $bid, $chapter->id), storage_path('app/' . $file));
  178. Storage::delete($file);
  179. }
  180. /**
  181. * 章节内容保存到redis
  182. * @param $chapter
  183. * @param $bid
  184. */
  185. public static function saveChapterToRedis($bid, $chapter)
  186. {
  187. return Redis::connection('chapter')->setex(sprintf('book_chapter_%s_%s', $bid, $chapter->id),3600, json_encode($chapter));
  188. }
  189. /**
  190. * oss 对象
  191. * @return null|OssClient
  192. */
  193. public static function ossObject()
  194. {
  195. $accessKeyId = env('OSS_ACCESS_ID');
  196. $accessKeySecret = env('OSS_ACCESS_KEY');
  197. $endpoint = env('OSS_END_POINT');
  198. $ossClient = null;
  199. try {
  200. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  201. } catch (OssException $e) {
  202. return null;
  203. }
  204. return $ossClient;
  205. }
  206. //编辑vip章节
  207. public static function editVip($bid,$seq){
  208. return Chapter::editVip($bid,$seq);
  209. }
  210. public static function editChapter($chapter){
  211. $bucket = env('OSS_BUCKET','zhuishuyun');
  212. $bid = $chapter->bid;
  213. //$res_db = Chapter::where('id',$chapter->id)->update(['content'=>$chapter->content,'size'=>$chapter->size,'name'=>$chapter->name]);
  214. $chapter->save();
  215. //$res_db = true;
  216. if(self::getChapterFromOss($bid,$chapter->id)){
  217. $oss = self::ossObject();
  218. $path_format = 'book/chapters/%s/%s.json';
  219. $oss->deleteObject($bucket,sprintf($path_format, $bid, $chapter->id));
  220. }
  221. self::saveChapterToOss($bid,$chapter);
  222. $res_redis = self::saveChapterToRedis($bid,$chapter);
  223. return $res_redis;
  224. }
  225. //章节列表和内容
  226. public static function getChapterPage($bid,$page_size=15){
  227. return Chapter::getChapterPage($bid,$page_size);
  228. }
  229. /**
  230. * 根据bid和顺序获取章节信息
  231. * @param $bid
  232. * @param $seq
  233. * @return mixed
  234. */
  235. public static function getChapterInfoByBidAndSeq($bid,$seq){
  236. return Chapter::where('bid',$bid)->where('sequence',$seq)->select('id','name')->first();
  237. }
  238. public static function updateSequence(int $bid,int $sequence){
  239. Chapter::where('bid',$bid)->where('sequence','>',$sequence)->decrement('sequence',1);
  240. }
  241. public static function updateSequenceIncr(int $bid,int $sequence){
  242. Chapter::where('bid',$bid)->where('sequence','>=',$sequence)->increment('sequence',1);
  243. }
  244. public static function updateChapterName($cid,$name){
  245. Chapter::where('id',$cid)->update(['name'=>$name]);
  246. }
  247. public static function createChapter($param){
  248. return Chapter::create($param);
  249. }
  250. public static function updateOne(int $id,array $param){
  251. if($param){
  252. return Chapter::where('id',$id)->update($param);
  253. }
  254. return null;
  255. }
  256. public static function splitContentAll($bid,$sequence=1){
  257. $chapters = Chapter::where('bid',$bid)->where('size','>=',3300)
  258. ->where('sequence','>=',$sequence)
  259. ->select('id','sequence')
  260. ->orderBy('sequence')->get();
  261. $i = 0;
  262. $count = 0;
  263. $end_sequence = 0;
  264. $start_sequence = 0;
  265. foreach ($chapters as $k=>$chapter){
  266. if($k == 0){
  267. $start_sequence = $chapter->sequence;
  268. }
  269. $content = self::splitContent($chapter->id);
  270. if(!$content) continue;
  271. //\Log::info($chapter);
  272. $count += self::createSplitContent($chapter->id,$content,false);
  273. $end_sequence = $chapter->sequence;
  274. $i++;
  275. }
  276. //\Log::info('$start_sequence is: '.$start_sequence);
  277. //\Log::info('$end_sequence is: '.($end_sequence+$i+$count));
  278. self::adjustSequent($bid,$start_sequence,$end_sequence+$i+$count);
  279. return $count;
  280. }
  281. public static function splitContent($chapter_id)
  282. {
  283. $chapter = self::getChapterById($chapter_id);
  284. if(!$chapter || $chapter->bid <0 || $chapter->size < 3300) return [];
  285. $content_list = explode("\r\n",$chapter->content);
  286. //print_r($content_list);
  287. $count = 1;
  288. $temp_size= 0;
  289. $temp_content = '';
  290. $data = [];
  291. foreach ($content_list as $item){
  292. if($temp_size >2500){
  293. $count++;
  294. $temp_content = '';
  295. $temp_size = 0;
  296. }
  297. $temp_size += mb_strlen($item);
  298. $temp_content .= $item."\r\n";
  299. $data[$count-1] = [
  300. 'bid'=>$chapter->bid,
  301. 'is_vip'=>$chapter->is_vip,
  302. 'name'=>sprintf('%s(%s)',$chapter->name,$count),
  303. 'sequence'=>$chapter->sequence+$count-1,
  304. 'size'=>$temp_size,
  305. 'prev_cid'=>0,
  306. 'next_cid'=>0,
  307. 'recent_update_at'=>$chapter->recent_update_at,
  308. 'content'=>$temp_content,
  309. ];
  310. }
  311. if($data && $data[$count-1]['size'] <800){
  312. $data[$count-2]['content'] = $data[$count-2]['content'].$data[$count-1]['content'];
  313. $data[$count-2]['size'] = $data[$count-2]['size']+$data[$count-1]['size'];
  314. unset($data[$count-1]);
  315. }
  316. return $data;
  317. }
  318. public static function createSplitContent($chapter_id,$data,$is_adjust = true)
  319. {
  320. if(count($data) <= 1){
  321. return 0;
  322. }
  323. Chapter::where('bid',$data[0]['bid'])->where('sequence','>=',$data[0]['sequence']+1)->increment('sequence',count($data)-1);
  324. Chapter::where('id',$chapter_id)->update([
  325. 'content'=>$data[0]['content'],
  326. 'size'=>$data[0]['size'],
  327. 'name'=>$data[0]['name']
  328. ]);
  329. $i = 0;
  330. foreach ($data as $kye=>$item){
  331. if($kye == 0) continue;
  332. $i++;
  333. self::createChapter($item);
  334. }
  335. if($is_adjust){
  336. self::adjustSequent($data[0]['bid'],$data[0]['sequence'],$data[0]['sequence']+count($data)+5);
  337. }
  338. if(self::getChapterFromOss($data[0]['bid'],$chapter_id)){
  339. $oss = self::ossObject();
  340. $bucket = env('OSS_BUCKET','zhuishuyun');
  341. $path_format = 'book/chapters/%s/%s.json';
  342. $oss->deleteObject($bucket,sprintf($path_format, $data[0]['bid'], $chapter_id));
  343. }
  344. $key = sprintf('book_chapter_%s_%s', $data[0]['bid'], $chapter_id);
  345. //Redis::connection('chapter')
  346. try{
  347. Redis::connection('chapter')->delete($key);
  348. }catch (\Exception $e){}
  349. $last = Chapter::where('bid',$data[0]['bid'])
  350. ->select('id','bid','name','sequence')
  351. ->orderBy('sequence','desc')
  352. ->limit(1)
  353. ->first();
  354. Book::where('id',$data[0]['bid'])->update([
  355. 'chapter_count'=>$last->sequence,
  356. 'last_chapter'=>$last->name,
  357. 'last_cid'=>$last->id,
  358. ]);
  359. return $i;
  360. }
  361. public static function adjustSequent($bid,$start_sequence,$end_sequence){
  362. $chapter_list = Chapter::where('bid',$bid);
  363. if($start_sequence){
  364. $chapter_list = $chapter_list->where('sequence','>=',$start_sequence);
  365. }
  366. if($end_sequence){
  367. $chapter_list = $chapter_list->where('sequence','<=',$end_sequence);
  368. }
  369. $chapter_list = $chapter_list->orderBy('sequence')->select('id')->get();
  370. $prev = 0;
  371. foreach ($chapter_list as $chapter){
  372. if($prev){
  373. Chapter::where('id',$chapter->id)->update(['prev_cid'=>$prev]);
  374. Chapter::where('id',$prev)->update(['next_cid'=>$chapter->id]);
  375. }
  376. $prev = $chapter->id;
  377. }
  378. }
  379. /***
  380. * 根据章节序号获取章节信息没有内容
  381. * name: realChapterNameById
  382. * @param $chapter
  383. * @param $bid
  384. * @return false
  385. * date 2022/08/24 11:00
  386. */
  387. private static function realChapterNameById($chapter, $bid)
  388. {
  389. $sequence = $chapter['sequence'] ?? 0;
  390. $new_chapter = Chapter::where('bid',$bid)->where('sequence','>=',$sequence)->orderBy('sequence','asc')->select('id','bid','name','sequence','size','is_vip','prev_cid','next_cid','recent_update_at')->first();
  391. if ($new_chapter){
  392. return $new_chapter;
  393. }
  394. return false;
  395. }
  396. }