TaobaoLianMengUtil.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 $goodsId
  170. * @return mixed
  171. * @throws TaobaoException
  172. */
  173. public static function getGoodsInfo($goodsId)
  174. {
  175. $app = self::getApp();
  176. $params = [
  177. "num_iids"=>$goodsId,
  178. ];
  179. $res = $app->item->getInfo($params);
  180. $instance = new LoggerFactoryUtil(TaobaoLianMengUtil::class);
  181. $instance->info("淘宝返回数据:".json_encode($res));
  182. $res = json_decode(json_encode($res),true);
  183. if(isset($res["code"])){
  184. throw new TaobaoException($res["sub_msg"]);
  185. }
  186. return $res["results"]["n_tbk_item"][0];
  187. }
  188. /**
  189. * 获取饿了么外卖推广链接
  190. * @param $userId string 用户ID
  191. * @param $actId string 活动ID
  192. * @param string $linkType string 类型
  193. * @param string $spreadId 推广位
  194. */
  195. public static function getElmWaimaiSpreadUrl($userId, $actId, $linkType="mini",$spreadId = "111538400047")
  196. {
  197. $key = "elmUrl:".$userId."linktype:".$linkType."actid:".$actId;
  198. //判断缓存中是否存在
  199. $val = self::getCacheFromRedis($key);
  200. if($val){
  201. return $val;
  202. }
  203. //不存在则从官方获取
  204. $res = self::getActivityInfo($spreadId,$actId);
  205. switch ($linkType){
  206. case "h5":
  207. //h5链接
  208. $val = $res["short_click_url"];
  209. break;
  210. case "mini":
  211. //小程序
  212. $val = $res["wx_miniprogram_path"];
  213. break;
  214. }
  215. self::setRedisCache($key,$val,0);
  216. return $val;
  217. }
  218. }