Common.php 4.88 KB
<?php

class YHMApi_Common
{
    protected static $get_fields = "";
    private static $currencyObj = "";
    protected static $channel;
    private static $platformList = array(
        'ios' => 'private_key',
        'iphone' => 'private_key',
        'android' => 'private_key',
        'show' => ''
    );

    private static $badParameter = array(
        '/api', 'q', 'client_secret', 'debug_data', '/api/api'
    );

    static function delParameter(array $params)
    {
        $_params = array();
        foreach ($params as $key => $val) {
            if (in_array($key, self::$badParameter)) {
                continue;
            }
            $_params[$key] = $params[$key];
        }
        return $_params;
    }

    static function init(array $params)
    {
        $clientSecret = $params['client_secret'];
        $debugData = (empty($params['debug_data']) || $params['debug_data'] != 'Y') ? 'N' : 'Y';
        $params = self::delParameter($params);
        self::$channel = $params['client_type'];
        $params['private_key'] = self::$platformList[$params['client_type']];
        $_params = self::packageSort($params);
        $_makeKey = self::makeSign($_params);

        $verifySign = self::verifySign($_makeKey, $clientSecret);
        $debug = 'N';
        if (!empty($params['debug']) && $params['debug'] == 'Y') {
            $debug = 'Y';
        }
        if ($verifySign == false && $debug == 'N') {
            $makeData = array(
                'server_client_secret' => $_makeKey,
                'make_data' => $_params
            );
            if ($debugData == 'N') {
                $makeData = array();
            }
            //self::echoJson(self::result(500, '数据验证错误.', $makeData));
        }
    }

    /**
     * 提取指定的字段
     * Enter description here ...
     * @param string $fields 要获取的字段
     * @param array $data 数据
     * @param string $fields_config 默认使用$get_fields,也可以自己指定
     */
    static function makeFields($fields, array $data, $fields_config = null)
    {
        if (empty($fields)) {
            $fields = empty($fields_config) ? self::$get_fields : $fields_config;
        }
        if (!is_string($fields)) {
            return array();
        }
        $fields_list = explode(',', $fields);
        $result = array();
        foreach ($fields_list as $val) {
            $fields_key = explode('.', $val);
            if (count($fields_key) == 2) {
                list($parent_key, $son_key) = $fields_key;
                if (isset($data[$parent_key]) && isset($data[$parent_key][$son_key])) {
                    $result[$parent_key][$son_key] = $data[$parent_key][$son_key];
                }
            } elseif (array_key_exists($val, $data)) {
                $result[$val] = $data[$val];
            }
        }
        return $result;
    }

    /**
     * 结果数据
     *
     * @param Integer $code 状态码
     * @param mixed $data 数据
     * @param String $message 数据信息Tag
     *
     */
    static function result($code, $message = null, $data = null)
    {
        $dataString = json_encode($data);
        //echo $code.'------'.$message;
        return array(
            'code' => $code,
            'message' => $message,
            'data' => $data,
            'md5' => md5($dataString)
        );
    }

    static function FormatCurrency($currency)
    {
        if (empty(self::$currencyObj)) {
            self::$currencyObj = new Zend_Currency('zh_CN');
            self::$currencyObj->setFormat(array(
                'precision' => 0
            ));
        }
        return self::$currencyObj->toCurrency($currency);
    }

    /**
     * 排序参数
     * @param array $package
     * @return array
     */
    static function packageSort(array $package)
    {
        ksort($package);
        reset($package);
        return $package;
    }

    /**
     * 组合签名
     * @param array $package
     * @return string
     */
    static function makeSign(array $package)
    {
        $packageList = array();
        foreach ($package as $key => $val) {
            $packageList[] = trim(trim($key) . '=' . urldecode($val));
        }
        return strtolower(md5(implode('&', $packageList)));
    }

    /**
     * 校验签名
     * @param $submitSign
     * @param $makeSign
     * @return bool
     */
    static function verifySign($submitSign, $makeSign)
    {
        return strtolower($submitSign) == strtolower($makeSign);
    }

    /**
     * JSON输出
     *
     * @param String $caption
     * @param Integer $code
     * @param mixed $content
     */
    static function echoJson($data = null)
    {
        header('Content-type: application/json');
        echo json_encode($data);
        exit();
    }

    static function paging()
    {
        return new Q_Paging();
    }

    static function getToken($uid)
    {
        return strrev(md5($uid . 'yohomark9646ghks'));
    }
}