ChapterService.php 12 KB

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