laravel 一些常用封装

首页 / 正文

返回信息

/**
     * @param $msg
     * @param $data
     * @return \Illuminate\Http\JsonResponse
     */

    public function reture_success($msg = 'success',$data)
    {
        return response()->json([
            'status' => true,
            'code' => 200,
            'message' => $msg,
            'data' => $data,
        ]);
    }

    /**
     * @param $msg
     * @param array $list
     * @param $count
     * @return \Illuminate\Http\JsonResponse
     */
    public function reture_list($msg = 'success',$list = [],$count)
    {
        return response()->json([
            'status' => true,
            'code' => 200,
            'message' => $msg,
            'list' => $list,
            'count'=>$count
        ]);
    }

    /**
     * @param $msg
     * @param array $data
     * @return \Illuminate\Http\JsonResponse
     */
    public function return_error($msg = 'error',$data = [])
    {
        return response()->json([
            'status' => false,
            'code' => 401,
            'error_message' => $msg,
            'data' => $data,
        ]);
    }

一些验证

    /**
     * @param $phone
     * @return bool
     */
    public static function checkPhone($phone){
        if(preg_match("/^1[345789]{1}\d{9}$/",$phone)){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param $email
     * @return bool
     */
    public static function checkEmail($email){
        $pattern = "/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/";
        if(preg_match($pattern, $email)){
            return true;
        }else{
            return false;
        }
    }
无标签
打赏
评论区
头像