TaobaoLianMengUtil.php 5.9 KB

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