注册:
<?php
namespace app\api\controller;
use app\api\model\Account;
use app\api\model\Student;
use think\Controller;
use think\Db;
class Register extends Controller
{
//学生注册部分部分 (学校编码、手机号 、密码)
public function student()
{
$data = input('post.');
$result = $this->validate($data, 'Student.student_reg');
if (true !== $result) {
return error($result);
}
//检查学校编码是否正确
$checkSchoolid = Db::name('school')
->where('schoolid', $data['schoolid'])
->find();
if (empty($checkSchoolid)) {
return error('学校编码错误');
};
//检查学号是否已经注册
$checknumber = Db::name('student')
->where('number', $data['number'])
->find();
if ($checknumber) {
return error("该学号已经被注册了");
}
//检查手机号是否注册
$checktel = Db::name('student')
->where('tel', $data['tel'])
->find();
if ($checktel) {
return error("该手机号已经被注册了");
}
//存入数据
$data['openid'] = time();
$data['password'] = encry($data['password']);
$data['state'] = 0; //状态 0可用 1禁用
$student = new Student($data);
$row = $student->allowField(true)->save();
if ($row !== 1) {
return error('新增记录失败');
} else {
return success('注册成功');
}
}
//班级管理员注册部分部分 (学校编码,管理员邀请码,密码,真实姓名,电话)
public function classmanager()
{
$data = input('post.');
$result = $this->validate($data, 'Banji.banji_reg');
if (true !== $result) {
return error($result);
}
//检查学校编码是否正确
$checkSchoolid = Db::name('school')
->where('schoolid', $data['schoolid'])
->find();
if (empty($checkSchoolid)) {
return error('学校编码错误');
}
//检查邀请码是否正确
$checkYqm = Db::name('school')
->where('yqm2', $data['yqm2'])
->find();
if (empty($checkYqm)) {
return error('邀请码错误');
}
//检查账户是否已经被注册
$checkaccount = Db::name('account')
->where('account', $data['account'])
->find();
if ($checkaccount) {
return error("该账户已经被注册了");
}
//存入数据
$data=['schoolid'=>$data['schoolid'],'account'=>$data['account'],'openid'=>time(),
'type'=>3,'tel'=>$data['tel'],'password'=>encry($data['password']),'state'=>0];
$cmanager = new Account($data);
$row = $cmanager->allowField(true)->save();
if ($row !== 1) {
return error('新增记录失败');
} else {
return success('注册成功');
}
}
//学校管理员注册部分部分
public function schoolmanager()
{
$data = input('post.');
$result = $this->validate($data, 'School.school_reg');
if (true !== $result) {
return error($result);
}
//检查学校编码是否正确
$checkSchoolid = Db::name('school')
->where('schoolid', $data['schoolid'])
->find();
if (empty($checkSchoolid)) {
return error('学校编码错误');
}
//检查邀请码是否正确
$checkYqm = Db::name('school')
->where('yqm1', $data['yqm1'])
->find();
if (empty($checkYqm)) {
return error('邀请码错误');
}
//检查账户是否已经被注册
$checkaccount = Db::name('account')
->where('account', $data['account'])
->find();
if ($checkaccount) {
return error("该账户已经被注册了");
}
//存入数据
$data=['schoolid'=>$data['schoolid'],'account'=>$data['account'],'openid'=>time(),
'type'=>2,'tel'=>$data['tel'],'password'=>encry($data['password']),'state'=>0];
$cmanager = new Account($data);
$row = $cmanager->allowField(true)->save();
if ($row !== 1) {
return error('新增记录失败');
} else {
return success('注册成功');
}
}
// 结束
}
//////////////////////////////以下是在本tp5上写的代码
前台:
<form action="{:url('login/regist')}" method="post">
<table border="0" style="width:420px; font-size:14px; margin-top:20px;" cellspacing="0" cellpadding="0">
<tr height="50">
<td align="right"><font color="#ff4e00">*</font> 用户名 </td>
<td><input type="text" name="username" value="" class="l_user" /></td>
</tr>
<tr height="50">
<td align="right"><font color="#ff4e00">*</font> 密码 </td>
<td><input type="password" name="pwd" value="" class="l_pwd" /></td>
</tr>
<tr height="50">
<td align="right"><font color="#ff4e00">*</font> 确认密码 </td>
<td><input type="password" name="rpwd" value="" class="l_pwd" /></td>
</tr>
<tr height="50">
<td align="right"><font color="#ff4e00">*</font> 邮箱 </td>
<td><input type="text" value="" name="email" class="l_email" /></td>
</tr>
<tr height="50">
<td align="right"><font color="#ff4e00">*</font> 手机 </td>
<td><input type="text" value="" name="phone" class="l_tel" /></td>
</tr>
<tr height="50">
<td align="right"> <font color="#ff4e00">*</font> 验证码 </td>
<td>
<input type="text" value="" name="code" class="l_ipt" />
<span id="code"><img src="{:captcha_src()}" alt="captcha" /></span>
</td>
</tr>
<tr height="60">
<td> </td>
<td><input type="submit" value="立即注册" class="log_btn" /></td>
</tr>
</table>
</form>
下面是针对有两次密码验证,验证码的验证,验证器:
//立即注册
public function regist()
{
if(Request::instance()->isGet()){
return view('regist');
}else{
//结束值
$arr=input('post.');
//验证器
$validate = new Validate([
'username' => 'require|max:25',
'phone' => 'require',
'pwd' => 'require',
'email' => 'email',
]);
if(!$validate->check($arr)){
//输出的提示错误信息
$error=$validate->getError();
$this->error($error,'login/regist');
}
//判断密码和确认码的
$rpwd=md5($_POST['rpwd']);
$pwd=md5($_POST['pwd']);
if($rpwd!=$pwd){
$this->error('两次密码不一致');
}
//判断验证码
$captcha=input('post.code');
if(!captcha_check($captcha)){
//验证失败
$this->error('验证码错误');
};
//删除确认密码
unset($arr['rpwd']);
unset($arr['code']);
$arr['pwd']=md5($_POST['pwd']);
$user=new Username();
$res=$user->save($arr);
if($res){
$this->success('注册成功','login/login');
}else{
$this->error('注册失败');
}
}
}