ReplyVipService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2020/3/31
  6. * Time: 下午3:28
  7. */
  8. namespace app\main\service;
  9. use app\admin\service\CustomService;
  10. use app\common\library\Redis;
  11. use app\main\constants\AdminConstants;
  12. use app\main\constants\CacheConstants;
  13. use app\main\constants\ErrorCodeConstants;
  14. use app\main\constants\SubscribeDelayConstants;
  15. use app\main\constants\WechatSubscribeConstants;
  16. use app\main\model\object\ReturnObject;
  17. class ReplyVipService extends BaseService
  18. {
  19. const TYPE_DEFAULT = 1;
  20. const TYPE_TEXT = 2;
  21. const URL_REFERRAL = 1;
  22. const URL_ACTIVITY = 2;
  23. const URL_RECENTLY = 3;
  24. /**
  25. * 定义属性
  26. *
  27. * @var ReplyVipService
  28. */
  29. protected static $self = null;
  30. /**
  31. * 返回实例
  32. *
  33. * @return ReplyVipService
  34. */
  35. public static function instance()
  36. {
  37. if (self::$self == null) {
  38. self::$self = new self();
  39. }
  40. return self::$self;
  41. }
  42. /**
  43. * 关键字回复
  44. * @param $params
  45. * @param $vipId
  46. * @return ReturnObject
  47. */
  48. public function createReply($params, $vipId)
  49. {
  50. $params['admin_group'] = [];
  51. if ($params['deploy_type'] == 2) {
  52. //过滤空值
  53. $params['selected'] = array_filter($params['selected'], function($value){
  54. return $value;
  55. });
  56. $params['admin_group'] = array_keys($params['selected']);
  57. unset($params['selected']);
  58. }
  59. $channelIds = SubscribeVipDelayService::instance()->getVipChannelIds($vipId, $params['admin_group'])->data;
  60. if (!$channelIds) {
  61. return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg('未查询到可以同步的渠道')->getReturn();
  62. }
  63. $params['text_tip_word'] = json_encode([
  64. 'text_split' => $params['text_split'],
  65. 'text_tip' => $params['text_tip'],
  66. ], JSON_UNESCAPED_UNICODE);
  67. $updateChannelDelay = [];
  68. $error = '';
  69. foreach ($channelIds as $channelId) {
  70. if ($channelId != -1) {
  71. $return = $this->updateReply($params, $channelId);
  72. if ($return->code == ErrorCodeConstants::SUCCESS) {
  73. $updateChannelDelay[] = $return->data;
  74. } else {
  75. $error = $return->msg;
  76. }
  77. } else {
  78. $error = '未查询到需要同步的渠道';
  79. }
  80. }
  81. if (!$updateChannelDelay) {
  82. if (!$error) {
  83. $error = '更新失败';
  84. }
  85. return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg($error)->getReturn();
  86. }
  87. $result = $this->updateReply($params, $vipId, true);
  88. if ($result->code == ErrorCodeConstants::SUCCESS) {
  89. model('wechat_autoreply')->whereIn('id', $updateChannelDelay)->update(['pid' => $result->data]);
  90. return $this->setData($result->data)->getReturn();
  91. }
  92. return $this->setCode(ErrorCodeConstants::DB_ERROR_UPDATE)->setMsg('更新失败')->getReturn();
  93. }
  94. /**
  95. * @param $params
  96. * @param $channel_id
  97. * @param $is_vip
  98. * @return ReturnObject
  99. */
  100. public function updateReply($params, $channel_id, $is_vip = false)
  101. {
  102. $where = [
  103. 'admin_id' => $channel_id,
  104. 'title' => $params['keywords'],
  105. ];
  106. if ($is_vip) {
  107. if (!empty($params['id'])) {
  108. $where = ['id'=>$params['id']];
  109. $row = model('wechat_autoreply')->where('id', $params['id'])->find();
  110. } else {
  111. $row = model('wechat_autoreply')->where($where)->find();
  112. }
  113. } else {
  114. $row = false;
  115. if (!empty($params['id'])) {
  116. $where = ['pid'=>$params['id']];
  117. $row = model('wechat_autoreply')
  118. ->where('pid', $params['id'])
  119. ->where('admin_id', $channel_id)
  120. ->find();
  121. }
  122. if (!$row) {
  123. $row = model('wechat_autoreply')->where($where)->find();
  124. }
  125. }
  126. //更新的数据
  127. $data = [];
  128. if ($params['resource_type'] == 2) {
  129. $data['type'] = 'text';
  130. } else {
  131. $data['type'] = 'news';
  132. }
  133. $data['status'] = 'normal';
  134. $data['title'] = $data['text'] = $params['keywords'];
  135. if (!$is_vip) {
  136. //文字模式
  137. if ($data['type'] == 'text') {
  138. $data['text_tip_word'] = $params['text_tip_word'];
  139. $text_content = [];
  140. $success = false;
  141. $textList = $params['text_content'];
  142. foreach ($textList as $text) {
  143. //小说
  144. if ($text['image_type'] == 1) {
  145. $content = [
  146. 'image_type' => (int)$text['image_type'],
  147. 'book_id' => (string)$text['book_id'],
  148. 'chapter_id' => (string)$text['chapter_id'],
  149. 'title' => $text['title']
  150. ];
  151. $content['url'] = getCurrentDomain($channel_id, '/index/book/chapter?book_id=' . $text['book_id'] . '&chapter_id=' . $text['chapter_id']);
  152. $success = true;
  153. //分流链接
  154. } else {
  155. $referralId = ReferralService::instance()->getChannelReferralFromVip($text['v_id'], $channel_id)->data;
  156. if (!$referralId) {
  157. continue;
  158. }
  159. $content = [
  160. 'title' => $text['title'],
  161. 'image_type' => (int)$text['image_type'],
  162. ];
  163. $success = true;
  164. $content['url'] = getCurrentDomain($channel_id, '/t/' . $referralId);
  165. }
  166. $text_content[] = $content;
  167. }
  168. if (!$success) {
  169. return $this->setCode(ErrorCodeConstants::RESULT_EMPTY)->setMsg('链接无效')->getReturn();
  170. }
  171. $data['text_content'] = json_encode($text_content, JSON_UNESCAPED_UNICODE);
  172. $data['text_content'] = UrlService::instance()->replaceReferralHost($channel_id, $data['text_content'])->data;
  173. } else {
  174. $news_content = [];
  175. $success = false;
  176. $newsList = $params['news_content'];
  177. foreach ($newsList as $news) {
  178. //文本内容
  179. if ($news['image_type'] == 1) {
  180. $content = [
  181. 'image_type' => (int)$news['image_type'],
  182. 'book_id' => (string)$news['book_id'],
  183. 'chapter_id' => (string)$news['chapter_id'],
  184. 'title' => $news['title'],
  185. 'image' => $news['image'],
  186. 'description' => $news['description'],
  187. ];
  188. $content['url'] = getCurrentDomain($channel_id, '/index/book/chapter?book_id=' . $news['book_id'] . '&chapter_id=' . $news['chapter_id']);
  189. $content['referral_url'] = $content['activity_url'] = '';
  190. $success = true;
  191. } else if ($news['image_type'] == 2) {
  192. $url = CustomService::instance()->_buildVipActivityUrl($channel_id, $news['activity_id']);
  193. if (!$url) {
  194. continue;
  195. }
  196. $content = [
  197. 'image_type' => (int)$news['image_type'],
  198. 'title' => $news['title'],
  199. 'image' => $news['image'],
  200. 'url' => $url
  201. ];
  202. $content['activity_url'] = $content['url'];
  203. $content['referral_url'] = '';
  204. $success = true;
  205. } else {
  206. $referralId = ReferralService::instance()->getChannelReferralFromVip($news['v_id'], $channel_id)->data;
  207. if (!$referralId) {
  208. continue;
  209. }
  210. $content = [
  211. 'image_type' => (int)$news['image_type'],
  212. 'title' => $news['title'],
  213. 'description' => $news['description'],
  214. 'image' => $news['image'],
  215. 'url' => getCurrentDomain($channel_id, '/t/' . $referralId)
  216. ];
  217. $content['referral_url'] = $content['url'];
  218. $content['activity_url'] = '';
  219. $success = true;
  220. }
  221. $news_content[] = $content;
  222. }
  223. if (!$success) {
  224. return $this->setCode(ErrorCodeConstants::RESULT_EMPTY)->setMsg('链接无效')->getReturn();
  225. }
  226. $data['news_content'] = json_encode($news_content, JSON_UNESCAPED_UNICODE);
  227. $data['news_content'] = UrlService::instance()->replaceReferralHost($channel_id, $data['news_content'])->data;
  228. }
  229. } else {
  230. if ($data['type'] == 'text') {
  231. $data['text_content'] = json_encode($params, JSON_UNESCAPED_UNICODE);
  232. $data['text_content'] = UrlService::instance()->replaceReferralHost($channel_id, $data['text_content'])->data;
  233. } else {
  234. $data['news_content'] = json_encode($params, JSON_UNESCAPED_UNICODE);
  235. $data['news_content'] = UrlService::instance()->replaceReferralHost($channel_id, $data['news_content'])->data;
  236. }
  237. }
  238. if ($row) {
  239. $id = $row['id'];
  240. $data['updatetime'] = time();
  241. model('wechat_autoreply')->update($data, ['id'=>$id]);
  242. $event_keys = json_decode($row['event_keys'], true);
  243. } else {
  244. $insert = array_merge($data, $where);
  245. //新增设置
  246. $insert['createtime'] = $insert['updatetime'] = time();
  247. $event_keys = [
  248. 'text' => $channel_id . '_' . uniqid(),
  249. 'news' => $channel_id . '_' . uniqid(),
  250. ];
  251. $insert['event_keys'] = json_encode($event_keys, JSON_UNESCAPED_UNICODE);
  252. $id = model('wechat_autoreply')->insertGetId($insert);
  253. }
  254. if (!$is_vip) {
  255. foreach ($event_keys as $type => $event_key) {
  256. $update = [];
  257. switch ($type) {
  258. case WechatSubscribeConstants::SUBCRIBE_TYPE_TEXT:
  259. if (!empty($data['text_content'])) {
  260. //组织内容
  261. $resourceContent = $this->getTextContent($channel_id, $data['text_content'], $params['text_split'], $params['text_tip']);
  262. $update = [
  263. 'type' => 'text',
  264. 'title' => '文字类型回复',
  265. 'content' => $resourceContent,
  266. 'updatetime' => time(),
  267. 'status' => 'normal',
  268. ];
  269. }
  270. break;
  271. default:
  272. if (!empty($data['news_content'])) {
  273. $update = [
  274. 'type' => 'news',
  275. 'title' => '图文类型回复',
  276. 'content' => $data['news_content'],
  277. 'updatetime' => time(),
  278. ];
  279. }
  280. break;
  281. }
  282. $where = [
  283. 'admin_id' => $channel_id,
  284. 'eventkey' => $event_key,
  285. ];
  286. if ($update) {
  287. $update['status'] = 'normal';
  288. if (!model("WechatResponse")->where($where)->find()) {
  289. $insert = array_merge($where, $update, ['createtime' => time()]);
  290. model("WechatResponse")->insert($insert);
  291. } else {
  292. model("WechatResponse")->update($update, $where);
  293. }
  294. }
  295. }
  296. }
  297. $cacheKey = CacheConstants::getSignReplyCache($channel_id);
  298. Redis::instance()->del($cacheKey);
  299. return $this->setData($id)->getReturn();
  300. }
  301. public function getTextContent($channel_id, $content, $text_split, $text_tip)
  302. {
  303. $temp = WechatSubscribeConstants::AUTOREPLY_TEXT_TEMPLATE;
  304. $bookTemp = WechatSubscribeConstants::SUBCRIBE_TEXT_BOOK_TEMPLATE;
  305. $bookSets = json_decode($content, true);
  306. $contentStr = [];
  307. $resourceContent = '';
  308. if (is_array($bookSets)) {
  309. foreach ($bookSets as $index => $bookSet) {
  310. if ($index == 1 && $text_split) {
  311. $contentStr[] = $text_split;
  312. }
  313. $url = $bookSet['url'];
  314. $title = $bookSet['title'];
  315. $contentStr[] = str_replace(['{URL}', '{TITLE}'], [$url, $title], $bookTemp);
  316. }
  317. $wxJson = model("AdminConfig")
  318. ->where('admin_id', $channel_id)
  319. ->value('json');
  320. $gzhName = '';
  321. if (!empty($wxJson)) {
  322. $wxInfo = json_decode($wxJson, true);
  323. $gzhName = $wxInfo['authorizer_info']['nick_name'];
  324. }
  325. $resourceContent = str_replace(['{TEXT_TIP}', '$gzh_name', '{CONTENT}'], [$text_tip, $gzhName, implode("\r\n\r\n", $contentStr)], $temp);
  326. }
  327. return $resourceContent;
  328. }
  329. public function createSignReply($params, $vipId)
  330. {
  331. $params['admin_group'] = [];
  332. if ($params['deploy_type'] == 2) {
  333. //过滤空值
  334. $params['selected'] = array_filter($params['selected'], function($value){
  335. return $value;
  336. });
  337. $params['admin_group'] = array_keys($params['selected']);
  338. unset($params['selected']);
  339. }
  340. $channelIds = SubscribeVipDelayService::instance()->getVipChannelIds($vipId, $params['admin_group'])->data;
  341. if (!$channelIds) {
  342. return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg('未查询到可以同步的渠道')->getReturn();
  343. }
  344. $updateChannelDelay = [];
  345. $error = '';
  346. unset($params['id']);
  347. foreach ($channelIds as $channelId) {
  348. if ($channelId != -1) {
  349. $return = $this->updateSignReply($params, $channelId);
  350. if ($return->code == ErrorCodeConstants::SUCCESS) {
  351. $updateChannelDelay[] = $return->data;
  352. } else {
  353. $error = $return->msg;
  354. }
  355. } else {
  356. $error = '未查询到需要同步的渠道';
  357. }
  358. }
  359. if (!$updateChannelDelay) {
  360. if (!$error) {
  361. $error = '更新失败';
  362. }
  363. return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg($error)->getReturn();
  364. }
  365. $result = $this->updateSignReply($params, $vipId, true);
  366. if ($result->code == ErrorCodeConstants::SUCCESS) {
  367. model('wechat_autoreply')->whereIn('id', $updateChannelDelay)->update(['pid' => $result->data]);
  368. return $this->setData($result->data)->getReturn();
  369. }
  370. return $this->setCode(ErrorCodeConstants::DB_ERROR_UPDATE)->setMsg('更新失败')->getReturn();
  371. }
  372. /**
  373. * @param $params
  374. * @param $channel_id
  375. * @param $is_vip
  376. * @return ReturnObject
  377. */
  378. public function updateSignReply($params, $channel_id, $is_vip = false)
  379. {
  380. $where = [
  381. 'admin_id' => $channel_id,
  382. 'title' => '签到',
  383. ];
  384. $row = model('wechat_autoreply')->where($where)->find();
  385. //更新的数据
  386. $data = [
  387. 'type' => 'text',
  388. 'text_tip_word' => $params['text_split'],
  389. 'updatetime' => time(),
  390. ];
  391. if ($params['type'] == self::TYPE_DEFAULT) {
  392. $data['status'] = 'hidden';
  393. if ($is_vip) {
  394. $data['text_content'] = json_encode($params, JSON_UNESCAPED_UNICODE);
  395. }
  396. } else {
  397. $data['status'] = 'normal';
  398. $textList = $params['text'];
  399. if (!$is_vip) {
  400. $text_content = [];
  401. $success = false;
  402. foreach ($textList as $text) {
  403. //文本内容
  404. $content = [
  405. 'url_type' => $text['url_type'],
  406. 'title' => $text['title'],
  407. 'referral_url' => '',
  408. 'activity_url' => '',
  409. ];
  410. if ($text['url_type'] == self::URL_REFERRAL) {
  411. $referralId = ReferralService::instance()->getChannelReferralFromVip($text['v_id'], $channel_id)->data;
  412. if (!$referralId) {
  413. continue;
  414. }
  415. $success = true;
  416. $content['referral_url'] = getCurrentDomain($channel_id, '/t/' . $referralId);
  417. } elseif ($text['url_type'] == self::URL_ACTIVITY) {
  418. $url = CustomService::instance()->_buildVipActivityUrl($channel_id, $text['activity_id']);
  419. if (!$url) {
  420. continue;
  421. }
  422. $success = true;
  423. $content['activity_url'] = $url;
  424. } elseif ($text['url_type'] == self::URL_RECENTLY) {
  425. $success = true;
  426. }
  427. $text_content[] = $content;
  428. }
  429. if (!$success) {
  430. return $this->setCode(ErrorCodeConstants::RESULT_EMPTY)->setMsg('链接无效')->getReturn();
  431. }
  432. $data['text_content'] = json_encode($text_content, JSON_UNESCAPED_UNICODE);
  433. $data['text_content'] = UrlService::instance()->replaceReferralHost($channel_id, $data['text_content'])->data;
  434. } else {
  435. $data['text_content'] = json_encode($params, JSON_UNESCAPED_UNICODE);
  436. }
  437. }
  438. if ($row) {
  439. $id = $row['id'];
  440. model('wechat_autoreply')->update($data, $where);
  441. } else {
  442. $insert = array_merge($data, $where);
  443. //新增设置
  444. $insert['title'] = $insert['text'] = '签到';
  445. $insert['status'] = 'normal';
  446. $insert['news_content'] = '[]';
  447. $insert['createtime'] = $insert['updatetime'] = time();
  448. $insert['event_keys'] = json_encode([
  449. 'text' => $channel_id . '_' . uniqid(),
  450. 'news' => $channel_id . '_' . uniqid(),
  451. ], JSON_UNESCAPED_UNICODE);
  452. $id = model('wechat_autoreply')->insertGetId($insert);
  453. }
  454. $cacheKey = CacheConstants::getSignReplyCache($channel_id);
  455. Redis::instance()->del($cacheKey);
  456. return $this->setData($id)->getReturn();
  457. }
  458. }