TaobaoLianMengUtil.php 8.5 KB

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