Index.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace app\kuaifen\controller;
  3. use app\common\service\KuaifenService;
  4. use app\main\constants\ErrorCodeConstants;
  5. use app\main\service\AdminService;
  6. use app\main\service\BookService;
  7. use app\main\service\LogService;
  8. use think\Config;
  9. use think\Controller;
  10. class Index extends Controller
  11. {
  12. public function index()
  13. {
  14. return $this->fetch();
  15. }
  16. public function bookinfo()
  17. {
  18. $book_id = input('book_id');
  19. $uid = input('uid');
  20. $book = model('Book')->BookInfo($book_id);
  21. if (empty($book)) {
  22. return $this->statuspage(6); //无此书籍
  23. }
  24. if ($book['state'] == 0) {
  25. return $this->statuspage(3); //本书已下架
  26. }
  27. $book['word_count'] = formatNumber($book['word_count']);
  28. $book['read_num'] = formatNumber($book['read_num']);
  29. $book['description'] = str_replace(["\n\r", "\r", "\n"], '', $book['description']);
  30. $this->assign('book', $book);
  31. $this->assign('uid', $uid);
  32. $this->assign('book_id', $book_id);
  33. return $this->fetch();
  34. }
  35. public function booklist()
  36. {
  37. $uid = input('uid');
  38. $bookIds = KuaifenService::instance()->getKuaifenBookIds();
  39. $booksInfo = model('book')->getBooksInfo($bookIds);
  40. $data = [];
  41. $categoryArr = model("BookCategory")->getCategoryList();
  42. foreach ($booksInfo as $book) {
  43. if ($book) {
  44. if ($book['state'] != 1) {
  45. continue;
  46. }
  47. $data[] = [
  48. 'id' => $book['id'],
  49. 'name' => $book['name'],
  50. 'description' => $book['description'],
  51. 'author' => $categoryArr[$book['book_category_id']],
  52. 'image' => $book['image'],
  53. 'read_num' => $book['read_num'],
  54. 'is_finish' => $book['is_finish'],
  55. ];
  56. }
  57. }
  58. $this->assign('booklist', $data);
  59. $this->assign('uid', $uid);
  60. return $this->view->fetch();
  61. }
  62. public function booklistapi()
  63. {
  64. $bookIds = KuaifenService::instance()->getKuaifenBookIds();
  65. $booksInfo = model('book')->getBooksInfo($bookIds);
  66. $data = [];
  67. $categoryArr = model("BookCategory")->getCategoryList();
  68. foreach ($booksInfo as $book) {
  69. if ($book) {
  70. if ($book['state'] != 1) {
  71. continue;
  72. }
  73. $data[] = [
  74. 'id' => $book['id'],
  75. 'name' => $book['name'],
  76. 'description' => $book['description'],
  77. 'author' => $categoryArr[$book['book_category_id']],
  78. 'image' => $book['image'],
  79. 'read_num' => $book['read_num'],
  80. 'is_finish' => $book['is_finish'],
  81. ];
  82. }
  83. }
  84. return json($data);
  85. }
  86. /**
  87. * 状态页
  88. * $code 1 正在码字| 2 已完结| 3 已下架| 4 章节内容为空| 5 404[并不处理] | 6 提示此本书暂时无法阅读
  89. * $chapter
  90. * $book_id
  91. */
  92. public function statuspage($code, $chapter = [], $book_id = '')
  93. {
  94. if (strpos($book_id, '&')) {
  95. $book_id = substr($book_id, 0, strpos($book_id, '&'));
  96. }
  97. switch ($code) {
  98. case 3:
  99. $template = 'forbiddenread';
  100. break;
  101. case 6:
  102. $template = 'nobook';
  103. break;
  104. default:
  105. break;
  106. }
  107. $bookinfo = [];
  108. if ($book_id) {
  109. $bookinfo = model('Book')->BookInfo($book_id);
  110. if ($bookinfo) {
  111. if ($bookinfo['recommand_book_id']) {
  112. $recmooand = model('Book')->BookInfo($bookinfo['recommand_book_id']);
  113. $bookinfo['recommand_book_image'] = $recmooand['image'];
  114. $bookinfo['recommand_book_name'] = $recmooand['name'];
  115. }
  116. } else {
  117. return $this->statuspage(6);
  118. }
  119. }
  120. $this->view->assign('book', $bookinfo);
  121. return $this->view->fetch('public/' . $template);
  122. }
  123. #region 与快分对接的API接口
  124. //获取第一章内容
  125. public function getfristchapter()
  126. {
  127. $errMsg = $this->_sign();
  128. if (!empty($errMsg)) {
  129. return $this->_errorResult($errMsg);
  130. }
  131. $bookId = input('xsid');
  132. if (empty($bookId)) {
  133. return $this->_errorResult('xsid不能为空');
  134. }
  135. $bookInfo = model('Book')->BookInfo($bookId);
  136. if (empty($bookInfo)) {
  137. return $this->_errorResult('书籍不存在');
  138. }
  139. $chapterResult = BookService::instance()->getChapterInfo($bookId, $bookInfo['first_chapter_id']);
  140. if ($chapterResult->code == ErrorCodeConstants::SUCCESS) {
  141. $chapter = $chapterResult->data;
  142. if (is_array($chapter['content'])) {
  143. $content = array_map(function ($item) {
  144. return '<p>' . $item . '</p>';
  145. }, $chapter['content']);
  146. $chapter['content'] = implode('', $content);
  147. }
  148. $channelId = KuaifenService::instance()->getKuaifenChannelId();
  149. $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channelId);
  150. $nextChapterUrl = sprintf('%s://%s.%s/index/book/chapter?book_id=%s&chapter_id=%s',
  151. Config::get('site.scheme'), $adminConfig['appid'], $adminConfig['ophost_host'], $bookId,
  152. $chapter['next_id']);
  153. $result = [
  154. 'xsid' => $bookId,
  155. 'content' => $chapter['content'],
  156. 'title' => $bookInfo['name'],
  157. 'next_type' => 2,
  158. 'next_url' => $nextChapterUrl,
  159. ];
  160. return $this->_infoResult($result);
  161. } else {
  162. return $this->_errorResult($chapterResult->msg);
  163. }
  164. }
  165. public function gettotalincomebyuser()
  166. {
  167. $errMsg = $this->_sign();
  168. if (!empty($errMsg)) {
  169. return $this->_errorResult($errMsg);
  170. }
  171. $kuaifenId = input('uid');
  172. if (empty($kuaifenId)) {
  173. return $this->_errorResult('uid不能为空');
  174. }
  175. $day = input('day');
  176. if (empty($day)) {
  177. return $this->_errorResult('day不能为空');
  178. }
  179. $inputTime = strtotime($day);
  180. if ($inputTime === false) {
  181. return $this->_errorResult('day格式错误');
  182. }
  183. $beginTime = strtotime(date('Y-m-d', $inputTime));
  184. $endTime = $beginTime + 86399;
  185. $bookId = input('xsid');
  186. $sumMoney = KuaifenService::instance()->getTotalIncomeByUser($kuaifenId, $beginTime, $endTime, $bookId);
  187. return $this->_infoResult([
  188. 'uid' => $kuaifenId,
  189. 'totalIncome' => $sumMoney,
  190. 'day' => $day,
  191. ]);
  192. }
  193. public function gettotalincome()
  194. {
  195. $errMsg = $this->_sign();
  196. if (!empty($errMsg)) {
  197. return $this->_errorResult($errMsg);
  198. }
  199. $day = input('day');
  200. if (empty($day)) {
  201. return $this->_errorResult('day不能为空');
  202. }
  203. $inputTime = strtotime($day);
  204. if ($inputTime === false) {
  205. return $this->_errorResult('day格式错误');
  206. }
  207. $beginTime = strtotime(date('Y-m-d', $inputTime));
  208. $endTime = $beginTime + 86399;
  209. $bookId = input('xsid');
  210. $sumMoney = KuaifenService::instance()->getTotalIncome($beginTime, $endTime, $bookId);
  211. return $this->_infoResult([
  212. 'totalIncome' => $sumMoney,
  213. 'day' => $day,
  214. ]);
  215. }
  216. public function verifyincome()
  217. {
  218. $errMsg = $this->_sign();
  219. if (!empty($errMsg)) {
  220. return $this->_errorResult($errMsg);
  221. }
  222. $cid = input('cid');
  223. if (empty($cid)) {
  224. return $this->_errorResult('cid不能为空');
  225. }
  226. $orders = KuaifenService::instance()->getOrdersKuaifen($cid);
  227. if (empty($orders)) {
  228. return $this->_errorResult('没有查到此订单');
  229. }
  230. return $this->_infoResult([
  231. 'cid' => $cid,
  232. 'uid' => $orders['kuaifen_id'],
  233. 'xsid' => $orders['book_id'],
  234. 'money' => $orders['money'],
  235. 'seconds' => date('Y-m-d H:i:s', $orders['updatetime']),
  236. 'chargeTime' => $orders['createtime'],
  237. ]);
  238. }
  239. private function _infoResult($data)
  240. {
  241. return json([
  242. 'code' => 1000,
  243. 'time' => time(),
  244. 'data' => $data,
  245. ]);
  246. }
  247. private function _errorResult($msg)
  248. {
  249. LogService::error($msg);
  250. return json([
  251. 'code' => 2000,
  252. 'time' => time(),
  253. 'err_msg' => $msg,
  254. ]);
  255. }
  256. private function _sign()
  257. {
  258. $params = $this->request->param();
  259. $originalSign = $this->request->param('sign');
  260. if (empty($originalSign)) {
  261. return '签名错误,1';
  262. }
  263. if (empty($params['t'])) {
  264. return '签名错误,2';
  265. }
  266. $time = time();
  267. $subTime = $params['t'] - $time;
  268. if (abs($subTime) > 20) {
  269. return 'sign超时';
  270. }
  271. unset($params['sign']);
  272. ksort($params);
  273. $str = http_build_query($params);
  274. $str = $str . 'KbGyb9AG';
  275. $sign = md5($str);
  276. if ($sign != $originalSign) {
  277. return 'sign参数校验错误';
  278. }
  279. }
  280. #endregion
  281. }