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(); } }