// +---------------------------------------------------------------------- if (!function_exists('getValidatorError')) { /** * 错误验证 * @param $error * @return mixed */ function getValidatorError($validator) { $errors = $validator->errors()->all(); return isset($errors[0]) ? $errors[0] : 1013; } } /** * 是否是手机号码 */ if (!function_exists('isPhone')) { function isPhone($mobile) { return preg_match("/^1[3-9][0-9]]{9}/", $mobile); } } if (!function_exists('moneyFormat')) { /** * 金钱格式化小数点 * @param $money * @param int $decimal 小数点位数,默认系统配置2位 * @return float */ function moneyFormat($money, $decimal = null) { $formatConfig = []; $charset = !empty($formatConfig['charset']) ? trim($formatConfig['charset']) : 'utf-8'; if ($decimal === null) { $decimal = isset($formatConfig['moneyDecimal']) ? intval($formatConfig['moneyDecimal']) : 2; } $money = round($money, $decimal + 1); $data = explode('.', $money); $money = isset($data[0]) ? $data[0] : 0; $float = isset($data[1]) ? $data[1] : ''; $len = $float ? mb_strlen($float, $charset) : 0; $decimal = $decimal >= 0 ? intval($decimal) : 2; $num1 = isset($data[1]) ? $data[1] : ''; $float = $len >= $decimal ? mb_substr($num1, 0, $decimal, $charset) : $float . str_repeat('0', $decimal - $len); $money = $money . '.' . $float; return number_format($money, $decimal, '.', ''); } } if (!function_exists('paramsFormat')) { /** * 格式化请求数组参数为&连接字符串 * @param array $data 参数数组 * @param false $filter 是否过滤空值字段 * @return string */ function paramsFormat(array $data, $filter=false){ $result = []; ksort($data); foreach($data as $k => $v){ if($filter && !empty($v) || !$filter){ $result[] = "{$k}={$v}"; } } return implode('&', $result); } } if (!function_exists('xmlToArray')) { /** * xml转数组 * @param $xml * @return array */ function xmlToArray($xml){ if(!$xml){ return []; } $result = []; $xml = (array)simplexml_load_string($xml); foreach($xml as $k => $v){ if(!is_object($v)){ $result[$k] = $v; }else{ $result[$k] = xmlToArray((array)$v); } } return $result; } } if (!function_exists('curl_api')) { /** * curl请求(POST) * @param $url 请求地址 * @param array $data 请求参数 * @return bool|string 返回结果 * @author laravel开发员 * @date 2019/6/5 */ function curl_api($url, $data = [], $header=[], $timeout=20) { // 初始化 $ch = curl_init(); // 设置post方式提交 curl_setopt($ch, CURLOPT_POST, 1); // 设置头文件的信息作为数据流输出 curl_setopt($ch, CURLOPT_HEADER, 0); // 超时 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // 是否要求返回数据 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 请求头 if($header){ curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } // 设置抓取的url curl_setopt($ch, CURLOPT_URL, $url); // 提交的数据 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 执行命令 $result = curl_exec($ch); // 关闭URL请求(释放句柄) curl_close($ch); return $result; } } if (!function_exists('formatTime')) { /** * 格式化到期时间段 * @param int $interval 时间秒 * @return string 输出格式化时间 * @author laravel开发员 * @date 2019/5/23 */ function formatTime($interval) { $format = array( '31536000' => '年', '2592000' => '个月', '604800' => '星期', '86400' => '天', '3600' => '小时', '60' => '分钟', '1' => '秒', ); foreach ($format as $key => $val) { $match = floor($interval / (int)$key); if (0 != $match) { return $match . $val; } } return date('Y-m-d', $interval); } } if (!function_exists('showJson')) { /** * 消息JSON * @param string $msg 提示文字 * @param bool $success 是否成功true或false * @param array $data 结果数据 * @param int $code 编码 * @return array 返回结果 * @author laravel开发员 * @date 2019/5/28 */ function showJson($msg = "1002", $success = true, $data = [], $code = 0) { $result = ['success' => $success, 'msg' => lang($msg), 'data' => $data]; if ($success) { $result['code'] = 0; } else { $result['code'] = $code ? $code : -1; } return json_encode($result, 256); } } if(!function_exists('getCode')){ /** * 生成唯一字符串 * @param string $prefix 长度最小8位 * @param int $length 长度最小9位,越小唯一性越差,9位百万唯一 * @return false|string */ function getCode($prefix='',$length=11){ $length = $length<9? 9 : $length; $code = strtoupper(md5(uniqid(mt_rand(), true).time())); $start = rand(1, strlen($code)-$length); return $prefix.mb_substr($code, $start, $length); } } if(!function_exists('getChatKey')){ /** * 获取聊天窗口KEY * @param $fromUid * @param $toUid * @return string */ function getChatKey($fromUid, $toUid){ $arr = array_values([$fromUid,$toUid]); asort($arr); return implode('', $arr); } } if(!function_exists('checkPassword')){ /** * 验证密码 * @param $password * @return string */ function checkPassword($password){ if(!preg_match("/^[0-9a-zA-Z\W_]{8,30}$/", $password)){ return false; } if(!preg_match("/[a-zA-Z]{1}/", $password)){ return false; } if(!preg_match("/[0-9]{1}/", $password)){ return false; } if(!preg_match("/([[:punct:]]){1}/", $password)){ return false; } return true; } }