123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2020/3/23
- * Time: 下午7:22
- */
- namespace app\main\service;
- use app\common\constants\Message;
- use app\main\constants\AdminConstants;
- use app\main\constants\ErrorCodeConstants;
- use app\main\constants\SubscribeDelayConstants;
- use app\main\model\object\ReturnObject;
- use app\admin\service\CustomService;
- class SubscribeVipDelayService extends BaseService
- {
- /**
- * 定义属性
- *
- * @var SubscribeVipDelayService
- */
- protected static $self = null;
- /**
- * 返回实例
- *
- * @return SubscribeVipDelayService
- */
- public static function instance()
- {
- if (self::$self == null) {
- self::$self = new self();
- }
- return self::$self;
- }
- public function getVipChannelIds($admin_id, $adminGroup = [])
- {
- if ($adminGroup) {
- $channel_ids = model('vip_group_info')->whereIn('group_id', $adminGroup)->column('channel_id');
- } else {
- $channel_ids = AdminService::instance()->getAdminId($admin_id)->data;
- }
- $channel_ids = array_unique($channel_ids);
- return $this->setData($channel_ids)->getReturn();
- }
- public function createDelayReply($params, $vipId)
- {
- $group = $params['admin_group'];
- $channelIds = $this->getVipChannelIds($vipId, $group)->data;
- if (!$channelIds) {
- return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg('未查询到可以同步的渠道')->getReturn();
- }
- LogService::info('同步的渠道'.json_encode($channelIds));
- $updateChannelDelay = [];
- $error = '';
- unset($params['id']);
- $hasMini = 0;
- //查看是否包含小程序类型 6是小程序类型
- if (isset($params['url_type']) && $params['url_type'] == 6){
- $hasMini = 1;
- }
- if ($params['content_type'] == 2 && !empty($params['text_content'])){
- $urlTypes = array_column($params['text_content'],'url_type');
- $hasMini = in_array(6,$urlTypes) ? 1 : 0;
- }
- foreach ($channelIds as $channelId) {
- if ($channelId != -1) {
- $isOpen = 1;
- if ($hasMini){
- $isOpen = MiniProgramService::instance()->miniWrite($channelId,3);
- }
- if (!$isOpen){
- LogService::info('vip同步延迟客服消息失败'.$channelId.'未开通小程序权限');
- continue;
- }
- $return = $this->updateDelayCustom($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->updateDelayCustom($params, $vipId, true);
- if ($result->code == ErrorCodeConstants::SUCCESS) {
- model('subscribe_delay_push')->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 updateDelayCustom($params, $channel_id, $is_vip = false)
- {
- $params['url_type'] = (int)$params['url_type'] ?? SubscribeDelayConstants::URL_TYPE_REFERRAL;
- $params['user_type'] = (int)$params['user_type'] ?? SubscribeDelayConstants::USER_TYPE_ALL;
- $content = [
- 'title' => $params['title'],
- 'image' => $params['image'],
- 'description' => $params['description'],
- 'user_type' => $params['user_type'],
- 'url_type' => $params['url_type'],
- 'sex' => $params['sex'],
- 'mobile_system' => $params['mobile_system'],
- 'mini_type' =>$params['mini_type'] ?? 0,
- 'text_tip_word'=>$params['text_tip_word'] ?? '',
- ];
- if ($is_vip && !empty($params['id'])) {
- $row = model('subscribe_delay_push')->where('id', $params['id'])->find();
- if (!$row) {
- return $this->setMsg('客服消息不存在')->setCode(ErrorCodeConstants::DB_ERROR_SELECT)->getReturn();
- }
- } else {
- $check = [
- 'admin_id' => $channel_id,
- 'type' => $params['unit'],
- 'status' => 'normal',
- 'delay' => $params['current_time'],
- ];
- $row = model('subscribe_delay_push')->where($check)->find();
- }
- if (!$is_vip) {
- if ($params['content_type'] == 2){ //文本类型
- $content['description'] = $this->getContent($params, $channel_id);
- }else{//图文类型
- //添加链接
- switch ($params['url_type']) {
- case SubscribeDelayConstants::URL_TYPE_REFERRAL:
- $referralId = ReferralService::instance()->getChannelReferralFromVip($params['v_id'], $channel_id)->data;
- if (!$referralId) {
- LogService::info('vip同步延迟客服消息失败('.$channel_id.')分流链接无效');
- return $this->setMsg('分流链接无效')->setCode(ErrorCodeConstants::RESULT_EMPTY)->getReturn();
- }
- $params['referral_url'] = $content['url'] = getCurrentDomain($channel_id, '/t/' . $referralId);
- break;
- case SubscribeDelayConstants::URL_TYPE_ACTIVITY:
- $url = CustomService::instance()->_buildVipActivityUrl($channel_id, $params['activity_id']);
- if (!$url) {
- LogService::info('vip同步延迟客服消息失败('.$channel_id.')活动不存在');
- return $this->setMsg('活动不存在')->setCode(ErrorCodeConstants::RESULT_EMPTY)->getReturn();
- }
- $params['activity_url'] = $content['url'] = $url;
- break;
- case SubscribeDelayConstants::URL_TYPE_RECENTLY_READ:
- $content['url'] = getCurrentDomain($channel_id, '/index/book/chapter');
- break;
- case SubscribeDelayConstants::URL_TYPE_SELF:
- $content['url'] = $params['self_url'];
- break;
- case SubscribeDelayConstants::URL_TYPE_SIGN:
- $content['url'] = "weixin://bizmsgmenu?msgmenucontent=" . Message::MESSAGE_KEYWORD . "&msgmenuid={{" . time() . "}}";
- break;
- case SubscribeDelayConstants::URL_TYPE_MINI:
- $content['mini_content'] = MiniProgramService::instance()->getMiniContent($channel_id,$params);
- break;
- }
- }
- }
- $id = false;
- if ($row) {
- if (!$is_vip) {
- $content = json_encode($content, JSON_UNESCAPED_UNICODE);
- $content = UrlService::instance()->replaceReferralHost($channel_id, $content)->data;
- } else {
- $content = '';
- }
- $data = [
- 'type' => $params['unit'],
- 'delay' => $params['current_time'],
- 'content' => $content,
- 'news_set' => json_encode($params, JSON_UNESCAPED_UNICODE),
- 'status' => 'normal',
- 'content_type'=>$params['content_type'],
- 'updatetime' => time()
- ];
- $result = model('subscribe_delay_push')->update($data, ['id'=>$row['id']]);
- if ($result !== false) {
- $id = $row['id'];
- }
- } else {
- //新增
- if (!$is_vip) {
- $content = json_encode($content, JSON_UNESCAPED_UNICODE);
- $content = UrlService::instance()->replaceReferralHost($channel_id, $content)->data;
- } else {
- $content = '';
- }
- $data = [
- 'admin_id' => $channel_id,
- 'type' => $params['unit'],
- 'delay' => $params['current_time'],
- 'content' => $content,
- 'news_set' => json_encode($params, JSON_UNESCAPED_UNICODE),
- 'content_type'=>$params['content_type'],
- 'status' => 'normal',
- 'updatetime' => time(),
- 'createtime' => time(),
- ];
- $id = model('subscribe_delay_push')->insertGetId($data);
- }
- if ($id !== false) {
- //需要清理一下缓存
- model('subscribe_delay_push')->delCache($channel_id);
- return $this->setData($id)->getReturn();
- } else {
- return $this->setMsg(model('subscribe_delay_push')->getError())->setCode(ErrorCodeConstants::DB_ERROR_UPDATE)->getReturn();
- }
- }
- /**
- * 延迟客服消息-处理文本类型
- * @param $args
- * @return string
- */
- public function getContent($args,$channelId)
- {
- if (!isset($args['text_content']) || $args['text_content'] == false){
- return '';
- }
- $wxJson = model("AdminConfig")
- ->where('admin_id', $channelId)
- ->value('json');
- $gzh_name = '';
- if (!empty($wxJson)) {
- $wxInfo = json_decode($wxJson, true);
- $gzh_name = $wxInfo['authorizer_info']['nick_name'];
- }
- $content = $args['text_content'];
- if (isset($args['text_tip_word']) && $args['text_tip_word'] != false){
- $str = "{$args['text_tip_word']}\r\n";
- }else{
- $str = " 欢迎关注「{$gzh_name}」,点击下方继续阅读\r\n";
- }
- foreach($content as $key =>$value){
- if ($value['url_type'] == 6){
- $str .= MiniProgramService::instance()->getMiniText2($value['url'],$value['name'],$channelId)."\r\n";
- continue;
- }
- if ($value['url_type'] == 3){
- $value['url'] = getCurrentDomain($channelId, '/index/book/chapter');
- }
- if ($value['url_type'] == 5){
- $value['url'] = "weixin://bizmsgmenu?msgmenucontent=" . Message::MESSAGE_KEYWORD . "&msgmenuid={{" . time() . "}}";
- }
- if ($key == 1){
- $str .= "\r\n更多精彩内容,点击下方蓝字内容\r\n";
- }
- $str .= "<a href='{$value['url']}'>{$value['name']}</a>\r\n";
- }
- $str .= "为了方便下次阅读,请置顶公众号\r\n";
- if ($args['is_sign'] == 2){
- $str .= "<a href='weixin://bizmsgmenu?msgmenucontent=" . Message::MESSAGE_KEYWORD . "&msgmenuid={{" . time() . "}}\"'>{$args['sign_content']}</a>";
- }else{
- $str .= '回复签到,获得书币';
- }
- return $str;
- }
- }
|