ChapterService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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]);
  210. //$res_db = true;
  211. if(self::getChapterFromOss($bid,$chapter->id)){
  212. $oss = self::ossObject();
  213. $path_format = 'book/chapters/%s/%s.json';
  214. $oss->deleteObject($bucket,sprintf($path_format, $bid, $chapter->id));
  215. }
  216. self::saveChapterToOss($bid,$chapter);
  217. $res_redis = self::saveChapterToRedis($bid,$chapter);
  218. return $res_db && $res_redis;
  219. }
  220. //章节列表和内容
  221. public static function getChapterPage($bid,$page_size=15){
  222. return Chapter::getChapterPage($bid,$page_size);
  223. }
  224. /**
  225. * 根据bid和顺序获取章节信息
  226. * @param $bid
  227. * @param $seq
  228. * @return mixed
  229. */
  230. public static function getChapterInfoByBidAndSeq($bid,$seq){
  231. return Chapter::where('bid',$bid)->where('sequence',$seq)->select('id','name')->first();
  232. }
  233. public static function updateSequence(int $bid,int $sequence){
  234. Chapter::where('bid',$bid)->where('sequence','>',$sequence)->decrement('sequence',1);
  235. }
  236. public static function updateSequenceIncr(int $bid,int $sequence){
  237. Chapter::where('bid',$bid)->where('sequence','>=',$sequence)->increment('sequence',1);
  238. }
  239. public static function updateChapterName($cid,$name){
  240. Chapter::where('id',$cid)->update(['name'=>$name]);
  241. }
  242. public static function createChapter($param){
  243. return Chapter::create($param);
  244. }
  245. public static function updateOne(int $id,array $param){
  246. if($param){
  247. return Chapter::where('id',$id)->update($param);
  248. }
  249. return null;
  250. }
  251. public static function splitContent($chapter_id)
  252. {
  253. $chapter = self::getChapterById($chapter_id);
  254. if(!$chapter || $chapter->bid <0 || $chapter->size < 4500) return [];
  255. $content_list = explode("\r\n",$chapter->content);
  256. //print_r($content_list);
  257. $count = 1;
  258. $temp_size= 0;
  259. $temp_content = '';
  260. $data = [];
  261. foreach ($content_list as $item){
  262. if($temp_size >2500){
  263. $count++;
  264. $temp_content = '';
  265. $temp_size = 0;
  266. }
  267. $temp_size += mb_strlen($item);
  268. $temp_content .= $item.'\r\n';
  269. $data[$count-1] = [
  270. 'bid'=>$chapter->bid,
  271. 'is_vip'=>$chapter->is_vip,
  272. 'name'=>sprintf('%s(%s)',$chapter->name,$count),
  273. 'sequence'=>$chapter->sequence+$count-1,
  274. 'size'=>$temp_size,
  275. 'prev_cid'=>0,
  276. 'next_cid'=>0,
  277. 'recent_update_at'=>$chapter->recent_update_at,
  278. 'content'=>$temp_content,
  279. ];
  280. }
  281. if($data && $data[$count-1]['size'] <1500){
  282. $data[$count-2]['content'] = $data[$count-2]['content'].$data[$count-1]['content'];
  283. $data[$count-2]['size'] = $data[$count-2]['size']+$data[$count-1]['size'];
  284. unset($data[$count-1]);
  285. }
  286. return $data;
  287. }
  288. public static function createSplitContent($chapter_id,$data)
  289. {
  290. if(count($data) == 1){
  291. return ;
  292. }
  293. Chapter::where('bid',$data[0]['bid'])->where('sequence','>=',$data[0]['sequence']+1)->increment('sequence',count($data)-1);
  294. Chapter::where('id',$chapter_id)->update([
  295. 'content'=>$data[0]['content'],
  296. 'size'=>$data[0]['size']
  297. ]);
  298. foreach ($data as $kye=>$item){
  299. if($kye == 0) continue;
  300. self::createChapter($item);
  301. }
  302. self::adjustSequent($data[0]['bid'],$data[0]['sequence'],$data[0]['sequence']+count($data));
  303. if(self::getChapterFromOss($data[0]['bid'],$chapter_id)){
  304. $oss = self::ossObject();
  305. $bucket = env('OSS_BUCKET','zhuishuyun');
  306. $path_format = 'book/chapters/%s/%s.json';
  307. $oss->deleteObject($bucket,sprintf($path_format, $data[0]['bid'], $chapter_id));
  308. }
  309. $key = sprintf('book_chapter_%s_%s', $data[0]['bid'], $chapter_id);
  310. //Redis::connection('chapter')
  311. Redis::connection('chapter')->delete($key);
  312. $last = Chapter::where('bid',$data[0]['bid'])
  313. ->select('id','bid','name','sequence')
  314. ->orderBy('sequence','desc')
  315. ->limit(1)
  316. ->first();
  317. Book::where('id',$data[0]['bid'])->update([
  318. 'chapter_count'=>$last->sequence,
  319. 'last_chapter'=>$last->last_chapter,
  320. 'last_cid'=>$last->id,
  321. ]);
  322. }
  323. public static function adjustSequent($bid,$start_sequence,$end_sequence){
  324. $chapter_list = Chapter::where('bid',$bid);
  325. if($start_sequence){
  326. $chapter_list->where('sequence','>=',$start_sequence);
  327. }
  328. if($end_sequence){
  329. $chapter_list->where('sequence','<=',$end_sequence);
  330. }
  331. $chapter_list->orderBy('sequence')->select('id')->get();
  332. $prev = 0;
  333. foreach ($chapter_list as $chapter){
  334. if($prev){
  335. Chapter::where('id',$chapter->id)->update(['prev_cid'=>$prev]);
  336. Chapter::where('id',$prev)->update(['next_cid'=>$chapter->id]);
  337. }
  338. $prev = $chapter->id;
  339. }
  340. }
  341. }