UcNotifyService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Created by: PhpStorm
  4. * User: lytian
  5. * Date: 2020/3/16
  6. * Time: 15:11
  7. */
  8. namespace app\main\service;
  9. use app\common\library\Redis;
  10. use app\main\constants\KafkaDotConstants;
  11. use app\main\constants\UcCacheConstants;
  12. use app\main\model\object\AnalysisObject;
  13. use app\main\model\object\ReturnObject;
  14. use fast\Http;
  15. use think\Config;
  16. class UcNotifyService extends BaseService
  17. {
  18. const EVENT_TYPE_FORM = 5;//表单提交
  19. const EVENT_TYPE_ADVISE = 1002;//有效咨询
  20. //const EVENT_TYPE_NEW = 19;//有效获客
  21. const EVENT_TYPE_PAY = 1000;//付费
  22. /**
  23. * 定义属性
  24. *
  25. * @var UcNotifyService
  26. */
  27. protected static $self = null;
  28. /**
  29. * 返回实例
  30. *
  31. * @return UcNotifyService
  32. */
  33. public static function instance()
  34. {
  35. if (self::$self == null) {
  36. self::$self = new self();
  37. }
  38. return self::$self;
  39. }
  40. /**
  41. * 头条回传
  42. * @param $link
  43. * @param int $type
  44. * @return ReturnObject
  45. */
  46. public function notify($userinfo, $link, $type = self::EVENT_TYPE_FORM, $money = 0)
  47. {
  48. $userinfo = (array)$userinfo;
  49. $urlback = "https://huichuan.uc.cn/callback/ct/add";
  50. //调用打点api
  51. LogService::info('geturl:' . $urlback . ':' . $type . ':' . $link);
  52. $linkInfo = explode('和', $link);
  53. $id = $linkInfo[0];
  54. $link = $linkInfo[1];
  55. $get = $urlback . '?event_type=' . $type . '&link=' . urlencode($link). '&event_time='. time(). '&source='. Config::get("site.theme");
  56. $res = json_decode(Http::get($get), true);
  57. if ($res['status'] == 0) {
  58. //成功了
  59. if ($id) {
  60. $oAna = new AnalysisObject();
  61. $oAna->data = [
  62. 'uc_id' => $id,
  63. ];
  64. switch ($type) {
  65. case self::EVENT_TYPE_FORM:
  66. $oAna->type = KafkaDotConstants::TYPE_SUBSCRIBE;
  67. $cacheKey = UcCacheConstants::getGuideWxNewCount($id);
  68. $cacheDayKey = UcCacheConstants::getGuideWxDayNewCount($id);
  69. Redis::instance()->incr($cacheKey);
  70. $update = [
  71. 'follow' => ['exp', "(follow+1)"],
  72. ];
  73. model("UcLandingPage")->update($update, ['id' => $id]);
  74. Redis::instance()->incr($cacheDayKey);
  75. Redis::instance()->expire($cacheDayKey, 86400);
  76. break;
  77. case self::EVENT_TYPE_PAY:
  78. $oAna->type = KafkaDotConstants::TYPE_ORDER_COMPLETE;
  79. $oAna->data['money'] = $money;
  80. $cacheKey = UcCacheConstants::getGuideWxPayCount($id);
  81. $cacheDayKey = UcCacheConstants::getGuideWxDayPayCount($id);
  82. $cacheMoney = UcCacheConstants::getGuideWxPayMoney($id);
  83. $cacheDayMoney = UcCacheConstants::getGuideWxDayPayMoney($id);
  84. $update = [
  85. 'orders_count' => ['exp', "(orders_count+1)"],
  86. 'orders_money' => ['exp', "(orders_money+{$money})"],
  87. ];
  88. model("UcLandingPage")->update($update, ['id' => $id]);
  89. Redis::instance()->incr($cacheKey);
  90. Redis::instance()->incr($cacheDayKey);
  91. Redis::instance()->expire($cacheDayKey, 86400);
  92. Redis::instance()->expire($cacheDayMoney, 86400);
  93. Redis::instance()->incrByFloat($cacheMoney, $money);
  94. Redis::instance()->incrByFloat($cacheDayMoney, $money);
  95. break;
  96. }
  97. KafkaDotService::instance()->sendMsg($userinfo['id'], $oAna);
  98. }
  99. LogService::info('ucreprot: channel_id:'.$userinfo['channel_id'].' user_id: ' .$userinfo['id']. ' : success' . ' type:' . $type . ' :' . $link);
  100. } else {
  101. //失败了
  102. LogService::info('ucreprot: channel_id:'.$userinfo['channel_id'].' user_id: '.$userinfo['id'].' : error' .$get. ' type:' . $type . ' :' . $link);
  103. }
  104. return $this->setData(true)->getReturn();
  105. }
  106. /**
  107. * 检查cache
  108. * @param $ip
  109. * @param $ua
  110. * @param $appid
  111. * @return ReturnObject
  112. */
  113. public function checkCache($ip, $ua, $appid)
  114. {
  115. $cacheSub = UcCacheConstants::getGuideWxSubscribe($ip, $ua);
  116. $link = Redis::instance()->get($cacheSub);
  117. if ($link) {
  118. $params = explode('和', $link);
  119. if (count($params) == 2) {
  120. Redis::instance()->del($cacheSub);
  121. return $this->setData($link)->getReturn();
  122. } else if (count($params) >= 3) {
  123. if ($params[0] == $appid) {
  124. Redis::instance()->del($cacheSub);
  125. array_shift($params);
  126. $link = implode('和', $params);
  127. return $this->setData($link)->getReturn();
  128. }
  129. }
  130. }
  131. return $this->setData(false)->getReturn();
  132. }
  133. }