123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- <?php
- namespace app\api\library;
- use app\common\constants\Message;
- use app\common\library\Redis;
- use app\common\library\Ua;
- use app\common\service\UserVipExtendService;
- use app\main\constants\CacheConstants;
- use app\main\service\AdminService;
- use app\main\service\AppGuideService;
- use app\main\service\ExportFansService;
- use app\main\service\LogService;
- use app\main\service\UrlService;
- use app\main\service\UserService;
- use think\Config;
- use think\Model;
- use function GuzzleHttp\Psr7\uri_for;
- class ReplayTemplate{
- protected $content;
- protected $channel_id;
- public function __construct($channel_id){
- $this->channel_id = $channel_id;
- }
- /**
- * 签到回复模板
- * @param $title
- * @param $continueBook
- * @param $readLogs
- * @param $recommandBooks
- */
- public function getSignReplayTemplate($title,$readLogs,$recommandBooks, $admin_id = 0, $user){
- $this->setTitle($title."\r\n\r\n");
- if (!$this->setSignReply($admin_id, $user)) {
- if ($readLogs) {
- $book = current($readLogs);
- $this->setContinueReadBook("【点此继续阅读】", $book);
- //二期导粉
- $this->leadConfig($admin_id, $user['id']);
- } else {
- //二期导粉
- $this->leadConfig($admin_id, $user['id']);
- }
- $this->setReadBookLogs($readLogs);
- $this->setRecommandBook($recommandBooks);
- }
- $this->setTopUrl();
- $guideArr = AppGuideService::instance()->getAppGuideInfo($admin_id);
- if (count($guideArr) && (Ua::getOs() != "iOS")) {
- $this->setSignText($admin_id);
- }
- return $this->content;
- }
- /**
- * 二期导粉配置
- * @param $admin_id
- * @param $user_id
- * @return bool
- */
- public function leadConfig($admin_id, $user_id)
- {
- $export = ExportFansService::instance()->checkFansLead($admin_id, $user_id)->data;
- if ($export) {
- if ($export['content']) {
- $url = getCurrentDomain($admin_id, '/index/share/landpage?id='.$export['id'].'&type=sign&from=wx');
- $data = nl2br($export['content']);
- if (strrpos($data, '<br />') != false) {
- foreach (explode('<br />', $data) as $v) {
- $v = preg_replace('|\s|', '', $v);
- if ($v) {
- $this->content .= "<a href='{$url}'>{$v}</a>\r\n";
- }
- }
- $this->content .= "\r\n";
- } else {
- $this->content .= "<a href='{$url}'>{$export['content']}</a>\r\n\r\n";
- }
- }
- return true;
- }
- return false;
- }
- public function setExportFans($admin_id, $user_id)
- {
- $export = ExportFansService::instance()->checkFansSignToExport($admin_id, $user_id)->data;
- if ($export) {
- $url = getCurrentDomain($admin_id, '/index/share/exportfans');
- $this->content .= "劲爆爽文,免费领取,戳我<a href='".$url."'>立即阅读</a>";
- return true;
- }
- return false;
- }
- public function setSignReply($admin_id, $user = null)
- {
- $cacheKey = CacheConstants::getSignReplyCache($admin_id);
- $userOs = UserVipExtendService::instance()->getUserOs($user['id']);
- $cacheKey = $cacheKey.'OS:'.$userOs;
- $content = Redis::instance()->get($cacheKey);
- if ($content !== false) {
- //二期导粉
- if ($content) {
- $this->leadConfig($admin_id, $user['id']);
- }
- $this->content .= $content;
- return $content;
- } else {
- $find = model('wechat_autoreply')->where(['admin_id'=>$admin_id,'title'=>'签到', 'type'=>'text', 'status'=>'normal'])->find();
- if ($find && $find['text_content']) {
- $list = json_decode($find['text_content'], JSON_UNESCAPED_UNICODE);
- $content .= $find['text_tip_word']."\r\n\r\n";
- foreach ($list as $item) {
- if (isset($item['url_type'])) {
- switch ($item['url_type']) {
- case '1'://推广链接
- $url = $item['referral_url'];
- break;
- case '2'://活动链接
- $url = $item['activity_url'];
- break;
- case '3'://最近阅读
- $url = getCurrentDomain($admin_id, '/index/user/recent');
- break;
- default:
- break;
- }
- } else {
- $url = $item['url'];
- }
- $url = UrlService::instance()->replaceReferralHost($admin_id, $url, false)->data;
- $userInfo = UserService::instance()->getUserModel()->getUserInfo($user['id']);
- $channelInfo =AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($userInfo['channel_id']);
- $isBlockIosUser = UserVipExtendService::instance()->isBlockIosUser($channelInfo,$user['id'], $url);
- if(!$isBlockIosUser){
- $content .= "<a href='{$url}'>{$item['title']}</a>\r\n\r\n";
- }
- }
- $this->leadConfig($admin_id, $user['id']);
- $this->content .= $content;
- Redis::instance()->setex($cacheKey, 120, $content);
- return true;
- } else {
- Redis::instance()->setex($cacheKey, 120, '');
- return false;
- }
- }
- }
- /**
- * 自定义二维码推广链接回复模板
- * @param $title
- * @param $url
- */
- public function getQRCodeRefReplayTemplate($title,$url){
- $this->setTitle($title."\r\n\r\n");
- $this->setContinueReadRef($url);
- $this->setTopUrl();
- return $this->content;
- }
- /**
- * 永久二维码关注回复模板
- * @param $title
- * @param $continueBook
- */
- public function getQRCodeReplayTemplate($title,$continueBook){
- $this->setTitle($title."\r\n\r\n");
- $this->setContinueReadBook("点此继续阅读《%s》",$continueBook);
- $this->setTopUrl();
- return $this->content;
- }
- /**
- * 默认关注回复模板
- * @param $title
- * @param $continueBook
- * @param $recommandBooks
- */
- public function getFollowReplayTemplate($title,$continueBook,$recommandBooks){
- if($continueBook){
- $this->setTitle($title."\r\n\r\n");
- }else{
- $this->setTitle($title);
- }
- $this->setContinueReadBook("【点此继续阅读】",$continueBook);
- $this->setRecommandBook($recommandBooks);
- $this->setTopUrl();
- $switch = AdminService::instance()->getSubscribeSwitchCache($this->channel_id);
- if (!empty($switch) && $switch['status'] == 1) {
- if ($switch['type'] == 1) {//系统默认
- $this->setSignTip();
- } else {//自定义
- $we = Redis::instance()->get('OSCCT:' . $this->channel_id);
- if (empty($we)) {
- $where = [];
- $where['admin_id'] = ['eq', $this->channel_id];
- $where['status'] = ['eq', 'normal'];
- $where['type'] = ['eq', 'text'];
- $where['starttime'] = ['<=', time()];
- $where['endtime'] = ['>', time()];
- $wechatSubcribeConfigObj = model('WechatSubscribeConfig')->where($where)->order('id', 'desc')->find();
- if (!empty($wechatSubcribeConfigObj)) {
- $we = $wechatSubcribeConfigObj->getData();
- } else {
- $we['sign_type'] = 1;
- }
- if (isset($we['endtime'])) {
- if ($we['endtime'] - time() >= 604800) {
- $expire = 604800;
- } else {
- if ($we['endtime'] > time()) {
- $expire = $we['endtime'] - time();
- } else {
- $expire = 0;
- }
- }
- } else {
- $expire = 604800;
- }
- Redis::instance()->setex('OSCCT:' . $this->channel_id, $expire,
- json_encode($we, JSON_UNESCAPED_UNICODE));
- } else {
- $we = json_decode($we, true);
- }
- if (!empty($we) && isset($we['sign_type']) && $we['sign_type'] == 2) {
- $this->content .= "\r\n\r\n";
- $this->content .= "<a href='weixin://bizmsgmenu?msgmenucontent=" . Message::MESSAGE_KEYWORD . "&msgmenuid={{" . time() . "}}\"'>{$we['sign_txt']}</a>";
- } else {
- $this->setSignTip();
- }
- }
- } else {
- $this->setSignTip();
- }
- return $this->content;
- }
- /**
- * 设置title
- * @param $title
- */
- protected function setTitle($title){
- $this->content.= $title;
- }
- /**
- * 设置推广链接继续阅读
- * @param $url
- * @param $title
- */
- protected function setContinueReadRef($url,$title = null){
- $this->content .= "<a href='{$url}'>".($title ?? "【点此继续阅读】")."</a>\r\n\r\n";
- }
- /**
- * 设置继续阅读
- * @param $book
- */
- protected function setContinueReadBook($tpl,$book){
- if($book){
- $params['book_id'] = $book['book_id'];
- $params['sid'] = $book['chapter_id'];
- $url = getCurrentDomain($this->channel_id,'/index/book/chapter',$params);
- $this->content .= "<a href='{$url}'>".sprintf($tpl,$book['book_name'])."</a>\r\n\r\n";
- }
- }
- /**
- * 设置阅读记录
- * @param $readLogs
- */
- protected function setReadBookLogs($readLogs){
- //阅读记录
- if($readLogs){
- $this->content.= "历史阅读记录:\r\n\r\n";
- foreach($readLogs as $book){
- $params['book_id'] = $book['book_id'];
- $params['sid'] = $book['chapter_id'];
- $url = getCurrentDomain($this->channel_id,'/index/book/chapter',$params);
- $this->content .= "<a href='{$url}'>{$book['book_name']}</a>\r\n\r\n";
- }
- }
- }
- /**
- * 设置推荐书籍
- * @param $recommandBook
- */
- protected function setRecommandBook($recommandBook){
- if($recommandBook){
- $this->content.= "推荐几本热门的小说,给你看看:\r\n\r\n";
- foreach($recommandBook as $book){
- $params['book_id'] = $book['book_id'];
- $url = getCurrentDomain($this->channel_id,'/index/book/chapter',$params);
- $this->content .= "<a href='{$url}'>{$book['book_name']}</a>\r\n\r\n";
- }
- }
- }
- /**
- * 设置置顶URL
- * @param string $url
- */
- protected function setTopUrl($url = ''){
- if(!$url){
- $top = model('ManageKey')->getOneByKey('top');
- if($top){
- $url .= $top['image'];
- }
- }
- $this->content .= "为方便下次阅读,请<a href='".cdnurl($url)."'>置顶公众号</a>";
- }
- /**
- * 设置签到提示文字(关注时自动回复)
- */
- protected function setSignTip(){
- $kandian = Config::get('site.kandian_sign');
- if($kandian){
- $this->content .= "\r\n\r\n回复“签到”或点击“签到送币”菜单,最高获得{$kandian}书币";
- }
- }
- /**
- * 下载APP客户端
- */
- protected function setSignText($admin_id)
- {
- if($admin_id){
- $guide_data = AppGuideService::instance()->getAppGuideInfo($admin_id);
- if (!empty($guide_data['replysigntxt'])) {
- $link_text = $guide_data['replysigntxt'];
- //$url = getCurrentDomain($this->channel_id, '/clientweb/signagent/index');
- $url = $guide_data['app_url'];
- $this->content .= "\r\n\r\n<a href='{$url}'>{$link_text}</a>";
- }
- }
- }
- }
|