123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- namespace app\api\controller;
- use app\common\library\Redis;
- use app\common\controller\Api;
- use think\Config;
- class Guide extends Api{
- /**
- * 导粉链接API接口
- * @return \think\response\Json
- */
- public function referral(){
- if(!$this->request->has('referral_id')){
- return json(['status'=>false,'message'=>'参数错误:无推广ID']);
- }
- if(!$referral_info = model('referral')->getone($this->request->param('referral_id'), false)){
- return json(['status'=>false,'message'=>'获取推广链接信息失败']);
- }
- list($channel_id,$isChannel) = $this->getChannelId($referral_info['admin_id']);
- $agent_id = $isChannel ? null : $referral_info['admin_id'];
- $short_id = $referral_info['short_id'] ?? null;
- if($short_id){
- $redirect_url = getCurrentDomain($channel_id,$referral_info['source_url'],['short_id'=>$short_id]);
- }else{
- if($referral_info['source_url']){
- $redirect_url = getCurrentDomain($channel_id,$referral_info['source_url']);
- }else{
- $redirect_url = getCurrentDomain($channel_id,$referral_info['source_url'],['referral_id'=>$referral_info['id']]);
- }
- }
- if(!$adminConfig = model('AdminConfig')->getAdminInfoAll($channel_id)){
- return json(['status'=>false,'message'=>'参数错误:获取渠道信息失败']);
- }
- return json([
- 'status' => true,
- 'data' => [
- 'type' => $referral_info['type'],
- 'subscribe_method' => $adminConfig['subscribe_method'],
- 'book_id' => $referral_info['book_id'],
- 'chapter_id' => $referral_info['chapter_id'],
- 'channel_id' => $channel_id,
- 'agent_id' => $agent_id,
- 'redirect_url' => $redirect_url,
- 'guide_chapter' => model('Guide')->getGuideChapter($agent_id,$referral_info['book_id'],$referral_info['id'],$channel_id),
- 'guide_qrcode' => $this->getGuideQrcode($channel_id),
- 'guide_host' => model('GuideRelation')->getRandGuideHost($channel_id),
- 'guide_title' => $referral_info['guide_title'],
- 'guide_image' => cdnurl($referral_info['guide_image']),
- 'guide_sex' => $referral_info['guide_sex'],
- 'guide_idx' => $referral_info['guide_idx'],
- ],
- 'message' => 'ok'
- ]);
- }
- /**
- * 返回书籍内容
- * @return \think\response\Json
- */
- public function book(){
- if($this->request->has('book_id') && $this->request->has('chapter_id')){
- $bookinfo = model('Book')->BookInfo($this->request->param('book_id'));
- $chapter = model('Book')->getChapterInfo($this->request->param('book_id'), $this->request->param('chapter_id'));
- if($chapter && isset($chapter['data'])){
- $chapter['data']['is_pay'] = $this->bookIsPay($this->request->param('book_id'), $chapter['data']['idx']);
- $chapter['data']['book_status'] = $bookinfo['state'] ?? null;
- return json([
- 'status' => true,
- 'data'=>$chapter['data'],
- 'message' => 'ok'
- ]);
- }else{
- return json([
- 'status' => false,
- 'message' => '获取书籍内容失败'
- ]);
- }
- }else{
- return json(['status'=>false,'message'=>'参数错误']);
- }
- }
- /**
- * 判断收费章节
- * @param $book_id
- * @param $chapter_idx
- * @return bool
- */
- private function bookIsPay($book_id,$chapter_idx){
- //获取书籍信息
- $book = model('Book')->BookInfo($book_id);
- //全局免费章节数
- $free_chapter_idx = config('site.book_free_chapter_num');
- //如果本书籍设置了免费章节数,以本书籍的免费章节数为准
- if(intval($book['free_chapter_num'])>0){
- $free_chapter_idx = $book['free_chapter_num'];
- }
- //容灾处理,如果计费出现问题在后台配置read_free=true则所有书籍都免费
- if(Config::get("site.free")==1){
- return false;
- }
- //此书设置了免费时长并且当前时间在免费时长内则免费阅读
- if(intval($book['free_stime'])<time() && intval($book['free_etime'])>time()){
- return false;
- }
- //到计费章节
- if($chapter_idx > $free_chapter_idx){
- return true;
- }
- //未到计费章节
- if($chapter_idx <= $free_chapter_idx){
- return false;
- }
- }
- /**
- * 获取用户关注状态
- * @return \think\response\Json
- */
- public function subscribe(){
- if($this->request->has('user_id')){
- $user = model('User')->getUserInfo($this->request->param('user_id'));
- $is_sub = $user['is_subscribe'] ?? false;
- return json(['status'=>true,'data'=>['subscribe'=>$is_sub,'message'=>'ok']]);
- }else{
- return json(['status'=>false,'message'=>'参数错误']);
- }
- }
- /**
- * 添加用户阅读记录
- * @return \think\response\Json
- */
- public function readRecord(){
- if($this->request->has('user_id') && $this->request->has('book_id') && $this->request->has('chapter_id')){
- $book_id = $this->request->param('book_id');
- $chapter_id = $this->request->param('chapter_id');
- $user_id = $this->request->param('user_id');
- if($book_id && $chapter_id && $user_id){
- $chapter = model('Book')->getChapterInfo($book_id, $chapter_id);
- $map = ['user_id'=>$user_id,'book_id'=>$book_id];
- $read_exists = model('UserRecentlyRead')->setConnect($user_id)->where($map)->find();
- $read_id = null;
- if($read_exists){
- $read_id = $read_exists['id'];
- $is_ok = model('UserRecentlyRead')->setConnect($user_id)->where($map)->update(['chapter_id'=>$chapter_id,'chapter_name'=>$chapter['data']['name'],'updatetime'=>time()]);
- }else{
- $is_ok = model('UserRecentlyRead')->setConnect($user_id)->insertGetId(array_merge($map,[
- 'chapter_id' => $chapter_id,
- 'chapter_name' => $chapter['data']['name'],
- 'createtime' => time(),
- 'updatetime' => time()
- ]));
- $read_id = $is_ok;
- }
- if($is_ok !== false){
- $redis = Redis::instance();
- $bookinfo = model('Book')->BookInfo($book_id);
- $key = 'U-R:'.$user_id; //最近阅读记录zset结构
- $hkey = 'U-B:'.$user_id.':'.$book_id; //最近阅读阅读记录每条的数据:hash结构
- $read_log['id'] = $read_id;
- $read_log['image'] = $bookinfo['image'];
- $read_log['book_name'] = $bookinfo['name'];
- $read_log['last_chapter_name'] = $bookinfo['last_chapter_name'];
- $read_log['last_chapter_utime'] = $bookinfo['last_chapter_utime'];
- $read_log['state'] = $bookinfo['state'];
- $read_log['user_id'] = $user_id;
- $read_log['book_id'] = $book_id;
- $read_log['chapter_id'] = $chapter_id;
- $read_log['chapter_name'] = $chapter['data']['name'];
- $read_log['createtime'] = time();
- $read_log['updatetime'] = time();
- $redis->hmset($hkey,$read_log);
- $redis->expire($hkey,43200);
- $redis->zrem($key, $hkey);
- $redis->zadd($key, time(), $hkey);
- $redis->expire($key,43200);
- return json(['status'=>true,'message'=>'阅读记录添加成功']);
- }else{
- return json(['status'=>false,'message'=>'阅读记录添加失败']);
- }
- }else{
- return json(['status'=>false,'message'=>'参数错误']);
- }
- }else{
- return json(['status'=>false,'message'=>'参数错误']);
- }
- }
- /**
- * 获取渠道ID,配号代理商返回自己的
- * @param $admin_id
- * @return array
- */
- private function getChannelId($admin_id){
- if(model('AdminConfig')->getAdminInfoAll($admin_id)){
- return [$admin_id,true];
- }else{
- if($extend = model('AdminExtend')->getInfo($admin_id)){
- if($extend['distribute']){
- return [$admin_id,false];
- }else{
- return [$extend['create_by'],false];
- }
- }
- }
- }
- /**
- * 获取倒粉二维码
- * @param $channel_id
- * @return mixed|null
- */
- private function getGuideQrcode($channel_id){
- $df_config = model('AdminConfig')->getAdminInfoAll($channel_id);
- //主服务号倒粉链接
- $df_img = $df_config['qrcode_image'] ?: 'https://open.weixin.qq.com/qr/code?username='.$df_config['json']['authorizer_info']['user_name'];
- //判断关注数有没有到上限
- $Scount = 0;
- $redis = Redis::instance();
- $countKey = 'SC:'.$df_config['admin_id'].':'.date('Ymd',$this->time);
- if($redis->exists($countKey)){
- $Scount = intval($redis->get($countKey));
- }
- //如果是-1的话,会走随机导粉。如果随机导粉公众号为空,降级为不关注
- if($df_config['fans_count'] !=0 ){
- if($Scount>=$df_config['fans_count']){ //如果关注数大于设置的关注数上限
- //查看该代理商有没有设置导粉账号,如果配置了导粉账号,则随机分配导导粉账号
- if($qrcodeImage = model('GuideWx')->getGw($df_config['admin_id'])){
- if(is_array($qrcodeImage)){
- $df_img = cdnurl($qrcodeImage['qrcode_image']);
- }else{
- $df_img = cdnurl($qrcodeImage);
- }
- }else{
- //降级为不倒粉
- $df_img = null;
- }
- }
- }
- return $df_img;
- }
- }
|