ReplayTemplate.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace app\api\library;
  3. use app\common\constants\Message;
  4. use app\common\library\Redis;
  5. use app\common\library\Ua;
  6. use app\common\service\UserVipExtendService;
  7. use app\main\constants\CacheConstants;
  8. use app\main\service\AdminService;
  9. use app\main\service\AppGuideService;
  10. use app\main\service\ExportFansService;
  11. use app\main\service\LogService;
  12. use app\main\service\UrlService;
  13. use app\main\service\UserService;
  14. use think\Config;
  15. use think\Model;
  16. use function GuzzleHttp\Psr7\uri_for;
  17. class ReplayTemplate{
  18. protected $content;
  19. protected $channel_id;
  20. public function __construct($channel_id){
  21. $this->channel_id = $channel_id;
  22. }
  23. /**
  24. * 签到回复模板
  25. * @param $title
  26. * @param $continueBook
  27. * @param $readLogs
  28. * @param $recommandBooks
  29. */
  30. public function getSignReplayTemplate($title,$readLogs,$recommandBooks, $admin_id = 0, $user){
  31. $this->setTitle($title."\r\n\r\n");
  32. if (!$this->setSignReply($admin_id, $user)) {
  33. if ($readLogs) {
  34. $book = current($readLogs);
  35. $this->setContinueReadBook("【点此继续阅读】", $book);
  36. //二期导粉
  37. $this->leadConfig($admin_id, $user['id']);
  38. } else {
  39. //二期导粉
  40. $this->leadConfig($admin_id, $user['id']);
  41. }
  42. $this->setReadBookLogs($readLogs);
  43. $this->setRecommandBook($recommandBooks);
  44. }
  45. $this->setTopUrl();
  46. $guideArr = AppGuideService::instance()->getAppGuideInfo($admin_id);
  47. if (count($guideArr) && (Ua::getOs() != "iOS")) {
  48. $this->setSignText($admin_id);
  49. }
  50. return $this->content;
  51. }
  52. /**
  53. * 二期导粉配置
  54. * @param $admin_id
  55. * @param $user_id
  56. * @return bool
  57. */
  58. public function leadConfig($admin_id, $user_id)
  59. {
  60. $export = ExportFansService::instance()->checkFansLead($admin_id, $user_id)->data;
  61. if ($export) {
  62. if ($export['content']) {
  63. $url = getCurrentDomain($admin_id, '/index/share/landpage?id='.$export['id'].'&type=sign&from=wx');
  64. $data = nl2br($export['content']);
  65. if (strrpos($data, '<br />') != false) {
  66. foreach (explode('<br />', $data) as $v) {
  67. $v = preg_replace('|\s|', '', $v);
  68. if ($v) {
  69. $this->content .= "<a href='{$url}'>{$v}</a>\r\n";
  70. }
  71. }
  72. $this->content .= "\r\n";
  73. } else {
  74. $this->content .= "<a href='{$url}'>{$export['content']}</a>\r\n\r\n";
  75. }
  76. }
  77. return true;
  78. }
  79. return false;
  80. }
  81. public function setExportFans($admin_id, $user_id)
  82. {
  83. $export = ExportFansService::instance()->checkFansSignToExport($admin_id, $user_id)->data;
  84. if ($export) {
  85. $url = getCurrentDomain($admin_id, '/index/share/exportfans');
  86. $this->content .= "劲爆爽文,免费领取,戳我<a href='".$url."'>立即阅读</a>";
  87. return true;
  88. }
  89. return false;
  90. }
  91. public function setSignReply($admin_id, $user = null)
  92. {
  93. $cacheKey = CacheConstants::getSignReplyCache($admin_id);
  94. $userOs = UserVipExtendService::instance()->getUserOs($user['id']);
  95. $cacheKey = $cacheKey.'OS:'.$userOs;
  96. $content = Redis::instance()->get($cacheKey);
  97. if ($content !== false) {
  98. //二期导粉
  99. if ($content) {
  100. $this->leadConfig($admin_id, $user['id']);
  101. }
  102. $this->content .= $content;
  103. return $content;
  104. } else {
  105. $find = model('wechat_autoreply')->where(['admin_id'=>$admin_id,'title'=>'签到', 'type'=>'text', 'status'=>'normal'])->find();
  106. if ($find && $find['text_content']) {
  107. $list = json_decode($find['text_content'], JSON_UNESCAPED_UNICODE);
  108. $content .= $find['text_tip_word']."\r\n\r\n";
  109. foreach ($list as $item) {
  110. if (isset($item['url_type'])) {
  111. switch ($item['url_type']) {
  112. case '1'://推广链接
  113. $url = $item['referral_url'];
  114. break;
  115. case '2'://活动链接
  116. $url = $item['activity_url'];
  117. break;
  118. case '3'://最近阅读
  119. $url = getCurrentDomain($admin_id, '/index/user/recent');
  120. break;
  121. default:
  122. break;
  123. }
  124. } else {
  125. $url = $item['url'];
  126. }
  127. $url = UrlService::instance()->replaceReferralHost($admin_id, $url, false)->data;
  128. $userInfo = UserService::instance()->getUserModel()->getUserInfo($user['id']);
  129. $channelInfo =AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($userInfo['channel_id']);
  130. $isBlockIosUser = UserVipExtendService::instance()->isBlockIosUser($channelInfo,$user['id'], $url);
  131. if(!$isBlockIosUser){
  132. $content .= "<a href='{$url}'>{$item['title']}</a>\r\n\r\n";
  133. }
  134. }
  135. $this->leadConfig($admin_id, $user['id']);
  136. $this->content .= $content;
  137. Redis::instance()->setex($cacheKey, 120, $content);
  138. return true;
  139. } else {
  140. Redis::instance()->setex($cacheKey, 120, '');
  141. return false;
  142. }
  143. }
  144. }
  145. /**
  146. * 自定义二维码推广链接回复模板
  147. * @param $title
  148. * @param $url
  149. */
  150. public function getQRCodeRefReplayTemplate($title,$url){
  151. $this->setTitle($title."\r\n\r\n");
  152. $this->setContinueReadRef($url);
  153. $this->setTopUrl();
  154. return $this->content;
  155. }
  156. /**
  157. * 永久二维码关注回复模板
  158. * @param $title
  159. * @param $continueBook
  160. */
  161. public function getQRCodeReplayTemplate($title,$continueBook){
  162. $this->setTitle($title."\r\n\r\n");
  163. $this->setContinueReadBook("点此继续阅读《%s》",$continueBook);
  164. $this->setTopUrl();
  165. return $this->content;
  166. }
  167. /**
  168. * 默认关注回复模板
  169. * @param $title
  170. * @param $continueBook
  171. * @param $recommandBooks
  172. */
  173. public function getFollowReplayTemplate($title,$continueBook,$recommandBooks){
  174. if($continueBook){
  175. $this->setTitle($title."\r\n\r\n");
  176. }else{
  177. $this->setTitle($title);
  178. }
  179. $this->setContinueReadBook("【点此继续阅读】",$continueBook);
  180. $this->setRecommandBook($recommandBooks);
  181. $this->setTopUrl();
  182. $switch = AdminService::instance()->getSubscribeSwitchCache($this->channel_id);
  183. if (!empty($switch) && $switch['status'] == 1) {
  184. if ($switch['type'] == 1) {//系统默认
  185. $this->setSignTip();
  186. } else {//自定义
  187. $we = Redis::instance()->get('OSCCT:' . $this->channel_id);
  188. if (empty($we)) {
  189. $where = [];
  190. $where['admin_id'] = ['eq', $this->channel_id];
  191. $where['status'] = ['eq', 'normal'];
  192. $where['type'] = ['eq', 'text'];
  193. $where['starttime'] = ['<=', time()];
  194. $where['endtime'] = ['>', time()];
  195. $wechatSubcribeConfigObj = model('WechatSubscribeConfig')->where($where)->order('id', 'desc')->find();
  196. if (!empty($wechatSubcribeConfigObj)) {
  197. $we = $wechatSubcribeConfigObj->getData();
  198. } else {
  199. $we['sign_type'] = 1;
  200. }
  201. if (isset($we['endtime'])) {
  202. if ($we['endtime'] - time() >= 604800) {
  203. $expire = 604800;
  204. } else {
  205. if ($we['endtime'] > time()) {
  206. $expire = $we['endtime'] - time();
  207. } else {
  208. $expire = 0;
  209. }
  210. }
  211. } else {
  212. $expire = 604800;
  213. }
  214. Redis::instance()->setex('OSCCT:' . $this->channel_id, $expire,
  215. json_encode($we, JSON_UNESCAPED_UNICODE));
  216. } else {
  217. $we = json_decode($we, true);
  218. }
  219. if (!empty($we) && isset($we['sign_type']) && $we['sign_type'] == 2) {
  220. $this->content .= "\r\n\r\n";
  221. $this->content .= "<a href='weixin://bizmsgmenu?msgmenucontent=" . Message::MESSAGE_KEYWORD . "&msgmenuid={{" . time() . "}}\"'>{$we['sign_txt']}</a>";
  222. } else {
  223. $this->setSignTip();
  224. }
  225. }
  226. } else {
  227. $this->setSignTip();
  228. }
  229. return $this->content;
  230. }
  231. /**
  232. * 设置title
  233. * @param $title
  234. */
  235. protected function setTitle($title){
  236. $this->content.= $title;
  237. }
  238. /**
  239. * 设置推广链接继续阅读
  240. * @param $url
  241. * @param $title
  242. */
  243. protected function setContinueReadRef($url,$title = null){
  244. $this->content .= "<a href='{$url}'>".($title ?? "【点此继续阅读】")."</a>\r\n\r\n";
  245. }
  246. /**
  247. * 设置继续阅读
  248. * @param $book
  249. */
  250. protected function setContinueReadBook($tpl,$book){
  251. if($book){
  252. $params['book_id'] = $book['book_id'];
  253. $params['sid'] = $book['chapter_id'];
  254. $url = getCurrentDomain($this->channel_id,'/index/book/chapter',$params);
  255. $this->content .= "<a href='{$url}'>".sprintf($tpl,$book['book_name'])."</a>\r\n\r\n";
  256. }
  257. }
  258. /**
  259. * 设置阅读记录
  260. * @param $readLogs
  261. */
  262. protected function setReadBookLogs($readLogs){
  263. //阅读记录
  264. if($readLogs){
  265. $this->content.= "历史阅读记录:\r\n\r\n";
  266. foreach($readLogs as $book){
  267. $params['book_id'] = $book['book_id'];
  268. $params['sid'] = $book['chapter_id'];
  269. $url = getCurrentDomain($this->channel_id,'/index/book/chapter',$params);
  270. $this->content .= "<a href='{$url}'>{$book['book_name']}</a>\r\n\r\n";
  271. }
  272. }
  273. }
  274. /**
  275. * 设置推荐书籍
  276. * @param $recommandBook
  277. */
  278. protected function setRecommandBook($recommandBook){
  279. if($recommandBook){
  280. $this->content.= "推荐几本热门的小说,给你看看:\r\n\r\n";
  281. foreach($recommandBook as $book){
  282. $params['book_id'] = $book['book_id'];
  283. $url = getCurrentDomain($this->channel_id,'/index/book/chapter',$params);
  284. $this->content .= "<a href='{$url}'>{$book['book_name']}</a>\r\n\r\n";
  285. }
  286. }
  287. }
  288. /**
  289. * 设置置顶URL
  290. * @param string $url
  291. */
  292. protected function setTopUrl($url = ''){
  293. if(!$url){
  294. $top = model('ManageKey')->getOneByKey('top');
  295. if($top){
  296. $url .= $top['image'];
  297. }
  298. }
  299. $this->content .= "为方便下次阅读,请<a href='".cdnurl($url)."'>置顶公众号</a>";
  300. }
  301. /**
  302. * 设置签到提示文字(关注时自动回复)
  303. */
  304. protected function setSignTip(){
  305. $kandian = Config::get('site.kandian_sign');
  306. if($kandian){
  307. $this->content .= "\r\n\r\n回复“签到”或点击“签到送币”菜单,最高获得{$kandian}书币";
  308. }
  309. }
  310. /**
  311. * 下载APP客户端
  312. */
  313. protected function setSignText($admin_id)
  314. {
  315. if($admin_id){
  316. $guide_data = AppGuideService::instance()->getAppGuideInfo($admin_id);
  317. if (!empty($guide_data['replysigntxt'])) {
  318. $link_text = $guide_data['replysigntxt'];
  319. //$url = getCurrentDomain($this->channel_id, '/clientweb/signagent/index');
  320. $url = $guide_data['app_url'];
  321. $this->content .= "\r\n\r\n<a href='{$url}'>{$link_text}</a>";
  322. }
  323. }
  324. }
  325. }