ChapterService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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\Chapter;
  10. use OSS\Core\OssException;
  11. use OSS\OssClient;
  12. use Redis;
  13. use Storage;
  14. use Log;
  15. class ChapterService
  16. {
  17. private static $bucket = env('OSS_BUCKET');
  18. /**
  19. * 获取章节内容
  20. * @param $bid
  21. * @param $chapter_id
  22. * @return array|bool|mixed|null|string
  23. */
  24. public static function getChapter($bid,$chapter_id){
  25. if(empty($bid) || empty($chapter_id))
  26. return false;
  27. //Redis::incr('book_'.$bid.'_click_count');
  28. //从redis获取
  29. try{
  30. $res = self::getChapterFromRedis($bid,$chapter_id);
  31. if($res) {
  32. return json_decode($res);
  33. }
  34. }catch (\Exception $e){
  35. Log::info($e);
  36. }
  37. //从oss获取
  38. try{
  39. $res = self::getChapterFromOss($bid,$chapter_id);
  40. if($res) {
  41. try{
  42. self::saveChapterToRedis($bid,json_decode($res));
  43. }catch (\Exception $redis){
  44. Log::info($redis);
  45. }
  46. return json_decode($res);
  47. }
  48. }catch (\Exception $e){
  49. Log::info($e);
  50. }
  51. $res = self::getChapterFromDb($bid,$chapter_id);
  52. if(empty($res)) return false;
  53. try{
  54. self::saveChapterToRedis($bid,$res);
  55. self::saveChapterToOss($bid,$res);
  56. }catch (\Exception $e){
  57. Log::info($e);
  58. }
  59. return $res;
  60. }
  61. /**
  62. * 根据cid获取章节内容
  63. * @param $chapter_id
  64. * @return mixed
  65. */
  66. public static function getChapterById($chapter_id){
  67. return Chapter::getChapterById($chapter_id);
  68. }
  69. /**
  70. * 获取目录不分页
  71. * @param $bid
  72. * @return mixed
  73. */
  74. public static function getChapterLists($bid)
  75. {
  76. $lists = Chapter::getChapterLists($bid);
  77. return $lists;
  78. }
  79. /**
  80. * 获取目录分页
  81. * @param $bid
  82. * @param int $page_size
  83. * @return mixed
  84. */
  85. public static function getChapterListsPage($bid,$page_size=15){
  86. $lists = Chapter::getChapterListsPage($bid,$page_size);
  87. return $lists;
  88. }
  89. /**
  90. * 获取章节信息没有内容
  91. * @param $chapter_id
  92. * @return mixed
  93. */
  94. public static function getChapterNameById($chapter_id,$bid)
  95. {
  96. $chapter = Chapter::getChapterNameById($chapter_id);
  97. if($chapter && $chapter->bid == $bid){
  98. return $chapter;
  99. }
  100. return false;
  101. }
  102. /**
  103. * 获取章节信息没有内容 不做bid的验证
  104. * @param $chapter_id
  105. * @return mixed
  106. */
  107. public static function getChapterNameByIdNoCheck($chapter_id){
  108. return Chapter::getChapterNameById($chapter_id);
  109. }
  110. /**
  111. * 获取前五章内容(推广需要)
  112. * @param $bid
  113. * @return mixed
  114. */
  115. public static function getTopFiveChapter($bid)
  116. {
  117. $res = Chapter::getTopFiveChapter($bid);
  118. return $res;
  119. }
  120. /**
  121. * 从oss获取章节内容
  122. * @param $chapter_id
  123. * @param $bid
  124. */
  125. public static function getChapterFromOss($bid, $chapter_id)
  126. {
  127. $bucket = env('OSS_BUCKET');
  128. $oss = self::ossObject();
  129. $path_format = 'book/chapters/%s/%s.json';
  130. $path = sprintf($path_format, $bid, $chapter_id);
  131. if ($oss->doesObjectExist($bucket, $path)) {
  132. return $oss->getObject($bucket, $path);
  133. }
  134. return null;
  135. }
  136. /**
  137. * 从redis获取章节内容
  138. * @param $chapter_id
  139. * @param $bid
  140. */
  141. public static function getChapterFromRedis($bid, $chapter_id)
  142. {
  143. $key = sprintf('book_chapter_%s_%s', $bid, $chapter_id);
  144. //Redis::connection('chapter')
  145. return Redis::connection('chapter')->get($key);
  146. }
  147. /**
  148. * 从db获取章节内容
  149. * @param $chapter_id
  150. * @param $bid
  151. */
  152. public static function getChapterFromDb($bid, $chapter_id)
  153. {
  154. $chapter = self::getChapterById($chapter_id);
  155. if ($chapter->bid != $bid) {
  156. return [];
  157. }
  158. return $chapter;
  159. }
  160. /**
  161. * 章节内容保存到oss
  162. * @param $chapter
  163. * @param $bid
  164. */
  165. public static function saveChapterToOss($bid, $chapter)
  166. {
  167. $bucket = env('OSS_BUCKET');
  168. $file = ('book/' . $bid . '_' . $chapter->id . '.json');
  169. Storage::put($file, json_encode($chapter));
  170. $path_format = 'book/chapters/%s/%s.json';
  171. self::ossObject()->uploadFile($bucket, sprintf($path_format, $bid, $chapter->id), storage_path('app/' . $file));
  172. Storage::delete($file);
  173. }
  174. /**
  175. * 章节内容保存到redis
  176. * @param $chapter
  177. * @param $bid
  178. */
  179. public static function saveChapterToRedis($bid, $chapter)
  180. {
  181. return Redis::connection('chapter')->setex(sprintf('book_chapter_%s_%s', $bid, $chapter->id),3600, json_encode($chapter));
  182. }
  183. /**
  184. * oss 对象
  185. * @return null|OssClient
  186. */
  187. public static function ossObject()
  188. {
  189. $accessKeyId = env('OSS_ACCESS_ID');
  190. $accessKeySecret = env('OSS_ACCESS_KEY');
  191. $endpoint = env('OSS_END_POINT');
  192. $ossClient = null;
  193. try {
  194. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  195. } catch (OssException $e) {
  196. return null;
  197. }
  198. return $ossClient;
  199. }
  200. //编辑vip章节
  201. public static function editVip($bid,$seq){
  202. return Chapter::editVip($bid,$seq);
  203. }
  204. public static function editChapter($chapter){
  205. $bucket = env('OSS_BUCKET');
  206. $bid = $chapter->bid;
  207. $res_db = Chapter::where('id',$chapter->id)->update(['content'=>$chapter->content,'size'=>$chapter->size]);
  208. //$res_db = true;
  209. if(self::getChapterFromOss($bid,$chapter->id)){
  210. $oss = self::ossObject();
  211. $path_format = 'book/chapters/%s/%s.json';
  212. $oss->deleteObject($bucket,sprintf($path_format, $bid, $chapter->id));
  213. }
  214. self::saveChapterToOss($bid,$chapter);
  215. $res_redis = self::saveChapterToRedis($bid,$chapter);
  216. return $res_db && $res_redis;
  217. }
  218. //章节列表和内容
  219. public static function getChapterPage($bid,$page_size=15){
  220. return Chapter::getChapterPage($bid,$page_size);
  221. }
  222. /**
  223. * 根据bid和顺序获取章节信息
  224. * @param $bid
  225. * @param $seq
  226. * @return mixed
  227. */
  228. public static function getChapterInfoByBidAndSeq($bid,$seq){
  229. return Chapter::where('bid',$bid)->where('sequence',$seq)->select('id','name')->first();
  230. }
  231. public static function updateSequence(int $bid,int $sequence){
  232. Chapter::where('bid',$bid)->where('sequence','>',$sequence)->decrement('sequence',1);
  233. }
  234. public static function updateSequenceIncr(int $bid,int $sequence){
  235. Chapter::where('bid',$bid)->where('sequence','>=',$sequence)->increment('sequence',1);
  236. }
  237. public static function updateChapterName($cid,$name){
  238. Chapter::where('id',$cid)->update(['name'=>$name]);
  239. }
  240. public static function createChapter($param){
  241. return Chapter::create($param);
  242. }
  243. public static function updateOne(int $id,array $param){
  244. if($param){
  245. return Chapter::where('id',$id)->update($param);
  246. }
  247. return null;
  248. }
  249. }