TaobaoLianMengUtil.php 5.8 KB

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