记拼多多 快团团 php 快团团创建团购接口 增加商品库存接口 规格创建 上传商品图等接口

    public function __construct(){
        parent::__construct();
        $user_id = $this->user_id;
        $user = $this->user;
        $tokenData = DB::name('ktt_user_token')->where('user_id',$user_id)->find();

        if(!$tokenData){
            echo json_encode(["errcode"=>11,"errmsg"=>'此账号未进行快团团授权,请联系客服']);
            die;
        }
        if($tokenData['expires_at'] < time()){
            echo json_encode(["errcode"=>11,"errmsg"=>'快团团授权已过期,请联系客服']);
            die;
        }
        $this->tokenData = $tokenData;
    
    }


    /*pdd.ktt.group.create快团团创建团购接口*/
    public function groupCreate(){
        $pdd = new Pdd();
        $user = $this->user;
        $tokenData = $this->tokenData;
        
        if($user['point'] <= 0){
             return json(['errcode'=>-1,'errmsg'=>'积分不足请充值']);
        }
        
        $goods_id = input('goods_id',0);
        
        if(!$goods_id){
            return json(['errcode'=>-1,'errmsg'=>'请选择正确商品']);
        }

        $goodsInfo = Db::name('goods')->where('goods_id',$goods_id)->find();
        if($goodsInfo['stock'] <= 5){
            return json(['errcode'=>-1,'errmsg'=>'库存较少,暂不支持团购']);
        }        
        if($goodsInfo['is_on_sale'] == 0){
            return json(['errcode'=>-1,'errmsg'=>'此商品已下架']);
        }
        
        
        $cat_name = getCatName($goodsInfo['cat_id']);
        
        if(!$cat_name){
            return json(['errcode'=>-1,'errmsg'=>'此商品暂无分类']);
        }
        // 规格处理
        $specArr = [];
        $spec_goods = Db::name('goods_spec_goods')->where('goods_id',$goods_id)->select();

        if($spec_goods){
            $count_spec = count($spec_goods);
            foreach ($spec_goods as $k=>$v) {
                $temp_spec_arr = explode(' ',trim($v['key_name'],' '));
                // $specArr = [];
                $v_temp = [];
                foreach ($temp_spec_arr as $ksa=>$vsa) {
                    $temp_spec_arr2 = explode(':',trim($vsa,':'));
                    $v_temp[$temp_spec_arr2[0]][] = $temp_spec_arr2[1];

                }
                $specArr[] = $v_temp;

            }
            $spec_id_list = [];
            foreach ($specArr as $sv) {
                // dump($sv);
                $temp_spec = [];
                foreach ($sv as $sp_k=>$sp_v) {
                    $spec_list = [$sp_k=>$sp_v];
                    $spec_map = $this->goodsUpSpec($spec_list);
                    $spec_id = $spec_map[$sp_k][0]['spec_id'];
                    $temp_spec[] = $spec_id;
                }
                $spec_id_list[] = $temp_spec;
            }
        }else{
            $spec_id_list = [];
            
            $spec_str = $goodsInfo['goods_spec'].''.$goodsInfo['goods_cap'].' '.$goodsInfo['color'];
            $specArr = array('规格'=>[$spec_str]);
            $spec_map = $this->goodsUpSpec($specArr);
            // dump($spec_str);
            $spec_id = '';
            if($spec_map){
                $spec_id = $spec_map['规格'][0]['spec_id'];
                $spec_id_list = [$spec_id];
            } 
        }
        
        
        $goods_thumb = '';
        if($goodsInfo['goods_thumb']){
            $goods_thumb = $this->goodsUpImg($goodsInfo['goods_thumb']);
            // dump($goods_thumb);
            if(!$goods_thumb){
                return json(['errcode'=>-1,'errmsg'=>'商品图片有问题,请检查商品图片格式']);
            }
        }
        
       
        $params = array();
        /*数据 设置开团 3月*/
        $params['access_token'] = $tokenData['access_token'];
        $params['end_time'] = getMillisecond()+1000 * 60 *60 *24 * 30 *3;
        $params['start_time'] = getMillisecond();
        $params['is_save_preview'] = 1;
        $params['title'] = '开团';
        $params['isv_no'] = 'abc';
        
        /*商品数据*/
        $goodsParams = array();
        $skuParams = array();
        $goodsParams['category_name'] = $cat_name;
        $goodsParams['goods_desc'] = $goodsInfo['goods_brief']?:$goodsInfo['goods_name'];
        $goodsParams['goods_name'] = $goodsInfo['goods_name'];
        $goodsParams['limit_buy'] = 0;
        $goodsParams['market_price'] = intval($goodsInfo['market_price']*100);
        $goodsParams['pic_url_list'] = [$goods_thumb];
        
        $skuParams['external_sku_id'] = $goodsInfo['goods_sn'];
        $skuParams['price_in_fen'] = intval($goodsInfo['tuan_market_price']*100);
        $skuParams['quantity_type'] = 0;
        $skuParams['spec_id_list'] = $spec_id_list;
        $skuParams['total_quantity'] = $goodsInfo['stock'];
        $skuParams['thumb_url'] = $goods_thumb;
        
        if($count_spec > 0){
            for ($i = 0; $i < $count_spec; $i++) {
                $ii = $i+1;
                 $skuParams['external_sku_id'] = $goodsInfo['goods_sn'].'_'.$ii;
                 $skuParams['spec_id_list'] = $spec_id_list[$i];
                 $goodsParams['sku_list'][] = $skuParams;
            }
        }else{
            $goodsParams['sku_list'][] = $skuParams;
        }

        $params['goods_list'][] = $goodsParams;
        $res = $pdd->GetPDDApi('pdd.ktt.group.create',$params);

        $kttRes = $res['response'];
        /*请求成功 调用查询接口完成创建*/
        if($kttRes['success']){
            $queryRes = $this->groupQuery($kttRes['activity_no']);
            
            $insertData=array();
            $insertData['goods_id'] = $goodsInfo['goods_id'];
            $insertData['user_id'] = $tokenData['user_id'];
            $insertData['reg_user_id'] = $goodsInfo['reg_user_id'];
            $insertData['goods_sn'] = $goodsInfo['goods_sn'];
            $insertData['goods_name'] = $goodsInfo['goods_name'];
            $insertData['activity_no'] = $kttRes['activity_no'];
            $insertData['spec_id'] = $spec_id;
            $insertData['create_time'] = time();
            $logRes = DB::name('ktt_goods_add_log')->insert($insertData);
        }
        if($logRes){
            /*21.11 新加创建成功减少团长积分*/
            $this->pointGoods($goods_id);
            return json(['errcode'=>0,'errmsg'=>'创建成功']);
        }
        
        return json(['errcode'=>-1,'errmsg'=>'创建失败']);
        
    } 
    // pdd.ktt.goods.create.spec快团团上传商品图接口
    public function goodsUpSpec($specArr){
        $pdd = new Pdd();
        $tokenData = $this->tokenData;

        $params = array();
        /*数据*/
        $params['access_token'] = $tokenData['access_token'];
        $spec_map = $specArr;
        $params['spec_map'] = $spec_map;

        $res = $pdd->GetPDDApi('pdd.ktt.goods.create.spec',$params);
        return $res['response']['spec_map'];
        
    }       

    // /**/pdd.ktt.group.query.status快团团查询团购创建结果接口
    public function groupQuery($activity_no){
        $pdd = new Pdd();
        $tokenData = $this->tokenData;

        $params = array();
        /*数据*/
        $params['access_token'] = $tokenData['access_token'];
        $params['activity_no'] = $activity_no;
        
        $res = $pdd->GetPDDApi('pdd.ktt.group.query.status',$params);
        return $res;

    }     
    
    // pdd.ktt.goods.upload.image快团团商品规格创建接口
    public function goodsUpImg($url){
        $pdd = new Pdd();
        
        $tokenData = $this->tokenData;
        $params = array();
        /*数据*/
        
        $params['access_token'] = $tokenData['access_token'];
        $params['url'] = $url;
        $res = $pdd->GetPDDApi('pdd.ktt.goods.upload.image',$params);;
        return $res['response']['result'];
        
    }     

    // pdd.ktt.goods.incr.quantity 快团团ERP增加商品库存接口
    public function goodsincrQuantity($params){
        $pdd = new Pdd();
        $tokenData = $this->tokenData;
        /*数据*/
        
        $params['access_token'] = $tokenData['access_token'];
        // 1增加 2修改
        $params['modify_quantity_type'] = 2;
        $params['quantity_delta'] = 50;
        $res = $pdd->GetPDDApi('pdd.ktt.goods.incr.quantity',$params);
        return $res;
        
    } 
    
    /*pdd.ktt.increment.order.query快团团增量查订单*/
    public function incerOrder(){
        $pdd = new Pdd();
        $tokenData = $this->tokenData;

        $params = array();
        /*数据*/
        
        $params['access_token'] = $tokenData['access_token'];
        $params['start_updated_at'] = strtotime(date("Ymd"))*1000;
        $params['end_updated_at'] = getMillisecond();
        $params['page_number'] = 1;
        $params['page_size'] = 100;
        $res = $pdd->GetPDDApi('pdd.ktt.increment.order.query',$params);
        $order_list = $res['ktt_increment_order_query_response']['order_list'];
        
        
    } 
    
    /*pdd.ktt.order.list根据成交时间拉取订单列表*/
    public function kttOrderList(){
        $pdd = new Pdd();
        $tokenData = $this->tokenData;
        $goods_id = input('goods_id',0);
        $params = array();
        /*数据*/
        
        // $params['access_token'] = $tokenData['access_token'];
        // $params['confirm_at_end'] = getMillisecond();
        // $params['confirm_at_begin'] = strtotime(date("Ymd"))*1000;
        // $params['page_number'] = 1;
        // $params['page_size'] = 100;
        // $res = $pdd->GetPDDApi('pdd.ktt.order.list',$params);
        
        
        $params['access_token'] = $tokenData['access_token'];
        $params['start_updated_at'] = strtotime(date("Ymd"))*1000;
        $params['end_updated_at'] = getMillisecond();
        $params['page_number'] = 1;
        $params['page_size'] = 100;
        
        $res = $pdd->GetPDDApi('pdd.ktt.increment.order.query',$params);
        $order_list = $res['ktt_increment_order_query_response']['order_list'];
    }    
    

拼多多接口类看上篇随笔

posted @ 2022-01-05 11:34  Liiu  阅读(574)  评论(0编辑  收藏  举报