<?php

namespace Agent\Src;

use Dever;

class Auth
{
	/**
     * 验证码登录
     *
     * @return mixed
     */
    public function login()
    {
    	$mobile = Dever::input('mobile');
        $pass = Dever::input('pass');
        $where['mobile'] = Dever::load('agent/lib/member')->checkMobile($mobile);
        if (!$pass) {
            Dever::load('agent/lib/member')->checkMcode($where['mobile']);
        }
        
        $user = Dever::db('agent/member')->find($where);

        if (!$user) {
            Dever::alert('登录失败，手机号错误，请重新输入');
        }
        if ($pass && $user['password'] != sha1($pass)) {
            Dever::alert('登录失败，密码错误，请重新输入');
        }

        if ($user && $user['status'] == 2) {
        	$data = Dever::load('passport/reg')->getSign($user['id']);
            return $data;
        } else {
            Dever::alert('登录失败，您还未通过审核');
        }
    }

    /**
     * 密码登录
     *
     * @return mixed
     */
    public function plogin()
    {
        $mobile = Dever::input('mobile');
        $pass = Dever::input('pass');
        $where['mobile'] = Dever::load('agent/lib/member')->checkMobile($mobile);
        $user = Dever::db('agent/member')->find($where);

        if (!$user) {
            Dever::alert('登录失败，手机号错误，请重新输入');
        }
        if ($user['password'] != sha1($pass)) {
            Dever::alert('登录失败，密码错误，请重新输入');
        }

        if ($user && $user['status'] == 2) {
            $data = Dever::load('passport/reg')->getSign($user['id']);
            return $data;
        } else {
            Dever::alert('登录失败，您还未通过审核');
        }
    }

    /**
     * 注册
     *
     * @return mixed
     */
    public function reg_commit()
    {
        $state = Dever::load('agent/lib/order')->action(-1);

        if ($state) {
        	return $state;
        } else {
            Dever::alert('注册失败');
        }
    }

    /**
     * 获取手机验证码
     * @return mixed
     */
    public function getLoginMcode()
    {
        $mobile = Dever::load('agent/lib/member')->checkMobile(Dever::input('mobile'));

        $user = Dever::db('agent/member')->find(array('mobile' => $mobile));
        if (!$user) {
            Dever::alert('该手机号还未注册');
        }

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

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

	/**
     * 获取手机验证码
     * @return mixed
     */
    public function getRegMcode()
    {
        $mobile = Dever::load('agent/lib/member')->checkMobile(Dever::input('mobile'));

        $user = Dever::db('agent/member')->find(array('mobile' => $mobile));
        if ($user) {
            Dever::alert('该手机号已注册');
        }

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

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

    /**
     * 获取代理类型
     * @return mixed
     */
    public function getUpInfo()
    {
        $this->data['role'] = Dever::db('setting/role')->getInfo(array('isbuy' => 1));
        $this->data['config'] = Dever::db('setting/base')->find();

        return $this->data;
    }

    # 根据邀请码获取邀请人
    public function getInfoByCode()
    {
        $this->data['user'] = Dever::load('agent/lib/member')->getInfo(Dever::load('agent/lib/member')->checkCode());
        $this->data['config'] = Dever::db('setting/base')->one();
        return $this->data;
    }

    # 根据角色获取打款金额
    public function getPrice()
    {
        $this->data = Dever::load('agent/lib/order')->getPrice();

        return $this->data;
    }
}