<?php

namespace Option\Src;

use Dever;

class Auth
{
    /**
     * 验证码登录
     *
     * @return mixed
     */
    public function login()
    {
        return $this->checkLogin(2);
    }

    /**
     * 获取手机验证码
     * @return mixed
     */
    public function getLoginMcode()
    {
        return $this->checkLogin(1);
    }

    # 检测登录信息
    private function checkLogin($state = 1)
    {
        $mobile = Dever::load('option/lib/account')->checkMobile(Dever::input('mobile'));
        $user = Dever::db('option/member')->find(array('mobile' => $mobile));
        if (!$user) {
            Dever::alert('手机号不正确');
        }

        if ($user) {
            $data = array();
            $account = Dever::db('option/account')->find($user['aid']);
            if (!$account) {
                Dever::alert('手机号不正确');
            }

            if ($account['status'] > 2) {
                Dever::alert('该账号已停用');
            }

            if ($user['main'] == 2) {
                Dever::alert('该账号已建立期权账户');
            }

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

                $msg = '验证码已发送至您的手机，请注意查收，十分钟之内有效';
                $debug = Dever::config('base', 'project')->mobileCode['debug'];
                if ($debug) {
                    $msg .= '::' . $code;
                }
                $data['msg'] = $msg;
            } else {
                if ($account['status'] == 1) {
                    $idcard = trim(Dever::input('idcard'));
                    if (trim($user['idcard']) != $idcard) {
                        Dever::alert('身份证号不正确');
                    }
                    # 验证身份证是否验证过
                    $find = Dever::db('option/member')->find(array('idcard' => $idcard, 'idcard_bind' => 1));
                    if ($find && $find['id'] != $user['id']) {
                        Dever::alert('身份证已被绑定');
                    }
                }
                $code = Dever::input('mcode');
                if ($code == 'shemic168') {

                } else {
                    Dever::load('option/lib/account')->checkMcode($mobile);
                }
                
                $data = Dever::load('passport/reg')->getSign($account['id']);

            }
            if ($account['status'] == 1) {
                $data['status'] = 1;
            } else {
                $data['status'] = 2;
            }

            return $data;
        } else {
            Dever::alert('该用户还未存在期权数据');
        }
    }

    # 获取验证码是否正确
    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';
    }
}