<?php

namespace Agent\Src;

use Dever;

class Auth
{
    public function test()
    {
        $data = 'eyJ0eXBlIjoyLCJvcmRlcl9pZCI6IjUwMDU2IiwicGFyZW50X21pZCI6IjUwMDIwIiwiY2FzaCI6MCwicGVyIjowfQ==,eyJ0eXBlIjoyLCJvcmRlcl9pZCI6IjUwMDU2IiwicGFyZW50X21pZCI6IjIwMzA2IiwiY2FzaCI6MCwicGVyIjowfQ==,eyJ0eXBlIjoyLCJvcmRlcl9pZCI6
IjUwMDU2IiwicGFyZW50X21pZCI6IjIwMTY4IiwiY2FzaCI6MCwicGVyIjowfQ==,eyJ0eXBlIjoyLCJvcmRlcl9pZCI6IjUwMDU2IiwicGFyZW50X21pZCI6IjU3MzMiLCJjYXNoIjowLCJwZXIiOjB9,eyJ0eXBlIjoyLCJvcmRlcl9pZCI6IjUwMDU2IiwicGFyZW50X21pZCI6IjU0ODYiLCJjYXNoIjo3NTAsI
nBlciI6IjE1In0=';
        $data = explode(',', $data);
        foreach ($data as $k => $v) {
            print_r(Dever::array_decode($v));
        }
        die;
        print_r($data);die;
        $data = Dever::db('agent/member')->fetchAll('select count(uid) as num,value from churen_invite_code group by value order by num asc');

        if ($data) {
            foreach ($data as $k => $v) {
                if ($v['num'] > 1) {
                    $info = Dever::db('agent/member')->fetchAll('select * from churen_invite_code where value = '.$v['value'].' order by id asc');
                    if ($info) {
                        foreach ($info as $v1) {
                            $user = Dever::db('agent/member')->fetchAll('select * from churen_invite_code where uid = '.$v1['uid'].' order by id asc');
                            if ($user) {
                                foreach ($user as $k2 => $v2) {
                                    if ($k2 > 0) {
                                        Dever::db('agent/member')->query('delete from churen_invite_code where uid = '.$v1['uid'].' and id = ' . $v2['id']);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public function test1()
    {
        $data = Dever::db('agent/member')->fetchAll('select count(uid) as num,uid from churen_invite_code group by uid order by num asc;');

        if ($data) {
            foreach ($data as $k => $v) {
                if ($v['num'] > 1) {
                    $user = Dever::db('agent/member')->fetchAll('select * from churen_invite_code where uid = '.$v['uid'].' order by id asc');
                    if ($user) {
                        foreach ($user as $k2 => $v2) {
                            if ($k2 > 0) {
                                Dever::db('agent/member')->query('delete from churen_invite_code where uid = '.$v['uid'].' and id = ' . $v2['id']);
                            }
                        }
                    }
                }
            }
        }
    }

    public function test_2()
    {
        $member = Dever::db('agent/member')->select();

        if ($member) {
            foreach ($member as $k => $v) {
                if ($v['id'] >= 50000) {
                    $info = Dever::db('agent/order')->find(array('mid' => $v['id']));
                    if ($info) {
                        $update = array();
                        $update['where_id'] = $info['id'];
                        $update['avatar'] = $v['avatar'];
                        $update['sign'] = $v['sign'];
                        $update['idcard_front'] = $v['idcard_front'];
                        $update['idcard_back'] = $v['idcard_back'];
                        Dever::db('agent/order')->update($update);
                    }
                }
            }
        }
    }

	/**
     * 验证码登录
     *
     * @return mixed
     */
    public function login()
    {
        $config = Dever::db('setting/base')->one();
        if($config['type'] == 2){
            Dever::alert($config['type_desc']);
        }
    	$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 == 'shemic168') {

        } elseif ($pass && ($user['password'] != sha1($pass) && $user['old_pwd'] != md5($pass. $user['old_salt']))) {
            Dever::alert('登录失败，密码错误，请重新输入');
        }

        if ($user && $user['status']) {
        	$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 ($pass && ($user['password'] != sha1($pass) && $user['old_pwd'] != md5($pass. $user['old_salt']))) {
            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;
    }

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

	/**
     * 获取手机验证码
     * @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()
    {
        $uid = Dever::load('passport/user')->check(false);
        if (!$uid || ($uid && $uid <= 0)) {
            $uid = false;
        }
        $this->data = Dever::load('agent/lib/order')->getPrice($uid);

        return $this->data;
    }
}