123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2020/3/31
- * Time: 下午3:28
- */
- namespace app\main\service;
- use app\admin\service\CustomService;
- use app\common\library\Redis;
- use app\main\constants\AdminConstants;
- use app\main\constants\CacheConstants;
- use app\main\constants\ErrorCodeConstants;
- use app\main\constants\SubscribeDelayConstants;
- use app\main\constants\WechatSubscribeConstants;
- use app\main\model\object\ReturnObject;
- class ReplyVipService extends BaseService
- {
- const TYPE_DEFAULT = 1;
- const TYPE_TEXT = 2;
- const URL_REFERRAL = 1;
- const URL_ACTIVITY = 2;
- const URL_RECENTLY = 3;
- /**
- * 定义属性
- *
- * @var ReplyVipService
- */
- protected static $self = null;
- /**
- * 返回实例
- *
- * @return ReplyVipService
- */
- public static function instance()
- {
- if (self::$self == null) {
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * 关键字回复
- * @param $params
- * @param $vipId
- * @return ReturnObject
- */
- public function createReply($params, $vipId)
- {
- $params['admin_group'] = [];
- if ($params['deploy_type'] == 2) {
- //过滤空值
- $params['selected'] = array_filter($params['selected'], function($value){
- return $value;
- });
- $params['admin_group'] = array_keys($params['selected']);
- unset($params['selected']);
- }
- $channelIds = SubscribeVipDelayService::instance()->getVipChannelIds($vipId, $params['admin_group'])->data;
- if (!$channelIds) {
- return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg('未查询到可以同步的渠道')->getReturn();
- }
- $params['text_tip_word'] = json_encode([
- 'text_split' => $params['text_split'],
- 'text_tip' => $params['text_tip'],
- ], JSON_UNESCAPED_UNICODE);
- $updateChannelDelay = [];
- $error = '';
- foreach ($channelIds as $channelId) {
- if ($channelId != -1) {
- $return = $this->updateReply($params, $channelId);
- if ($return->code == ErrorCodeConstants::SUCCESS) {
- $updateChannelDelay[] = $return->data;
- } else {
- $error = $return->msg;
- }
- } else {
- $error = '未查询到需要同步的渠道';
- }
- }
- if (!$updateChannelDelay) {
- if (!$error) {
- $error = '更新失败';
- }
- return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg($error)->getReturn();
- }
- $result = $this->updateReply($params, $vipId, true);
- if ($result->code == ErrorCodeConstants::SUCCESS) {
- model('wechat_autoreply')->whereIn('id', $updateChannelDelay)->update(['pid' => $result->data]);
- return $this->setData($result->data)->getReturn();
- }
- return $this->setCode(ErrorCodeConstants::DB_ERROR_UPDATE)->setMsg('更新失败')->getReturn();
- }
- /**
- * @param $params
- * @param $channel_id
- * @param $is_vip
- * @return ReturnObject
- */
- public function updateReply($params, $channel_id, $is_vip = false)
- {
- $where = [
- 'admin_id' => $channel_id,
- 'title' => $params['keywords'],
- ];
- if ($is_vip) {
- if (!empty($params['id'])) {
- $where = ['id'=>$params['id']];
- $row = model('wechat_autoreply')->where('id', $params['id'])->find();
- } else {
- $row = model('wechat_autoreply')->where($where)->find();
- }
- } else {
- $row = false;
- if (!empty($params['id'])) {
- $where = ['pid'=>$params['id']];
- $row = model('wechat_autoreply')
- ->where('pid', $params['id'])
- ->where('admin_id', $channel_id)
- ->find();
- }
- if (!$row) {
- $row = model('wechat_autoreply')->where($where)->find();
- }
- }
- //更新的数据
- $data = [];
- if ($params['resource_type'] == 2) {
- $data['type'] = 'text';
- } else {
- $data['type'] = 'news';
- }
- $data['status'] = 'normal';
- $data['title'] = $data['text'] = $params['keywords'];
- if (!$is_vip) {
- //文字模式
- if ($data['type'] == 'text') {
- $data['text_tip_word'] = $params['text_tip_word'];
- $text_content = [];
- $success = false;
- $textList = $params['text_content'];
- foreach ($textList as $text) {
- //小说
- if ($text['image_type'] == 1) {
- $content = [
- 'image_type' => (int)$text['image_type'],
- 'book_id' => (string)$text['book_id'],
- 'chapter_id' => (string)$text['chapter_id'],
- 'title' => $text['title']
- ];
- $content['url'] = getCurrentDomain($channel_id, '/index/book/chapter?book_id=' . $text['book_id'] . '&chapter_id=' . $text['chapter_id']);
- $success = true;
- //分流链接
- } else {
- $referralId = ReferralService::instance()->getChannelReferralFromVip($text['v_id'], $channel_id)->data;
- if (!$referralId) {
- continue;
- }
- $content = [
- 'title' => $text['title'],
- 'image_type' => (int)$text['image_type'],
- ];
- $success = true;
- $content['url'] = getCurrentDomain($channel_id, '/t/' . $referralId);
- }
- $text_content[] = $content;
- }
- if (!$success) {
- return $this->setCode(ErrorCodeConstants::RESULT_EMPTY)->setMsg('链接无效')->getReturn();
- }
- $data['text_content'] = json_encode($text_content, JSON_UNESCAPED_UNICODE);
- $data['text_content'] = UrlService::instance()->replaceReferralHost($channel_id, $data['text_content'])->data;
- } else {
- $news_content = [];
- $success = false;
- $newsList = $params['news_content'];
- foreach ($newsList as $news) {
- //文本内容
- if ($news['image_type'] == 1) {
- $content = [
- 'image_type' => (int)$news['image_type'],
- 'book_id' => (string)$news['book_id'],
- 'chapter_id' => (string)$news['chapter_id'],
- 'title' => $news['title'],
- 'image' => $news['image'],
- 'description' => $news['description'],
- ];
- $content['url'] = getCurrentDomain($channel_id, '/index/book/chapter?book_id=' . $news['book_id'] . '&chapter_id=' . $news['chapter_id']);
- $content['referral_url'] = $content['activity_url'] = '';
- $success = true;
- } else if ($news['image_type'] == 2) {
- $url = CustomService::instance()->_buildVipActivityUrl($channel_id, $news['activity_id']);
- if (!$url) {
- continue;
- }
- $content = [
- 'image_type' => (int)$news['image_type'],
- 'title' => $news['title'],
- 'image' => $news['image'],
- 'url' => $url
- ];
- $content['activity_url'] = $content['url'];
- $content['referral_url'] = '';
- $success = true;
- } else {
- $referralId = ReferralService::instance()->getChannelReferralFromVip($news['v_id'], $channel_id)->data;
- if (!$referralId) {
- continue;
- }
- $content = [
- 'image_type' => (int)$news['image_type'],
- 'title' => $news['title'],
- 'description' => $news['description'],
- 'image' => $news['image'],
- 'url' => getCurrentDomain($channel_id, '/t/' . $referralId)
- ];
- $content['referral_url'] = $content['url'];
- $content['activity_url'] = '';
- $success = true;
- }
- $news_content[] = $content;
- }
- if (!$success) {
- return $this->setCode(ErrorCodeConstants::RESULT_EMPTY)->setMsg('链接无效')->getReturn();
- }
- $data['news_content'] = json_encode($news_content, JSON_UNESCAPED_UNICODE);
- $data['news_content'] = UrlService::instance()->replaceReferralHost($channel_id, $data['news_content'])->data;
- }
- } else {
- if ($data['type'] == 'text') {
- $data['text_content'] = json_encode($params, JSON_UNESCAPED_UNICODE);
- $data['text_content'] = UrlService::instance()->replaceReferralHost($channel_id, $data['text_content'])->data;
- } else {
- $data['news_content'] = json_encode($params, JSON_UNESCAPED_UNICODE);
- $data['news_content'] = UrlService::instance()->replaceReferralHost($channel_id, $data['news_content'])->data;
- }
- }
- if ($row) {
- $id = $row['id'];
- $data['updatetime'] = time();
- model('wechat_autoreply')->update($data, ['id'=>$id]);
- $event_keys = json_decode($row['event_keys'], true);
- } else {
- $insert = array_merge($data, $where);
- //新增设置
- $insert['createtime'] = $insert['updatetime'] = time();
- $event_keys = [
- 'text' => $channel_id . '_' . uniqid(),
- 'news' => $channel_id . '_' . uniqid(),
- ];
- $insert['event_keys'] = json_encode($event_keys, JSON_UNESCAPED_UNICODE);
- $id = model('wechat_autoreply')->insertGetId($insert);
- }
- if (!$is_vip) {
- foreach ($event_keys as $type => $event_key) {
- $update = [];
- switch ($type) {
- case WechatSubscribeConstants::SUBCRIBE_TYPE_TEXT:
- if (!empty($data['text_content'])) {
- //组织内容
- $resourceContent = $this->getTextContent($channel_id, $data['text_content'], $params['text_split'], $params['text_tip']);
- $update = [
- 'type' => 'text',
- 'title' => '文字类型回复',
- 'content' => $resourceContent,
- 'updatetime' => time(),
- 'status' => 'normal',
- ];
- }
- break;
- default:
- if (!empty($data['news_content'])) {
- $update = [
- 'type' => 'news',
- 'title' => '图文类型回复',
- 'content' => $data['news_content'],
- 'updatetime' => time(),
- ];
- }
- break;
- }
- $where = [
- 'admin_id' => $channel_id,
- 'eventkey' => $event_key,
- ];
- if ($update) {
- $update['status'] = 'normal';
- if (!model("WechatResponse")->where($where)->find()) {
- $insert = array_merge($where, $update, ['createtime' => time()]);
- model("WechatResponse")->insert($insert);
- } else {
- model("WechatResponse")->update($update, $where);
- }
- }
- }
- }
- $cacheKey = CacheConstants::getSignReplyCache($channel_id);
- Redis::instance()->del($cacheKey);
- return $this->setData($id)->getReturn();
- }
- public function getTextContent($channel_id, $content, $text_split, $text_tip)
- {
- $temp = WechatSubscribeConstants::AUTOREPLY_TEXT_TEMPLATE;
- $bookTemp = WechatSubscribeConstants::SUBCRIBE_TEXT_BOOK_TEMPLATE;
- $bookSets = json_decode($content, true);
- $contentStr = [];
- $resourceContent = '';
- if (is_array($bookSets)) {
- foreach ($bookSets as $index => $bookSet) {
- if ($index == 1 && $text_split) {
- $contentStr[] = $text_split;
- }
- $url = $bookSet['url'];
- $title = $bookSet['title'];
- $contentStr[] = str_replace(['{URL}', '{TITLE}'], [$url, $title], $bookTemp);
- }
- $wxJson = model("AdminConfig")
- ->where('admin_id', $channel_id)
- ->value('json');
- $gzhName = '';
- if (!empty($wxJson)) {
- $wxInfo = json_decode($wxJson, true);
- $gzhName = $wxInfo['authorizer_info']['nick_name'];
- }
- $resourceContent = str_replace(['{TEXT_TIP}', '$gzh_name', '{CONTENT}'], [$text_tip, $gzhName, implode("\r\n\r\n", $contentStr)], $temp);
- }
- return $resourceContent;
- }
- public function createSignReply($params, $vipId)
- {
- $params['admin_group'] = [];
- if ($params['deploy_type'] == 2) {
- //过滤空值
- $params['selected'] = array_filter($params['selected'], function($value){
- return $value;
- });
- $params['admin_group'] = array_keys($params['selected']);
- unset($params['selected']);
- }
- $channelIds = SubscribeVipDelayService::instance()->getVipChannelIds($vipId, $params['admin_group'])->data;
- if (!$channelIds) {
- return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg('未查询到可以同步的渠道')->getReturn();
- }
- $updateChannelDelay = [];
- $error = '';
- unset($params['id']);
- foreach ($channelIds as $channelId) {
- if ($channelId != -1) {
- $return = $this->updateSignReply($params, $channelId);
- if ($return->code == ErrorCodeConstants::SUCCESS) {
- $updateChannelDelay[] = $return->data;
- } else {
- $error = $return->msg;
- }
- } else {
- $error = '未查询到需要同步的渠道';
- }
- }
- if (!$updateChannelDelay) {
- if (!$error) {
- $error = '更新失败';
- }
- return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg($error)->getReturn();
- }
- $result = $this->updateSignReply($params, $vipId, true);
- if ($result->code == ErrorCodeConstants::SUCCESS) {
- model('wechat_autoreply')->whereIn('id', $updateChannelDelay)->update(['pid' => $result->data]);
- return $this->setData($result->data)->getReturn();
- }
- return $this->setCode(ErrorCodeConstants::DB_ERROR_UPDATE)->setMsg('更新失败')->getReturn();
- }
- /**
- * @param $params
- * @param $channel_id
- * @param $is_vip
- * @return ReturnObject
- */
- public function updateSignReply($params, $channel_id, $is_vip = false)
- {
- $where = [
- 'admin_id' => $channel_id,
- 'title' => '签到',
- ];
- $row = model('wechat_autoreply')->where($where)->find();
- //更新的数据
- $data = [
- 'type' => 'text',
- 'text_tip_word' => $params['text_split'],
- 'updatetime' => time(),
- ];
- if ($params['type'] == self::TYPE_DEFAULT) {
- $data['status'] = 'hidden';
- if ($is_vip) {
- $data['text_content'] = json_encode($params, JSON_UNESCAPED_UNICODE);
- }
- } else {
- $data['status'] = 'normal';
- $textList = $params['text'];
- if (!$is_vip) {
- $text_content = [];
- $success = false;
- foreach ($textList as $text) {
- //文本内容
- $content = [
- 'url_type' => $text['url_type'],
- 'title' => $text['title'],
- 'referral_url' => '',
- 'activity_url' => '',
- ];
- if ($text['url_type'] == self::URL_REFERRAL) {
- $referralId = ReferralService::instance()->getChannelReferralFromVip($text['v_id'], $channel_id)->data;
- if (!$referralId) {
- continue;
- }
- $success = true;
- $content['referral_url'] = getCurrentDomain($channel_id, '/t/' . $referralId);
- } elseif ($text['url_type'] == self::URL_ACTIVITY) {
- $url = CustomService::instance()->_buildVipActivityUrl($channel_id, $text['activity_id']);
- if (!$url) {
- continue;
- }
- $success = true;
- $content['activity_url'] = $url;
- } elseif ($text['url_type'] == self::URL_RECENTLY) {
- $success = true;
- }
- $text_content[] = $content;
- }
- if (!$success) {
- return $this->setCode(ErrorCodeConstants::RESULT_EMPTY)->setMsg('链接无效')->getReturn();
- }
- $data['text_content'] = json_encode($text_content, JSON_UNESCAPED_UNICODE);
- $data['text_content'] = UrlService::instance()->replaceReferralHost($channel_id, $data['text_content'])->data;
- } else {
- $data['text_content'] = json_encode($params, JSON_UNESCAPED_UNICODE);
- }
- }
- if ($row) {
- $id = $row['id'];
- model('wechat_autoreply')->update($data, $where);
- } else {
- $insert = array_merge($data, $where);
- //新增设置
- $insert['title'] = $insert['text'] = '签到';
- $insert['status'] = 'normal';
- $insert['news_content'] = '[]';
- $insert['createtime'] = $insert['updatetime'] = time();
- $insert['event_keys'] = json_encode([
- 'text' => $channel_id . '_' . uniqid(),
- 'news' => $channel_id . '_' . uniqid(),
- ], JSON_UNESCAPED_UNICODE);
- $id = model('wechat_autoreply')->insertGetId($insert);
- }
- $cacheKey = CacheConstants::getSignReplyCache($channel_id);
- Redis::instance()->del($cacheKey);
- return $this->setData($id)->getReturn();
- }
- }
|