橘子味的心
ThinkPHP5手机号登录,注册,获取验证码,验证验证码
ThinkPHP5手机号登录,注册,获取验证码,验证验证码
发布于:

登录:

/**
 * 用户手机号登录
 */
public function login()
{  
    $obtain_token = new \app\api\controller\Token();
	$phone = input('phone');
	$password = input('password');
    $result = Db::name('user')->where('phone',$phone)->find();
    //判断用户是否存在
    if (empty($result)) {
        return json(100,'用户不存在');
    }
    //获取配置文件进行缓存
    $config = Api('Config/lists');
    if($config['web_site_close'] == 0){
            return json(100,'站点已经正在维护中!,请稍后访问');
    }
    if($config['admin_allow_ip']){
        if(in_array(request()->ip(),explode(',',$config['admin_allow_ip']))){
            return json(100,'IP禁止访问');
        }
    }
    //判断密码是否正确
    if ($result['password']  != md5(md5($password).md5($result['salt']))) {
        return json(100,'密码错误');
    }
    //更新用户状态
     $data = [
         'token' => $obtain_token->settoken()
        , 'time_out' => time() + 604800
        , 'last_login_ip' => request()->ip()
        , 'last_login_time' => time()
    ];
    Db::name('user')->where('id',$result['id'])->update($data);
    $res = Db::name('user')->where('phone',$phone)->field('password',true)->find();
    return json(200,'ok!',$res);
}

注册:

/**
 * 用户手机号注册
 */
public function register()
{
    $phone = input('phone');
	$password = input('password');
    $p_phone = input('p_phone');
    $code = input('code');
    /**-------------- 验证验证码是否正确 --------------*/
    $this->is_phone_code($phone,$code);
    /**-------------- 验证手机号是否注册 --------------*/
    if(Db::name('user')->where('phone',$phone)->count()){
        return json(100,'该手机号已经注册!');
    }
    $salt = mt_rand(100000,999999);
    $data = [
        'phone' => $phone
        ,'password' => md5(md5($password).md5($salt))
        ,'salt' => $salt
        ,'p_phone' => $p_phone
    ];
    if(Db::name('user')->insert($data) == false){
        return json(100,'注册失败!');
    }
    return json(200,'注册成功!');
}

重置密码:

/**
 * 重置密码
 */
public function repassword()
{
    $phone = input('phone');
    $password = input('password');
    $code = input('code');
    if(empty($password)){
        return json(100,'请输入密码!');
    }
    $this->is_phone_code($phone,$code);
    $salt = mt_rand(100000,999999);
    $data = [
        'password' => md5(md5($password).md5($salt))
        ,'salt' => $salt
    ];
    if(Db::name('user')->where('phone',$phone)->update($data) == false){
        return json(100,'重置密码失败!');
    }
    return json(200,'重置密码成功!');
}

验证验证码:

//验证验证码
private function is_phone_code($phone,$code,$type=1){
    $code_time = 300;
    $res = Db::name('code')->where(['phone'=>$phone,'code'=>$code])->order('create_time','desc')->find();
    if(empty($res)){
        return json(100,'验证码不正确!');
    }
    if($res['create_time']< time() - $code_time){
        return json(100,'验证码已过期!');
    }
    Db::name('code')->where(['phone'=>$phone,'code'=>$code])->update(['is_use'=>1]);
    return true;
}

获取验证码:

//获取验证码
public function get_phone_code(){   
    if(request()->isPost()){
        $phone = input('phone');
        // $type = input('type',0);
        $result = $this->validate(['phone'=>$phone],'app\api\validate\Login.yzm');
        if(true != $result){
            return json(100,$result);
        }
		//1 注册 2找回 3修改
        // if(!in_array($type,config_group('web_phone_code'))){
        //     return json(100,'请求类型不正确');
        // }
        $code_data = Db::name('code')->where(['phone'=>$phone,'is_use'=>0])->whereTime('create_time','>',time()-60)->find();
        if(!empty($code_data)){
            return json(100,'请等待60秒后在获取验证码');
        }
        
        $code = mt_rand(100000,999999);
        $data = [
            'phone'=>$phone,
            'code'=>$code,
            // 'type'=>$type,
            'create_time'=>time()
        ];
        try {
            Db::name('code')->insert($data);
            $smsConf = [
                'mobile' => $phone,
                'tpl_id' => 208963,
                'tpl_value' => '#code#='.$code.'&#company#=聚合数据',
            ];
            $this->send_msg($smsConf);
        } catch (\Exception $e) {
            return json(100,'短信发送失败');
        }
        return json(200,'短信发送成功!');
    }
}
//发送消息
private function send_msg($smsConf){
    //暂时的
    $da = [
        'd_key'=>'3d86c116af0c7f153b9acb639bd63fdb',//秘钥
        'd_url'=>'http://v.juhe.cn/sms/send',//请求地址
    ];
    $sendUrl = trim($da['d_url']); //短信接口的URL
    $smsConf['key'] = $da['d_key'];
    $smsConf['d_url'] = $da['d_url'];
    $content = $this->duanxen($sendUrl, $smsConf);
    if ($content == false) {
        return json(100,'短信发送失败');
    }
}

