ChapterService.php 12 KB

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