Tools.php 8.5 KB
<?php

use Action\WebAction;
use WebPlugin\Cache;
use Configs\WebCacheConfig;
use WebPlugin\AuthCode;
use Index\HomeModel;
use Api\Yohobuy;
use Api\Sign;
use LibModels\Web\Home\IndexData;
use Index\BrandsModel;

class ToolsController extends WebAction
{

    public function init()
    {
        if (APPLICATION_ENV == 'production') {
            $valid_passwords = array('yoho' => '9646abcdef');
        }
        else {
            $valid_passwords = array('test' => '123456');
        }

        $valid_users = array_keys($valid_passwords);
        $user = $pass = '';
        if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_USER'])) {
            $user = $_SERVER['PHP_AUTH_USER'];
            $pass = $_SERVER['PHP_AUTH_PW'];
        }
        $validated = (in_array($user, $valid_users)) && ($pass == $valid_passwords[$user]);
        if (!$validated) {
            header('WWW-Authenticate: Basic realm="www.yohobuy.com"');
            header('HTTP/1.0 401 Unauthorized');
            die("Not authorized");
        }
    }

    /**
     * 工具首页
     */
    public function indexAction()
    {
        $html = '<link href="http://cdn.bootcss.com/bootstrap/2.3.2/css/bootstrap.min.css" rel="stylesheet"><div style="margin-left:100px;"><br/><h2>工具首页</h2>';
        $channels = array(
            'JAVA API' => '/tools/javaapi', 
            '缓存页面' => '/tools/cache',
            '服务器配置' =>'/tools/serverinfo',
            '服务器'=>'/tools/server',
        );
        $html.='<ol>';
        foreach ($channels as $name => $url) {
            $html .= sprintf('<li style="font-size:15px;height:35px;"><a href="%s" target="_blank">%s</a></li>', $url, $name);
        }
        $html.='</ol></div>';
        echo $html;
    }

    /**
     * java api测试
     */
    public function javaapiAction()
    {
        $html = '<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script><link href="http://cdn.bootcss.com/bootstrap/2.3.2/css/bootstrap.min.css" rel="stylesheet">
            <div style="margin-left:100px;"><h3 style="color:blue;">java api测试</h3>';
        $channels = array('获取资源位' => $this->getResourceUrl(), '获取最新上架' => '');
        foreach ($channels as $name => $url) {
            $html .= sprintf('<li style="font-size:20px;height:40px;"><a href="%s" target="_blank">%s</a></li>', $url, $name);
        }
        $html.='</div>';
        $html.= '<br/><div style="margin-left:100px;">生成参数接口地址:
            <div style="margin-top:20px;">
            <textarea id="params" style="margin: 0px 0px 10px; width: 654px; height: 90px;">url=http://testservice.yoho.cn:28077/operations/api/v5/resource/get&app_version=3.8.2&client_type=web&os_version=yohobuy%3Ah5&screen_size=720x1280&v=7&content_code=0c911d3000f52e8ca7cffb74f5864c29</textarea>(参数,可以传url,参数为url)</div>
            <div><input type="submit" id="btn" value="提交"/></div>
            <br/><span id="tips"></span>
            </div><script>
                $(function(){ 
                    $("#btn").bind("click", function()
                    {
                        $.ajax({
                            type:"post",
                            url:"/tools/buildurl",
                            data:{params:$("#params").val()},
                            success:function(data)
                            {
                                $("#tips").html("<a href=\""+data+"\" target=\"_blank\">" +data + "</a>");
                            }
                        });
                    })});
            </script>';
        echo $html;
    }
    
    /**
     * 生成url
     */
    public function buildurlAction()
    {
        $data = urldecode(trim($this->get('params','')));
        if(empty($data)) {
            $data = trim($this->post('params',''));
        }
        if(!empty($data)) {
            $params = array();
            $dataVals  = explode('&', $data);
            foreach($dataVals as $val) {
                $variable = array();
                if(strpos($val, '=')!== false) {
                    $variable = explode('=', $val);
                    $params[$variable[0]] = $variable[1];
                }
            }
            if(!empty($params)) {
               $params = array_merge($params, Yohobuy::param());
               unset($params['client_secret']);
               $client_type = 'web';
               $url = Yohobuy::API_URL;
               if(isset($params['client_type'])) {
                   $client_type = $params['client_type'];
               }
               if(isset($params['url'])) {
                   $url = $params['url'];
                   unset($params['url']);
               }
               $params['private_key'] = Yohobuy::$privateKeyList[$client_type];
               $params['client_secret'] = Sign::getSign($params);
               echo Yohobuy::httpBuildQuery($url, $params);
            }
        }
      
    }
    
    /**
     * 获取资源位地址
     * 
     * @return string
     */
    private function getResourceUrl()
    {
        $client_type = 'web';
        $content_code = '0c911d3000f52e8ca7cffb74f5864c29';
        $params = Yohobuy::param();
        $params['content_code'] = $content_code;
        $params['client_type'] = $client_type;
        $params['private_key'] = Yohobuy::$privateKeyList[$client_type];
        $params['client_secret'] = Sign::getSign($params);
        return Yohobuy::httpBuildQuery(Yohobuy::SERVICE_URL . IndexData::URL_OPERATIONS_RESOURCE_GET, $params);
    }

    /**
     * 缓存html
     */
    public function cacheAction()
    {
        //频道=>key
        $channels = array(
            '导航' => WebCacheConfig::KEY_WEB_HOME_NAVBAR_DATA,
            '男生首页' => WebCacheConfig::KEY_WEB_HOME_CHANNEL_DATA . '_' . HomeModel::CODE_BOYS_CHANNEL,
            '女生首页' => WebCacheConfig::KEY_WEB_HOME_CHANNEL_DATA . '_' . HomeModel::CODE_GIRLS_CHANNEL,
            'kids首页' => WebCacheConfig::KEY_WEB_HOME_CHANNEL_DATA . '_' . HomeModel::CODE_KIDS_CHANNEL,
            'lifestyle首页' => WebCacheConfig::KEY_WEB_HOME_CHANNEL_DATA . '_' . HomeModel::CODE_LIFESTYLE_CHANNEL,
            'plusstar男页' => WebCacheConfig::KEY_WEB_BRAND_PLUSSTARINDEX_DATA . '_' . BrandsModel::BRAND_PLUSSTARINDEX_BOYS,
            'plusstar女页' => WebCacheConfig::KEY_WEB_BRAND_PLUSSTARINDEX_DATA . '_' . BrandsModel::BRAND_PLUSSTARINDEX_GIRLS,
            '品牌一览-男生' => WebCacheConfig::KEY_WEB_BRAND_INDEX_VIEW_TOP_DATA . '_' . BrandsModel::BOYS,
            '品牌一览-女生' => WebCacheConfig::KEY_WEB_BRAND_INDEX_VIEW_TOP_DATA . '_' . BrandsModel::GIRLS,
            '品牌一览-潮童' => WebCacheConfig::KEY_WEB_BRAND_INDEX_VIEW_TOP_DATA . '_' . BrandsModel::KIDS,
            '品牌一览-创意生活' => WebCacheConfig::KEY_WEB_BRAND_INDEX_VIEW_TOP_DATA . '_' . BrandsModel::LIFESTYLE,
        );
        $html = '<link href="http://cdn.bootcss.com/bootstrap/2.3.2/css/bootstrap.min.css" rel="stylesheet"><br/>
            <div style="margin-left:100px;"><h3 style="color:red;">缓存页面</h3>';
        $html.='<ul>';
        foreach ($channels as $key => $val) {
            $url = '/tools/cacheclear?key=' . urlencode(AuthCode::encode($val, 'yohobuy'));
            $html.= sprintf('<li style="height:30px;"><a href="%s" target="_blank">清除%s缓存</a></li>', $url, $key);
        }
        $html.='</ul></div>';
        echo $html;
    }

    /**
     * 清除缓存
     *
     * @return json
     */
    public function cacheclearAction()
    {
        $key = $this->get('key');
        if (!empty($key)) {
            $key = AuthCode::decode($key, 'yohobuy');
            if (!empty($key)) {
                //导航清除
                if ($key == WebCacheConfig::KEY_WEB_HOME_NAVBAR_DATA) {
                    $list = array(HomeModel::COOKIE_NAME_BOYS, HomeModel::COOKIE_NAME_GIRLS, HomeModel::COOKIE_NAME_KIDS, HomeModel::COOKIE_NAME_LIFESTYLE);
                    foreach ($list as $val) {
                        Cache::delete($key . '_' . $val);
                    }
                }
                else {
                    Cache::delete($key);
                }
                echo '清除成功';
            }
        }
    }

    public function serverAction()
    {
        print_r($_SERVER);
    }
    
    public function serverinfoAction()
    {
        phpinfo();
    }
}

?>