1. 程式人生 > >API返回引數統一格式化

API返回引數統一格式化

<?php
namespace V3\Format;
/**
 * 格式化AccountController
 * Class FormatAccount
 * @auth LiKang
 * @date 2015-12-08
 */
class FormatAccount extends Format{

    /**
     * 格式化login
     */
    public function login( $data ){
        $template = array(
            'account_id' => 'i',
            'token' => 's',
            'account_name' => 's',
            'account_status' => 'i',
            'account_type' => 'i',
            'phone' => 's',
            'sex' => 'i',
            'head_image' => 's',
            'shop_id' => 'i',
            'shop_name' => 's',
            'area_id' => 's',
            'area_info' => 's',
            'account_balance' => 'DOC2X',
            'real_name' => 's',
            'id_card' => 's',
            'unread_count' => 'i',
            'account_source' => 'i',
            'account_source_desc' => 's',
        );
        $this -> format( $data ,$template );
        return $data;
    }

    /**
     * 格式化register
     */
    public function register( $data ){
        $template = array(
            'account_id' => 'i',
            'token' => 's',
            'account_name' => 's',
            'account_status' => 'i',
            'account_status_desc' => 's',
            'account_type' => 'i',
            'phone' => 's',
            'head_image' => 's',
            'shop_id' => 'i',
            'shop_name' => 's',
            'area_id' => 's',
            'area_id' => 's',
            'account_balance' => 'i',
            'real_name' => 's',
            'id_card' => 's',
            'unread_count' => 'i',
            'account_source' => 'i',
            'account_source_desc' => 's',
        );
        $this -> format( $data ,$template );
        return $data;
    }

    /**
     * 格式化finishAccountInfo
     * @param $data
     */
    public function finishAccountInfo( $data ){
        $template = array(
            'account_id' => 'i',
            'token' => 's',
            'account_name' => 's',
            'account_status' => 'i',
            'account_status_desc' => 's',
            'account_type' => 'i',
            'phone' => 's',
            'head_image' => 's',
            'shop_id' => 'i',
            'shop_name' => 's',
            'area_id' => 's',
            'area_id' => 's',
            'account_balance' => 'i',
            'real_name' => 's',
            'id_card' => 's',
            'unread_count' => 'i',
            'account_source' => 'i',
            'account_source_desc' => 's',
        );
        $this -> format( $data ,$template );
        return $data;
    }

    /**
     * 格式化getAccountInfo
     * @param $data
     */
    public function getAccountInfo( $data ){
        $template = array(
            'account_id' => 'i',
            'token' => 's',
            'account_name' => 's',
            'account_status' => 'i',
            'account_status_desc' => 's',
            'account_type' => 'i',
            'phone' => 's',
            'head_image' => 's',
            'shop_id' => 'i',
            'shop_name' => 's',
            'area_id' => 's',
            'area_info' => 's',
            'shipping_address' => 's',
            'address' => 's',
            'account_balance' => 'i',
            'real_name' => 's',
            'id_card' => 's',
            'unread_count' => 'i',
            'account_source' => 'i',
            'account_source_desc' => 's',
        );
        $this -> format( $data ,$template );
        return $data;
    }

    /**
     * 格式化finishAccountInfo
     * @param $data
     */
    public function logout( $data ){
        return $data;
    }

    /**
     * 格式化changePsd
     * @param $data
     * @return mixed
     */
    public function changePsd( $data ){
        return $data;
    }

    /**
     * 格式化changeAccountInfo
     */
    public function changeAccountInfo( $data ){
        return $data;
    }

    /**
     * 格式化forgetPasswordOne
     */
    public function forgetPasswordOne( $data ){
        return $data;
    }

    /**
     * 格式化forgetPasswordTwo
     */
    public function forgetPasswordTwo( $data ){
        return $data;
    }

    /**
     *  格式化getPcode
     */
    public function getPcode ( $data ){
        return $data;
    }

    /**
     * 格式化checkVcode
     */
    public function checkVcode( $data ){
        return $data;
    }
}
<?php
namespace V3\Format;
/**
 * 格式化資料
 * Class Format
 * @package Home\Format
 */
class Format{
/**
     * 格式化資料
     * @param array $param 需要格式化的資料
     * @param array $template 格式化的模板
     * @return array
     */
protected  function format(&$param,$template){
if(is_array($template) && 
is_array($param) ){ foreach ($param as $key=> &$val){ if(is_numeric($key)){ $this->format($val,$template[0]); }else if(is_string($template[$key])){ if(isset($template[$key])){ $this->f($template[$key],$val); }else{ unset($param[$key]); }
}else if(is_array($template[$key])){ $k = key($template[$key]); if(is_numeric($k) && is_array( $param[$key] ) ){ foreach ($param[$key] as $nk=> &$item){ $this->format($item,$template[$key][$k]); } }else if(is_string($k)){ foreach ($val as $mk=>&$mv){ $this->f($template[$key][$mk],$mv); } } } } }else if(is_string($template)){ $this->f($template,$param); } } /** * 按照定義格式格式化引數 * @param unknown $f * @param unknown $val */ private function f($f,&$val){ switch (strtoupper($f)){ case 'PASS' :;break; case 'I': $val = intval($val);break; case 'S': $val = strval($val);break; case 'F': $val = floatval($val);break; case 'D': $val = doubleval($val);break; case 'T10': $val = (string) $val;break; case 'T13': $val = (string) $val * 1000 ;break; case 'DOC2X' : $val = decimalFormat( $val , 2 );break; case 'DOC4X' : $val = decimalFormat( $val , 4 );break; case 'DOC6X' : $val = decimalFormat( $val , 6 );break; case 'DOC8X' : $val = decimalFormat( $val , 8 );break; case 'SF' : $val = numFormat( $val , 2 );break; } } }