123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- namespace App\Http\Controllers\QuickApp\SendOrder;
- use App\Http\Controllers\QuickApp\BaseController;
- use App\Modules\Book\Models\Chapter;
- use App\Modules\Book\Services\BookConfigService;
- use App\Modules\Channel\Services\QappSendOrderContentShieldConfigService;
- use App\Modules\SendOrder\Models\QappSendOrder;
- use App\Modules\SendOrder\Services\SendOrderService;
- use Illuminate\Http\Request;
- use Cookie;
- use Illuminate\Support\Facades\DB;
- use Redis;
- class AdReplaceBookController extends BaseController
- {
- private $send_order_id;
- private $package;
- public function getAdvReplaceBookInfo(Request $request)
- {
- $book = [];
- try {
- $package = $request->header('x-package', '');
-
- if (!in_array($package, [ "com.beidao.kuaiying.haohannew" , "com.beidao.kuaiying.haohan"])) {
- $send_order_id = $request->input('send_order_id', 0);
- $book = $this->sendOrderBookReplace($send_order_id);
- if ($book) $book['bid'] = str_encode($book['bid']);
- }
- } catch (\Exception $exception) {
- $book = [];
- }
- return response()->success($book);
- }
-
- public function advShield(Request $request)
- {
- try {
- $this->send_order_id = $request->input('send_order_id', 0);
- $this->package = $request->input('package');
- $checkout_tx = strtolower($request->input('checkout_tx', ''));
- $platform = strtolower($request->input('platform', ''));
- $send_order = SendOrderService::getSendOrderStatic($this->send_order_id);
- $this->sendOrderStatistic($send_order);
- $doShied = $this->getRealBookInfo($checkout_tx, $platform);
- if ($doShied) {
- $doShied['bid'] = str_encode($doShied['bid']);
- }else{
- $doShied = [];
- }
- } catch (\Exception $exception) {
- $doShied = [];
- }
- return response()->success($doShied);
- }
-
- private function sendOrderStatistic($send_order)
- {
- $key = date('Y-m-d');
- $send_order_flag = Cookie::get('send_order_flag');
- $send_orders = explode(',', trim($send_order_flag, ','));
-
- if (!Cookie::get('send_order_flag_' . $this->send_order_id) && !in_array($this->send_order_id, $send_orders)) {
- Redis::hincrby('send_order_uv_' . $this->send_order_id, $key, 1);
- Redis::hincrby('send_order_uv_' . $this->send_order_id, 'total', 1);
- array_push($send_orders, $this->send_order_id);
- $str = implode(',', $send_orders);
- Cookie::queue('send_order_flag', $str, env('U_COOKIE_EXPIRE'), null, null, false, false);
- }
- if (Cookie::get('send_order_flag_' . $this->send_order_id)) {
- array_push($send_orders, $this->send_order_id);
- $str = implode(',', $send_orders);
- Cookie::queue('send_order_flag', $str, env('U_COOKIE_EXPIRE'), null, null, false, false);
- Cookie::queue('send_order_flag_' . $this->send_order_id, null, -1);
- }
-
- Redis::hincrby('send_order_pv_' . $this->send_order_id, $key, 1);
- Redis::hincrby('send_order_pv_' . $this->send_order_id, 'total', 1);
- Redis::sadd('send_order' . $key, $this->send_order_id);
- if (isset($send_order->send_time) && $send_order->send_time) {
- } else {
- $uv = Redis::hget('send_order_uv_' . $this->send_order_id, $key);
- if ($uv && $uv > 20) {
- SendOrderService::updateSendOrderTime($this->send_order_id);
- }
- }
- }
-
- private function getRealBookInfo($checkout_tx = '', $platform = "")
- {
-
- $info = QappSendOrderContentShieldConfigService::doShied($this->send_order_id, _getIp());
- if ($info) {
- return $this->checkoutBookInfo($info);
- }
-
- if ($checkout_tx) {
- $tx_info = $this->getNoAdvBookInfo();
- if ($tx_info) {
- return $this->checkoutBookInfo($tx_info);
- }
- }
- if (!in_array($platform, ['tx_adq', 'tx_adq_new'])) {
-
- $send_order_replace_info = $this->sendOrderBookReplace($this->send_order_id);
- if ($send_order_replace_info) {
- return $this->checkoutBookInfo($send_order_replace_info);
- }
- }
- return [];
- }
-
- private function getNoAdvBookInfo(): array
- {
- $extraConfig = QappSendOrder::where('send_order_id', $this->send_order_id)
- ->where('platform', 'tx_adq_new')
- ->value('extra_config');
- if ($extraConfig) {
- $extraConfigDecode = json_decode($extraConfig, true);
- $bid = str_decode($extraConfigDecode['txAdqNoAdvBid'] ?? '');
- $cid = $extraConfigDecode['txAdqNoAdvCid'] ?? 0;
- if ($bid) {
- return ['bid' => $bid, 'cid' => $cid];
- }
- }
- return [];
- }
-
- private function sendOrderBookReplace($send_order_id = 0): array
- {
- $book = [];
- if ($send_order_id > 0) {
- $extra_config = QappSendOrder::where(['send_order_id' => $send_order_id])->value('extra_config');
- $extra_config = empty($extra_config) ? [] : json_decode($extra_config, true);
- $status = $extra_config['replace_book_status'] ?? 0;
- $bid = $extra_config['replace_book_bid'] ?? "";
- $cid = $extra_config['replace_chapter_cid'] ?? 0;
- if ($status == 1) {
- if (!empty($bid) && !empty($cid)) {
- $book = ['bid' => str_decode($bid), 'cid' => $cid];
- } else {
- myLog("SendOrderReplaceBookError")->info(['send_order_id' => $send_order_id, 'extra_config' => $extra_config]);
- }
- }
- }
- return $book;
- }
- private function checkoutBookInfo($info)
- {
- $bid = $info['bid'] ?? 0;
-
- if (!BookConfigService::getAvailableBIdsbyBids([$bid], $this->distribution_channel_id)) {
- $info = $this->getAvailableBookInfoByBid($bid);
- }
- return $info;
- }
-
- private function getAvailableBookInfoByBid($bid)
- {
- $channel = DB::table('book_categories')->join('books', 'books.category_id', '=', 'book_categories.id')->where('books.id', $bid)->value('book_categories.pid');
- $channel = $channel ?: 2;
- $bids = BookConfigService::getRecommendBids($this->package, $channel, [], 1);
- if ($bids) {
- $bid = $bids[0];
- }
- $cid = Chapter::where('bid', $bid)->orderBy('sequence', 'asc')->value('id');
- return compact('bid', 'cid');
- }
- }
|