123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <?php
- namespace App\Http\Utils\TaoBao;
- use App\Exceptions\TaobaoException;
- use App\Http\Utils\BaseUtil;
- use App\Http\Utils\LoggerFactoryUtil;
- use App\Models\TaobaoPidModel;
- use ETaobao\Factory;
- /**
- * 淘宝联盟工具类
- * Class TaobaoLianMengUtil
- * @package App\Http\Utils\TaoBao
- */
- class TaobaoLianMengUtil extends BaseUtil
- {
- /**
- * 获取淘宝联盟实例对象
- * @return \ETaobao\Tbk\Application
- */
- public static function getApp()
- {
- $config = config("taobao");
- return Factory::tbk($config);
- }
- /**
- * 获取官方活动信息接口
- * @param $adzone_id string 推广位ID
- * @param $activity_material_id string 活动ID
- */
- public static function getActivityInfo($adzone_id,$activity_material_id)
- {
- $instance = new LoggerFactoryUtil(TaobaoLianMengUtil::class);
- //根据pid找到淘宝信息
- $taobao = TaobaoPidModel::query()
- ->where("adzone_id",$adzone_id)
- ->first();
- $config = [
- "appkey"=>$taobao["taobao_app_key"],//淘宝客appKey
- "secretKey"=>$taobao["taobao_app_secret"],//secretKey
- "format"=>"json",//返回的数据格式
- "session"=>"",//授权接口(sc类的接口)需要带上
- "sandbox"=>false,
- ];
- $app = Factory::tbk($config);
- $params = [
- "adzone_id"=>$adzone_id,
- "activity_material_id"=>$activity_material_id
- ];
- $res = $app->content->getActivityInfo($params);
- $instance->info("淘宝返回数据:".$res);
- $res = json_decode(json_encode($res),true);
- if(isset($res["code"])){
- throw new TaobaoException($res["sub_msg"]);
- }
- return $res["data"];
- }
- /**
- * 获取私域用户邀请码
- * @param $codeType int 邀请码类型,1 - 渠道邀请,2 - 渠道裂变,3 -会员邀请
- * @param int $relationId 渠道关系ID
- */
- public static function getInviteCode($codeType, $relationId = 0)
- {
- $app = self::getApp();
- $params = [
- "relation_app"=>"common",
- "code_type"=>$codeType
- ];
- if($relationId){
- $params["relation_id"] = $relationId;
- }
-
- $res = $app->sc->getInviteCode($params);
- $res = json_decode(json_encode($res),true);
- if(isset($res["code"])){
- throw new TaobaoException($res["msg"]);
- }
- return $res["data"];
- }
- /**
- * 渠道备案
- * @param $inviteCode string 邀请码
- * @param $infoType int 类型,必选 默认为1:
- */
- public static function savePublisherInfo($inviteCode, $infoType)
- {
- $app = self::getApp();
- $params = [
- "inviter_code"=>$inviteCode,
- "info_type"=>$infoType
- ];
- $res = $app->sc->savePublisherInfo($params);
- $res = json_decode(json_encode($res),true);
- if(isset($res["code"])){
- throw new TaobaoException($res["sub_msg"]);
- }
- return $res["data"];
- }
- /**
- * 获取备案信息
- * @param $infoType int 类型,必选 1:渠道信息;2:会员信息
- * @param $id string 渠道或者会员ID
- * @param string $relationApp 默认common
- */
- public static function getPublisherInfo($infoType,$id,$relationApp="common")
- {
- $app = self::getApp();
- $params = [
- "info_type"=>$infoType,
- "relation_app"=>$relationApp
- ];
- switch ($infoType){
- case 1:
- $params["relation_id"] = $id;
- break;
- case 2:
- $params["special_id"] = $id;
- break;
- }
- $res = $app->sc->getPublisherInfo($params);
- $res = json_decode(json_encode($res),true);
- if(isset($res["code"])){
- throw new TaobaoException($res["sub_msg"]);
- }
- return $res["data"];
- }
- /**
- * 获取拉新用户订单
- * @param $activityId string 活动ID
- * @param int $page int 第几页
- */
- public static function getNewOrder($activityId, $page = 1)
- {
- $app = self::getApp();
- $params = [
- "page_no"=>$page,
- "activity_id"=>$activityId
- ];
- $res = $app->dg->getOrderNewUser($params);
- $res = json_decode(json_encode($res),true);
- if(isset($res["code"])){
- throw new TaobaoException($res["sub_msg"]);
- }
- return $res["results"];
- }
- /**
- * 获取淘口令接口
- * @param $url string 地址
- * @param $adzone_id string 推广位
- */
- public static function taokoulingCreate($url,$adzone_id)
- {
- //根据pid找到淘宝信息
- $taobao = TaobaoPidModel::query()
- ->where("adzone_id",$adzone_id)
- ->first();
- $config = [
- "appkey"=>$taobao["taobao_app_key"],//淘宝客appKey
- "secretKey"=>$taobao["taobao_app_secret"],//secretKey
- "format"=>"json",//返回的数据格式
- "session"=>"",//授权接口(sc类的接口)需要带上
- "sandbox"=>false,
- ];
- $app = Factory::tbk($config);
- $params = [
- "url"=>$url
- ];
- $res = $app->tpwd->create($params);
- $res = json_decode(json_encode($res),true);
- if(isset($res["code"])){
- throw new TaobaoException($res["sub_msg"]);
- }
- return $res["data"];
- }
- /**
- * 淘口令解析&转链
- * @param $kouling 淘口令
- * @param $adzone_id 推广位
- */
- public static function taokoulingConvert($kouling,$adzone_id)
- {
- //根据pid找到淘宝信息
- $taobao = TaobaoPidModel::query()
- ->where("adzone_id",$adzone_id)
- ->first();
- $config = [
- "appkey"=>$taobao["taobao_app_key"],//淘宝客appKey
- "secretKey"=>$taobao["taobao_app_secret"],//secretKey
- "format"=>"json",//返回的数据格式
- "session"=>"",//授权接口(sc类的接口)需要带上
- "sandbox"=>false,
- ];
- $app = Factory::tbk($config);
- $params = [
- "password_content"=>$kouling,
- "adzone_id"=>$adzone_id
- ];
- $res = $app->tpwd->httpPost("taobao.tbk.tpwd.convert",$params);
- $instance = new LoggerFactoryUtil(TaobaoLianMengUtil::class);
- $instance->info("淘宝返回数据:".json_encode($res));
- $res = json_decode(json_encode($res),true);
- if(isset($res["code"])){
- throw new TaobaoException($res["sub_msg"]);
- }
- return $res["data"];
- }
- /**
- * 获取商品详情
- * @param $goodsId
- * @return mixed
- * @throws TaobaoException
- */
- public static function getGoodsInfo($goodsId,$adzone_id)
- {
- //根据pid找到淘宝信息
- $taobao = TaobaoPidModel::query()
- ->where("adzone_id",$adzone_id)
- ->first();
- $config = [
- "appkey"=>$taobao["taobao_app_key"],//淘宝客appKey
- "secretKey"=>$taobao["taobao_app_secret"],//secretKey
- "format"=>"json",//返回的数据格式
- "session"=>"",//授权接口(sc类的接口)需要带上
- "sandbox"=>false,
- ];
- $app = Factory::tbk($config);
- $params = [
- "num_iids"=>$goodsId,
- ];
- $res = $app->item->getInfo($params);
- $instance = new LoggerFactoryUtil(TaobaoLianMengUtil::class);
- $instance->info("淘宝返回数据:".json_encode($res));
- $res = json_decode(json_encode($res),true);
- if(isset($res["code"])){
- throw new TaobaoException($res["sub_msg"]);
- }
- return $res["results"]["n_tbk_item"][0];
- }
- /**
- * 获取饿了么外卖推广链接
- * @param $userId string 用户ID
- * @param $actId string 活动ID
- * @param string $linkType string 类型
- * @param string $spreadId 推广位
- */
- public static function getElmWaimaiSpreadUrl($userId, $actId, $linkType="mini",$spreadId = "111538400047")
- {
- $key = "elmUrl:".$userId."linktype:".$linkType."actid:".$actId;
- //判断缓存中是否存在
- $val = self::getCacheFromRedis($key);
- if($val){
- return $val;
- }
- //不存在则从官方获取
- $res = self::getActivityInfo($spreadId,$actId);
- switch ($linkType){
- case "h5":
- //h5链接
- $val = $res["short_click_url"];
- break;
- case "mini":
- //小程序
- $val = $res["wx_miniprogram_path"];
- break;
- }
- self::setRedisCache($key,$val,0);
- return $val;
- }
- }
|