TaobaoLianMengUtil.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 App\Models\TaobaoPidModel;
  7. use ETaobao\Factory;
  8. /**
  9. * 淘宝联盟工具类
  10. * Class TaobaoLianMengUtil
  11. * @package App\Http\Utils\TaoBao
  12. */
  13. class TaobaoLianMengUtil extends BaseUtil
  14. {
  15. /**
  16. * 获取淘宝联盟实例对象
  17. * @return \ETaobao\Tbk\Application
  18. */
  19. public static function getApp()
  20. {
  21. $config = config("taobao");
  22. return Factory::tbk($config);
  23. }
  24. /**
  25. * 获取官方活动信息接口
  26. * @param $adzone_id string 推广位ID
  27. * @param $activity_material_id string 活动ID
  28. */
  29. public static function getActivityInfo($adzone_id,$activity_material_id)
  30. {
  31. $instance = new LoggerFactoryUtil(TaobaoLianMengUtil::class);
  32. //根据pid找到淘宝信息
  33. $taobao = TaobaoPidModel::query()
  34. ->where("adzone_id",$adzone_id)
  35. ->first();
  36. $config = [
  37. "appkey"=>$taobao["taobao_app_key"],//淘宝客appKey
  38. "secretKey"=>$taobao["taobao_app_secret"],//secretKey
  39. "format"=>"json",//返回的数据格式
  40. "session"=>"",//授权接口(sc类的接口)需要带上
  41. "sandbox"=>false,
  42. ];
  43. $app = Factory::tbk($config);
  44. $params = [
  45. "adzone_id"=>$adzone_id,
  46. "activity_material_id"=>$activity_material_id
  47. ];
  48. $res = $app->content->getActivityInfo($params);
  49. $instance->info("淘宝返回数据:".$res);
  50. $res = json_decode(json_encode($res),true);
  51. if(isset($res["code"])){
  52. throw new TaobaoException($res["sub_msg"]);
  53. }
  54. return $res["data"];
  55. }
  56. /**
  57. * 获取私域用户邀请码
  58. * @param $codeType int 邀请码类型,1 - 渠道邀请,2 - 渠道裂变,3 -会员邀请
  59. * @param int $relationId 渠道关系ID
  60. */
  61. public static function getInviteCode($codeType, $relationId = 0)
  62. {
  63. $app = self::getApp();
  64. $params = [
  65. "relation_app"=>"common",
  66. "code_type"=>$codeType
  67. ];
  68. if($relationId){
  69. $params["relation_id"] = $relationId;
  70. }
  71. $res = $app->sc->getInviteCode($params);
  72. $res = json_decode(json_encode($res),true);
  73. if(isset($res["code"])){
  74. throw new TaobaoException($res["msg"]);
  75. }
  76. return $res["data"];
  77. }
  78. /**
  79. * 渠道备案
  80. * @param $inviteCode string 邀请码
  81. * @param $infoType int 类型,必选 默认为1:
  82. */
  83. public static function savePublisherInfo($inviteCode, $infoType)
  84. {
  85. $app = self::getApp();
  86. $params = [
  87. "inviter_code"=>$inviteCode,
  88. "info_type"=>$infoType
  89. ];
  90. $res = $app->sc->savePublisherInfo($params);
  91. $res = json_decode(json_encode($res),true);
  92. if(isset($res["code"])){
  93. throw new TaobaoException($res["sub_msg"]);
  94. }
  95. return $res["data"];
  96. }
  97. /**
  98. * 获取备案信息
  99. * @param $infoType int 类型,必选 1:渠道信息;2:会员信息
  100. * @param $id string 渠道或者会员ID
  101. * @param string $relationApp 默认common
  102. */
  103. public static function getPublisherInfo($infoType,$id,$relationApp="common")
  104. {
  105. $app = self::getApp();
  106. $params = [
  107. "info_type"=>$infoType,
  108. "relation_app"=>$relationApp
  109. ];
  110. switch ($infoType){
  111. case 1:
  112. $params["relation_id"] = $id;
  113. break;
  114. case 2:
  115. $params["special_id"] = $id;
  116. break;
  117. }
  118. $res = $app->sc->getPublisherInfo($params);
  119. $res = json_decode(json_encode($res),true);
  120. if(isset($res["code"])){
  121. throw new TaobaoException($res["sub_msg"]);
  122. }
  123. return $res["data"];
  124. }
  125. /**
  126. * 获取拉新用户订单
  127. * @param $activityId string 活动ID
  128. * @param int $page int 第几页
  129. */
  130. public static function getNewOrder($activityId, $page = 1)
  131. {
  132. $app = self::getApp();
  133. $params = [
  134. "page_no"=>$page,
  135. "activity_id"=>$activityId
  136. ];
  137. $res = $app->dg->getOrderNewUser($params);
  138. $res = json_decode(json_encode($res),true);
  139. if(isset($res["code"])){
  140. throw new TaobaoException($res["sub_msg"]);
  141. }
  142. return $res["results"];
  143. }
  144. /**
  145. * 获取淘口令接口
  146. * @param $url string 地址
  147. * @param $adzone_id string 推广位
  148. */
  149. public static function taokoulingCreate($url,$adzone_id)
  150. {
  151. //根据pid找到淘宝信息
  152. $taobao = TaobaoPidModel::query()
  153. ->where("adzone_id",$adzone_id)
  154. ->first();
  155. $config = [
  156. "appkey"=>$taobao["taobao_app_key"],//淘宝客appKey
  157. "secretKey"=>$taobao["taobao_app_secret"],//secretKey
  158. "format"=>"json",//返回的数据格式
  159. "session"=>"",//授权接口(sc类的接口)需要带上
  160. "sandbox"=>false,
  161. ];
  162. $app = Factory::tbk($config);
  163. $params = [
  164. "url"=>$url
  165. ];
  166. $res = $app->tpwd->create($params);
  167. $res = json_decode(json_encode($res),true);
  168. if(isset($res["code"])){
  169. throw new TaobaoException($res["sub_msg"]);
  170. }
  171. return $res["data"];
  172. }
  173. /**
  174. * 淘口令解析&转链
  175. * @param $kouling 淘口令
  176. * @param $adzone_id 推广位
  177. */
  178. public static function taokoulingConvert($kouling,$adzone_id)
  179. {
  180. //根据pid找到淘宝信息
  181. $taobao = TaobaoPidModel::query()
  182. ->where("adzone_id",$adzone_id)
  183. ->first();
  184. $config = [
  185. "appkey"=>$taobao["taobao_app_key"],//淘宝客appKey
  186. "secretKey"=>$taobao["taobao_app_secret"],//secretKey
  187. "format"=>"json",//返回的数据格式
  188. "session"=>"",//授权接口(sc类的接口)需要带上
  189. "sandbox"=>false,
  190. ];
  191. $app = Factory::tbk($config);
  192. $params = [
  193. "password_content"=>$kouling,
  194. "adzone_id"=>$adzone_id
  195. ];
  196. $res = $app->tpwd->httpPost("taobao.tbk.tpwd.convert",$params);
  197. $instance = new LoggerFactoryUtil(TaobaoLianMengUtil::class);
  198. $instance->info("淘宝返回数据:".json_encode($res));
  199. $res = json_decode(json_encode($res),true);
  200. if(isset($res["code"])){
  201. throw new TaobaoException($res["sub_msg"]);
  202. }
  203. return $res["data"];
  204. }
  205. /**
  206. * 获取商品详情
  207. * @param $goodsId
  208. * @return mixed
  209. * @throws TaobaoException
  210. */
  211. public static function getGoodsInfo($goodsId,$adzone_id)
  212. {
  213. //根据pid找到淘宝信息
  214. $taobao = TaobaoPidModel::query()
  215. ->where("adzone_id",$adzone_id)
  216. ->first();
  217. $config = [
  218. "appkey"=>$taobao["taobao_app_key"],//淘宝客appKey
  219. "secretKey"=>$taobao["taobao_app_secret"],//secretKey
  220. "format"=>"json",//返回的数据格式
  221. "session"=>"",//授权接口(sc类的接口)需要带上
  222. "sandbox"=>false,
  223. ];
  224. $app = Factory::tbk($config);
  225. $params = [
  226. "num_iids"=>$goodsId,
  227. ];
  228. $res = $app->item->getInfo($params);
  229. $instance = new LoggerFactoryUtil(TaobaoLianMengUtil::class);
  230. $instance->info("淘宝返回数据:".json_encode($res));
  231. $res = json_decode(json_encode($res),true);
  232. if(isset($res["code"])){
  233. throw new TaobaoException($res["sub_msg"]);
  234. }
  235. return $res["results"]["n_tbk_item"][0];
  236. }
  237. /**
  238. * 获取饿了么外卖推广链接
  239. * @param $userId string 用户ID
  240. * @param $actId string 活动ID
  241. * @param string $linkType string 类型
  242. * @param string $spreadId 推广位
  243. */
  244. public static function getElmWaimaiSpreadUrl($userId, $actId, $linkType="mini",$spreadId = "111538400047")
  245. {
  246. $key = "elmUrl:".$userId."linktype:".$linkType."actid:".$actId;
  247. //判断缓存中是否存在
  248. $val = self::getCacheFromRedis($key);
  249. if($val){
  250. return $val;
  251. }
  252. //不存在则从官方获取
  253. $res = self::getActivityInfo($spreadId,$actId);
  254. switch ($linkType){
  255. case "h5":
  256. //h5链接
  257. $val = $res["short_click_url"];
  258. break;
  259. case "mini":
  260. //小程序
  261. $val = $res["wx_miniprogram_path"];
  262. break;
  263. }
  264. self::setRedisCache($key,$val,0);
  265. return $val;
  266. }
  267. }