Book.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * 前台书籍控制器
  4. */
  5. namespace app\clientweb\controller;
  6. use app\common\controller\ClientWeb;
  7. use app\common\library\Redis;
  8. use app\common\library\Ua;
  9. use app\common\model\GuideWx;
  10. use app\main\constants\ErrorCodeConstants;
  11. use app\main\service\AdminService;
  12. use app\main\service\BookService;
  13. use app\main\service\ClientAppService;
  14. use app\main\service\FinancialService;
  15. use app\main\service\UrlService;
  16. use app\main\service\UserService;
  17. use think\Config;
  18. use think\Cookie;
  19. use think\Exception;
  20. use think\Log;
  21. use think\Request;
  22. class Book extends ClientWeb
  23. {
  24. protected $layout = '';
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. }
  29. /**
  30. * 书籍列表页
  31. * @param string $type 列表标示
  32. * @return html
  33. */
  34. function list() {
  35. $sex = input('sex');
  36. $type = input('type');
  37. $description ='暂无该类别图书,换个条件试试看吧!';
  38. switch ($type) {
  39. case 'free':
  40. $title = '限免专区';
  41. $description = '暂无限免小说';
  42. break;
  43. case 'finish':
  44. $title = '完本书籍';
  45. break;
  46. case 'click':
  47. $title = '点击榜';
  48. break;
  49. case 'sell':
  50. $title = '畅销榜';
  51. break;
  52. default:
  53. $title = '书籍列表';
  54. break;
  55. }
  56. $this->assign('title', $title);
  57. $this->assign('description',$description);
  58. $this->assign('sex', $sex);
  59. $this->assign('type', $type);
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 首页配置的指定区块书籍列表页
  64. * @param int $block_id 区块id
  65. * @return html
  66. */
  67. public function blocklist()
  68. {
  69. $block_id = input('block_id');
  70. $block = model('client_manage_block')::get($block_id);
  71. $this->assign('block', $block);
  72. $this->assign('sex', $this->sex);
  73. return $this->view->fetch();
  74. }
  75. /**
  76. * 书籍详情页
  77. */
  78. public function info()
  79. {
  80. $book_id = input('book_id');
  81. $book = model('Book')->BookInfo($book_id);
  82. if (empty($book)) {
  83. return $this->statuspage(6); //无此书籍
  84. }
  85. if ($book['state'] == 0) {
  86. return $this->statuspage(3); //本书已下架
  87. }
  88. $book['word_count'] = formatNumber($book['word_count']);
  89. $book['read_num'] = formatNumber($book['read_num']);
  90. $headTenChapters = model('Book')::getChapterLimit($book_id, 0, 9);
  91. if (empty($headTenChapters)) {
  92. return $this->statuspage(6); //当接口出问题获取不到数据,提示暂时无法阅读。
  93. }
  94. //VIP充值地址
  95. $vipEndtime = 0;
  96. if ($this->isLogin) {
  97. $vipEndtime = $this->userInfo['vip_endtime'];
  98. }
  99. $this->assign('vip_endtime',$vipEndtime);
  100. $index = new Index();
  101. $pagedata = $index->recommendList($book_id);
  102. $this->view->assign('pagedata', $pagedata);
  103. $this->view->assign('sex', $this->sex);
  104. $this->assign('book', $book);
  105. $this->assign('chapters', $headTenChapters);
  106. //书的信息 提供js调用
  107. $appBookInfo = ClientAppService::instance()->userReadBooksInfo([$book['id'] => $book['last_chapter_id']])->data;
  108. $appBookInfo = $appBookInfo['bookList'][0];
  109. $this->assign('app_params', json_encode($appBookInfo));
  110. return $this->view->fetch();
  111. }
  112. /**
  113. * 书籍目录页
  114. * @param int $book_id 书籍id
  115. * @param int $page 页码
  116. * @param int chapter_id 如果传chapter_id 则直接显示所在page
  117. * @return html
  118. */
  119. public function menu()
  120. {
  121. $book_id = input('book_id');
  122. $page = input('page', 1);
  123. $chapter_id = input('chapter_id');
  124. $book = model('Book')->bookInfo($book_id);
  125. if (empty($book)) {
  126. return $this->statuspage(6); //无此书籍
  127. }
  128. if ($book['state'] == 0) {
  129. return $this->statuspage(3); //本书已下架
  130. }
  131. $page_num = ceil($book['chapter_num'] / 100);
  132. $menuindex = array();
  133. for ($i = 1; $i <= $page_num; $i++) {
  134. $start_num = ($i - 1) * 100;
  135. if ($i < $page_num) {
  136. $end_num = $start_num + 100;
  137. } else {
  138. $end_num = $book['chapter_num'];
  139. }
  140. $menuindex[$i] = array('url' => url('clientweb/book/menu', array('book_id' => $book_id, 'page' => $i)), 'name' => 1+$start_num . '-' . $end_num . '章');
  141. }
  142. if($chapter_id && $book_id){
  143. $chapter = model('Book')->getChapterInfo($book_id, $chapter_id);
  144. if($chapter['data']){
  145. $chapter_num = $chapter['data']['idx'];
  146. $page_chapter_idx = ceil($chapter_num / 100);
  147. $page = $page_chapter_idx ? $page_chapter_idx : $page;
  148. }
  149. }
  150. $chapters = model("Book")::getChapterList($book_id, $page, 100);
  151. $no_menu = 0;
  152. $this->assign('no_menu',$no_menu);
  153. $this->assign('book', $book);
  154. $this->assign('page', $page);
  155. $this->assign('chapter_id', $chapter_id);
  156. $this->assign('menuindex', $menuindex);
  157. $this->assign('menuindexjson', json_encode($menuindex));
  158. $this->assign('chapters', $chapters['data']['data']);
  159. $this->view->assign('sex', $this->sex);
  160. //书的信息 提供js调用
  161. $appBookInfo = ClientAppService::instance()->userReadBooksInfo([$book['id'] => $book['last_chapter_id']])->data;
  162. $appBookInfo = $appBookInfo['bookList'][0];
  163. $this->assign('app_params', json_encode($appBookInfo));
  164. return $this->view->fetch();
  165. }
  166. /**
  167. * 状态页
  168. * $code 1 正在码字| 2 已完结| 3 已下架| 4 章节内容为空| 5 404[并不处理] | 6 提示此本书暂时无法阅读
  169. * $chapter
  170. * $book_id
  171. */
  172. public function statuspage($code, $chapter = [],$book_id='')
  173. {
  174. switch ($code) {
  175. case 1:
  176. $template = 'typing';
  177. $this->ranklist($book_id);
  178. break;
  179. case 2:
  180. $template = 'finish';
  181. $this->ranklist($book_id);
  182. break;
  183. case 3:
  184. $template = 'forbiddenread';
  185. break;
  186. case 4:
  187. $template = 'nocontent';
  188. $chapter['next_link'] = '/clientweb/book/chapter?book_id=' . $chapter['book_id'] . '&chapter_id=' . $chapter['next_id'];
  189. $this->view->assign('chapter', $chapter);
  190. break;
  191. case 5:
  192. $template = '404';
  193. break;
  194. case 6:
  195. $template = 'nobook';
  196. break;
  197. default:
  198. $template = 'typing';
  199. $this->ranklist($book_id);
  200. }
  201. $bookinfo = [];
  202. if($book_id){
  203. $bookinfo = model('Book')->BookInfo($book_id);
  204. if($bookinfo['recommand_book_id']){
  205. $recmooand = model('Book')->BookInfo($bookinfo['recommand_book_id']);;
  206. $bookinfo['recommand_book_image'] =$recmooand['image'];
  207. $bookinfo['recommand_book_name'] =$recmooand['name'];
  208. }
  209. }
  210. $this->view->assign('book',$bookinfo);
  211. return $this->view->fetch('public/' . $template);
  212. }
  213. /*
  214. * 为你推荐
  215. * 调用页面-最近阅读页??
  216. * 调用页面-完结页
  217. * 调用页面-作者码字页
  218. * 从当前列表去掉当前书籍
  219. */
  220. public function ranklist($book_id){
  221. $sex_type = model('User')->getUserSex($this->user->sex,$this->adminconfig,true);
  222. $index = new Index();
  223. $ranklist = $index->ranklist($sex_type);
  224. if($book_id){
  225. foreach ($ranklist['idx'] as $k=>$value){
  226. if($value['id']==$book_id){
  227. unset($ranklist['idx'][$k]);
  228. }
  229. }
  230. }
  231. $this->view->assign('ranklist', $ranklist);
  232. }
  233. }