TaobaoLianMengUtil.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Http\Utils\TaoBao;
  3. use App\Exceptions\CommonException;
  4. use App\Exceptions\TaobaoException;
  5. use App\Http\Enum\AccountTypeEnum;
  6. use App\Http\Enum\ErrorEnum;
  7. use App\Http\Enum\MeiTuanLinkTypeEnum;
  8. use App\Http\Utils\BaseUtil;
  9. use App\Http\Utils\LoggerFactoryUtil;
  10. use App\Models\UnionActivityModel;
  11. use App\Models\WechatSpreadAccountModel;
  12. use ETaobao\Factory;
  13. /**
  14. * 淘宝联盟工具类
  15. * Class TaobaoLianMengUtil
  16. * @package App\Http\Utils\TaoBao
  17. */
  18. class TaobaoLianMengUtil extends BaseUtil
  19. {
  20. /**
  21. * 获取淘宝联盟实例对象
  22. * @return \ETaobao\Tbk\Application
  23. */
  24. public static function getApp()
  25. {
  26. $config = config("taobao");
  27. return Factory::tbk($config);
  28. }
  29. /**
  30. * 获取官方活动信息接口
  31. * @param $adzone_id string 推广位ID
  32. * @param $activity_material_id string 活动ID
  33. */
  34. public static function getActivityInfo($adzone_id,$activity_material_id)
  35. {
  36. $app = self::getApp();
  37. $params = [
  38. "adzone_id"=>$adzone_id,
  39. "activity_material_id"=>$activity_material_id
  40. ];
  41. $res = $app->content->getActivityInfo($params);
  42. $res = json_decode(json_encode($res),true);
  43. if(isset($res["code"])){
  44. throw new TaobaoException($res["sub_msg"]);
  45. }
  46. return $res["data"];
  47. }
  48. /**
  49. * 获取私域用户邀请码
  50. * @param $codeType int 邀请码类型,1 - 渠道邀请,2 - 渠道裂变,3 -会员邀请
  51. * @param int $relationId 渠道关系ID
  52. */
  53. public static function getInviteCode($codeType, $relationId = 0)
  54. {
  55. $app = self::getApp();
  56. $params = [
  57. "relation_app"=>"common",
  58. "code_type"=>$codeType
  59. ];
  60. if($relationId){
  61. $params["relation_id"] = $relationId;
  62. }
  63. $res = $app->sc->getInviteCode($params);
  64. $res = json_decode(json_encode($res),true);
  65. if(isset($res["code"])){
  66. throw new TaobaoException($res["msg"]);
  67. }
  68. return $res["data"];
  69. }
  70. /**
  71. * 渠道备案
  72. * @param $inviteCode string 邀请码
  73. * @param $infoType int 类型,必选 默认为1:
  74. */
  75. public static function savePublisherInfo($inviteCode, $infoType)
  76. {
  77. $app = self::getApp();
  78. $params = [
  79. "inviter_code"=>$inviteCode,
  80. "info_type"=>$infoType
  81. ];
  82. $res = $app->sc->savePublisherInfo($params);
  83. $res = json_decode(json_encode($res),true);
  84. if(isset($res["code"])){
  85. throw new TaobaoException($res["sub_msg"]);
  86. }
  87. return $res["data"];
  88. }
  89. /**
  90. * 获取备案信息
  91. * @param $infoType int 类型,必选 1:渠道信息;2:会员信息
  92. * @param $id string 渠道或者会员ID
  93. * @param string $relationApp 默认common
  94. */
  95. public static function getPublisherInfo($infoType,$id,$relationApp="common")
  96. {
  97. $app = self::getApp();
  98. $params = [
  99. "info_type"=>$infoType,
  100. "relation_app"=>$relationApp
  101. ];
  102. switch ($infoType){
  103. case 1:
  104. $params["relation_id"] = $id;
  105. break;
  106. case 2:
  107. $params["special_id"] = $id;
  108. break;
  109. }
  110. $res = $app->sc->getPublisherInfo($params);
  111. $res = json_decode(json_encode($res),true);
  112. if(isset($res["code"])){
  113. throw new TaobaoException($res["sub_msg"]);
  114. }
  115. return $res["data"];
  116. }
  117. /**
  118. * 获取拉新用户订单
  119. * @param $activityId string 活动ID
  120. * @param int $page int 第几页
  121. */
  122. public static function getNewOrder($activityId, $page = 1)
  123. {
  124. $app = self::getApp();
  125. $params = [
  126. "page_no"=>$page,
  127. "activity_id"=>$activityId
  128. ];
  129. $res = $app->dg->getOrderNewUser($params);
  130. $res = json_decode(json_encode($res),true);
  131. if(isset($res["code"])){
  132. throw new TaobaoException($res["sub_msg"]);
  133. }
  134. return $res["results"];
  135. }
  136. /**
  137. * 获取淘口令接口
  138. * @param $url string 地址
  139. */
  140. public static function taokoulingCreate($url)
  141. {
  142. $app = self::getApp();
  143. $params = [
  144. "url"=>$url
  145. ];
  146. $res = $app->tpwd->create($params);
  147. $res = json_decode(json_encode($res),true);
  148. if(isset($res["code"])){
  149. throw new TaobaoException($res["sub_msg"]);
  150. }
  151. return $res["data"];
  152. }
  153. /**
  154. * 获取饿了么外卖推广链接
  155. * @param $userId string 用户ID
  156. * @param $actId string 活动ID
  157. * @param string $linkType string 类型
  158. * @param string $spreadId 推广位
  159. */
  160. public static function getElmWaimaiSpreadUrl($userId, $actId, $linkType="mini",$spreadId = "111538400047")
  161. {
  162. $key = "elmUrl:".$userId."linktype:".$linkType."actid:".$actId;
  163. //判断缓存中是否存在
  164. $val = self::getCacheFromRedis($key);
  165. if($val){
  166. return $val;
  167. }
  168. //不存在则从官方获取
  169. $res = self::getActivityInfo($spreadId,$actId);
  170. switch ($linkType){
  171. case "h5":
  172. //h5链接
  173. $val = $res["short_click_url"];
  174. break;
  175. case "mini":
  176. //小程序
  177. $val = $res["wx_miniprogram_path"];
  178. break;
  179. }
  180. self::setRedisCache($key,$val,0);
  181. return $val;
  182. }
  183. }