当前位置: 首页 > 工具软件 > TP-COUPON > 使用案例 >

有赞微商城接口tp5

何哲
2023-12-01
/** * 调用接口, $data是数组参数 * @return  */
function http_request($url, $data = null, $headers=array()) {
    $curl = curl_init();
    if( count($headers) >= 1 ){
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    }
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

//获取有赞access_token
function getYouZanAccessToken(){
    $api=db('youzan')->find(1);
    if($api['time']<=time() || !$api['token']){
        $url='https://open.youzanyun.com/auth/token';
        $he=['Content-Type:application/json'];
        $post=[
            "authorize_type"=>"silent",
            "client_id"=>config('site.client_id'),
            "client_secret"=>config('site.client_secret'),
            "grant_id"=> config('site.grant_id'),
            "refresh"=> false
        ];
        $ret=http_request($url,json_encode($post),$he);
        $ret=\GuzzleHttp\json_decode($ret,true);
        if($ret['code']==200){
            $access_token=$ret['data']['access_token'];
            db('youzan')->where('id',1)->update(['token'=>$access_token,'time'=>time()+7*24*3600/*$ret['data']['expires']*/]);
        }else{
            exception($ret['message']);
        }
    }else{
        $access_token=$api['token'];
    }
    return $access_token;
}

//有赞创建客户到店铺   https://doc.youzanyun.com/detail/API/0/92
function createUser($mobile){
    $access_token=getYouZanAccessToken();
    $url='https://open.youzanyun.com/api/youzan.scrm.customer.create/3.0.0?access_token='.$access_token;
    $params =[
        "mobile"=>$mobile,
    ];
    $ret=http_request($url,$params);
    $ret=\GuzzleHttp\json_decode($ret,true);
//    dump($ret);die;
    if($ret['code']==200){
        return ['code'=>1];
    }else{
        return ['code'=>0,'msg'=>$ret['message']];
    }
}

//有赞发放优惠券优惠码  https://doc.youzanyun.com/detail/API/0/979
function voucheractivity($mobile,$activity_id){
    $access_token=getYouZanAccessToken();
    $url='https://open.youzanyun.com/api/youzan.ump.voucheractivity.send/3.0.1?access_token='.$access_token;
    $params =[
        "activity_id"=>$activity_id,
        'mobile'=>$mobile
    ];
    $ret=http_request($url,$params);
    $ret=\GuzzleHttp\json_decode($ret,true);
//    dump($ret);die;
    if($ret['code']==200){
        return ['code'=>1];
    }else{
        return ['code'=>0,'msg'=>$ret['message']];
    }
}

//有赞获取所有未结束的优惠卷/码活动列表  https://doc.youzanyun.com/detail/API/0/160
function getActivity(){
    $access_token=getYouZanAccessToken();
    $url='https://open.youzanyun.com/api/youzan.ump.coupons.unfinished.search/3.0.0?access_token='.$access_token;
    $params =['fields'=>'group_id,title,stat_fetch_num,total,value,description,stock,status,is_at_least,at_least'];
    $ret=http_request($url,$params);
    $ret=\GuzzleHttp\json_decode($ret,true);
//    dump($ret);die;
    if($ret['code']==200){
    	db()->query("truncate fa_coupon");
        $list=$ret['data']['coupons'];
        foreach($list as $k=>$v){
            unset($v['created']);
            $v['id']=$v['group_id'];
            unset($v['group_id']);
            db('coupon')->insert($v);
            //$r=db('coupon')->where('id',$v['id'])->find();
            //if($r){
            //    db('coupon')->update($v);
            //}else{
            //    db('coupon')->insert($v);
            //}
        }
        return ['code'=>1,'msg'=>'ok'];
    }else{
        return ['code'=>0,'msg'=>$ret['message']];
    }
}

//获取用户的权益卡列表    https://doc.youzanyun.com/detail/API/0/869
function cardlist($page,$mobile){
    $access_token=getYouZanAccessToken();
    $url='https://open.youzanyun.com/api/youzan.scrm.customer.card.list/4.0.0?access_token='.$access_token;
    $params =[
        "params"=>[
            "page_no"=>"$page",
            "user"=>[
                "account_type"=>"2",
                "account_id"=>"$mobile"
            ]
        ]
    ];
    $he=['Content-Type:application/json'];
    $ret=http_request($url,json_encode($params),$he);
    $ret=\GuzzleHttp\json_decode($ret,true);
    if($ret['code']==200){
        return ['code'=>1,'data'=>$ret['data']];
    }else{
        return ['code'=>0,'msg'=>$ret['message']];
    }
}

//获取出售中的商品列表        https://doc.youzanyun.com/detail/API/0/45
function getGoodsList($page,$page_size=100){
    $access_token=getYouZanAccessToken();
    $url='https://open.youzanyun.com/api/youzan.items.onsale.get/3.0.0?access_token='.$access_token;
    $params =[
        'page_no'=>$page,//页码
        'page_size'=>$page_size
    ];
    $ret=http_request($url,$params);
    $ret=\GuzzleHttp\json_decode($ret,true);
    if($ret['code']==200){
        return ['code'=>1,'data'=>$ret['data']];
    }else{
        return ['code'=>0,'msg'=>$ret['message']];
    }
}

//全量或增量方式更新SKU库存    https://doc.youzanyun.com/detail/API/0/48
function updategoods($item_id,$num,$sku_id=0,$type=1){
    $access_token=getYouZanAccessToken();
    $url='https://open.youzanyun.com/api/youzan.item.quantity.update/3.0.0?access_token='.$access_token;
    $params =[
        "item_id"=>$item_id,//商品Id
        'quantity'=>$num,//规格库存数量
        'type'=>$type,//库存更新方式,0—全量更新,1—增量更新(可选,默认为0)
    ];
    if($sku_id){
        $params['sku_id']=$sku_id;//商品规格Id,微商城店铺商品规格标识(同一商品Id下,规格id唯一
    }
    $ret=http_request($url,$params);
    $ret=\GuzzleHttp\json_decode($ret,true);
//    dump($ret);die;
    if($ret['code']==200){
        if(!$ret['data']['is_success']){
            return ['code'=>0,'msg'=>$ret['message']];
        }
        return ['code'=>1];
    }else{
        return ['code'=>0,'msg'=>$ret['message']];
    }
}

//获取客户列表        https://doc.youzanyun.com/detail/API/0/2203
function getCustomer($page,$page_size=20){
    $access_token=getYouZanAccessToken();
    $url='https://open.youzanyun.com/api/youzan.scrm.customer.list/1.0.0?access_token='.$access_token;
    $params =[
        'has_mobile'=>true,
        'page_no'=>$page,//页码
        'page_size'=>$page_size
    ];
    $he=['Content-Type:application/json'];
    $ret=http_request($url,json_encode($params),$he);
    $ret=\GuzzleHttp\json_decode($ret,true);
    if($ret['code']==200){
        return ['code'=>1,'data'=>$ret['data']];
    }else{
        return ['code'=>0,'msg'=>$ret['message']];
    }
}
 //更新用户
    public function updatas(){
        $page=1;$page_size=50;
        do{
            $res=getCustomer($page,$page_size);
            if($res['code']==1){
                $list=$res['data']['record_list'];
                foreach($list as $value){
                    $arr=[];
                    $arr['mobile']=@$value['mobile'];
                    $users=db('user')->where($arr)->find();
                    $arr['gender']=$value['gender']==1?1:0;
                    $arr['nickname']=$value['show_name'];
                    if(!$users){
                        $arr['jointime']=$value['created_at'];
                        $arr['status']='normal';
                        db('user')->insert($arr);
                    }else{
                        db('user')->where('id',$users['id'])->update($arr);
                    }
                }
                $page++;
            }else{
                return json($res);
            }
        } while($page-1<$res['data']['total']/$page_size);
        $this->success('成功获取'.$res['data']['total'].'条记录');
    }
 类似资料: