<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 15/11/3
 * Time: 下午7:31
 * 从杭州那边同步获取数据，
 */

require_once (BASE_CORE_PATH . '/framework/function/http.php');
class page_req
{
    private $total = '10';
    private $leftcount;
    private $dispatch = NULL;

    static private $page_url = 'http://crm-api.lrlz.com/api_app/api_main.py';
    static private $page_method = 'api.info.item.list';
    static private $item_method = 'api.info.item.get';

    public function __construct() {
        $this->dispatch = new pretreat_tmdata();
    }

    public function proc_page($shopname,$date,$method)
    {
        $curpage = 1;
        $pagesize = 10;

        do
        {
            $params = array('seq' => $this->init_sqe(),
                'method' => page_req::$page_method,
                'page' => $curpage,
                'pagesize' => $pagesize,
                'nick' => $shopname,
                'jdp_modified' => $date);

            $resp = http_request(page_req::$page_url,$params);
            if(empty($resp)) {
                break;
            }

            $data = $this->parse_page($resp);
            if(empty($data)) {
                break;
            }

            $curpage += 1;
            $this->leftcount -= count($data);

            foreach ($data as $num_iid) {
                echo "$num_iid\r\n";
                $body = $this->req_item($num_iid);
                $this->dispatch->$method($body);
            }

        } while($this->leftcount > 0);
    }

    private function req_item($num_iid)
    {
        $params = array('seq' => $this->init_sqe(),
            'method' => page_req::$item_method,
            'num_iid' => $num_iid);
        $resp = http_request(page_req::$page_url,$params);
        $str = iconv("gbk","utf-8",$resp);

        return $str;
    }

    private function parse_page($body)
    {
        $data = json_decode($body, true);

        if($data['success']=="true")
        {
            $this->total = intval(trim($data['total']));
            $this->curpage = intval(trim($data["page"]));

            if(!isset($this->leftcount)) {
                $this->leftcount = $this->total;
            }

            $infos = $data["info"];
            $count = count($infos);

            $items = array();
            for ($i = 0; $i < $count; $i++) {
                $num_iid = $infos[$i]['num_iid'];
                array_push($items, $num_iid);
            }
            return $items;
        }else{
            return NULL;
        }
    }

    /*
    * 生成sqe
    * 生成规则：
     加密的当前时间戳(精确到秒)+4位随机数 18位数字(yyyymmddhhmmss+rrrr)
     加密算法为 字符串反向 后 各偶数位x=9-x
     2015-0106-010203-1234
     ->4[3]2[1]3[0]2[0]1[0]6[0]1[0]5[1]0[2]
     ->5[3]7[1]6[0]7[0]8[0]3[0]8[0]4[1]9[2]
    */
    private function init_sqe()
    {
        date_default_timezone_set('Etc/GMT-8');
        $d =  date('YmdHis',time());
        $r = rand(1000,9999);
        $str = strrev($d.$r);
        $str2 = "";
        for ($i=0; $i < strlen($str); $i++)
        {
            if($i % 2 == 0) {
                $str2 = $str2.(intval(9-$str[$i]));
            } else {
                $str2 = $str2.$str[$i];
            }
        }
        return $str2;
    }
}
