<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/3/1
 * Time: 下午3:35
 */

namespace frontend\base;

use Yii;
use yii\web\Response;
class Help
{
    const ERROR = 300;
    const SUCCESS = 200;
    public static function JsonData($code,$msg,$count = 0,$data)
    {

        Yii::$app->response->format = Response::FORMAT_JSON;
        $list['code'] = $code;
        $list['msg'] = $msg;
        $list['count'] = $count;
        $list['data'] = !empty($data) ? $data : [] ;
        return $list;
    }

    /*
     * code 返回状态码 100=失败  200=成功
     * msg  返回信息
     * data 返回数据 格式 array
     * */
    public static function JsonCode($code,$msg = '',$data = null)
    {
        Yii::$app->response->format=Response::FORMAT_JSON;
        $list['code'] = $code;
        $list['msg'] = $msg;
        $list['data'] = $data;
        return $list;
    }

    public static function Attributes($model,$model1)
    {

//        p($model1->attributes);
//        p($model->attributes);
//        exit;
//            foreach ($model1->attributes as $key=>$val)
//            {
//                    if($model->attributes[$key] != $val)
//                    {
//                        $model1->$key =$val;
//                    }
//            }
//            return $model1;
//           foreach ($model->attributes as $k=>$v)
//           {
//               if($model1->attributes[$k] != $v)
//               {
//                   $model1->$k = $v;
//               }
//           }
//                    return $model1;
    }

    /*
     * params $input 提交的表单
     * params $model 验证过后的attributes
     * params $setModel 确认需要修改的attributesßß
     *
     * */
    public static function SetAttr($input,$model,$setModel)
    {
//        p($setModel->attributes);
        if(is_array($input))
        {
            foreach ($input as $key=>$val)
            {
//                    if(array_key_exists($key,$model->attributes))
//                    {
////                        echo 111;
//                        $setModel[$key] = $model[$key];
//                    }
                if(isset($model[$key]))
                {
                    $setModel[$key] = $model[$key];
                }
            }
//                p($setModel);
            return $setModel;
        }
    }


    //实现PHP异步请求
    public function AjaxRequest($event)
    {
        if(empty($event->data['path']) || empty($event->data['data'])) return false;
        $host = $_SERVER['HTTP_HOST'];
        $query_Data = http_build_query($event->data['data']);
        $fp = fsockopen($host, 80, $errno, $errstr, 30);
        if (!$fp) {
            return false;
        }
        $out  = "POST ".$event->data['path']." HTTP/1.1\r\n";
        $out .= "Host: $host\r\n";
        $out .= "Content-type: application/x-www-form-urlencoded\r\n";
        $out .= "Content-Length: ".strlen($query_Data)."\r\n";
        $out .= "Connection: Close\r\n\r\n";
        $out .= $query_Data;
        fwrite($fp, $out);
        //忽略执行结果
//                while (!feof($fp)) {
//                    echo fgets($fp, 128);
//                }
        fclose($fp);
    }
}