| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- 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;
- }
- }
|