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