Help.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/1
  6. * Time: 下午3:35
  7. */
  8. namespace frontend\base;
  9. use Yii;
  10. use yii\web\Response;
  11. class Help
  12. {
  13. const ERROR = 300;
  14. const SUCCESS = 200;
  15. public static function JsonData($code,$msg,$count = 0,$data)
  16. {
  17. Yii::$app->response->format = Response::FORMAT_JSON;
  18. $list['code'] = $code;
  19. $list['msg'] = $msg;
  20. $list['count'] = $count;
  21. $list['data'] = !empty($data) ? $data : [] ;
  22. return $list;
  23. }
  24. /*
  25. * code 返回状态码 100=失败 200=成功
  26. * msg 返回信息
  27. * data 返回数据 格式 array
  28. * */
  29. public static function JsonCode($code,$msg = '',$data = null)
  30. {
  31. Yii::$app->response->format=Response::FORMAT_JSON;
  32. $list['code'] = $code;
  33. $list['msg'] = $msg;
  34. $list['data'] = $data;
  35. return $list;
  36. }
  37. public static function Attributes($model,$model1)
  38. {
  39. // p($model1->attributes);
  40. // p($model->attributes);
  41. // exit;
  42. // foreach ($model1->attributes as $key=>$val)
  43. // {
  44. // if($model->attributes[$key] != $val)
  45. // {
  46. // $model1->$key =$val;
  47. // }
  48. // }
  49. // return $model1;
  50. // foreach ($model->attributes as $k=>$v)
  51. // {
  52. // if($model1->attributes[$k] != $v)
  53. // {
  54. // $model1->$k = $v;
  55. // }
  56. // }
  57. // return $model1;
  58. }
  59. /*
  60. * params $input 提交的表单
  61. * params $model 验证过后的attributes
  62. * params $setModel 确认需要修改的attributesßß
  63. *
  64. * */
  65. public static function SetAttr($input,$model,$setModel)
  66. {
  67. // p($setModel->attributes);
  68. if(is_array($input))
  69. {
  70. foreach ($input as $key=>$val)
  71. {
  72. // if(array_key_exists($key,$model->attributes))
  73. // {
  74. //// echo 111;
  75. // $setModel[$key] = $model[$key];
  76. // }
  77. if(isset($model[$key]))
  78. {
  79. $setModel[$key] = $model[$key];
  80. }
  81. }
  82. // p($setModel);
  83. return $setModel;
  84. }
  85. }
  86. //实现PHP异步请求
  87. public function AjaxRequest($event)
  88. {
  89. if(empty($event->data['path']) || empty($event->data['data'])) return false;
  90. $host = $_SERVER['HTTP_HOST'];
  91. $query_Data = http_build_query($event->data['data']);
  92. $fp = fsockopen($host, 80, $errno, $errstr, 30);
  93. if (!$fp) {
  94. return false;
  95. }
  96. $out = "POST ".$event->data['path']." HTTP/1.1\r\n";
  97. $out .= "Host: $host\r\n";
  98. $out .= "Content-type: application/x-www-form-urlencoded\r\n";
  99. $out .= "Content-Length: ".strlen($query_Data)."\r\n";
  100. $out .= "Connection: Close\r\n\r\n";
  101. $out .= $query_Data;
  102. fwrite($fp, $out);
  103. //忽略执行结果
  104. // while (!feof($fp)) {
  105. // echo fgets($fp, 128);
  106. // }
  107. fclose($fp);
  108. }
  109. }