Index.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace app\clientwebapi\controller;
  3. use app\api\library\OrderDeduction;
  4. use app\common\controller\Api;
  5. use app\common\controller\ClientWebApi;
  6. use app\common\library\Redis;
  7. use app\main\service\ClientAppService;
  8. use Think\Db;
  9. /**
  10. * 首页接口
  11. */
  12. class Index extends ClientWebApi
  13. {
  14. protected $noNeedLogin = ['*'];
  15. protected $noNeedRight = ['*'];
  16. /**
  17. * 首页
  18. *
  19. * 必选参数:无<br>
  20. * 可选参数:lng,lat
  21. */
  22. public function index()
  23. {
  24. $this->success('请求成功');
  25. }
  26. /**
  27. * 首页书籍榜单列表数据
  28. * @param string $type 榜单类别(free finish click sell 免费榜 完结榜 点击榜 销售榜)
  29. * @param int $page 页数
  30. * @return json
  31. */
  32. public function booklistapi()
  33. {
  34. $sex = input('sex', 0);
  35. $type = input('type');
  36. $page = input('page', 1);
  37. $books = model('Index')::booklist($sex, $type, $page);
  38. return json($books);
  39. }
  40. /**
  41. * 首页配置的指定区块书籍列表接口
  42. * @param int $block_id 区块id
  43. * @param int $page 页数
  44. * @return json $books 书籍列表信息
  45. */
  46. public function blockbookapi()
  47. {
  48. $block_id = input('block_id');
  49. $page = input('page', 1);
  50. $data = model('AppIndex')::blockbooklist($block_id, $page);
  51. return json($data);
  52. }
  53. /**
  54. * 热门搜索(搜索页推荐书籍)接口
  55. * @param int $sex 性别
  56. * @return json
  57. */
  58. public function hotsearchapi()
  59. {
  60. $redis = Redis::instance();
  61. $sex = input('sex');
  62. if ($sex == 'girl') {
  63. $sexVal = 2;
  64. } else {
  65. $sexVal = 1;
  66. }
  67. $key = 'CHS:' . $sexVal;
  68. if ($redis->exists($key)) {
  69. $data = $redis->get($key);
  70. $data = json_decode($data, true);
  71. } else {
  72. $data = Db::table('client_search_keyword')->field('keyword as name,book_id')->where("sex", $sexVal)->order('weigh', 'desc')->select();
  73. $json = json_encode($data, JSON_UNESCAPED_UNICODE);
  74. $redis->setex($key, 86400, $json);
  75. }
  76. return json($data);
  77. }
  78. /**
  79. * 搜索接口
  80. * @param string $keyword 关键词
  81. * @param int $page 页码
  82. * @return json
  83. */
  84. public function searchapi()
  85. {
  86. $keyword = input('keyword');
  87. $page = input('page', 1);
  88. if (!empty($keyword)) {
  89. $where = array();
  90. $where['state'] = 1;
  91. $where['name'] = array('like', '%' . $keyword . '%');
  92. $total = model('book')->where($where)->count();
  93. $books = model('book')->where($where)->page($page, 10)->select();
  94. } else {
  95. $total = 0;
  96. $books = array();
  97. }
  98. if(!empty($books)){
  99. $categoryArr = model("BookCategory")->getCategoryList();
  100. foreach ($books as $k=>$v){
  101. $books[$k]['author'] = $categoryArr[$v['book_category_id']];
  102. }
  103. }
  104. $result = array(
  105. 'priMap' => array(
  106. 'total' => $total,
  107. 'searchList' => $books,
  108. ),
  109. 'index' => $page,
  110. 'ptype' => 'searchResult',
  111. 'keyword' => $keyword,
  112. );
  113. return json($result);
  114. }
  115. /**
  116. * 搜索页
  117. * @param int $sex 频道标示
  118. */
  119. public function search()
  120. {
  121. $userId = 0;
  122. if ($this->isLogin) {
  123. $userId = $this->userInfo['id'];
  124. }
  125. $books = model('User')->getLike($userId);
  126. $categoryArr = model("BookCategory")->getCategoryList();
  127. foreach ($books as $k=>$v){
  128. $books[$k]['author'] = $categoryArr[$v['book_category_id']];
  129. }
  130. if($this->request->isAjax()){
  131. return json_encode($books);
  132. }else{
  133. $sex = input('sex');
  134. $this->assign('sex', $sex);
  135. $pagedata['name'] = '精彩推荐';
  136. $pagedata['block_resource'] = $books;
  137. $this->view->assign('value', $pagedata);
  138. return $this->view->fetch();
  139. }
  140. }
  141. public function recommendAjax(){
  142. $book_id = $this->request->get("book_id");
  143. if($book_id){
  144. $index = new \app\clientweb\controller\Index();
  145. $pagedata = $index->recommendList($book_id);
  146. }else{
  147. return false;
  148. }
  149. return json($pagedata);
  150. }
  151. /* public function getAppBookJson()
  152. {
  153. $book_id = input('book_id');
  154. if (empty($book_id)) {
  155. $this->error('无此书籍');
  156. }
  157. $book = model('Book')->BookInfo($book_id);
  158. if (empty($book)) {
  159. $this->error('无此书籍');
  160. }
  161. //书的信息 提供js调用
  162. $appBookInfo = ClientAppService::instance()->userReadBooksInfo([$book['id'] => $book['last_chapter_id']])->data;
  163. $appBookInfo = $appBookInfo['bookList'][0];
  164. $jumpChapterId = input('chapter_id', 0);
  165. if (intval($jumpChapterId) > 0) {
  166. $appBookInfo['currentCid'] = $jumpChapterId;
  167. }
  168. $this->success('成功', $appBookInfo);
  169. }*/
  170. //JS登录加密
  171. public function getJsLoginToken()
  172. {
  173. if ($this->checkSign()) {
  174. $uid = $this->aCommon['uid'];
  175. $token = $this->aCommon['token'];
  176. if ($this->request->isAjax() && $uid && $token) {
  177. //设置cookie
  178. $arrSign['token'] = $token;
  179. $arrSign[] = 'key=ddbc9169242b479da867eb24efb735d1';
  180. $strSign = implode('&', $arrSign);
  181. $this->success('成功', ['sign' => md5($strSign), 'uid' => $uid, 'token' => $token]);
  182. }
  183. $this->error('错误的请求');
  184. }
  185. $this->error('签名错误');
  186. }
  187. }