123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?php
- namespace App\Http\Controllers\QuickApp\User;
- use App\Consts\SysConsts;
- use App\Modules\Subscribe\Services\BookOrderService;
- use App\Modules\Subscribe\Services\ChapterOrderService;
- use App\Modules\Subscribe\Services\YearOrderService;
- use Illuminate\Http\Request;
- use App\Http\Controllers\QuickApp\BaseController;
- use App\Modules\Book\Services\BookConfigService;
- use App\Modules\Book\Services\UserShelfBooksService;
- use App\Http\Controllers\QuickApp\User\Transformers\ReadRecordTransformer;
- use App\Modules\Book\Services\BookService;
- use App\Modules\ShareFree\Services\ShareUsersService;
- use App\Modules\User\Services\ReadRecordService;
- use Redis;
- class ReadRecordController extends BaseController
- {
-
-
- public function index(Request $request)
- {
- $res = ReadRecordService::getReadRecord($this->uid);
- $package = $request->header('x-package', '');
-
-
- $year_account = YearOrderService::getRecord($this->uid);
- if ($res) {
- $bids = array_column($res,'bid');
- $channel_id = $request->input('distribution_channel_id',0);
- $book = BookConfigService::getBooksByIds($bids, [], false);
- foreach ($res as $key => &$value) {
- $value['cover'] = '';
- $value['last_chapter'] = 0;
- $value['intro'] = '';
- $value['status'] = '';
- $value['size'] = 0;
- $value['author'] = '';
- foreach ($book as $val) {
- if ($value['bid'] == $val->bid) {
- $value['book_name'] = $val->book_name;
- $value['cover'] = $val->cover;
- $value['last_chapter'] = $val->last_chapter;
- $value['intro'] = $val->intro;
- $value['status'] = $val->status;
- $value['size'] = $val->size;
- $value['author'] = $val->author;
- $hidden = getHiddenCp($package);
- if((!$year_account && !in_array($val->is_on_shelf,[1,2])) || in_array($val->cp_source,$hidden) && BookConfigService::bookCopyright($val->bid,$channel_id)){
-
- $charge_type = $val->charge_type;
- if($charge_type == 'BOOK'){
-
- $result = BookOrderService::getRecordByuidBid($this->uid,$val->bid);
- }elseif($charge_type == 'CHAPTER'){
-
- $result = ChapterOrderService::checkBookIsOrdered($this->uid,$val->bid);
- }else{
- $result = false;
- }
- if (!$result) {
- unset($res[$key]);
- ReadRecordService::delReadRecordStatic($this->uid,[$val->bid]);
- }
- }
- break;
- }
- }
- }
- }
- usort($res, function ($a, $b) {
- if ($a['time'] >= $b['time']) return -1;
- return 1;
- });
- return response()->collection(new ReadRecordTransformer(), array_to_object($res));
- }
-
- public function lastReadRecord(Request $request)
- {
- $res = ReadRecordService::getReadRecord($this->uid);
- $package = $request->header('x-package', '');
-
-
- $year_account = YearOrderService::getRecord($this->uid);
- if ($res) {
- $bids = array_column($res,'bid');
- $channel_id = $request->input('distribution_channel_id',0);
- $book = BookConfigService::getBooksByIds($bids, [], false);
- $book = array_column(($book->toArray()),null,'bid');
- foreach ($res as &$value) {
- $value['cover'] = '';
- $value['last_chapter'] = 0;
- $value['intro'] = '';
- $value['status'] = '';
- $value['size'] = 0;
- $value['author'] = '';
- if(isset($book[$value['bid']])){
- $info = $book[$value['bid']];
- $value['book_name'] = $info['book_name'];
- $value['cover'] = $info['cover'];
- $value['last_chapter'] = $info['last_chapter'];
- $value['intro'] = $info['intro'];
- $value['status'] = $info['status'];
- $value['size'] = $info['size'];
- $value['author'] = $info['author'];
- $hidden = getHiddenCp($package);
- if((!$year_account && !in_array( $info['is_on_shelf'],[1,2])) || in_array( $info['cp_source'],$hidden)){
-
- $charge_type = $info['charge_type'];
- if($charge_type == 'BOOK'){
-
- $result = BookOrderService::getRecordByuidBid($this->uid, $info['bid']);
- }elseif($charge_type == 'CHAPTER'){
-
- $result = ChapterOrderService::checkBookIsOrdered($this->uid,$info['bid']);
- }else{
- $result = false;
- }
- if (!$result) {
- unset($value);
- ReadRecordService::delReadRecordStatic($this->uid,[$info['bid']]);
- }
- }
- }else{
- unset($value);
- }
- }
- unset($value);
- }
- usort($res, function ($a, $b) {
- if ($a['time'] >= $b['time']) return -1;
- return 1;
- });
- if (count($res) > 0){
- return response()->success((new ReadRecordTransformer())->transform(array_to_object($res[0])));
- }
- return response()->success();
- }
-
- public function addReadRecord(Request $request)
- {
- $param = $request->except('_url');
- if (checkParam($param, ['bid', 'cid', 'chapter_name'])) {
- return response()->error('LACK_PARAM');
- }
- $param['uid'] = $this->uid;
- $param['bid'] = BookService::decodeBidStatic($param['bid']);
- Redis::hget('book_read:' . $this->uid, $param['bid']);
- $param['book_name'] = '';
- ReadRecordService::addReadRecord($param);
- return response()->success();
- }
-
- public function delReadRecord(Request $request)
- {
- $param = $request->except('_url');
- if (checkParam($param, ['bid'])) {
- return response()->error('LACK_PARAM');
- }
- $bids = explode(',', $param['bid']);
- array_walk($bids, function (&$item) {
- $item = BookService::decodeBidStatic($item);
- });
- ReadRecordService::delReadRecordStatic($this->uid, $bids);
- return response()->success();
- }
- }
|