三种身份登录:
<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Login extends Controller
{
public function student(){
$data=input('param.');
// 格式验证
$result=$this->validate($data,'Student.student_login');
if (true !== $result){
return error($result);
}
// 查找表内容
$stu = Db::name('student')
->field('id,name,schoolid,tel,state,password')
->where('tel',$data['tel'])
->where('password',encry($data['password']))
->find();
// print_r($stu);
// exit();
if (empty($stu)){
return error('手机号或密码错误');
}
$token=token();
$account['role']=4;
cache($token,$stu,1800);
return success(['message'=>'登录成功','token'=>$token,'role'=>4]);
}
public function classmanager(){
$data=input('param.');
// 格式验证
$result=$this->validate($data,'Banji.banji_login');
if (true !== $result){
return error($result);
}
// 查找表内容
$account=Db::name('account')
->field('id,name,account,tel,schoolid,state,type')
->where('account',$data['account'])
->where('password',encry($data['password']))
->find();
if (empty($account)){
return error('账号或密码错误');
}
if (3 !== $account['type']){
return error('该账号不是班级管理员');
}
$token=token();
$account['role']=3;
cache($token,$account,1800);
return success(['message'=>'登录成功','token'=>$token,'role'=>3]);
}
public function schoolmanager(){
$data=input('param.');
// 格式验证
$result=$this->validate($data,'School.school_login');
if (true !== $result){
return error($result);
}
// 查找表内容
$account=Db::name('account')
->field('id,name,account,tel,schoolid,state,type')
->where('account',$data['account'])
->where('password',encry($data['password']))
->find();
if (empty($account)){
return error('账号或密码错误');
}
if (2 !== $account['type']){
return error('该账户不是学校管理员');
}
$token=token();
$account['role']=2;
cache($token,$account,1800);
return success(['message'=>'登录成功','token'=>$token,'role'=>2]);
}
}
<?php
namespace app\common\validate;
use think\Validate;
class Student extends Validate
{
protected $rule=[
'schoolid'=>'require|number|length:6,6',
'tel'=>'require|number|length:11,11',
'password'=>'require|length:6,10',
'number'=>'require|number|length:10,10'
];
protected $message=[
'schoolid.require'=>'学校编码不能为空',
'schoolid.number'=>'学校编码必须为数字',
'schoolid.length'=>'学校编码必须是6位数字',
'tel.require'=>'手机号不能为空',
'tel.length'=>'手机号必须是11位',
'number.require'=>'学号不能为空',
'number.number'=>'学号必须为数字',
'number.length'=>'学号必须为10位',
'password.require'=>'密码不能为空',
'password.length'=>'密码必须是6-10位'
];
protected $scene = [
'student_reg'=>['schoolid','tel','number','password'],
'student_login'=>['tel','password'],
'update_number'=>['number'],
'update_tel'=>['tel'],
'update_password'=>['password']
];
}