setAttributes($arr); return $model; } /** * 将数据库中获取的对象数组转为数组 * @param array $arr * @return array */ public static function objectToArray(array $arr, $column = '') { $ret = []; if (!empty($arr)) { foreach ($arr as $item) { if ($column) { $ret[$item->$column] = $item->attributes; } else { $ret[] = $item->attributes; } } } return $ret; } /** * 获取图片完整路径 * @param $imageUrl * @param string $default * * @return string * @author lizhi <1458705589@qq.com> * @date 2021/5/11 */ public static function getImageUrl($imageUrl, string $default = '', $params = []): string { if (empty($imageUrl)) { return $default; } if (str_contains($imageUrl, 'http')) { return $imageUrl; } $imageUrl = IMAGEDOMAIN . '/' . ltrim($imageUrl, '/'); if (self::getArrParam($params, 'from_type', 'string') == 'console') { // 把后端用到图片的地方,加个后缀,类似!max200 !width600这种 $imageUrl .= '!max200'; } return $imageUrl; } public static function time_tran($time) { $text = '请选择正确的时间!'; if(!$time){ return $text; } $current = time(); $t = $current - $time; $retArr = array('刚刚','秒前','分钟前','小时前','天前','月前','年前'); switch($t){ case $t < 0://时间大于当前时间,返回格式化时间 $text = date('Y-m-d',$time); break; case $t == 0://刚刚 $text = $retArr[0]; break; case $t < 60:// 几秒前 $text = $t.$retArr[1]; break; case $t < 3600://几分钟前 $text = floor($t / 60).$retArr[2]; break; case $t < 86400://几小时前 $text = floor($t / 3600).$retArr[3]; break; case $t < 2592000: //几天前 $text = floor($t / 86400).$retArr[4]; break; case $t < 31536000: //几个月前 $text = floor($t / 2592000).$retArr[5]; break; default : //几年前 $text = floor($t / 31536000).$retArr[6]; } return $text; } public static function getPostString($key, $default = null) { return self::getArrParam($_POST, $key, 'string', $default); } public static function getPostInt($key, $default = 0) { return self::getArrParam($_POST, $key, 'int', $default); } public static function getGetString($key, $default = null) { return self::getArrParam($_GET, $key, 'string', $default); } public static function getGetDate($key, $default = null) { return self::getArrParam($_GET, $key, 'date', $default); } public static function getPostDatetime($key, $default = null) { return self::getArrParam($_POST, $key, 'datetime', $default); } public static function getGetDatetime($key, $default = null) { return self::getArrParam($_GET, $key, 'datetime', $default); } public static function getPostDate($key, $default = null) { return self::getArrParam($_POST, $key, 'date', $default); } public static function getGetInt($key, $default = 0) { return self::getArrParam($_GET, $key, 'int', $default); } /** * 从数组中获取字段 */ public static function getArrParam($arr, $name, $type = 'string', $default = null) { if (isset($arr[$name])) { $param = $arr[$name]; return self::formatByType($param, $type, $default); } return $default; } /** * @param string $name 从数组中获取字段,并进行安全处理 多维数组示例 area.name * @see self::formatByType() */ public static function getByKey($arr, $name, $type = '', $default = null) { $nameArr = array_filter(explode('.', $name)); if (count($nameArr) == 1) { if (!isset($arr[$name])) { return $default; } else { $param = $arr[$name]; } } else if (count($nameArr) == 2) { if (!isset($arr[$nameArr[0]][$nameArr[1]])) { return $default; } else { $param = $arr[$nameArr[0]][$nameArr[1]]; } } else { return $default; } return self::formatByType($param, $type, $default); } /** * 格式化数据 * @param mixed $param * @param string $type 数据类型,将元素处理成这样的数据类型 - string 字符串 - int 数字 - float float数字 - date 日期 - datetime 日期时间 - array_string 包含字符串的数组 - array_float 包含float的数组 - array_int 包含int的数组 * @param mixed $default * @return mixed */ public static function formatByType($param, $type, $default) { if ($type == 'string') { $param = addslashes(trim($param)); } elseif ($type == 'int') { $param = intval($param); } elseif ($type == 'float') { $param = floatval($param); } elseif ($type == 'date') { if (!empty($param)) { $param = date('Y-m-d', strtotime($param)); } else { $param = $default; } } elseif ($type == 'datetime') { if (!empty($param)) { $param = date('Y-m-d H:i:s', strtotime($param)); } else { $param = $default; } } elseif ($type == 'array_string') { if (!empty($param)) { foreach ($param as $key => &$item) { $item = addslashes(trim($item)); } } } elseif ($type == 'array_float') { if (!empty($param)) { foreach ($param as $key => &$item) { $item = floatval($item); } } } elseif ($type == 'array_int') { if (!empty($param)) { foreach ($param as $key => &$item) { $item = intval($item); } } } return $param; } /** * 获取date格式的日期间隔 */ public static function intervalDays($date1, $date2) { $d = @(strtotime($date2) - strtotime($date1)) / (3600 * 24); return $d; } /** * 获取一个对象中的某个字段,返回数组 * @param array $arr 通过查询返回的数组 * @param string $column 字段名 */ public static function getObjectColumn(array $arr, $column = 'id') { $ret = []; if (!empty($arr)) { foreach ($arr as $key => $item) { $attributes = $item->attributes; isset($attributes[$column]) && $ret[] = $attributes[$column]; } } return $ret; } public static function concatArray($arr1, $arr2) { foreach ($arr2 as $item) { if (!in_array($item, $arr1)) { $arr1[] = $item; } } return $arr1; } public static function toJson($code = 200, $data = [], $msg = '请求成功') { @header("Content-Type: text/json; charset=UTF-8"); echo json_encode([ 'code' => $code, 'msg' => $msg, 'data' => $data ]); \Yii::app()->end(); } public static function imageUpload($imagepath, $uploadpath) { try { // 腾讯云 $region = 'ap-shanghai'; // 用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket $cosClient = new \Qcloud\Cos\Client( array( 'region' => $region, // 'scheme' => 'https', //协议头部,默认为 http 'credentials'=> array( 'secretId' => 'AKIDviIeLyQluythLAykSJ6oXH91upgR6iT8' , 'secretKey' => 'tobSCsOn7yc6ToBSWegaM9rVGyiR6f95' ) ) ); $bucket = 'lewaimai-image-1251685925'; $file = fopen($imagepath, "rb"); if ($file) { $result = $cosClient->putObject(['Bucket' => $bucket, 'Key' => $uploadpath, 'Body' => $file]); } } catch(Exception $e) { throw $e; } } public static function error($msg, $code = 400, $data = []) { self::toJson($code, $data, $msg); } public static function ok($data = [], $msg = '操作成功', $code = 200) { self::toJson($code, $data, $msg); } public static function commonError($_msg = '', $_code = 500, $_data = []) { return self::commonReturn($_data, $_msg, $_code); } public static function commonReturn($_data = [], $_msg = '', $_code = 200) { return [ 'code' => $_code, 'msg' => $_msg, 'data' => $_data ]; } //php防注入和XSS攻击通用过滤 public static function safeFilter (&$arr) { $ra=Array('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/','/script/','/javascript/','/vbscript/','/expression/','/applet/' ,'/meta/','/xml/','/blink/','/link/','/style/','/embed/','/object/','/frame/','/layer/','/title/','/bgsound/' ,'/base/','/onload/','/onunload/','/onchange/','/onsubmit/','/onreset/','/onselect/','/onblur/','/onfocus/', '/onabort/','/onkeydown/','/onkeypress/','/onkeyup/','/onclick/','/ondblclick/','/onmousedown/','/onmousemove/' ,'/onmouseout/','/onmouseover/','/onmouseup/','/onunload/'); if (is_array($arr)) { foreach ($arr as $key => $value) { if (!is_array($value)) { if (!get_magic_quotes_gpc()) //不对magic_quotes_gpc转义过的字符使用addslashes(),避免双重转义。 { $value = addslashes($value); //给单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符) #加上反斜线转义 } $value = preg_replace($ra,'',$value); //删除非打印字符,粗暴式过滤xss可疑字符串 // $arr[$key] = htmlentities(strip_tags($value)); //去除 HTML 和 PHP 标记并转换为 HTML 实体 } else { self::SafeFilter($arr[$key]); } } } return $arr; } public static function getSign($data) { return md5($data . "145709480B89EE59E3F4D43A56C355F2"); } /** * 将二维数组转化为对象数组 * @param array $arr * @return array */ public static function arrayToObjects(array $arr) { $ret = []; foreach ($arr as $item) { $ret[] = (object)$item; } return $ret; } /** * 异步批量get请求 * * @param array $nodes 格式: 'key' => ['url', 'data'] key是标识key,url是请求host,data是post传递的数据 * @param integer $timeOut 超时时间 * @return void */ public static function multiGet($nodes, $options = [] ,$timeOut = 5) { $mh = curl_multi_init(); $curl_arr = []; // 将请求放入curl中 foreach ($nodes as $key => $node) { if (!is_array($node)) { continue; } $ch = curl_init(); $url = $node['url'] . (strpos($node['url'], '?') === FALSE ? '?' : ''); if (isset($node['data'])) { $url .= http_build_query($node['data']); } curl_setopt($ch, CURLOPT_URL, $url); // 其他的参数按照 function GET($url, $get = array(), array $options = array()) 来 curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 15); curl_setopt($ch, CURLOPT_SSLVERSION, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 超时时间 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeOut); foreach ($options as $k => $v) { curl_setopt($ch, $k, $v); } $curl_arr[$key] = $ch; curl_multi_add_handle($mh, $curl_arr[$key]); } // 发送请求 $running = null; do { usleep(10000); curl_multi_exec($mh, $running); } while ($running > 0); // 获取请求数据,并删除请求 $ret = []; foreach ($nodes as $key => $node) { $ret[$key] = curl_multi_getcontent($curl_arr[$key]); curl_multi_remove_handle($mh, $curl_arr[$key]); } curl_multi_close($mh); return $ret; } /** * array_column 一样用法 */ public static function arrayColumn($array, $column_key, $index_key = null): array { return $array && $array['datas'] ? array_column($array['datas'], $column_key, $index_key) : []; } }