路由
<?php
use Illuminate\ Support\ Facades\ Route ;
Route: : get ( '/' , function ( ) {
return view ( 'welcome' ) ;
} ) ;
Route: : group ( [ 'namespace' = > 'H5' ] , function ( ) {
Route: : any ( '/login' , 'UserController@login' ) ;
Route: : any ( '/register' , 'UserController@register' ) ;
Route: : any ( '/sendSmsCode' , 'UserController@sendSmsCode' ) ;
} ) ;
APICOFIG.PHP
<?php
namespace App\ Http\ Controllers ;
class APICOFIG
{
const HPST = 'http://Api.ldyphp.top' ;
const LOGIN = APICOFIG : : HPST . '/user/login' ;
const GET_IMAGE_CODE_URL = APICOFIG : : HPST . '/getImageCodeUrl' ;
const SENDSMS = APICOFIG : : HPST . '/sendSms' ;
const REGISTER = APICOFIG : : HPST . '/register' ;
}
公共的Common控制器
<?php
namespace App\ Http\ Controllers ;
use Illuminate\ Http\ Request ;
class CommonController extends Controller
{
protected function builApiParam ( array $data )
{
$api_request_arr = [ ] ;
$api_request_arr [ 'data' ] = $this - > RsaEncrpt ( $data ) ;
return $api_request_arr ;
}
public function RsaEncrpt ( array $data )
{
$data_str = json_encode ( $data ) ;
$i = 1 ;
$all = '' ;
while ( $sub_str = substr ( $data_str , ( $i - 1 ) * 117 , 117 ) ) {
openssl_public_encrypt (
$sub_str ,
$encrypt ,
file_get_contents ( public_path ( ) . '/public.key' ) ,
OPENSSL_PKCS1_PADDING
) ;
$all . = $encrypt ;
$i ++ ;
}
return base64_encode ( $all ) ;
}
private function AesEncrpt ( array $data )
{
$key = env ( 'AES_KEY' ) ;
$iv = env ( 'AES_IV' ) ;
$json_str = json_encode ( $data , JSON_UNESCAPED_UNICODE ) ;
$decrypt = openssl_encrypt (
$json_str ,
'AES-256-CBC' ,
$key ,
OPENSSL_RAW_DATA ,
$iv
) ;
return base64_encode ( $decrypt ) ;
}
public function CurlPost ( $url , $data , $header = [ ] , $show_error = 0 ) {
$ch = curl_init ( ) ;
curl_setopt ( $ch , CURLOPT_URL , $url ) ;
curl_setopt ( $ch , CURLOPT_POST , true ) ;
curl_setopt ( $ch , CURLOPT_POSTFIELDS , http_build_query ( $data ) ) ;
curl_setopt ( $ch , CURLOPT_TIMEOUT , 5 ) ;
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true ) ;
curl_setopt ( $ch , CURLOPT_SSLCERT , false ) ;
curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST , false ) ;
$api_return = curl_exec ( $ch ) ;
if ( $show_error == 1 ) {
echo $api_return ; exit;
}
$api_reslut = json_decode ( $api_return , true ) ;
if ( curl_errno ( $ch ) !== 0 ) {
$api_reslut = [
'status' = > '10000' ,
'msg' = > 'cuel_error:' . curl_error ( $ch ) ,
'data' = > [ ]
] ;
}
curl_close ( $ch ) ;
return $api_reslut ;
}
public function checkApiParam ( Request $request , $keys )
{
$value = $request - > post ( $keys ) ;
if ( empty ( $value ) ) {
$this - > jsonOutPut ( 1 , '缺少参数' . $keys ) ;
} else {
return $value ;
}
}
public function jsonOutPut ( $status , $msg = 'success' , $data = [ ] )
{
echo json_encode ( [
'status' = > $status ,
'msg' = > $msg ,
'data' = > [ ]
] ) ;
exit;
}
}
User控制器
<?php
namespace App\ Http\ Controllers\ H5 ;
use App\ Http\ Controllers\ APICOFIG as API ;
use App\ Http\ Controllers\ APICOFIG ;
use App\ Http\ Controllers\ CommonController ;
use Illuminate\ Http\ Request ;
class UserController extends CommonController
{
public function login ( Request $request )
{
if ( $request - > method ( ) == 'POST' && $request - > ajax ( ) ) {
$user_name = $this - > checkApiParam ( $request , 'user_name' ) ;
$psd = $this - > checkApiParam ( $request , 'psd' ) ;
$arr = [
'user_name' = > $user_name ,
'password' = > $psd ,
'tt' = > 4
] ;
return $this - > curlPost ( API : : LOGIN , $this - > builApiParam ( $arr ) ) ;
} else {
return view ( 'user.login' ) ;
}
}
public function register ( Request $request )
{
if ( $request - > method ( ) == 'POST' && $request - > ajax ( ) ) {
$api_data = [ ] ;
$api_data [ 'phone' ] = $this - > checkApiParam ( $request , 'phone' ) ;
$api_data [ 'mcode' ] = $this - > checkApiParam ( $request , 'mcode' ) ;
$api_data [ 'password' ] = $this - > checkApiParam ( $request , 'password' ) ;
return $this - > curlPost (
API : : REGISTER ,
$this - > builApiParam ( $api_data )
) ;
} else {
$api_arr = $this - > curlPost ( API : : GET_IMAGE_CODE_URL , [ ] ) ;
return view ( 'user.register' , [
'image_code' = > $api_arr [ 'data' ]
] ) ;
}
}
public function sendSmsCode ( Request $request )
{
$phone = $this - > checkApiParam ( $request , 'phone' ) ;
$sid = $this - > checkApiParam ( $request , 'sid' ) ;
$image_code = $this - > checkApiParam ( $request , 'image_code' ) ;
$arr = [
'phone' = > $phone ,
'sid' = > $sid ,
'image_code' = > $image_code
] ;
return $this - > curlPost (
API : : SENDSMS ,
$this - > builApiParam ( $arr )
) ;
}
}
注册register.blade.php
< ! DOCTYPE html>
< html lang= "{{ str_replace('_', '-', app()->getLocale()) }}" >
< head>
< meta charset= "utf-8" >
< meta name= "csrf-token" content= "{{ csrf_token() }}" >
< meta name= "viewport" content= "width=device-width, initial-scale=1" >
< / head>
< body>
< center>
< div> < h3> h5- 注册页< / h3> < / div>
< div>
< table>
< tr>
< td> 手机号< / td>
< td>
< input type= "text" id= "phone" value= "12345678912" >
< button onclick= "sendSms()" > 获取验证码< / button>
< / td>
< / tr>
< tr>
< td>
图片验证码
< input type= "hidden" id= "sid" value= "{{ $image_code [ 'sid' ] } }" >
< / td>
< td>
< img id= "imgCode" src= "{{ $image_code [ 'image_url' ] } }" style= "width: 100px" >
< input type= "text" size= "10" id= "imageCode" value= "8888" >
< / td>
< / tr>
< tr>
< td> 短信验证码< / td>
< td> < input type= "text" id= "mcode" value= "" > < / td>
< / tr>
< tr>
< td> 密码< / td>
< td> < input type= "password" id= "password" value= "1" > < / td>
< / tr>
< tr>
< td> 确认密码< / td>
< td> < input type= "password" id= "repassword" value= "1" > < / td>
< / tr>
< tr>
< td colspan= "2" style= "text-align: center" > < button onclick= "register()" > 注册< / button> < / td>
< td> < / td>
< / tr>
< / table>
< / div>
< / center>
< / body>
< / html>
< script src= "https://libs.baidu.com/jquery/2.1.1/jquery.min.js" > < / script>
< script type= "text/javascript" >
$( '#imgCode' ) . click ( function ( ) {
$( this) . prop ( 'src' , $( this) . prop ( 'src' ) + '&r' + Math. random ( ) ) ;
} ) ;
$. ajaxSetup ( {
headers: {
'X-CSRF-TOKEN' : $( 'meta[name="csrf-token"]' ) . attr ( 'content' )
}
} ) ;
function sendSms ( )
{
let phone= $( '#phone' ) . val ( ) ;
let image_code= $( '#imageCode' ) . val ( ) ;
let sid= $( '#sid' ) . val ( ) ;
if ( image_code== '' ) {
alert ( '请先输入图片验证码' ) ;
return false ;
}
if ( phone== '' ) {
alert ( '手机号不能为空' ) ;
return false ;
}
$. ajax ( {
url: "{{url('sendSmsCode')}}" ,
data: {
phone: phone,
image_code: image_code,
sid: sid
} ,
dataType: 'json' ,
type: 'post' ,
success: function ( json_info) {
alert ( json_info. msg) ;
}
} )
}
function register ( ) {
let phone= $( '#phone' ) . val ( ) ;
let image_code= $( '#imageCode' ) . val ( ) ;
let sid= $( '#sid' ) . val ( ) ;
if ( image_code== '' ) {
alert ( '请先输入图片验证码' ) ;
return false ;
}
let password= $( '#password' ) . val ( ) ;
let repassword= $( '#repassword' ) . val ( ) ;
if ( password== '' ) {
alert ( '密码不能为空' ) ;
return false ;
}
if ( password !== repassword) {
alert ( '两次密码不一致' ) ;
return false ;
}
if ( phone== '' ) {
alert ( '手机号不能为空' ) ;
return false ;
}
let mcode= $( '#mcode' ) . val ( ) ;
$. ajax ( {
url: "{{url('register')}}" ,
data: {
phone: phone,
image_code: image_code,
sid: sid,
password: password,
mcode: mcode
} ,
dataType: 'json' ,
type: 'post' ,
success: function ( json_info) {
alert ( json_info. msg) ;
}
} ) ;
}
< / script>
登录login.blade.php
< ! doctype html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< meta name= "viewport" content= "width=device-width,initial-scale=1" >
< meta name= "csrf-token" content= "{{ csrf_token() }}" >
< title> H5 login< / title>
< style>
< / style>
< / head>
< body>
< div class = "flex-center position-ref full-height" >
< div class = "content" >
< div class = "title" >
H5 Login
< / div>
< table>
< tr>
< td> 用户名< / td>
< td> < input id= "name" value= "小小" type= "text" > < / td>
< / tr>
< tr>
< td> 密码< / td>
< td> < input id= "password" value= "111" type= "password" > < / td>
< / tr>
< tr>
< td colspan= "2" > < button onclick= "login()" > 登录< / button> < / td>
< / tr>
< / table>
< / div>
< / div>
< / body>
< / html>
< script src= "https://libs.baidu.com/jquery/2.1.1/jquery.min.js" > < / script>
< script>
$. ajaxSetup ( {
headers: {
'X-CSRF-TOKEN' : $( 'meta[name="csrf-token"]' ) . attr ( 'content' )
}
} ) ;
var login= function ( ) {
var user_name= $( '#name' ) . val ( ) ;
var psd= $( '#password' ) . val ( ) ;
if ( user_name== '' || password== '' ) {
alert ( '请填写你的用户名和密码' ) ;
return false ;
}
$. ajax ( {
url: "{{ url('login') }}" ,
type: 'post' ,
dataType: 'json' ,
data: {
user_name: user_name,
psd: psd
} ,
success: function ( json_info ) {
if ( json_info. status== 200 ) {
alert ( 'login ok' ) ;
}
}
} )
}
< / script>
loginbake.blade.php
< ! doctype html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< meta name= "viewport" content= "width=device-width,initial-scale=1" >
< title> H5 login< / title>
< style>
< / style>
< / head>
< body>
< div class = "flex-center position-ref full-height" >
< div class = "content" >
< div class = "title" >
H5 Login
< / div>
< table>
< tr>
< td> 用户名< / td>
< td> < input type= "text" > < / td>
< / tr>
< tr>
< td> 密码< / td>
< td> < input type= "text" > < / td>
< / tr>
< tr>
< td colspan= "2" > < button onclick= "login()" > jsonp登录< / button> < / td>
< / tr>
< tr>
< td colspan= "2" > < button onclick= "login2()" > 登录< / button> < / td>
< / tr>
< / table>
< / div>
< / div>
< / body>
< / html>
< script src= "https://libs.baidu.com/jquery/2.1.1/jquery.min.js" > < / script>
< script>
var url= 'http://api.ldyphp.top/login' ;
function login ( )
{
$. ajax ( {
url: url,
data: {
user_name: "zhangsan"
} ,
type: 'post' ,
dataType: 'jsonp' ,
jsonpCallback: 'loginCallBack' ,
success: function ( json_info) {
console. log ( json_info. msg) ;
alert ( JSON . stringify ( json) ) ;
}
} )
}
function login2 ( )
{
var url= 'http://api.ldyphp.top/login2' ;
$. ajax ( {
url: url,
data: {
user_name: "zhangsan"
} ,
type: 'post' ,
success: function ( json_info) {
console. log ( json_info. msg) ;
alert ( JSON . stringify ( json) ) ;
}
} )
}
< / script>