123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- /**
- * Created by: PhpStorm
- * User: lytian
- * Date: 2020/3/16
- * Time: 15:11
- */
- namespace app\main\service;
- use app\common\library\Redis;
- use app\main\constants\KafkaDotConstants;
- use app\main\constants\UcCacheConstants;
- use app\main\model\object\AnalysisObject;
- use app\main\model\object\ReturnObject;
- use fast\Http;
- use think\Config;
- class UcNotifyService extends BaseService
- {
- const EVENT_TYPE_FORM = 5;//表单提交
- const EVENT_TYPE_ADVISE = 1002;//有效咨询
- //const EVENT_TYPE_NEW = 19;//有效获客
- const EVENT_TYPE_PAY = 1000;//付费
- /**
- * 定义属性
- *
- * @var UcNotifyService
- */
- protected static $self = null;
- /**
- * 返回实例
- *
- * @return UcNotifyService
- */
- public static function instance()
- {
- if (self::$self == null) {
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * 头条回传
- * @param $link
- * @param int $type
- * @return ReturnObject
- */
- public function notify($userinfo, $link, $type = self::EVENT_TYPE_FORM, $money = 0)
- {
- $userinfo = (array)$userinfo;
- $urlback = "https://huichuan.uc.cn/callback/ct/add";
- //调用打点api
- LogService::info('geturl:' . $urlback . ':' . $type . ':' . $link);
- $linkInfo = explode('和', $link);
- $id = $linkInfo[0];
- $link = $linkInfo[1];
- $get = $urlback . '?event_type=' . $type . '&link=' . urlencode($link). '&event_time='. time(). '&source='. Config::get("site.theme");
- $res = json_decode(Http::get($get), true);
- if ($res['status'] == 0) {
- //成功了
- if ($id) {
- $oAna = new AnalysisObject();
- $oAna->data = [
- 'uc_id' => $id,
- ];
- switch ($type) {
- case self::EVENT_TYPE_FORM:
- $oAna->type = KafkaDotConstants::TYPE_SUBSCRIBE;
- $cacheKey = UcCacheConstants::getGuideWxNewCount($id);
- $cacheDayKey = UcCacheConstants::getGuideWxDayNewCount($id);
- Redis::instance()->incr($cacheKey);
- $update = [
- 'follow' => ['exp', "(follow+1)"],
- ];
- model("UcLandingPage")->update($update, ['id' => $id]);
- 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 = UcCacheConstants::getGuideWxPayCount($id);
- $cacheDayKey = UcCacheConstants::getGuideWxDayPayCount($id);
- $cacheMoney = UcCacheConstants::getGuideWxPayMoney($id);
- $cacheDayMoney = UcCacheConstants::getGuideWxDayPayMoney($id);
- $update = [
- 'orders_count' => ['exp', "(orders_count+1)"],
- 'orders_money' => ['exp', "(orders_money+{$money})"],
- ];
- model("UcLandingPage")->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($userinfo['id'], $oAna);
- }
- LogService::info('ucreprot: channel_id:'.$userinfo['channel_id'].' user_id: ' .$userinfo['id']. ' : success' . ' type:' . $type . ' :' . $link);
- } else {
- //失败了
- LogService::info('ucreprot: channel_id:'.$userinfo['channel_id'].' user_id: '.$userinfo['id'].' : error' .$get. ' type:' . $type . ' :' . $link);
- }
- return $this->setData(true)->getReturn();
- }
- /**
- * 检查cache
- * @param $ip
- * @param $ua
- * @param $appid
- * @return ReturnObject
- */
- public function checkCache($ip, $ua, $appid)
- {
- $cacheSub = UcCacheConstants::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();
- }
- }
|