/* 短信 */
private function duanxen($sendUrl, $smsConf) {
    $content = $this->juhecurl($sendUrl, $smsConf, 1); //请求发送短信
    if ($content) {
        $result = json_decode($content, true);
        $error_code = $result['error_code'];
        if ($error_code == 0) {
            //状态为0,说明短信发送成功
            return true;
        } else {
            //状态非0,说明失败
            return false;
        }
    } else {
    	//数据异常
        return false;
    }
}
/**
 * 请求接口返回内容
 * @param  string $url [请求的URL地址]
 * @param  string $params [请求的参数]
 * @param  int $ipost [是否采用POST形式]
 * @return  string
 */
private function juhecurl($url, $params = false, $ispost = 0) {
    $httpInfo = array();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if ($ispost) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_URL, $url);
    } else {
        if ($params) {
            curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
        } else {
            curl_setopt($ch, CURLOPT_URL, $url);
        }
    }
    $response = curl_exec($ch);
    if ($response === FALSE) {
        //echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
    curl_close($ch);
    return $response;
}

//验证场景

<?php 
namespace app\api\validate;
use think\Validate;
class Login extends Validate
{
    protected $rule =   [
        'phone'  => 'require|max:11|/^1[3-8]{1}[0-9]{9}$/',
    ];
    protected $message  =   [
        'phone.require' => '手机号必须',
        'phone.max'     => '手机号最多11位', 
    ];
    protected $scene = [
        'yzm'  =>  ['phone'],
    ];
}

数据库用户(user):

CREATE TABLE `bc_user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户ID',
  `uid` varchar(128) DEFAULT NULL COMMENT 'uid',
  `user_name` varchar(255) CHARACTER SET utf8 DEFAULT NULL COMMENT '用户昵称',
  `phone` varchar(11) DEFAULT NULL COMMENT '手机号',
  `openid` varchar(50) CHARACTER SET utf8 DEFAULT NULL COMMENT '微信openId',
  `inviter_id` int(10) DEFAULT NULL COMMENT '邀请人id',
  `up_id` int(10) DEFAULT NULL COMMENT '上级id',
  `p_phone` varchar(11) DEFAULT NULL COMMENT '上级邀请人手机号',
  `unionid` varchar(50) CHARACTER SET utf8 DEFAULT NULL COMMENT '微信唯一id',
  `token` varchar(50) DEFAULT NULL COMMENT '用户token',
  `time_out` int(10) DEFAULT NULL COMMENT 'token失效时间',
  `user_type` int(10) unsigned DEFAULT '0' COMMENT '用户类型',
  `headimgurl` varchar(512) CHARACTER SET utf8 DEFAULT NULL COMMENT '头像',
  `password` varchar(512) CHARACTER SET utf8 DEFAULT NULL COMMENT '密码',
  `encryption` varchar(512) CHARACTER SET utf8 DEFAULT NULL COMMENT '密码后缀',
  `salt` varchar(50) DEFAULT NULL COMMENT '交易密码',
  `pay_encryption` varchar(512) DEFAULT NULL COMMENT '交易密码后缀',
  `profit_sum` decimal(20,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '总收益',
  `profit_sy` decimal(20,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '剩余收益(可提现)',
  `yjtx` decimal(20,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '已经提现',
  `is_certified` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否实名认证 0:否 1:是',
  `is_del` int(2) DEFAULT '0' COMMENT '是否删除 1是 0否',
  `status` tinyint(1) unsigned DEFAULT '1' COMMENT '是否启用 0:否 1:是',
  `region` varchar(50) DEFAULT NULL COMMENT '地区',
  `create_time` int(10) DEFAULT NULL COMMENT '创建时间',
  `update_time` int(10) DEFAULT NULL COMMENT '更新时间',
  `last_login_ip` varchar(50) DEFAULT NULL COMMENT '登录ip',
  `last_login_time` int(10) DEFAULT NULL COMMENT '登录时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT;

数据库验证码(code):

CREATE TABLE `bc_code` (
  `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '手机验证码ID ',
  `phone` varchar(11) CHARACTER SET utf8 DEFAULT NULL COMMENT '手机号',
  `code` varchar(10) CHARACTER SET utf8 DEFAULT NULL COMMENT '短信验证码',
  `type` int(10) DEFAULT '0' COMMENT '验证吗类型',
  `is_use` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否使用 0:无 1:是',
  `create_time` int(10) DEFAULT NULL COMMENT '创建时间',
  `update_time` int(10) DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='手机验证码表';


阅读 0

分类

    热门排行