Laravel 7 常用封装函数

荆轲刺秦王

对于用户输入的值永远是需要过滤处理的,为了避免每次都要重新写,就直接写到  lib/function.php 中。

<?php

if (!function_exists('log_write')) {
    /**
     * 日志打印
     *
     * @param        $content
     * @param string $logName
     */
    function log_write($content, $logName = '', $level=\Lib\PublicClass\Log::INFO )
    {
        app('Lib\PublicClass\Log')->log_write($content, $logName, $level );
    }
}
else
{
    exit('Lib\function.php\log_write冲突');
}

if (!function_exists('txt_filter')) {
    function txt_filter( $txt )
    {
        $txt = trim($txt);  //清理空格
        $txt = strip_tags($txt);   //过滤html标签
        $txt = htmlspecialchars($txt);   //将字符内容转化为html实体
        $txt = addslashes($txt);  //防止SQL注入
        return $txt;
    }
}
else
{
    exit('Lib\function.php\txt_filter冲突');
}

if(! function_exists('array_txt_filter')) {
    /**
     * 过滤html标签sql注入 仅支持规则的一二维数组,字符串
     * @param $param array|string
     * @return $param
     */
    function array_txt_filter($param)
    {
        $dimension = '';
        if (is_array($param)) $dimension = array_depth($param);
        if ($dimension == 1) {
            foreach ($param as $k => $value) {
                $param[$k] = htmlspecialchars(trim($value));//将字符内容转化为html实体
                $param[$k] = addslashes($param[$k]);  //防止SQL注入
            }
            return $param;
        }

        if ($dimension == 2) {
            foreach ($param as $k => $value) {
                if(is_array($value)){
                    foreach ($value as $key => $v) {
                        $param[$k][$key] = htmlspecialchars(trim($v));   //将字符内容转化为html实体
                        $param[$k][$key] = addslashes($param[$k][$key]);  //防止SQL注入
                    }
                }
            }
            return $param;
        }

        if (is_string($param)) {
            $param = htmlspecialchars(trim($param));//将字符内容转化为html实体
            $param = addslashes($param);  //防止SQL注入
            return $param;
        }

        return $param;
    }
}
else
{
    exit('Lib\function.php\array_txt_filter冲突');
}

if (! function_exists('array_filtration')) {
    /**
     * 输入数组字段过滤
     * @param $array
     * @param $param
     * @return array
     */
    function array_filtration( $array, $param )
    {
        return array_intersect_key( $array, array_flip( $param ));
    }
}
else
{
    exit('Lib\function.php\array_filtration冲突');
}

if (! function_exists('paging_page')) {
    /**
     * 输入数组字段过滤
     * @param $array
     * @param $param
     * @return array
     */
    function paging_page( $count, $page, $limit )
    {
        $return = [];
        if( (int)$page < 1 ) $page = 1;

        $pageTotal = (int)ceil( $count / $limit );
        $return['offset'] = ( $page - 1 ) * $limit;
        $return['current_page'] = (int)$page;

        if( $page > $pageTotal ) $page = $pageTotal;
        $from      = ( $page - 1 ) * $limit + 1;
        $to        = $page * $limit;
        if( $to > $count ) $to = $count;

        $return['from']      = $from;
        $return['to']        = $to;
        $return['last_page'] = $pageTotal;
        $return['per_page']  = (int)$limit;
        $return['total']     = $count;
        return $return;
    }
}
else
{
    exit('Lib\function.php\paging_page冲突');
}

if (!function_exists('get_millisecond'))
{
    /**
     * 获取毫秒级时间戳
     * @return int
     */
    function get_millisecond()
    {
        list( $t1, $t2 ) = explode( ' ', microtime());
        return (int)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
    }
}
else
{
    exit('Lib\function.php\get_millisecond冲突');
}

if(! function_exists('array_depth'))
{
    /**
     * 获取数组维数
     * @param $name
     * @return $dimension
     */
    function array_depth( $param )
    {

        static $dimension = 1;
        if(!is_array($param)) return $dimension;

        foreach ($param as $value) {

            if (is_array($value)) {
                $dimension ++;
                array_depth($value) ;
            }
        }
        return $dimension;
    }
}
else
{
    exit('Lib\function.php\array_depth');
}

if (! function_exists('arr_get')) {
    /**
     * Arr::get()方法调用简化
     * @param $array
     * @param $key
     * @param $default
     * @return mixed
     */
    function arr_get( $array, $key, $default=null )
    {
        return \Illuminate\Support\Arr::get( $array, $key, $default );
    }
}
else
{
    exit('Lib\function.php\arr_get');
}

if (!function_exists('get_order_seq'))
{
    function get_order_seq()
    {
        return app('Lib\PublicClass\S')-> getOrderSeq();
    }
}

if (!function_exists('verify_mobile'))
{
    function verify_mobile( $mobile )
    {
        $regex = '/^\+[1-9]{1}+[0-9]{10,14}$|^[1-9]{1}+[0-9]{10,14}$/';
        if( preg_match( $regex, $mobile ))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

if (!function_exists('image_resize'))
{
    /**
     * 生成缩略图
     * @param $file
     * @param $name
     * @param $width
     * @param $high
     * @return bool
     */
    function image_resize( $file, $name, $width, $high )
    {
        $temp = [ 1=>'gif', 2=>'jpeg', 3=>'png' ];
        list( $fw, $fh, $tmp ) = getimagesize( $file );
        if(!$temp[$tmp]){
            return false;
        }
        $tmp     = $temp[$tmp];
        $infunc  = "imagecreatefrom$tmp";
        $outfunc = "image$tmp";

        $fimg = $infunc( $file );
//      $fw = 10;
//      $fh = 4;
//      $tw = 4;
//      $th = 2;
        // 把图片铺满要缩放的区域
        if( $fw / $width > $fh / $high )
        {
            $zh  = $high;
            $zw  = $zh * ( $fw / $fh );
            $_zw = ( $zw - $width ) / 2;
            $_zh = 0;
        }else{
            $zw  = $width;
            $zh  = $zw * ( $fh / $fw );
            $_zw = 0;
            $_zh = ( $zh - $high ) / 2;
        }
//        echo $zw."<br>";
//        echo $zh."<br>";
//        echo $_zw."<br>";
//        echo $_zh."<br>";
//        exit;
        $zimg = imagecreatetruecolor($zw, $zh);

        // 先把图像放满区域
        imagecopyresampled($zimg, $fimg, 0,0, 0,0, $zw,$zh, $fw,$fh);

        // 再截取到指定的宽高度
        $timg = imagecreatetruecolor($width, $high);
        imagecopyresampled( $timg, $zimg, 0, 0, 0+$_zw, 0+$_zh, $width, $high, $zw-$_zw*2, $zh-$_zh*2 );

        if( $outfunc( $timg, $name ))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

本文主要对:  arr_get    函数做一个例子:

public function getWxList(Request $request , MsgPushDao $msgPushDao)
    {
        $param = $request->all();
        $msgName    = txt_filter($request -> get('template_name'));
        $page      = txt_filter(arr_get($param,'page','1'));
        $limit      = txt_filter(arr_get($param,'limit','15'));
        if (empty($page)) $page = 1;
        if (empty($limit))  $limit = 15;
        $list = $msgPushDao -> getWxMsgList($msgName,$page,$limit);
        return S::jsonReturn($list);
    }

在做分页的时候,使用 arr_get  函数是一个非常棒的选择,它实际上是:

Arr::get()

方法的简化。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值