<?php
/**
 * Created by PhpStorm.
 * Date: 2017/09/01
 * Time: 15:06
 */

namespace common\components;

use Yii;
use yii\helpers\ArrayHelper;

class Request extends \yii\web\Request {

    const DEVICE_PC = 'pc';
    const DEVICE_Mobile = 'mobile';
    const DEVICE_APP = 'app';

    private $_device = null;
    private $_deviceInfo = null;
    private $_suppressResponseCode = null;
    /**
     * 返回 post 和 get 的数据
     * @param null $name
     * @return array|mixed
     */
    public function request($name = null){
        $result = $this->post($name);
        if(!$result){
            $result = $this->get($name);
        }
        return $result;
    }

    /**
     * 合并获取参数
     *
     * @param $name
     * @param null $default
     * @return null
     */
    public function all($name = null ,$default = NULL){
        if(is_null($name)){
            return ArrayHelper::merge($this->getQueryParams(), $this->post());
        }
        $params = $this->getQueryParams();
        if(isset($params[$name])){
            return $params[$name];
        }
        $value = $this->post($name ,$default);
        return $value ? : $default;
    }

    /**
     * 校验完成Csrf之后清空csrf
     * @param null $clientSuppliedToken
     * @return bool
     */
    public function validateCsrfToken($clientSuppliedToken = null)
    {
        if(Yii::$app->controller->module->id == 'gii'){
            return true;
        }
        return parent::validateCsrfToken($clientSuppliedToken);
    }

    /**
     * 获取设备类型
     * @return mixed
     */
    public function getDevice(){
        if($this->_device === null){
            $header = Yii::$app->request->getHeaders();
            if(isset($header['device-type']) && ($header['device-type'] == self::DEVICE_PC || $header['device-type'] == self::DEVICE_APP)){
                $this->_device = $header['device-type'];
            } else {
                $this->_device = self::DEVICE_PC;
            }
        }
        return $this->_device;
    }

    /**
     * 获取设备信息
     * @return mixed|null|string
     */
    public function getDeviceInfo(){
        if($this->_deviceInfo === null){
            $header = Yii::$app->request->getHeaders();
            if(isset($header['device-info']) && $header['device-info']){
                // System,Version,NetworkType,UUID
                $pattern = '/System\:([\w]+)\;\s+Version\:([\w\.]+)\;\s+NetworkType\:([\w\.]+)\;\s+UUID\:([\-\w]+)/i';
                $infoArr = [];
                if(preg_match($pattern, $header['device-info'], $infoArr)){
                    $this->_deviceInfo = [
                        'system' => $infoArr[1],
                        'version' => $infoArr[2],
                        'networkType' => $infoArr[3],
                        'uuid' => $infoArr[4],
                    ];
                }
            } else {
                $this->_deviceInfo = [];
            }
        }
        return $this->_deviceInfo;
    }

    /**
     * 获取是否支持json响应代码
     * @return bool
     */
    public function getSuppressResponseCode(){
        if($this->_suppressResponseCode === null){
            $header = Yii::$app->request->getHeaders();
            if(isset($header['Suppress-Response-Code']) && $header['Suppress-Response-Code'] == 1){
                $this->_suppressResponseCode = true;
            }
            elseif (Yii::$app->request->get('suppress_response_code')){
                $this->_suppressResponseCode = true;
            }
            else {
                $this->_suppressResponseCode = false;
            }
        }
        return $this->_suppressResponseCode;
    }

    /**
     * 向query中添加数据
     * @param array $params
     * @return array|bool
     */
    public function appendQueryParams(array $params = []){
        if(!$params){
            return false;
        }
        $queryParams = \Yii::$app->request->getQueryParams();
        if($queryParams){
            $queryParams = ArrayHelper::merge($queryParams, $params);
        }else{
            $queryParams = $params;
        }
        \Yii::$app->request->setQueryParams($queryParams);
        return $queryParams;
    }
}