ChapterService.php 12 KB

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