123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <?php
- namespace app\kuaifen\controller;
- use app\common\service\KuaifenService;
- use app\main\constants\ErrorCodeConstants;
- use app\main\service\AdminService;
- use app\main\service\BookService;
- use app\main\service\LogService;
- use think\Config;
- use think\Controller;
- class Index extends Controller
- {
- public function index()
- {
- return $this->fetch();
- }
- public function bookinfo()
- {
- $book_id = input('book_id');
- $uid = input('uid');
- $book = model('Book')->BookInfo($book_id);
- if (empty($book)) {
- return $this->statuspage(6); //无此书籍
- }
- if ($book['state'] == 0) {
- return $this->statuspage(3); //本书已下架
- }
- $book['word_count'] = formatNumber($book['word_count']);
- $book['read_num'] = formatNumber($book['read_num']);
- $book['description'] = str_replace(["\n\r", "\r", "\n"], '', $book['description']);
- $this->assign('book', $book);
- $this->assign('uid', $uid);
- $this->assign('book_id', $book_id);
- return $this->fetch();
- }
- public function booklist()
- {
- $uid = input('uid');
- $bookIds = KuaifenService::instance()->getKuaifenBookIds();
- $booksInfo = model('book')->getBooksInfo($bookIds);
- $data = [];
- $categoryArr = model("BookCategory")->getCategoryList();
- foreach ($booksInfo as $book) {
- if ($book) {
- if ($book['state'] != 1) {
- continue;
- }
- $data[] = [
- 'id' => $book['id'],
- 'name' => $book['name'],
- 'description' => $book['description'],
- 'author' => $categoryArr[$book['book_category_id']],
- 'image' => $book['image'],
- 'read_num' => $book['read_num'],
- 'is_finish' => $book['is_finish'],
- ];
- }
- }
- $this->assign('booklist', $data);
- $this->assign('uid', $uid);
- return $this->view->fetch();
- }
- public function booklistapi()
- {
- $bookIds = KuaifenService::instance()->getKuaifenBookIds();
- $booksInfo = model('book')->getBooksInfo($bookIds);
- $data = [];
- $categoryArr = model("BookCategory")->getCategoryList();
- foreach ($booksInfo as $book) {
- if ($book) {
- if ($book['state'] != 1) {
- continue;
- }
- $data[] = [
- 'id' => $book['id'],
- 'name' => $book['name'],
- 'description' => $book['description'],
- 'author' => $categoryArr[$book['book_category_id']],
- 'image' => $book['image'],
- 'read_num' => $book['read_num'],
- 'is_finish' => $book['is_finish'],
- ];
- }
- }
- return json($data);
- }
- /**
- * 状态页
- * $code 1 正在码字| 2 已完结| 3 已下架| 4 章节内容为空| 5 404[并不处理] | 6 提示此本书暂时无法阅读
- * $chapter
- * $book_id
- */
- public function statuspage($code, $chapter = [], $book_id = '')
- {
- if (strpos($book_id, '&')) {
- $book_id = substr($book_id, 0, strpos($book_id, '&'));
- }
- switch ($code) {
- case 3:
- $template = 'forbiddenread';
- break;
- case 6:
- $template = 'nobook';
- break;
- default:
- break;
- }
- $bookinfo = [];
- if ($book_id) {
- $bookinfo = model('Book')->BookInfo($book_id);
- if ($bookinfo) {
- if ($bookinfo['recommand_book_id']) {
- $recmooand = model('Book')->BookInfo($bookinfo['recommand_book_id']);
- $bookinfo['recommand_book_image'] = $recmooand['image'];
- $bookinfo['recommand_book_name'] = $recmooand['name'];
- }
- } else {
- return $this->statuspage(6);
- }
- }
- $this->view->assign('book', $bookinfo);
- return $this->view->fetch('public/' . $template);
- }
- #region 与快分对接的API接口
- //获取第一章内容
- public function getfristchapter()
- {
- $errMsg = $this->_sign();
- if (!empty($errMsg)) {
- return $this->_errorResult($errMsg);
- }
- $bookId = input('xsid');
- if (empty($bookId)) {
- return $this->_errorResult('xsid不能为空');
- }
- $bookInfo = model('Book')->BookInfo($bookId);
- if (empty($bookInfo)) {
- return $this->_errorResult('书籍不存在');
- }
- $chapterResult = BookService::instance()->getChapterInfo($bookId, $bookInfo['first_chapter_id']);
- if ($chapterResult->code == ErrorCodeConstants::SUCCESS) {
- $chapter = $chapterResult->data;
- if (is_array($chapter['content'])) {
- $content = array_map(function ($item) {
- return '<p>' . $item . '</p>';
- }, $chapter['content']);
- $chapter['content'] = implode('', $content);
- }
- $channelId = KuaifenService::instance()->getKuaifenChannelId();
- $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channelId);
- $nextChapterUrl = sprintf('%s://%s.%s/index/book/chapter?book_id=%s&chapter_id=%s',
- Config::get('site.scheme'), $adminConfig['appid'], $adminConfig['ophost_host'], $bookId,
- $chapter['next_id']);
- $result = [
- 'xsid' => $bookId,
- 'content' => $chapter['content'],
- 'title' => $bookInfo['name'],
- 'next_type' => 2,
- 'next_url' => $nextChapterUrl,
- ];
- return $this->_infoResult($result);
- } else {
- return $this->_errorResult($chapterResult->msg);
- }
- }
- public function gettotalincomebyuser()
- {
- $errMsg = $this->_sign();
- if (!empty($errMsg)) {
- return $this->_errorResult($errMsg);
- }
- $kuaifenId = input('uid');
- if (empty($kuaifenId)) {
- return $this->_errorResult('uid不能为空');
- }
- $day = input('day');
- if (empty($day)) {
- return $this->_errorResult('day不能为空');
- }
- $inputTime = strtotime($day);
- if ($inputTime === false) {
- return $this->_errorResult('day格式错误');
- }
- $beginTime = strtotime(date('Y-m-d', $inputTime));
- $endTime = $beginTime + 86399;
- $bookId = input('xsid');
- $sumMoney = KuaifenService::instance()->getTotalIncomeByUser($kuaifenId, $beginTime, $endTime, $bookId);
- return $this->_infoResult([
- 'uid' => $kuaifenId,
- 'totalIncome' => $sumMoney,
- 'day' => $day,
- ]);
- }
- public function gettotalincome()
- {
- $errMsg = $this->_sign();
- if (!empty($errMsg)) {
- return $this->_errorResult($errMsg);
- }
- $day = input('day');
- if (empty($day)) {
- return $this->_errorResult('day不能为空');
- }
- $inputTime = strtotime($day);
- if ($inputTime === false) {
- return $this->_errorResult('day格式错误');
- }
- $beginTime = strtotime(date('Y-m-d', $inputTime));
- $endTime = $beginTime + 86399;
- $bookId = input('xsid');
- $sumMoney = KuaifenService::instance()->getTotalIncome($beginTime, $endTime, $bookId);
- return $this->_infoResult([
- 'totalIncome' => $sumMoney,
- 'day' => $day,
- ]);
- }
- public function verifyincome()
- {
- $errMsg = $this->_sign();
- if (!empty($errMsg)) {
- return $this->_errorResult($errMsg);
- }
- $cid = input('cid');
- if (empty($cid)) {
- return $this->_errorResult('cid不能为空');
- }
- $orders = KuaifenService::instance()->getOrdersKuaifen($cid);
- if (empty($orders)) {
- return $this->_errorResult('没有查到此订单');
- }
- return $this->_infoResult([
- 'cid' => $cid,
- 'uid' => $orders['kuaifen_id'],
- 'xsid' => $orders['book_id'],
- 'money' => $orders['money'],
- 'seconds' => date('Y-m-d H:i:s', $orders['updatetime']),
- 'chargeTime' => $orders['createtime'],
- ]);
- }
- private function _infoResult($data)
- {
- return json([
- 'code' => 1000,
- 'time' => time(),
- 'data' => $data,
- ]);
- }
- private function _errorResult($msg)
- {
- LogService::error($msg);
- return json([
- 'code' => 2000,
- 'time' => time(),
- 'err_msg' => $msg,
- ]);
- }
- private function _sign()
- {
- $params = $this->request->param();
- $originalSign = $this->request->param('sign');
- if (empty($originalSign)) {
- return '签名错误,1';
- }
- if (empty($params['t'])) {
- return '签名错误,2';
- }
- $time = time();
- $subTime = $params['t'] - $time;
- if (abs($subTime) > 20) {
- return 'sign超时';
- }
- unset($params['sign']);
- ksort($params);
- $str = http_build_query($params);
- $str = $str . 'KbGyb9AG';
- $sign = md5($str);
- if ($sign != $originalSign) {
- return 'sign参数校验错误';
- }
- }
- #endregion
- }
|