<?php
namespace Active\Src;

use Dever;
Class Auth
{
	#验证码登录
	public function login()
	{
		$mobile = Dever::input('mobile');
		$pass = Dever::input('pass');
		$user = Dever::db('active/user')->find(array('mobile'=>$mobile));
		if (!$pass) {
			$this->checkMcode($mobile);
		} 
		if ($user) {
			if($pass && $user['pass'] != sha1($pass) && $user['old_pwd'] != md5($pass. $user['old_salt'])) {
				Dever::alert('密码不正确');
			}
			$data = Dever::load('passport/reg')->getSign($user['id']);
            return $data;
		} else {
			$member = Dever::load('active/lib/user')->agent(array('mobile' => $mobile));
			if ($member) {
				if($pass && $member['pass'] != sha1($pass) && $member['old_pwd'] != md5($pass. $member['old_salt'])) {
					Dever::alert('密码不正确');
				}
				$insert['name'] = $member['name'];
				$insert['mobile'] = $member['mobile'];
				$insert['idcard'] = $member['idcard'];
				$insert['pass'] = $member['password'];
				$insert['old_pwd'] = $member['old_pwd'];
				$insert['old_salt'] = $member['old_salt'];
				$ids = Dever::db('active/user')->insert($insert);
			} else {
				$where['mobile'] = $mobile;
				$where['name'] = substr($mobile, 0, 3).'****'.substr($mobile, 7);
				$ids = Dever::db('active/user')->insert($where);
			}
			if ($ids) {
				$data = Dever::load('passport/reg')->getSign($ids);
        		return $data;
			}
		}
	}

	#注册
	public function reg()
	{
		$where['name'] = Dever::input('name');
		$where['mobile'] = Dever::input('mobile');
		$where['idcard'] = Dever::input('idcard');
		$data = Dever::db('active/user')->find(array('mobile'=>$mobile));
		if (!$data) {
			$this->checkMcode($where['mobile']);
			$ids = Dever::db('active/user')->insert($where);
			if ($ids) {
				$data = Dever::load('passport/reg')->getSign($ids);
				return $data;
			}
			
		} else {
			Dever::alert('该手机号已注册');
		}
	}
	#验证码
	public function getLoginMcode()
    {
        $mobile = Dever::input('mobile');

        $code = Dever::load('passport/reg')->mcode($mobile);

        $msg = '验证码已发送至您的手机，请注意查收，十分钟之内有效';
        $debug = Dever::config('base', 'project')->mobileCode['debug'];
        if ($debug) {
            $msg .= '::' . $code;
        }
        return $msg;
    }

    public function checkMcode()
    {
        $mobile = Dever::input('mobile');
        $code = Dever::input('mcode');
        if (!$code) {
            Dever::alert('请输入验证码');
        }
        $code = Dever::load('passport/reg')->mcode($mobile, $code, 2);
        if (!$code) {
            Dever::alert('验证码输入错误');
        }
        return 'ok';
    }

    #核销登录
    public function offLogin()
    {
    	$mobile = Dever::input('mobile');
    	if (!$mobile) {
    		Dever::alert('手机号不存在');
    	}
    	$pass = Dever::input('pass');
    	$data = Dever::db('active/info_off')->find(array('mobile'=>$mobile));
    	if ($mobile != $data['mobile']) {
    		Dever::alert('手机号还未注册');
    	}
    	if (!$pass || $pass != $data['pass']) {
    		Dever::alert('密码不能为空或密码不存在');
    	}
    	if ($data) {
			$data = Dever::load('passport/reg')->getSign($data['id']);
            return $data;
		}

    }
}