123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260 |
- <?php
- namespace App\Http\Controllers\QuickApp\Book;
- use App\Modules\Push\Models\QappPushTask;
- use App\Modules\SendOrder\Models\SendOrder;
- use App\Modules\SendOrder\Services\SendOrderService;
- use App\Modules\Statistic\Services\WapVisitStatService;
- use App\Modules\User\Models\QappUser;
- use App\Modules\User\Services\UserBookCombinationConfigService;
- use Illuminate\Http\Request;
- use App\Http\Controllers\QuickApp\BaseController;
- use Redis;
- use App\Modules\Book\Services\ChapterService;
- use App\Modules\User\Services\ReadRecordService;
- use App\Http\Controllers\QuickApp\Book\Transformers\ChapterTransformer;
- use App\Modules\Book\Services\BookConfigService;
- use App\Http\Controllers\QuickApp\Book\Transformers\ChapterListTransformer;
- use App\Jobs\UserRententionJob;
- use App\Modules\Book\Services\BookService;
- use App\Modules\Subscribe\Services\BookOrderService;
- use App\Modules\Subscribe\Services\ChapterOrderService;
- use App\Modules\Subscribe\Services\YearOrderService;
- use App\Modules\OfficialAccount\Services\ForceSubscribeService;
- use App\Modules\Subscribe\Services\ChapterReminderService;
- use App\Modules\User\Services\UserDeepReadTagService;
- use App\Modules\UserTask\Services\BaseTask;
- use App\Modules\UserTask\Services\UserTaskService;
- use App\Modules\SendOrder\Models\QappSendOrder;
- class ChapterController extends BaseController
- {
- private $book_info;
- private $push_id;
- public function getCatalog(Request $request, $bid)
- {
- $this->push_id = $request->header('x-pushid', '');
- $bid = BookService::decodeBidStatic($bid);
- $lists = ChapterService::getChapterLists($bid);
- $book_info = BookConfigService::getBookById($bid);
- if (!$book_info) {
- return response()->error('PARAM_ERROR');
- }
- $this->book_info = $book_info;
- $lists = $this->getChapterCatalog($bid, $lists, $book_info);
- return response()->collection(new ChapterListTransformer, $lists);
- }
- public function getCatalogPerPage(Request $request, $bid)
- {
- $this->push_id = $request->header('x-pushid', '');
- $bid = BookService::decodeBidStatic($bid);
- $book_info = BookConfigService::getBookById($bid);
- if (!$book_info) {
- return response()->error('PARAM_ERROR');
- }
- $this->book_info = $book_info;
- $page_size = $request->input('page_size', 15);
- // if ($page_size >= 100) $page_size = 100;
- if ($page_size >= 20) $page_size = 20;
- // $page_size = 15;
- $res = ChapterService::getChapterListsPage($bid, $page_size);
- $lists = $this->getChapterCatalog($bid, $res, $book_info);
- return response()->pagination(new ChapterListTransformer, $lists);
- }
- private function getChapterCatalog(int $bid, $chapters, $book_info)
- {
- // 查询书籍是否限免
- $isFree = BookConfigService::judgeBookIsFree($bid);
- //渠道自定义vip章节
- //$vip_sequence = Redis::hget('channel:chapterfee:setting:' . $this->distribution_channel_id, $bid);
- $vip_sequence = BookService::getVipSequence($bid, $this->distribution_channel_id, $this->send_order_id);
- list($is_split, $is_change_chapter_name) = BookService::splitContent($bid);
- $change_chapter_name = 0;
- if ($is_split && ($book_info->channel_name == '男频' || $is_change_chapter_name)) {
- $change_chapter_name = 1;
- }
- $account = $this->getAccount();
- $work_wechat_info = $this->getWorkWechatInfo($bid);
- switch ($book_info->charge_type) {
- case 'BOOK':
- $price = $this->getPriceNew($book_info, 0, $account);
- $is_need_charge = $this->isBookNeedCharge($bid, $price);
- foreach ($chapters as $v) {
- $v->next_chapter_status = 0;
- $v->next_price = 0;
- $v->is_need_charge = $v->is_vip ? $is_need_charge : false;
- $v->price = $price;
- if ($work_wechat_info['force_add_work_wechat_type'] > 0 && $v->sequence >= $work_wechat_info['force_add_work_wechat_seq']){
- $v->force_add_work_wechat_type = $work_wechat_info['force_add_work_wechat_type'];
- $v->force_add_work_wechat_url = $work_wechat_info['force_add_work_wechat_url'];
- }else{
- $v->force_add_work_wechat_type = 0;
- $v->force_add_work_wechat_url = "";
- }
- // 限免判断
- if ($isFree) {
- $v->is_need_charge = false;
- $v->price = 0;
- }
- if ($vip_sequence) {
- if ($v->sequence >= $vip_sequence) {
- $v->is_vip = 1;
- } else {
- $v->is_vip = 0;
- }
- }
- //拆章
- if ($change_chapter_name) {
- $v->name = '第' . $v->sequence . '章';
- }
- }
- break;
- default:
- $chapterIds = $this->getChapterIds($chapters);
- $rules = ['check' => 0, 'paycid' => []];
- if (!$isFree && !empty($chapterIds)) {
- $rules = $this->getHandelData($bid, $chapterIds);
- }
- foreach ($chapters as $v) {
- if ($vip_sequence) {
- if ($v->sequence >= $vip_sequence) {
- $v->is_vip = 1;
- } else {
- $v->is_vip = 0;
- }
- }
- if ($work_wechat_info['force_add_work_wechat_type'] > 0 && $v->sequence >= $work_wechat_info['force_add_work_wechat_seq']){
- $v->force_add_work_wechat_type = $work_wechat_info['force_add_work_wechat_type'];
- $v->force_add_work_wechat_url = $work_wechat_info['force_add_work_wechat_url'];
- }else{
- $v->force_add_work_wechat_type = 0;
- $v->force_add_work_wechat_url = "";
- }
- // 限免判断
- if ($isFree) {
- $v->is_need_charge = false;
- $v->price = 0;
- } else {
- $v->price = $v->is_vip ? $this->getPriceNew($book_info, $v->size, $account) : 0;
- $v->is_need_charge = $v->is_vip ? $this->isChapterNeedChargeNew($v->id, $rules, $v->price) : false;
- }
- // //下一章付费信息
- $v->next_chapter_status = 0;
- $v->next_price = 0;
- // if($v->is_vip){
- // if(in_array($v->next_price,$chapterIds)){
- //
- // }else{
- // $next_chapter_order_status = $this->nextChapterOrderStatus($bid,$v->next_cid);
- // $v->next_chapter_status = $next_chapter_order_status['next_chapter_status'];
- // $v->next_price = $next_chapter_order_status['next_price'];
- // }
- //
- // }
- $v->next_chapter_status = $this->isSubscribe($v->id, $rules);
- $v->next_price = $v->price;
- //拆章
- if ($change_chapter_name) {
- $v->name = '第' . $v->sequence . '章';
- }
- }
- break;
- }
- return $chapters;
- }
- private function getChapterCatalogOld(int $bid, $chapters, $book_info)
- {
- // 查询书籍是否限免
- $isFree = BookConfigService::judgeBookIsFree($bid);
- //渠道自定义vip章节
- //$vip_sequence = Redis::hget('channel:chapterfee:setting:' . $this->distribution_channel_id, $bid);
- $vip_sequence = BookService::getVipSequence($bid, $this->distribution_channel_id, $this->send_order_id);
- list($is_split, $is_change_chapter_name) = BookService::splitContent($bid);
- $change_chapter_name = 0;
- if ($is_split && ($book_info->channel_name == '男频' || $is_change_chapter_name)) {
- $change_chapter_name = 1;
- }
- switch ($book_info->charge_type) {
- case 'BOOK':
- $price = $this->getPrice($book_info);
- $is_need_charge = $this->isBookNeedCharge($bid, $price);
- foreach ($chapters as $v) {
- $v->next_chapter_status = 0;
- $v->next_price = 0;
- $v->is_need_charge = $v->is_vip ? $is_need_charge : false;
- $v->price = $price;
- // 限免判断
- if ($isFree) {
- $v->is_need_charge = false;
- $v->price = 0;
- }
- if ($vip_sequence) {
- if ($v->sequence >= $vip_sequence) {
- $v->is_vip = 1;
- } else {
- $v->is_vip = 0;
- }
- }
- //拆章
- if ($change_chapter_name) {
- $v->name = '第' . $v->sequence . '章';
- }
- }
- break;
- default:
- foreach ($chapters as $v) {
- if ($vip_sequence) {
- if ($v->sequence >= $vip_sequence) {
- $v->is_vip = 1;
- } else {
- $v->is_vip = 0;
- }
- }
- // 限免判断
- if ($isFree) {
- $v->is_need_charge = false;
- $v->price = 0;
- } else {
- $v->price = $v->is_vip ? $this->getPrice($book_info, $v->size) : 0;
- $v->is_need_charge = $v->is_vip ? $this->isChapterNeedCharge($bid, $v->id, $v->price) : false;
- }
- //下一章付费信息
- $v->next_chapter_status = 0;
- $v->next_price = 0;
- if ($v->is_vip) {
- $next_chapter_order_status = $this->nextChapterOrderStatus($bid, $v->next_cid);
- $v->next_chapter_status = $next_chapter_order_status['next_chapter_status'];
- $v->next_price = $next_chapter_order_status['next_price'];
- }
- //拆章
- if ($change_chapter_name) {
- $v->name = '第' . $v->sequence . '章';
- }
- }
- break;
- }
- return $chapters;
- }
- public function index(Request $request, $bid, $cid)
- {
- $this->push_id = $request->header('x-pushid', '');
- $send_order_id = $request->header('send_order_id', '');
- $package = $request->header('x-package', '');
- //每次绑定用户和派单的关系
- $this->bindSendOrderId($this->uid, $send_order_id);
- $oldbid = $bid;
- $bid = BookService::decodeBidStatic($bid);
- //获取图书信息
- $book_info = BookConfigService::getBookById($bid);
- if (empty($book_info))
- return response()->error('QAPP_SYS_ERROR');
- $this->book_info = $book_info;
- $book_auth = BookConfigService::bookCopyright($bid, $this->distribution_channel_id);
- if ($book_auth == 2) {
- return response()->error('QAPP_OFF_SHELF');
- }
- //yuyuedu、xinghe 快应用这两个cp的书屏蔽下 wutong,wutong2,wutong3下所有内容都不放快应用
- if (in_array($book_info->cp_source, getHiddenCp($package)) && $book_auth != 1) {
- return response()->error('QAPP_OFF_SHELF');
- }
- if ($this->distribution_channel_id == 7477 && $bid == 13765) {
- $book_info->is_on_shelf = 2;
- }
- $special = get_special_bid();
- if (in_array($this->distribution_channel_id, [9487, 9390]) && in_array($book_info->bid, $special)) {
- $book_info->is_on_shelf = 2;
- }
- if ($bid == 58886) {
- $book_info->is_on_shelf = 0;
- }
- // 华为整改强制下架
- if ($this->distribution_channel_id == 13131 && in_array($bid,[13762,65327,66643])){
- $device = QappUser::where(['uid' => $this->uid])->where('created_at','>','2023-02-03')->value("device_info");
- $device = json_decode($device,true);
- $device = strtolower($device['manufacturer'] ?? "");
- if ($device == "huawei"){
- return response()->error('QAPP_OFF_SHELF');
- }
- if ($device == "huawei"){
- return response()->error('QAPP_OFF_SHELF');
- }
- $book_info->is_on_shelf = 4;
- }
- if (!in_array($book_info->is_on_shelf, [1, 2])) {
- if ($book_info->is_on_shelf != 4) {
- return response()->error('QAPP_OFF_SHELF');
- }
- //补充操作:如果该用户未订阅该下架的书籍则删除其阅读记录(书架不予显示)
- //判断是否属于包年用户
- $year_account = YearOrderService::getRecord($this->uid);
- if (!$year_account) {
- //获取书籍充值类型
- $charge_type = $book_info->charge_type;
- if ($charge_type == 'BOOK') {
- //是否购买过该书,购买过则不删除
- $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
- } elseif ($charge_type == 'CHAPTER') {
- if ($this->distribution_channel_id == 13131 && in_array($bid,[13762,65327,66643])){
- $res = ReadRecordService::getReadRecord($this->uid);
- if ($res){
- $res = array_column($res,null,'bid');
- $res = $res[$bid] ?? [];
- }
- if (is_empty($res)){
- return response()->error('QAPP_OFF_SHELF');
- }
- }
- //是否购买过该书章节,购买过则不删除
- $res = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
- } else {
- $res = false;
- }
- if (!$res) {
- ReadRecordService::delReadRecordStatic($this->uid, [$bid]);
- return response()->error('QAPP_OFF_SHELF');
- }
- }
- }
- //wutong,wutong2,wutong3下所有内容都不放快应用
- // if(in_array($book_info->cp_source,['wutong','wutong2','wutong3','youyan2'])){
- // return response()->error('QAPP_OFF_SHELF');
- // }
- $this->book_info = $book_info;
- //获取章节信息
- $chapter = ChapterService::getChapterNameById($cid, $bid);
- if (!$chapter) {
- //短推长
- $combination_chapter = $this->chapterNotExists($bid, $cid);
- if ($combination_chapter) {
- $chapter = $combination_chapter;
- $bid = $chapter->bid;
- $book_info = BookConfigService::getBookById($bid);
- $oldbid = \Hashids::encode($bid);
- $this->book_info = $book_info;
- } else {
- return response()->error('QAPP_SYS_ERROR');
- }
- }
- list($is_split, $is_change_chapter_name) = BookService::splitContent($bid);
- if ($is_split && ($book_info->channel_name == '男频' || $is_change_chapter_name)) {
- $chapter->name = '第' . $chapter->sequence . '章';
- }
- $is_next_day = date('Y-m-d', strtotime($this->user_info->created_at)) == date('Y-m-d', strtotime('-1 days'));
- if ($is_next_day) {
- $job = new UserRententionJob($this->uid, now(), $this->user_info->created_at);
- dispatch($job)->onConnection('redis_queue')->onQueue('{user_rentention_queue}');
- }
- //自定义vip章节
- //$vip_sequence = Redis::hget('channel:chapterfee:setting:' . $this->distribution_channel_id, $bid);
- $vip_sequence = BookService::getVipSequence($bid, $this->distribution_channel_id, $this->send_order_id);
- if ($vip_sequence) {
- if ($chapter->sequence >= $vip_sequence) {
- $chapter->is_vip = 1;
- } else {
- $chapter->is_vip = 0;
- }
- }
- if ($chapter->is_vip == 0) {
- ReadRecordService::addReadLog($this->uid, [
- 'distribution_channel_id' => $this->distribution_channel_id,
- 'bid' => $bid,
- 'cid' => $chapter->id,
- 'uid' => $this->uid,
- 'send_order_id' => $this->send_order_id,
- 'sequence' => $chapter->sequence,
- ]);
- ReadRecordService::addReadRecord([
- 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
- 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
- ]);
- return response()->item(new ChapterTransformer, $this->getChapter($bid, $chapter->id, $chapter));
- }
- // 书籍是否限免
- $free = BookConfigService::judgeBookIsFree($bid);
- if ($free) {
- ReadRecordService::addReadLog($this->uid, [
- 'distribution_channel_id' => $this->distribution_channel_id,
- 'bid' => $bid,
- 'cid' => $chapter->id,
- 'uid' => $this->uid,
- 'send_order_id' => $this->send_order_id,
- 'sequence' => $chapter->sequence,
- ]);
- ReadRecordService::addReadRecord([
- 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
- 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
- ]);
- if ($chapter->is_vip == 1) {
- $fee = $this->getPrice($book_info, $chapter->size);
- $now = date('Y-m-d');
- Redis::hincrby('qapp:book:free:virtual:' . $free->id, $now, $fee);
- Redis::sadd('qapp:free:virtual' . $now, $free->id);
- Redis::sadd('qapp:free:virtual:uids' . $now . $free->id, $this->uid);
- Redis::sadd('qapp:free:virtual:uids' . $free->id, $this->uid);
- }
- return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
- }
- //已经付费
- if ($this->getOrderRecord($bid, $cid)) {
- ReadRecordService::addReadLog($this->uid, [
- 'distribution_channel_id' => $this->distribution_channel_id,
- 'bid' => $bid,
- 'cid' => $chapter->id,
- 'uid' => $this->uid,
- 'send_order_id' => $this->send_order_id,
- 'sequence' => $chapter->sequence,
- ]);
- ReadRecordService::addReadRecord([
- 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
- 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
- ]);
- return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
- }
- //未付费 要提醒
- $user_info = $this->user_info;
- //未付费 余额不足
- $fee = $this->getPrice($book_info, $chapter->size);
- $data = [
- 'book_id' => $oldbid,
- 'book_name' => $book_info->book_name,
- 'chapter_name' => $chapter->name,
- 'chapter_id' => $cid,
- 'pay_type' => $book_info->charge_type,
- 'fee' => $fee,
- 'user_balance' => $user_info->balance,
- 'product_id' => $book_info->product_id,
- 'uid' => $this->uid,
- 'distribution_channel_id' => $this->distribution_channel_id,
- 'is_discount' => 0,
- 'discount_fee' => '',
- 'discount' => ''
- ];
- if ($user_info['balance'] < $fee) {
- if ($book_info->charge_type == 'BOOK') {
- return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
- } elseif ($book_info->charge_type == 'CHAPTER') {
- return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
- } else {
- return response()->error('QAPP_SYS_ERROR');
- }
- }
- if (!$this->send_order_id) {
- if ($book_info->charge_type == 'BOOK') {
- return response()->error('QAPP_BOOK_BUY', $data);
- }/*else{
- return response()->error('QAPP_CHAPTER_BUY', $data);
- }*/
- }
- //付费 不提醒
- if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, 0)) {
- UserTaskService::addUserTaskQueue($this->uid, BaseTask::read, UserTaskService::judge_trigger);
- ReadRecordService::addReadLog($this->uid, [
- 'distribution_channel_id' => $this->distribution_channel_id,
- 'bid' => $bid,
- 'cid' => $chapter->id,
- 'uid' => $this->uid,
- 'send_order_id' => $this->send_order_id,
- 'sequence' => $chapter->sequence,
- ]);
- ReadRecordService::addReadRecord([
- 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
- 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
- ]);
- //限免统计
- $free_book = BookConfigService::getByBidNoFilter($bid);
- if ($free_book) {
- if (strtotime($free_book->end_time) + 7 * 86400 >= strtotime(date('Y-m-d'))) {
- if (Redis::Sismember('qapp:free:virtual:uids' . $free_book->id, $this->uid)) {
- $now = date('Y-m-d');
- Redis::hincrby('qapp:book:free:actuality:' . $free_book->id, $now, $fee);
- Redis::sadd('qapp:free:actuality' . $now, $free_book->id);
- Redis::sadd('qapp:free:actuality:uids' . $now . $free_book->id, $this->uid);
- }
- }
- }
- if ($this->uid == 247081369) {
- $item = itemTransform(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
- myLog('xueqi')->info($item);
- }
- return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
- } else {
- if ($book_info->charge_type == 'BOOK') {
- return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
- } elseif ($book_info->charge_type == 'CHAPTER') {
- return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
- } else {
- return response()->error('QAPP_SYS_ERROR');
- }
- }
- }
- public function pay(Request $request, $bid, $cid)
- {
- $this->push_id = $request->header('x-pushid', '');
- $remind = (int)$request->input('remind');
- $oldbid = $bid;
- $bid = BookService::decodeBidStatic($bid);
- $book_info = BookConfigService::getBookById($bid);;
- if (empty($book_info)) response()->error('QAPP_SYS_ERROR');
- $this->book_info = $book_info;
- if ($this->distribution_channel_id == 7477 && $bid == 13765) {
- $book_info->is_on_shelf = 2;
- }
- if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
- if (!$this->isBookOrdered($bid)) {
- response()->error('QAPP_OFF_SHELF');
- }
- }
- //获取章节
- $chapter = ChapterService::getChapterNameById($cid, $bid);
- if (!$chapter) {
- $combination_chapter = $this->chapterNotExists($bid, $cid);
- if ($combination_chapter) {
- $chapter = $combination_chapter;
- $bid = $chapter->bid;
- $book_info = BookConfigService::getBookById($bid);
- $oldbid = \Hashids::encode($bid);
- $this->book_info = $book_info;
- } else {
- return response()->error('QAPP_SYS_ERROR');
- }
- }
- if ($this->getOrderRecord($bid, $cid)) {
- ReadRecordService::addReadLog($this->uid, [
- 'distribution_channel_id' => $this->distribution_channel_id,
- 'bid' => $bid,
- 'cid' => $chapter->id,
- 'uid' => $this->uid,
- 'send_order_id' => $this->send_order_id,
- 'sequence' => $chapter->sequence,
- ]);
- ReadRecordService::addReadRecord([
- 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
- 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
- ]);
- return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
- }
- if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, $remind)) {
- UserTaskService::addUserTaskQueue($this->uid, BaseTask::read, UserTaskService::judge_trigger);
- ReadRecordService::addReadLog($this->uid, [
- 'distribution_channel_id' => $this->distribution_channel_id,
- 'bid' => $bid,
- 'cid' => $chapter->id,
- 'uid' => $this->uid,
- 'send_order_id' => $this->send_order_id,
- 'sequence' => $chapter->sequence,
- ]);
- ReadRecordService::addReadRecord([
- 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
- 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
- ]);
- $free_book = BookConfigService::getByBidNoFilter($bid);
- if ($free_book) {
- if (strtotime($free_book->end_time) + 7 * 86400 >= strtotime(date('Y-m-d'))) {
- if (Redis::Sismember('qapp:free:virtual:uids' . $free_book->id, $this->uid)) {
- $now = date('Y-m-d');
- $fee = $this->getPrice($book_info, $chapter->size);
- Redis::hincrby('qapp:book:free:actuality:' . $free_book->id, $now, $fee);
- Redis::sadd('qapp:free:actuality' . $now, $free_book->id);
- Redis::sadd('qapp:free:actuality:uids' . $now . $free_book->id, $this->uid);
- }
- }
- }
- return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
- } else {
- $fee = $this->getPrice($book_info, $chapter->size);
- $data = [
- 'book_id' => $oldbid,
- 'book_name' => $book_info->book_name,
- 'chapter_name' => $chapter->name,
- 'chapter_id' => $cid,
- 'pay_type' => $book_info->charge_type,
- 'fee' => $fee,
- 'user_balance' => $this->user_info['balance'],
- 'product_id' => $book_info->product_id,
- 'uid' => $this->uid,
- 'distribution_channel_id' => $this->distribution_channel_id,
- 'is_discount' => 0,
- 'discount_fee' => '',
- 'discount' => ''
- ];
- if ($book_info->charge_type == 'BOOK') {
- return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
- } elseif ($book_info->charge_type == 'CHAPTER') {
- return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
- } else {
- return response()->error('QAPP_SYS_ERROR');
- }
- }
- }
- /**
- * 余额支付
- * @param $book_info
- * @param $chapter_id
- * @param $chapter_size
- * @return bool
- */
- protected function balancePay($book_info, $chapter_id, $chapter_size, $chapter_name, $is_remind)
- {
- $fee = $this->getPrice($book_info, $chapter_size);
- if ((int)$this->user_info['balance'] >= $fee) {
- if ($this->bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)) {
- return true;
- }
- return false;
- } else {
- return false;
- }
- }
- /**
- * 获取章节内容
- * @param $bid
- * @param $cid
- * @return bool|mixed
- */
- protected function getChapter($bid, $cid, $chapter)
- {
- $chapter_content = ChapterService::getChapter($bid, $cid);
- if (!$chapter_content) return false;
- $chapter->content = trim(str_replace($chapter_content->name, '', $chapter_content->content));
- // 格式特殊处理
- $qian = array(" ", " ", "\t");
- $hou = array("", "", "");
- // 去掉所有空格
- $chapter->content = str_replace($qian, $hou, $chapter->content);
- // 2换行变1换行
- $chapter->content = str_replace("\n\n", "\n", $chapter->content);
- $chapter->content = str_replace("\r\n\r\n", "\r\n", $chapter->content);
- // \Log::info('getChapter_chapter:'.$bid.' cid:'.$cid);
- // \Log::info($chapter->content);
- //统计点击率
- $key = 'book_click_num_bid_' . $bid;
- $field = date('Y-m-d');
- $old = Redis::hget($key, $field);
- if (!$old) $old = 0;
- Redis::hset($key, $field, $old + 1);
- [$force_add_desk_type,$is_need_query_send_order] = $this->addDesktopType($bid, $chapter->sequence);
- $chapter->force_add_desk_type = $force_add_desk_type;
- $force_add_work_wechat_type = 0;
- $force_add_work_wechat_url = "";
- // 派单企微弹出查询
- if($is_need_query_send_order){
- [$force_add_work_wechat_type, $force_add_work_wechat_url] = $this->addWorkWechatType($chapter->sequence);
- }
- // push企微弹出查询
- if ($this->push_id > 0 && $force_add_work_wechat_type == 0){
- [$force_add_work_wechat_type, $force_add_work_wechat_url] = $this->addWorkWechatType($chapter->sequence,2);
- }
- $chapter->force_add_work_wechat_type = $force_add_work_wechat_type;
- $chapter->force_add_work_wechat_url = $force_add_work_wechat_url;
- //统计
- $this->stats();
- $next_chapter_order_status = $this->nextChapterOrderStatus($bid, $chapter->next_cid);
- $chapter->next_chapter_status = $next_chapter_order_status['next_chapter_status'];
- $chapter->next_price = $next_chapter_order_status['next_price'];
- $chapter->charge_type = $this->book_info->charge_type;
- $this->userBookCombination($chapter);
- return $chapter;
- }
- private function nextChapterOrderStatus($bid, $cid)
- {
- $chapter = ChapterService::getChapterNameById($cid, $bid);
- if (!$chapter || $chapter->is_vip == 0 || $this->book_info->charge_type == 'BOOK') {
- return ['next_chapter_status' => 0, 'next_price' => 0];
- }
- $is_paid = $this->getOrderRecord($bid, $cid);
- if ($is_paid) {
- return ['next_chapter_status' => 1, 'next_price' => 0];
- }
- $fee = $this->getPrice($this->book_info, $chapter->size);
- return ['next_chapter_status' => 2, 'next_price' => $fee];
- }
- //短续长
- private function userBookCombination($chapter)
- {
- if ($chapter->prev_cid && $chapter->next_cid) {
- return;
- }
- if ($chapter->next_cid == 0 && $this->book_info->charge_type == 'BOOK') {
- $bid = UserBookCombinationConfigService::selectAndSave($this->uid, $this->book_info->bid);
- if ($bid) {
- $bookInfo = BookConfigService::getBookById($bid);
- $chapter->next_cid = $bookInfo->first_cid;
- }
- }
- if ($chapter->prev_cid == 0 && $this->book_info->charge_type == 'CHAPTER') {
- $bid = UserBookCombinationConfigService::getShortBookFromLongBid($this->uid, $this->book_info->bid);
- if ($bid) {
- $bookInfo = BookConfigService::getBookById($bid);
- $chapter->prev_cid = $bookInfo->last_cid;
- }
- }
- }
- private function chapterNotExists($bid, $cid)
- {
- //1.全局图书组合配置
- $combination = BookConfigService::getCombinationInfo($bid);
- if ($combination) {
- $chapter = ChapterService::getChapterNameByIdNoCheck($cid);
- if ($chapter) {
- return $chapter;
- }
- return false;
- }
- //2.用户图书组合配置
- if ($this->book_info->charge_type == 'CHAPTER') {
- $user_combination = UserBookCombinationConfigService::getShortBookFromLongBook($this->uid, $bid);
- } else {
- $user_combination = UserBookCombinationConfigService::getLongBookFromShortBook($this->uid, $bid);
- }
- if ($user_combination) {
- $chapter = ChapterService::getChapterNameByIdNoCheck($cid);
- if ($chapter) {
- return $chapter;
- }
- return false;
- }
- return false;
- }
- /**
- * 添加订购记录
- * @param $book_info
- * @param $chapter_id
- * @param $fee
- * @return bool
- */
- protected function bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)
- {
- if ($book_info['charge_type'] == 'BOOK') {
- $data = [
- 'uid' => $this->uid,
- 'fee' => $fee,
- 'u' => $this->send_order_id,
- 'distribution_channel_id' => $this->distribution_channel_id,
- 'bid' => $book_info->bid,
- 'book_name' => $book_info->book_name,
- 'send_order_id' => $this->send_order_id,
- ];
- return BookOrderService::addOrderRecodeAndDecrUserBalance($data, $this->uid);
- } else {
- $data = [
- 'uid' => $this->uid,
- 'fee' => $fee,
- 'cid' => $chapter_id,
- 'bid' => $book_info->bid,
- 'distribution_channel_id' => $this->distribution_channel_id,
- 'book_name' => $book_info->book_name,
- 'chapter_name' => $chapter_name,
- 'send_order_id' => $this->send_order_id,
- 'is_remind' => $is_remind
- ];
- if ($is_remind) {
- $this->addOrderRemind($book_info->bid);
- }
- return ChapterOrderService::addOrderAndDecrUserBalance($data, $this->uid);
- }
- }
- protected function addOrderRemind($bid)
- {
- if (ChapterReminderService::checkIsNoReminder($this->uid, $bid)) {
- return true;
- } else {
- ChapterReminderService::add($this->uid, $bid);
- return true;
- }
- }
- /**
- * 是否订购提醒
- * @param $chapter_id
- * @return bool
- */
- protected function isOrderRemind($bid)
- {
- $is_no_reminder = ChapterReminderService::checkIsNoReminder($this->uid, $bid) ? 1 : 0;
- return $is_no_reminder == 0;
- }
- /**
- * 用户是否关注
- * @param $uid
- * @return bool
- */
- protected function getSubscribe()
- {
- $res = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
- if ($res) return true;
- return false;
- }
- /**
- * 获取订购记录
- * @param $book_info
- * @param $chapter_id
- * @return bool
- */
- protected function getOrderRecord($bid, $chapter_id)
- {
- //包年记录
- $uid = $this->uid;
- $res = YearOrderService::getRecord($uid);
- if ($res) return true;
- $res = null;
- //单本订购记录
- $res = BookOrderService::getRecordByuidBid($uid, $bid);
- if ($res) return true;
- $res = null;
- //章节订购记录
- $chapterOrder = new ChapterOrderService();
- if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
- return false;
- }
- /**
- * 计算价格
- * @param $book_info
- * @param $chapter_size
- * @return float
- */
- private function getPrice($book_info, $chapter_size = 0)
- {
- if ($book_info->charge_type == 'BOOK') {
- if (BookOrderService::isHasBookOrder($this->uid)) {
- $this->is_first_book_order = 0;
- return 1399;
- } else {
- $this->is_first_book_order = 1;
- return 899;
- }
- } else {
- // 获取投放后台账号,
- $account = '';
- $account_send_order = $this->getNowSendOrderInfo();
- if ($account_send_order) {
- $account = isset($account_send_order['account']) ? $account_send_order['account'] : '';
- }
- $fee = BookService::getPrice($book_info, $this->distribution_channel_id, $chapter_size, $account);
- return $fee;
- }
- }
- /**
- * 计算价格
- * @param $book_info
- * @param $chapter_size
- * @return float
- */
- private function getPriceNew($book_info, $chapter_size = 0, $account = "")
- {
- if ($book_info->charge_type == 'BOOK') {
- if (BookOrderService::isHasBookOrder($this->uid)) {
- $this->is_first_book_order = 0;
- return 1399;
- } else {
- $this->is_first_book_order = 1;
- return 899;
- }
- } else {
- return BookService::getPrice($book_info, $this->distribution_channel_id, $chapter_size, $account);
- }
- }
- /**
- * 获取投放后台账号,
- * name: getAccount
- * @return mixed|string
- * date 2022/10/20 11:29
- */
- protected function getAccount()
- {
- $account = '';
- $account_send_order = $this->getNowSendOrderInfo();
- if ($account_send_order) {
- $account = isset($account_send_order['account']) ? $account_send_order['account'] : '';
- }
- return $account;
- }
- /**
- * 用户添加标签
- * @param $book_info
- */
- protected function addTag($book_info)
- {
- if (!UserDeepReadTagService::isAddTag($this->uid, $book_info->bid)) {
- try {
- UserDeepReadTagService::addTag([
- 'uid' => $this->uid,
- 'bid' => $book_info->bid,
- 'book_name' => $book_info->book_name,
- 'category_id' => $book_info->category_id,
- 'category_name' => $book_info->category_name,
- 'sex_preference' => $book_info->channel_name ? $book_info->channel_name : '',
- 'distribution_channel_id' => $this->distribution_channel_id ? $this->distribution_channel_id : '0',
- 'send_order_id' => $this->send_order_id,
- ]);
- } catch (\Exception $e) {
- }
- }
- }
- protected function isBookOrdered($bid)
- {
- $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
- if ($chapter_order) return true;
- $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
- if ($res) return true;
- return false;
- }
- /**
- * 判断是否需要充值
- */
- private function isBookNeedCharge(int $bid, float $price)
- {
- $book_order = $this->getOrderRecord($bid, 0);
- if ($book_order) {
- return false;
- } else {
- $user_info = $this->user_info;
- return $user_info['balance'] < $price;
- }
- }
- /**
- * 判断章节是否需要充值
- */
- private function isChapterNeedCharge(int $bid, int $cid, float $price)
- {
- $book_order = $this->getOrderRecord($bid, $cid);
- if ($book_order) {
- return false;
- } else {
- $user_info = $this->user_info;
- return $user_info['balance'] < $price;
- }
- }
- private function stats()
- {
- //阅读器统计
- WapVisitStatService::recordReaderUvAndPv($this->uid, $this->distribution_channel_id);
- }
- //加桌类型
- private function addDesktopType($bid, $sequence)
- {
- $deault_force_add_desk_type = 0;
- $send_order_id = $this->GetBindSendOrderId($this->uid);
- $is_need_query_order = 0;
- if ($send_order_id) {
- $send_order_info = SendOrderService::getById($send_order_id);
- if (!$send_order_info) return [$deault_force_add_desk_type,$is_need_query_order];
- if ($send_order_info->book_id == $bid) {
- $is_need_query_order = 1;
- // 派单书籍和观看书籍一致,并设置了强加桌,判断当前章节和设置的强加桌章节
- if ($send_order_info->force_add_desk_type == 1 && $send_order_info->force_add_desk_seq) {
- //在设置的强加桌章节前一章开始
- if ($sequence >= ($send_order_info->force_add_desk_seq - 1)) {
- $force_add_desk_type = $send_order_info->force_add_desk_type;
- return [$force_add_desk_type,$is_need_query_order];
- }
- }
- if ($send_order_info->force_add_desk_type == 2) {
- if ($sequence >= $this->book_info->force_subscribe_chapter_seq && $sequence <= $this->book_info->force_subscribe_chapter_seq + 3) {
- $force_add_desk_type = $send_order_info->force_add_desk_type;
- return [$force_add_desk_type,$is_need_query_order];
- }
- }
- if ($send_order_info->force_add_desk_type == 3) {
- //弱加桌+强加桌 ,即强加桌章节之前的章节均弱加桌,之后的章节显示强加桌:
- $need_sequence = $send_order_info->force_add_desk_seq;
- if (!$need_sequence) {
- $need_sequence = $this->book_info->force_subscribe_chapter_seq;
- }
- if ($sequence >= ($need_sequence - 1)) {
- //之后的章节显示强加桌
- return [1,$is_need_query_order];
- } else {
- ////强加桌章节之前的章节均弱加桌
- return [2,$is_need_query_order];
- }
- }
- return [$deault_force_add_desk_type,$is_need_query_order];
- }
- }
- //无派单,或者派单书籍与 此次书籍不一致,直接使用 原书籍的默认强关章节
- $book_info = BookConfigService::getBookById($bid);
- if ($book_info && $book_info->force_subscribe_chapter_seq) {
- if ($sequence >= ($book_info->force_subscribe_chapter_seq - 1)) {
- return [2,$is_need_query_order];
- }
- }
- return [$deault_force_add_desk_type,$is_need_query_order];
- }
- /**
- * 获取章节详情企微弹窗设置
- * name: addWorkWechatType
- * @param mixed $sequence 章节序号
- * @param mixed $type 类型 1 派单 2 push
- * @return array
- * date 2023/02/13 16:08
- */
- private function addWorkWechatType($sequence,$type = 1)
- {
- $default_force_add_desk_type = 0;
- $work_wechat_url = "";
- if ($type == 1){
- // 派单
- $send_order_id = $this->GetBindSendOrderId($this->uid);
- if ($send_order_id) {
- $send_order_info = QappSendOrder::getSendOrderById($send_order_id);
- if (!$send_order_info) return [$default_force_add_desk_type, $work_wechat_url];
- if($send_order_info['force_add_work_wechat_status'] == 1 && $send_order_info['force_add_work_wechat_seq'] <= ($sequence+1)){
- $default_force_add_desk_type =$send_order_info['force_add_work_wechat_type'];
- $work_wechat_url =$send_order_info['force_add_work_wechat_url'];
- }
- }
- }else if ($type == 2){
- // push
- $pushInfo = QappPushTask::where(['id' => $this->push_id,'uid' => $this->distribution_channel_id])
- ->select('id','force_add_work_wechat_status','force_add_work_wechat_type','force_add_work_wechat_seq','force_add_work_wechat_url')->first() ;
- if (!$pushInfo) return [$default_force_add_desk_type, $work_wechat_url];
- if($pushInfo['force_add_work_wechat_status'] == 1 && $pushInfo['force_add_work_wechat_seq'] <= ($sequence+1)){
- $default_force_add_desk_type =$pushInfo['force_add_work_wechat_type'];
- $work_wechat_url = $pushInfo['force_add_work_wechat_url'];
- }
- }
- return [$default_force_add_desk_type, $work_wechat_url];
- }
- /**
- * [bindSendOrderId description]
- * @param [type] $uid [description]
- * @param [type] $send_order_id [description]
- * @return [type] [description]
- */
- public function bindSendOrderId($uid, $send_order_id)
- {
- if ($send_order_id) {
- $res = Redis::hset('book_read_chapter:' . $uid, 'send_order_id', $send_order_id);
- }
- }
- /**
- * [bindSendOrderId description]
- * @param [type] $uid [description]
- * @param [type] $send_order_id [description]
- * @return [type] [description]
- */
- public function GetBindSendOrderId($uid)
- {
- try {
- $send_order_id = Redis::hget('book_read_chapter:' . $uid, 'send_order_id');
- if ($send_order_id)
- return (int)$send_order_id;
- } catch (\Exception $e) {
- }
- }
- private function getChapterIds($chapters): array
- {
- $list = [];
- if ($chapters) {
- foreach ($chapters as $v) {
- $list[] = $v->id;
- }
- }
- return $list;
- }
- private function getHandelData(int $bid, array $chapterIds): array
- {
- $uid = $this->uid;
- $res = YearOrderService::getRecord($uid);
- if ($res) {
- return $handle = ['check' => 0, 'paycid' => []];
- }
- $res = null;
- //单本订购记录
- $res = BookOrderService::getRecordByuidBid($uid, $bid);
- if ($res) {
- return $handle = ['check' => 0, 'paycid' => []];
- }
- //章节订购记录
- $chapterOrder = new ChapterOrderService();
- $res = $chapterOrder->getOrdersByChapterIds($uid, $bid, $chapterIds);
- return $handle = ['check' => 1, 'paycid' => $res];
- }
- /**
- * 判断是否需要充值
- * name: isChapterNeedChargeNew
- * @param $cid
- * @param $rules
- * @return bool
- * date 2022/10/20 16:08
- */
- private function isChapterNeedChargeNew($cid, $rules, $price = 0): bool
- {
- if ($rules['check'] != 1) {
- return false;
- }
- if (empty($rules['paycid']) || !in_array($cid, $rules['paycid'])) {
- $user_info = $this->user_info;
- return $user_info['balance'] < $price;
- } else {
- return false;
- }
- }
- /*****
- * 是否订阅
- * name: isSubscribe
- * @param $cid
- * @param $rules
- * @return int
- * date 2022/11/02 17:19
- */
- private function isSubscribe($cid, $rules): int
- {
- if ($rules['check'] != 1) {
- return 1;
- }
- if (empty($rules['paycid']) || !in_array($cid, $rules['paycid'])) {
- return 2;
- } else {
- return 1;
- }
- }
- /**
- * 获取用户相关的企微测试
- * name: getWorkWechatInfo
- * @param $bid
- * @return array
- * date 2023/02/13 16:58
- */
- private function getWorkWechatInfo($bid)
- {
- $info = ['force_add_work_wechat_type' => 0,'force_add_work_wechat_url' => "",'force_add_work_wechat_seq' => 0];
- $send_order_id = $this->GetBindSendOrderId($this->uid);
- if ($send_order_id) {
- $send_order_info = QappSendOrder::getSendOrderById($send_order_id);
- if (!$send_order_info) return $info;
- $send_order_bid = SendOrder::where(['id' => $send_order_id])->value('book_id');
- if($send_order_info['force_add_work_wechat_status'] == 1 && $send_order_bid == $bid){
- $info['force_add_work_wechat_type'] = $send_order_info['force_add_work_wechat_type'];
- $info['force_add_work_wechat_url'] = $send_order_info['force_add_work_wechat_url'];
- $info['force_add_work_wechat_seq'] = $send_order_info['force_add_work_wechat_seq'];
- }
- }
-
- if($info['force_add_work_wechat_type'] == 0 && $this->push_id > 0){
- $pushInfo = QappPushTask::where(['id' => $this->push_id,'uid' => $this->distribution_channel_id])
- ->select('id','force_add_work_wechat_status','force_add_work_wechat_type','force_add_work_wechat_seq','force_add_work_wechat_url')->first() ;
- if($pushInfo['force_add_work_wechat_status'] == 1){
- $info['force_add_work_wechat_type'] = $pushInfo['force_add_work_wechat_type'];
- $info['force_add_work_wechat_url'] = $pushInfo['force_add_work_wechat_url'];
- $info['force_add_work_wechat_seq'] =$pushInfo['force_add_work_wechat_seq'];
- }
- }
- return $info;
- }
- }
|