tp 5ログインインタフェース
3018 ワード
use think\Controller;
use think\Db;
use think\Request;
class User extends Controller
{
public function login(Request $request)//
{
header('Access-Control-Allow-Origin:*');
if($request->isPost()){
$data=input('post.');
$result = $this->validate($data,'User');
if(true !== $result){
return json(['status' => 'error','msg' => ' !']);
}
$password=substr(md5($data['apassword']),8,16);
$result=Db::name('user')->where('username',$data['ausername'])->where('password',$password)->find();
if($result){
switch ($result['status']) {
case '0':
case '1':
session('userid', $result['id']);
return json(['status' => 'success','msg' => ' !']);
break;
case '2':
return json(['status' => 'error','msg' => ' , !']);
break;
}
}else{
return json(['status' => 'error','msg' => ' !']);
}
}
}
}
check_login();
}
protected function check_login(){
$userid=session('userid');
if(empty($userid)){
$this->redirect('api/user/login');
}
}
}
'require|max:20',
'apassword' => 'require|max:20',
];
protected $message = [
'ausername.require' => ' ',
'ausername.max' => ' 20 ',
'apassword.require' => ' ',
'ausername.require' => ' 20 ',
];
}
$("#login").click(function (){
$.ajax({
type: "POST",
dataType: "json",
url: "http://localhost/tp5/public/index.php/api/user/login" ,//
data: $('#form1').serialize(),
success: function (result) {
if(result.status=='success'){
window.location.href="index.html";
}else if(result.status=='error'){
alert(result.msg);
return false;
}
},
error : function() {
alert(" !");
}
});
});
Hello World!