123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2020/2/4
- * Time: 上午11:42
- */
- namespace app\main\service;
- use app\admin\model\Postbackklreferral;
- use app\common\model\Postbackrules;
- use app\common\library\Redis;
- use app\main\constants\AdminConstants;
- use app\main\constants\CacheConstants;
- use app\main\constants\KafkaDotConstants;
- use app\main\constants\PostbackConstants;
- use app\main\helper\ArrayHelper;
- use app\main\helper\DateHelper;
- use app\main\model\object\AnalysisObject;
- use app\main\model\object\OrderObject;
- use app\main\model\object\ReturnObject;
- use app\main\model\object\UserObject;
- use GuzzleHttp\Client;
- use GuzzleHttp\Client as Http;
- use Jenssegers\Agent\Agent;
- use think\Config;
- use think\db\Query;
- /**
- * Class ToutiaoNotifyService
- * @package app\main\service
- */
- class ToutiaoNotifyService extends BaseService
- {
- const EVENT_TYPE_ACTIVATE = 0;//激活
- const EVENT_TYPE_FORM = 3;//表单提交
- const EVENT_TYPE_ADVISE = 5;//有效咨询
- const EVENT_TYPE_NEW = 19;//有效获客
- const EVENT_TYPE_PAY = 2;//付费
- const ORANGE_EVENT_TYPE_NEW = 'in_wechat_login';
- const ORANGE_EVENT_TYPE_PAY = 'in_wechat_pay';
- /**
- * 定义属性
- *
- * @var ToutiaoNotifyService
- */
- protected static $self = null;
- /**
- * 返回实例
- *
- * @return ToutiaoNotifyService
- */
- public static function instance()
- {
- if (self::$self == null) {
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * 头条回传
- * @param $link
- * @param $user_id
- * @param int $type
- * @return ReturnObject
- */
- public function notify($link, $user_id, $type = self::EVENT_TYPE_ACTIVATE, $money = 0)
- {
- $urlback = Config::get('wechat.tturlback');
- //调用打点api
- LogService::info('geturl:' . $urlback . ':' . $type . ':' . $link);
- $linkInfo = explode('和', $link);
- $id = $linkInfo[0];
- $link = $linkInfo[1];
- $get = $urlback . '?event_type=' . $type . '&link=' . urlencode($link);
- if ($id) {
- $oAna = new AnalysisObject();
- $oAna->data = [
- 'toutiao_id' => $id,
- ];
- switch ($type) {
- case self::EVENT_TYPE_ACTIVATE:
- $oAna->type = KafkaDotConstants::TYPE_SUBSCRIBE;
- $cacheKey = CacheConstants::getGuideWxNewCount($id);
- $cacheDayKey = CacheConstants::getGuideWxDayNewCount($id);
- $update = [
- 'follow' => ['exp', "(follow+1)"],
- ];
- model("ToutiaoLandingPage")->update($update, ['id' => $id]);
- Redis::instance()->incr($cacheKey);
- Redis::instance()->incr($cacheDayKey);
- Redis::instance()->expire($cacheDayKey, 86400);
- break;
- case self::EVENT_TYPE_PAY:
- $oAna->type = KafkaDotConstants::TYPE_ORDER_COMPLETE;
- $oAna->data['money'] = $money;
- $cacheKey = CacheConstants::getGuideWxPayCount($id);
- $cacheDayKey = CacheConstants::getGuideWxDayPayCount($id);
- $cacheMoney = CacheConstants::getGuideWxPayMoney($id);
- $cacheDayMoney = CacheConstants::getGuideWxDayPayMoney($id);
- $update = [
- 'orders_count' => ['exp', "(orders_count+1)"],
- 'orders_money' => ['exp', "(orders_money+{$money})"],
- ];
- model("ToutiaoLandingPage")->update($update, ['id' => $id]);
- Redis::instance()->incr($cacheKey);
- Redis::instance()->incr($cacheDayKey);
- Redis::instance()->expire($cacheDayKey, 86400);
- Redis::instance()->expire($cacheDayMoney, 86400);
- Redis::instance()->incrByFloat($cacheMoney, $money);
- Redis::instance()->incrByFloat($cacheDayMoney, $money);
- break;
- }
- KafkaDotService::instance()->sendMsg($user_id, $oAna);
- }
- $client = new Client();
- $client->get($get);
- return $this->setData(true)->getReturn();
- }
- /**
- * 橘子建站上报
- * @param $adminConfig
- * @param UserObject $user
- * @param $type
- * @param OrderObject|null $orderObject
- * @return ReturnObject
- */
- public function OrangeNotify($adminConfig, UserObject $user, $type, OrderObject $orderObject = null)
- {
- $return = false;
- try {
- if (!ArrayHelper::array_find($adminConfig, 'orange_callback')) {
- LogService::info('回传未开启,不需要回传');
- return $this->setData($return)->getReturn();
- }
- $cache = Redis::instance()->hGetAll(CacheConstants::getUserClientCache($user->id));
- if (!$cache) {
- LogService::info('缓存失效,不需要回传');
- return $this->setData($return)->getReturn();
- }
- $wxname = ArrayHelper::array_find($adminConfig, 'json.authorizer_info.alias');
- if (!$wxname) {
- LogService::info('微信号为空,不需要回传');
- return $this->setData($return)->getReturn();
- }
- $data = [
- "_legacy_event_type" => $type,
- "context" => [
- "ad" => [
- "attributed" => "false"
- ],
- "device" => [
- "open_id" => $user->openid
- ],
- "ip" => $cache['ip'],
- "userAgent" => $cache['ua']
- ],
- "properties" => [
- "source" => Config::get('site.theme'),
- "we_chat_app_id" => $adminConfig['appid'],
- "we_chat_official_account_id" => $wxname
- ],
- "timestamp" => DateHelper::ios_8601()
- ];
- if ($type == self::ORANGE_EVENT_TYPE_PAY) {
- if (!$orderObject) {
- LogService::info('订单为空,不需要回传');
- return $this->setData($return)->getReturn();
- } else {
- $data['properties']['book_id'] = (string)$orderObject->book_id;
- $data['properties']['amount'] = (string)($orderObject->money * 100);
- }
- }
- $httpConfig = [
- 'base_uri' => 'https://analytics.oceanengine.com/api/v1/',
- 'connect_timeout' => 10,
- 'timeout' => 30,
- 'http_errors' => true, //抛出异常 true是 false否
- 'verify' => false, //不验证ssl证书
- ];
- $client = new Http($httpConfig);
- $result = $client->request('POST',
- "track/wechat",
- [
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'charset' => 'utf-8'
- ],
- 'body' => json_encode($data, JSON_UNESCAPED_UNICODE)
- ]
- );
- $status = $result->getStatusCode();
- if ($status == 200) {
- LogService::info("JuiceNotify: Success Data:" . json_encode($data));
- } else {
- LogService::error("JuiceNotify: Fail Data:" . json_encode($data) . ',body:' . $result->getBody() . ',code:' . $status);
- }
- return $this->setData($return)->getReturn();
- } catch (\Exception $e) {
- LogService::exception($e);
- return $this->setData($return)->getReturn();
- }
- }
- /**
- * 获取请求设备信息
- * @param $ua
- * @return ReturnObject
- */
- public function getUaInfo($ua)
- {
- $agent = new Agent();
- LogService::info("REF_UA:" . $ua);
- if ($ua) {
- $agent->setUserAgent($ua);
- $device = $agent->device();
- $pt = $agent->platform();
- $pt_version = $agent->version($pt);
- $info = $device . ':' . $pt . ':' . rtrim($pt_version, 0);
- // $info = $pt . ':' . $pt_version;
- } else {
- $info = 'default:default:default';
- }
- return $this->setData($info)->getReturn();
- }
- /**
- * 获取金额阈值
- * @param $rule_id
- * @param $type
- * @param $field
- * @return ReturnObject
- */
- public function getKlValue($rule_id, $type, $field = 'money')
- {
- $where = [
- 'id' => $rule_id,
- 'state' => PostbackConstants::STATE_SHOW
- ];
- $mPostRule = new PostbackRules();
- $data = $mPostRule->where($where)->find();
- if ($data) {
- $return = Config::get('site.' . $type . '_' . $field);
- if ($data[$field] != -1) {
- $return = $data[$field];
- }
- } else {
- $return = '-1';
- }
- return $this->setData($return)->getReturn();
- }
- /**
- * 检测是否非KL
- * @param $money
- * @param $kl
- * @param $order_money
- * @return ReturnObject
- */
- public function checkNotKl($money, $kl, $order_money)
- {
- if ($money == '-1' || $kl == '-1') {
- LogService::info('klresult:!k');
- return $this->setData(true)->getReturn();
- }
- $rate = rand(1, 100);
- $kl *= 100;
- if ($order_money >= $money && $rate > $kl) {
- LogService::info('klinfo:' . $order_money . '>=' . $money . ' and ' . $rate . '>' . $kl);
- LogService::info('klresult:!k');
- return $this->setData(true)->getReturn();
- } else {
- if ($order_money < $money) {
- LogService::info('klinfo:money:' . $order_money . '<' . $money);
- }
- if ($rate <= $kl) {
- LogService::info('klinfo:kl:' . $rate . '<=' . $kl);
- }
- LogService::info('klresult:k');
- return $this->setData(false)->getReturn();
- }
- }
- /**
- * 渠道获取规则id
- * @param $channel_id
- * @param $type
- * @return ReturnObject
- */
- public function getKlRuleId($channel_id, $type = PostbackConstants::TYPE_TOUTIAO)
- {
- $mPostBackRule = new PostbackRules();
- $data = $mPostBackRule
- ->where(function (Query $query) use ($channel_id) {
- $query->where('find_in_set(' . $channel_id . ', channel_ids)')
- ->whereOr('channel_ids', '*');
- })
- ->where('type', $type)
- ->where('state', PostbackConstants::STATE_SHOW)
- ->order('weight', 'desc')
- ->value('id');
- return $this->setData($data)->getReturn();
- }
- /**
- * 获取KL配置
- * @param $channel_id
- * @param string $type
- * @param string $referral_id
- * @return ReturnObject
- */
- public function getKlConfig($channel_id, $type = PostbackConstants::TYPE_TOUTIAO, $referral_id = '')
- {
- $ruleId = $this->getKlRuleId($channel_id, $type)->data;
- if ($ruleId) {
- $config = [
- 'money' => ToutiaoNotifyService::instance()->getKlValue($ruleId, $type, 'money')->data,
- 'kl' => ToutiaoNotifyService::instance()->getKlValue($ruleId, $type, 'kl')->data
- ];
- if ($referral_id) {
- $where = [
- 'rule_id' => $ruleId,
- 'type' => $type,
- 'referral_id' => $referral_id,
- 'state' => 'show'
- ];
- $mPostReferral = new Postbackklreferral();
- $referral = $mPostReferral->where($where)->find();
- if ($referral) {
- if ($referral['money'] != '-1') {
- $config['money'] = $referral['money'];
- }
- if ($referral['kl'] != '-1') {
- $config['kl'] = $referral['kl'];
- }
- }
- }
- } else {
- $config = [
- 'money' => '-1',
- 'kl' => '-1',
- ];
- }
- return $this->setData($config)->getReturn();
- }
- /**
- * 检查cache
- * @param $ip
- * @param $ua
- * @param $appid
- * @return ReturnObject
- */
- public function checkCache($ip, $ua, $appid)
- {
- $cacheSub = CacheConstants::getGuideWxSubscribe($ip, $ua);
- $link = Redis::instance()->get($cacheSub);
- if ($link) {
- $params = explode('和', $link);
- if (count($params) == 2) {
- Redis::instance()->del($cacheSub);
- return $this->setData($link)->getReturn();
- } else if (count($params) >= 3) {
- if ($params[0] == $appid) {
- Redis::instance()->del($cacheSub);
- array_shift($params);
- $link = implode('和', $params);
- return $this->setData($link)->getReturn();
- }
- }
- }
- return $this->setData(false)->getReturn();
- }
- /**
- * 检测关注时间
- * @param $subscribe_time
- * @param $type
- * @return ReturnObject
- */
- public function checkCallbackTime($subscribe_time, $type)
- {
- $return = false;
- switch ($type) {
- case AdminConstants::CALLBACK_TIME_ALL_DAY:
- case AdminConstants::CALLBACK_TIME_ONCE_DAY:
- $return = date('Ymd', $subscribe_time) == date('Ymd');
- break;
- case AdminConstants::CALLBACK_TIME_ALL_24:
- case AdminConstants::CALLBACK_TIME_ONCE_24:
- $return = $subscribe_time + 86400 > time();
- break;
- }
- if ($return) {
- LogService::info('关注时间:需要回传');
- } else {
- LogService::info('关注时间:不需要回传');
- }
- return $this->setData($return)->getReturn();
- }
- }
|