<?php

//验证随机串
function yan_url($str)
{
    $Model = new Model() ;
    $weibo_data = $Model->query("select * from " . C('DB_PREFIX') . "weibo where goods_url='" . $str . "'") ;
    if ( count($weibo_data) <= 0 )
    {
        return FALSE ;
    } else
    {
        return TRUE ;
    }
}

//生成随机串
function rand_str()
{
    $str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
    $n = 8 ;
    $len = strlen($str) - 1 ;
    for ($i = 0 ; $i < $n ; $i++)
    {
        $s.=$str[rand(0 , $len)] ;
    }
    return $s ;
}

function goods_getarrayinfo($good_id , $host , $good_url)
{

    $return = '' ;
    if ( 'taobao.com' == $host || 'tmall.com' == $host )
    {
        // 生成随机串，判断数据库是否存在
        while (TRUE)
        {
            $goods_str = rand_str() ;
            $yan = yan_url($goods_str) ;
            if ( !$yan )
            {
                break ;
            }
        }
        $return['rand_url'] = $goods_str ;
        // 调用淘宝API
        include_once(SITE_PATH . '/addons/libs/goods/Taobao.php') ;
		$key = model( "Xdata" )->lget( "platform" );
		$taobaoke_key = $key['taobaoke_key'];
		$taobaoke_nick = $key['taobaoke_nick'];
        $goods = new Taobao ;
		$goods_data = $goods->get_taobaoke_array($taobaoke_nick , $good_id , $goods_str) ;
		$goods_fenlei = $goods->get_taobaoke_fenlei($goods_data['cid']) ;
		$goods_taobaoke_array=array_merge($goods_fenlei,$goods_data);
		
        // 如果该商品不是淘宝客商品，提取商品信息
        if ( $goods_taobaoke_array['num_iid'] != "" )
        {
            //商品类型
            $return['host'] = "taobao" ;
            //商品信息
            $return['data'] = $goods_taobaoke_array ;
            //判断是否是淘宝客商品
            $return['taoke'] = 1 ;
            return $return ;
        } else
        {

            $goods_arrayss = $goods->get_taobao_array($good_id) ;
			$goods_fenlei = $goods->get_taobaoke_fenlei($goods_arrayss['cid']) ;
		    $goods_array=array_merge($goods_fenlei,$goods_arrayss);
            //print_r($goods_array);
            if ( $goods_array )
            {
                //商品类型
                $return['host'] = "taobao" ;
                //商品信息
                $return['data'] = $goods_array ;
                //判断是否是淘宝客商品
                $return['taoke'] = 0 ;

                return $return ;
            } else
            {
                $return['error'] = 1 ;
                return $return ;
            }
        }
    }


    if ( 'paipai.com' == $host )
    {
        include_once(SITE_PATH . '/addons/libs/goods/Paipai.php') ;

        $paipai_array = get_paipai_goods($good_id , 'xml' , $good_url) ;
        if ( $paipai_array['error_Code'] == 0 )
        {
            $return['host'] = "paipai" ;
            $return['data']['title'] = $paipai_array['itemName'] ;
            $return['data']['price'] = $paipai_array['itemPrice'] / 100 ;
            $return['data']['pic_url'] = $paipai_array['picLink'] ;
            $return['data']['click_url'] = $good_url ;
            $return['taoke'] = 0 ;
            while (TRUE)
            {
                $goods_str = rand_str() ;
                $yan = yan_url($goods_str) ;
                if ( !$yan )
                {
                    break ;
                }
            }
            $return['rand_url'] = $goods_str ;

            return $return ;
        }
    }
}

?>