123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- /**
- * Created by PhpStorm.
- * User: xiaofeng
- * Date: 2018/3/1
- * Time: 下午3:35
- */
- namespace backend\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);
- }
-
- /**
- * 隐藏电话号码中间四位
- * @param $phone
- * @return string|string[]|null
- */
- public static function HideTel(int $phone){
- $IsWhat = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i',$phone); //固定电话
- if($IsWhat == 1){
- return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i','$1****$2',$phone);
- }else{
- return preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$phone);
- }
- }
- }
|