|
@@ -5,12 +5,15 @@ namespace App\Http\Controllers\QuickApp\Book;
|
|
|
use App\Consts\BaseConst;
|
|
|
use App\Libs\Utils;
|
|
|
use App\Modules\Book\Services\BookAuditService;
|
|
|
+use App\Modules\Channel\Models\ChannelAdvert;
|
|
|
+use App\Modules\Channel\Services\ChannelAdvertService;
|
|
|
use App\Modules\Channel\Services\ChannelRecommendBookConfigService;
|
|
|
use App\Modules\Channel\Services\ChannelRecommendBooksService;
|
|
|
use App\Modules\RecommendBook\Services\QappRecommendService;
|
|
|
use App\Modules\RecommendBook\Services\RecommendService;
|
|
|
use App\Modules\Book\Services\RecoBannerService;
|
|
|
use App\Modules\Trade\Services\OrderService;
|
|
|
+use App\Modules\User\Models\ChannelAdUser;
|
|
|
use App\Modules\User\Models\QappPackage;
|
|
|
use App\Modules\User\Services\QappUserService;
|
|
|
|
|
@@ -29,6 +32,7 @@ use App\Modules\Subscribe\Services\ChapterOrderService;
|
|
|
use App\Modules\Subscribe\Services\YearOrderService;
|
|
|
use App\Modules\User\Services\ReadRecordService;
|
|
|
use Hashids;
|
|
|
+use Log;
|
|
|
use Illuminate\Support\Facades\Redis;
|
|
|
|
|
|
class BookController extends BaseController
|
|
@@ -201,19 +205,7 @@ class BookController extends BaseController
|
|
|
}
|
|
|
|
|
|
if($isAuth){
|
|
|
- $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
|
|
|
-
|
|
|
- $books->transform(function ($item) {
|
|
|
- $result = $this->getBidCidFromUrl($item->redirect_url);
|
|
|
- $item->bid = $result['bid'];
|
|
|
- $item->cid = $result['cid'];
|
|
|
- if ($result['cid']) {
|
|
|
- $item->redirect_url = "views/Reader";
|
|
|
- } else {
|
|
|
- $item->redirect_url = "views/Detail";
|
|
|
- }
|
|
|
- return $item;
|
|
|
- });
|
|
|
+ $books = $this->getDefaultBanner();
|
|
|
}else{
|
|
|
$home = config('home.ycsd');
|
|
|
$banner = $home['reco_banner'];
|
|
@@ -566,19 +558,7 @@ class BookController extends BaseController
|
|
|
*/
|
|
|
public function recommen()
|
|
|
{
|
|
|
- $reco_banner_type = ['FEMALE', 'PUBLIC'];
|
|
|
- $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
|
|
|
- $books->transform(function ($item) {
|
|
|
- $result = $this->getBidCidFromUrl($item->redirect_url);
|
|
|
- $item->bid = $result['bid'];
|
|
|
- $item->cid = $result['cid'];
|
|
|
- if ($result['cid']) {
|
|
|
- $item->redirect_url = "views/Reader";
|
|
|
- } else {
|
|
|
- $item->redirect_url = "views/Detail";
|
|
|
- }
|
|
|
- return $item;
|
|
|
- });
|
|
|
+ $books = $this->getDefaultBanner();
|
|
|
return response()->success($books);
|
|
|
}
|
|
|
/**
|
|
@@ -601,20 +581,144 @@ class BookController extends BaseController
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ $books = $this->getDefaultBanner();
|
|
|
+ return response()->success($books);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新获取各种广告列表
|
|
|
+ * @param Request $request
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function getRecommendBanners(Request $request)
|
|
|
+ {
|
|
|
+ $release_type = $request->get('release_type','');
|
|
|
+ $distribution_id = $this->distribution_channel_id;
|
|
|
+ if(empty($release_type)){
|
|
|
+ //默认原banner
|
|
|
+ $banner = $this->getDefaultBanner();
|
|
|
+ return response()->success($banner);
|
|
|
+ }
|
|
|
+
|
|
|
+ if($release_type == '4' || $release_type == '5'){
|
|
|
+ //弹窗和充值页返回需要先判断频率跟权限
|
|
|
+ $advert = ChannelAdvert::select('id','photo as banner_url','type','content','person','frequency')
|
|
|
+ ->where('distribution_id',$distribution_id)
|
|
|
+ ->where('release_type',$release_type)
|
|
|
+ ->where('status',1)
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if(!$advert){
|
|
|
+ return response()->success([]);
|
|
|
+ }
|
|
|
+ $advert = $advert->toArray();
|
|
|
+ $advert['ids'] = Hashids::encode($advert['id']);
|
|
|
+ if($advert['type'] == 1){
|
|
|
+ $advert['redirect_url'] = 'views/Reader';
|
|
|
+ $content = explode(';',$advert['content']);
|
|
|
+ $advert['bid'] = isset($content[2]) ? $content[2] : '';
|
|
|
+ $advert['cid'] = isset($content[3]) ? $content[3] : '';
|
|
|
+ }else{
|
|
|
+ $advert['redirect_url'] = 'views/Detail';
|
|
|
+ }
|
|
|
+
|
|
|
+ if($release_type == '4'){
|
|
|
+ //弹窗需要判断频率
|
|
|
+ if($advert['frequency'] == 'always'){
|
|
|
+ $advert = self::getBackFormat($advert);
|
|
|
+ return response()->success($advert);
|
|
|
+ }
|
|
|
+
|
|
|
+ $day = strtotime(date('Y-m-d H:i:s'));
|
|
|
+ $nextDay = strtotime( date('Y-m-d'). ' +1 day');
|
|
|
+ $nextWeek = strtotime(date('Y-m-d',strtotime('+1 week last monday')));
|
|
|
+ if($advert['frequency'] == 'day'){
|
|
|
+ if(!Redis::exists('banner:'.$distribution_id.':'.$this->uid)){
|
|
|
+ Redis::setex('banner:'.$distribution_id.':'.$this->uid,($nextDay-$day),1);
|
|
|
+ $advert = self::getBackFormat($advert);
|
|
|
+ return response()->success($advert);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if($advert['frequency'] == 'week'){
|
|
|
+ if(!Redis::exists('banner:'.$distribution_id.':'.$this->uid)){
|
|
|
+ Redis::setex('banner:'.$distribution_id.':'.$this->uid,($nextWeek-$day),1);
|
|
|
+ $advert = self::getBackFormat($advert);
|
|
|
+ return response()->success($advert);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return response()->success([]);
|
|
|
+ }
|
|
|
+
|
|
|
+ if($release_type == '5'){
|
|
|
+ //充值页返回需要判断用户权限
|
|
|
+ $check_user = $this->checkUsers($advert['person']);
|
|
|
+ if(!$check_user){
|
|
|
+ return response()->success([]);
|
|
|
+ }
|
|
|
+ $advert = self::getBackFormat($advert);
|
|
|
+ return response()->success($advert);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //男女频,书架列表
|
|
|
+ $banner = ChannelAdvertService::getAdvertList($distribution_id,$release_type);
|
|
|
+ if($banner->isEmpty()){
|
|
|
+ $banner = $this->getDefaultBanner();
|
|
|
+ return response()->success($banner);
|
|
|
+ }
|
|
|
+
|
|
|
+ $banner->transform(function ($item) {
|
|
|
+ $item->ids = Hashids::encode($item->id);
|
|
|
+ if($item->type == 1){
|
|
|
+ $item->redirect_url = 'views/Reader';
|
|
|
+ $content = explode(';',$item->content);
|
|
|
+ $item->bid = isset($content[2]) ? $content[2] : '';
|
|
|
+ $item->cid = isset($content[3]) ? $content[3] : '';
|
|
|
+ }else{
|
|
|
+ $item->redirect_url = 'views/Detail';
|
|
|
+ }
|
|
|
+ self::getBackFormat($item);
|
|
|
+ return $item;
|
|
|
+ });
|
|
|
+ return response()->success($banner);
|
|
|
+ }
|
|
|
+
|
|
|
+ static function getBackFormat($data)
|
|
|
+ {
|
|
|
+ if(isset($data['id']) || $data->id) unset($data['id'],$data->id);
|
|
|
+ if(isset($data['type']) || $data->type) unset($data['type'],$data->type);
|
|
|
+ if(isset($data['person']) || $data->person) unset($data['person'],$data->person);
|
|
|
+ if(isset($data['frequency']) || $data->frequency) unset($data['frequency'],$data->frequency);
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 默认获取banner
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ protected function getDefaultBanner()
|
|
|
+ {
|
|
|
$reco_banner_type = ['FEMALE', 'PUBLIC'];
|
|
|
- $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
|
|
|
- $books->transform(function ($item) {
|
|
|
+ $banner = (new RecoBannerService)->getByType($reco_banner_type, 2);
|
|
|
+ $banner->transform(function ($item) {
|
|
|
$result = $this->getBidCidFromUrl($item->redirect_url);
|
|
|
$item->bid = $result['bid'];
|
|
|
$item->cid = $result['cid'];
|
|
|
+ $item->ids = Hashids::encode($item->id);
|
|
|
+ $item->type = 'default';
|
|
|
if ($result['cid']) {
|
|
|
$item->redirect_url = "views/Reader";
|
|
|
} else {
|
|
|
$item->redirect_url = "views/Detail";
|
|
|
}
|
|
|
+ unset($item->id);
|
|
|
return $item;
|
|
|
});
|
|
|
- return response()->success($books);
|
|
|
+ return $banner;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -682,9 +786,9 @@ class BookController extends BaseController
|
|
|
*/
|
|
|
public function recommendBooks(Request $request)
|
|
|
{
|
|
|
- $package = $request->header('x-package','');
|
|
|
+ $distribution_id = $this->distribution_channel_id;
|
|
|
$bid = $request->get('bid',0);
|
|
|
- if(empty($package)){
|
|
|
+ if(empty($distribution_id)){
|
|
|
\Log::info('recommendBooks:1');
|
|
|
return response()->success([]);
|
|
|
}
|
|
@@ -695,14 +799,14 @@ class BookController extends BaseController
|
|
|
}
|
|
|
|
|
|
//判断包是否存在
|
|
|
- $package_info = QappPackage::getPackageByPackage($package);
|
|
|
+ $package_info = QappPackage::getPackage($distribution_id);
|
|
|
if(empty($package_info) || !isset($package_info->channel_id)){
|
|
|
\Log::info('recommendBooks:3');
|
|
|
return response()->success([]);
|
|
|
}
|
|
|
|
|
|
//包对应有没有配置开启推荐书单
|
|
|
- $config = ChannelRecommendBookConfigService::getRecommendConfigs($package_info->channel_id);
|
|
|
+ $config = ChannelRecommendBookConfigService::getRecommendConfigs($distribution_id);
|
|
|
if(empty($config) || !isset($config->status) || $config->status == 0){
|
|
|
\Log::info('recommendBooks:4');
|
|
|
return response()->success([]);
|
|
@@ -715,10 +819,10 @@ class BookController extends BaseController
|
|
|
return response()->success([]);
|
|
|
}
|
|
|
|
|
|
- $list = ChannelRecommendBooksService::getRecommendBooks($package_info->channel_id,$bid);
|
|
|
+ $list = ChannelRecommendBooksService::getRecommendBooks($distribution_id,$bid);
|
|
|
if(!$list->isEmpty()){
|
|
|
foreach($list as $key => $item){
|
|
|
- $this->incrRecommendNum($package_info->channel_id,$item->bid);
|
|
|
+ $this->incrRecommendNum($distribution_id,$item->bid);
|
|
|
}
|
|
|
\Log::info('recommendBooks:6');
|
|
|
return response()->collection(new BookTransformer(), $list);
|
|
@@ -734,26 +838,22 @@ class BookController extends BaseController
|
|
|
*/
|
|
|
public function clickRecommendBooks(Request $request)
|
|
|
{
|
|
|
- $package = $request->header('x-package','');
|
|
|
+ $distribution_id = $this->distribution_channel_id;
|
|
|
$bid = $request->get('bid','');
|
|
|
if(empty($bid)){
|
|
|
return response()->success();
|
|
|
}
|
|
|
|
|
|
//判断包是否存在
|
|
|
- $package_info = QappPackage::getPackageByPackage($package);
|
|
|
+ $package_info = QappPackage::getPackage($distribution_id);
|
|
|
if(empty($package_info) || !isset($package_info->channel_id)){
|
|
|
return response()->success([]);
|
|
|
}
|
|
|
$date = date('Ymd');
|
|
|
$bid = Hashids::decode($bid)[0];
|
|
|
|
|
|
- $cacheKey = 'recommend:click:'.$date.':'.$package_info->channel_id.$bid;
|
|
|
- if(!Redis::exists($cacheKey)){
|
|
|
- Redis::set($cacheKey,1);
|
|
|
- }else{
|
|
|
- Redis::incrBy($cacheKey, 1);
|
|
|
- }
|
|
|
+ $cacheKey = 'recommend:click:'.$date.':'.$distribution_id.$bid;
|
|
|
+ $this->incrRedisKey($cacheKey);
|
|
|
return response()->success();
|
|
|
}
|
|
|
|
|
@@ -767,8 +867,50 @@ class BookController extends BaseController
|
|
|
\Log::info($config);
|
|
|
\Log::info('$config->person:'.$config->person);
|
|
|
\Log::info($this->uid);
|
|
|
+
|
|
|
+ $res = $this->checkUsers($config->person);
|
|
|
+ if($res === false){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ //频率判断
|
|
|
+ if($config->frequency == 'back'){
|
|
|
+ //返回即推送
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ $day = strtotime(date('Y-m-d H:i:s'));
|
|
|
+ $nextDay = strtotime( date('Y-m-d'). ' +1 day');
|
|
|
+ $nextWeek = strtotime(date('Y-m-d',strtotime('+1 week last monday')));
|
|
|
+ if($config->frequency == 'day'){
|
|
|
+ //每日推送
|
|
|
+ if(!Redis::exists('recommend:'.$config->channel_id.':'.$this->uid)){
|
|
|
+ Redis::setex('recommend:'.$config->channel_id.':'.$this->uid,($nextDay-$day),1);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ \Log::info('当天已经推送过了');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($config->frequency == 'week'){
|
|
|
+ //每周推送
|
|
|
+ if(!Redis::exists('recommend:'.$config->channel_id.':'.$this->uid)){
|
|
|
+ Redis::setex('recommend:'.$config->channel_id.':'.$this->uid,($nextWeek-$day),1);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ \Log::info('该周已经推送过了');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断用户是否有权限
|
|
|
+ * @param $type
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ protected function checkUsers($type)
|
|
|
+ {
|
|
|
$res = false;
|
|
|
- switch($config->person)
|
|
|
+ switch($type)
|
|
|
{
|
|
|
case 'pay':
|
|
|
//付费用户
|
|
@@ -795,55 +937,79 @@ class BookController extends BaseController
|
|
|
\Log::info('用户是否是付费派单用户:$this->send_order_id:'.$this->send_order_id);
|
|
|
\Log::info($orders);
|
|
|
break;
|
|
|
+ case 'unpaid':
|
|
|
+ $orders = OrderService::getUserLastestOrder($this->uid);
|
|
|
+ if(!$orders){
|
|
|
+ $res = true;
|
|
|
+ }
|
|
|
+ break;
|
|
|
case 'all':
|
|
|
$res = true;
|
|
|
\Log::info('所有用户返回');
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- if($res === false){
|
|
|
- return false;
|
|
|
- }
|
|
|
+ return $res;
|
|
|
+ }
|
|
|
|
|
|
- //频率判断
|
|
|
- if($config->frequency == 'back'){
|
|
|
- //返回即推送
|
|
|
- return true;
|
|
|
+ /**
|
|
|
+ * 推广书籍推荐次数
|
|
|
+ * @param $channel_id
|
|
|
+ * @param $bid
|
|
|
+ */
|
|
|
+ protected function incrRecommendNum($channel_id,$bid)
|
|
|
+ {
|
|
|
+ $date = date('Ymd');
|
|
|
+ $cacheKey = 'recommend:sum:'.$date.':'.$channel_id.$bid;
|
|
|
+ $this->incrRedisKey($cacheKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 点击广告统计
|
|
|
+ * @param Request $request
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function getCheckAdvertisement(Request $request)
|
|
|
+ {
|
|
|
+ $type = $request->get('type','');
|
|
|
+ $id = $request->get('ids','');
|
|
|
+ $distribution_id = $this->distribution_channel_id;
|
|
|
+ if(empty($type) || empty($id) || empty($distribution_id)){
|
|
|
+ return response()->success();
|
|
|
}
|
|
|
|
|
|
- $day = strtotime(date('Y-m-d H:i:s'));
|
|
|
- $nextDay = strtotime( date('Y-m-d'). ' +1 day');
|
|
|
- $nextWeek = strtotime(date('Y-m-d',strtotime('+1 week last monday')));
|
|
|
- if($config->frequency == 'day'){
|
|
|
- //每日推送
|
|
|
- if(!Redis::exists('recommend:'.$config->channel_id.':'.$this->uid)){
|
|
|
- Redis::setex('recommend:'.$config->channel_id.':'.$this->uid,($nextDay-$day),1);
|
|
|
- return true;
|
|
|
- }
|
|
|
- \Log::info('当天已经推送过了');
|
|
|
- return false;
|
|
|
+ $id = Hashids::decode($id)[0];
|
|
|
+ if(!$id){
|
|
|
+ return response()->success();
|
|
|
}
|
|
|
|
|
|
- if($config->frequency == 'week'){
|
|
|
- //每周推送
|
|
|
- if(!Redis::exists('recommend:'.$config->channel_id.':'.$this->uid)){
|
|
|
- Redis::setex('recommend:'.$config->channel_id.':'.$this->uid,($nextWeek-$day),1);
|
|
|
- return true;
|
|
|
+ if($type == 'default'){
|
|
|
+ //默认原表reco_banners广告,区分跟新表channel_advert的id
|
|
|
+ $id = -$id;
|
|
|
+ }else{
|
|
|
+ $advert = ChannelAdvert::find($id);
|
|
|
+ if(!$advert){
|
|
|
+ \Log::info('getCheckAdvertisement:error:不存在该广告');
|
|
|
+ \Log::info(json_encode($request->all()));
|
|
|
+ return response()->success();
|
|
|
}
|
|
|
- \Log::info('该周已经推送过了');
|
|
|
- return false;
|
|
|
}
|
|
|
+
|
|
|
+ $where = ['channel_ad_id' => $id, 'uid' => $this->uid, 'distribution_id' => $distribution_id];
|
|
|
+ ChannelAdUser::firstOrCreate($where);
|
|
|
+
|
|
|
+ $date = date('Ymd');
|
|
|
+ $cacheKey = 'advertisement:pv:'.$date.':'.$distribution_id.$id;
|
|
|
+ $this->incrRedisKey($cacheKey);
|
|
|
+ return response()->success();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 推广书籍推荐次数
|
|
|
- * @param $channel_id
|
|
|
- * @param $bid
|
|
|
+ * redis新增
|
|
|
+ * @param $cacheKey
|
|
|
*/
|
|
|
- protected function incrRecommendNum($channel_id,$bid)
|
|
|
+ protected function incrRedisKey($cacheKey)
|
|
|
{
|
|
|
- $date = date('Ymd');
|
|
|
- $cacheKey = 'recommend:sum:'.$date.':'.$channel_id.$bid;
|
|
|
if(!Redis::exists($cacheKey)){
|
|
|
Redis::set($cacheKey,1);
|
|
|
}else{
|