Initial commit
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
<?php
|
||||
namespace Member\Model;
|
||||
use Think\Model;
|
||||
use Org\MobileVerify\MobileVerifyCode;
|
||||
|
||||
class PublicModel extends Model{
|
||||
protected $tableName="member";
|
||||
/**
|
||||
* 验证字段
|
||||
* @var array
|
||||
*/
|
||||
protected $_validate=array(
|
||||
//array("字段","验证规则","错误提示",["验证条件","附加条件","验证时间"]),
|
||||
array("mobile","require","手机号码不能为空"),
|
||||
array('mobile','/^1[3|4|5|7|8][0-9]\d{8}$/','手机号码错误!','0','regex',1),
|
||||
array('mobile','','手机号码已存在!',0,'unique',1), // 在新增的时候验证手机号码字段是否唯一
|
||||
array("verifycode","require","请输入短信验证码"),
|
||||
array("verifycode","6","短信验证码为6位",2,'length'),
|
||||
array("password","require","密码不能为空"),
|
||||
array("password","6,40","密码长度需要6位以上",2,"length"),
|
||||
array("agree","require","请同意《服务条款》",1),
|
||||
array('repassword','password','确认密码不正确',0,'confirm'), // 验证确认密码是否和密码一致
|
||||
);
|
||||
public function checkTruename(){
|
||||
$member_type=I("member_type");
|
||||
$truename=I("truename");
|
||||
if($member_type=="person"&&!$truename){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 重写save
|
||||
* @param $data 保存的数据,默认为空
|
||||
*/
|
||||
public function save($data=null){
|
||||
// 判断短信验证码是否正确
|
||||
$verifycode = I('post.verifycode');
|
||||
$mobile = I('post.mobile');
|
||||
$mVC = new MobileVerifyCode();
|
||||
if(!$mVC->check($mobile,$verifycode)){
|
||||
$this->error = $mVC->error;
|
||||
return false;
|
||||
}
|
||||
$data['register_time'] = time();
|
||||
$data['checked'] =1;
|
||||
$data['status'] =1;
|
||||
$data['password'] = md6($data['password']);
|
||||
if($data['member_id']){
|
||||
return parent::save($data);
|
||||
}else{
|
||||
$mVC = new MobileVerifyCode();
|
||||
$mVC->clear();//清空验证码
|
||||
$this->error = "注册成功";
|
||||
return $this->add($data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断用户登陆
|
||||
* @return array or false
|
||||
*/
|
||||
public function checkLogin(){
|
||||
$username = I("mobile");
|
||||
$password = md6(I("password"));
|
||||
$member_r = $this->where(array("mobile" => $username))->find();
|
||||
if (!$member_r) {
|
||||
$this->error = "用户名或密码错误";
|
||||
return false;
|
||||
}
|
||||
if ($member_r["password"] === $password) {
|
||||
if (!$member_r[status]) {
|
||||
$this->error = "用户已被禁用";
|
||||
return false;
|
||||
}elseif (!$member_r[checked]) {
|
||||
$this->error = "用户未审核通过";
|
||||
return false;
|
||||
}else{
|
||||
return $this->setLogin($member_r);
|
||||
}
|
||||
}
|
||||
$this->error = "用户名或密码错误";
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $member_r 用户信息
|
||||
* @return bool [true or false] 保存登陆信息
|
||||
*/
|
||||
public function setLogin($member_r){
|
||||
$session_data = array(
|
||||
"member_id" => $member_r["member_id"],
|
||||
"truename" => $member_r["truename"],
|
||||
"zhizhao" => $member_r["zhizhao"],
|
||||
"mobile" => $member_r["mobile"],
|
||||
);
|
||||
session("member",$session_data);
|
||||
$newdata["member_id"]=$member_r['member_id'];
|
||||
$newdata["login_num"]=$member_r['login_num']+1;
|
||||
$newdata['last_login_ip']= get_client_ip();
|
||||
$newdata['last_login_time']=time();
|
||||
return parent::save($newdata);
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用create
|
||||
* @return array or false
|
||||
*/
|
||||
public function create2(){
|
||||
$rules = array(
|
||||
array("mobile", "require", "手机号不能为空"),
|
||||
array('mobile','/^(1(([35789][0-9])|(47)))\d{8}$/','手机号码错误!','0','regex',1),
|
||||
array("password", "require", "密码不能为空"),
|
||||
);
|
||||
$this->validate($rules);
|
||||
return parent::create();
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送注册短信验证码
|
||||
* @param $mobile
|
||||
* @return bool
|
||||
*/
|
||||
public function sendCode($mobile){
|
||||
if(!self::check_phone($mobile)){
|
||||
$this->error = "请输入正确手机号码";
|
||||
return false;
|
||||
}
|
||||
if(self::exists_phone($mobile)){
|
||||
$this->error = "手机号码已存在";
|
||||
return false;
|
||||
}
|
||||
|
||||
$mVC = new MobileVerifyCode();
|
||||
return $mVC->send_code($mobile);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断手机号码数据库是否存在
|
||||
* @param $phone 手机号
|
||||
* @return bool [true or false]
|
||||
*/
|
||||
public static function exists_phone($phone)
|
||||
{
|
||||
$where["mobile"] = $phone;
|
||||
return M("member")->where($where)->find() ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用正则判断手机号码格式是否正确
|
||||
* @param $phone 手机号
|
||||
* @return bool [true or false]
|
||||
*/
|
||||
public static function check_phone($phone)
|
||||
{
|
||||
$check = '/^(1(([35789][0-9])|(47)))\d{8}$/';
|
||||
if (preg_match($check, $phone)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存签名的密码
|
||||
* @param $phone 手机号
|
||||
* @return bool [true or false]
|
||||
*/
|
||||
public static function md5_sign($password)
|
||||
{
|
||||
return md5(md5($password.'jijin'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 推出登陆 清空客户端登陆session
|
||||
*/
|
||||
public static function logout()
|
||||
{
|
||||
session("member",null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user