<?php
/**
 * User: nickbai
 * Date: 2017/10/24 10:46
 * Email: 1902822973@qq.com
 */

namespace app\index\controller;

use think\Db;
use app\index\controller\Common;

class Register extends Common
{
    // 注册首页
    public function index()
    {
        $this->assign([
            'version' => config('version')
        ]);

        return $this->fetch();
    }

    // 处理注册
    public function doRegister()
    {
        if (request()->isPost()) {

            $userName = input("param.user_name/s");
            $userEmail = input("param.user_email/s");
            $password = input("param.password/s");
            $phone = input("param.phone/s");
            $emailCode = input("param.code/s");

            if (empty($userName)) {
                return json(['code' => -1, 'data' => '', 'msg' => '用户名不能为空']);
            }

            if (empty($userEmail)) {
                return json(['code' => -2, 'data' => '', 'msg' => '邮箱不能为空']);
            }

            if (empty($password)) {
                return json(['code' => -3, 'data' => '', 'msg' => '密码不能为空']);
            }

            if (empty($phone)) {
                return json(['code' => -4, 'data' => '', 'msg' => '密码不能为空']);
            }

            if (empty($emailCode)) {
                return json(['code' => -5, 'data' => '', 'msg' => '验证码不能为空']);
            }

            if ($emailCode != session('code')) {
                return json(['code' => -6, 'data' => '', 'msg' => '验证码不正确']);
            }

            $name = db('accounts')->where('account_name', $userName)->find();
            if (!empty($name)) {
                return json(['code' => -7, 'data' => '', 'msg' => '用户名已存在']);
            }

            $email = db('accounts')->where('account_email', $userEmail)->find();
            if (!empty($email)) {
                return json(['code' => -8, 'data' => '', 'msg' => '邮箱已存在']);
            }

            // 添加用户信息
            $userInfo = [
                'account_name' => $userName,
                'account_email' => $userEmail,
                'password' => md5($password . config('salt')),
                'account_phone' => $phone,
                'status' => 1,
                'add_time' => time(),
                'last_login_time' => time()
            ];

            $user_id = db('accounts')->insertGetId($userInfo);

            // 注册成功 生成token
            $module = mt_rand(100000, 999999);
            $token = base64_encode($module . '#$@%!^*/' . time() . '/' . $userInfo['id']);
            // 更新用户状态
            $param = [
                'token' => $token,
                'expire_time' => time(),
            ];
            db('accounts')->where('id', $user_id)->update($param);

            return json(['code' => 1, 'data' => url('user/index'), 'msg' => '注册成功']);
        }
    }


    //自动注册
    public function autoReg()
    {
        //验证用户IP
        $settings = db('settings')->where('id',1)->find();
        if($settings['account_black_list'] == 'on'){
            @$ip = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
            $ip = ($ip) ? $ip : $_SERVER["REMOTE_ADDR"];
            //将ip地址转换成int型
            $intip = bindec(decbin(ip2long($ip)));
            $res = db('iplimit')->where('ip',$ip)->where('object',1)->find();
            $result = db('iplimit')->where('start_ip','<=',$intip)->where('end_ip','>=',$intip)->where('object',1)->find();
            if(!empty($res) || !empty($result)){
                return json(['code' => 0, 'data' => [], 'msg' => '访问受限']);
            }
        }

        /*if (!$this->verifyApiToken()) {
            return json(['code' => -6, 'data' => [], 'msg' => 'require false']);
        }*/

        $appid = trim(input("post.appid/s", ''));
        $appuid = trim(input("post.appuid/s", ''));
        $token = trim(input("post.token/s", ''));
        $nowuid = (empty($appuid) || empty($appuid)) ? uniqid('anon_') : $appid . '_' . $appuid;

        if (!empty($nowuid) && !empty($token)) {
            $old = Db::name('accounts')->where(['account_name' => $nowuid, 'tokenvip' => $token])->find();
            if ($old) {
                Db::name('accounts')->where(['account_name' => $nowuid])->update(['last_login_time' => time()]);
                return json(['code' => 1, 'data' => ['id' => $old['id'], 'name' => $nowuid, 'token' => $old['tokenvip']], 'msg' => '注册成功', 'tutype' => 1]);
            }
        }

        if ($token) {
            $old = db('accounts')->where(['token' => $token])->find();
            if ($old) {
                if ($old['status'] != 1) {
                    return json(['code' => 0, 'data' => [], 'msg' => '禁止登陆']);
                }
                Db::name('accounts')->where(['token' => $token])->update(['last_login_time' => time()]);
                return json(['code' => 1, 'data' => ['id' => $old['id'], 'name' => $old['account_name'], 'token' => $old['token']], 'msg' => '注册成功', 'tutype' => 0]);
            }
        }

        NEWUSERLABLE:
        $now = time();
        $token = md5(uniqid() . rand(10000, 50000));
        $tokenvip = md5(md5(uniqid() . rand(60000, 90000)));
        $newdata = [
            'account_name' => $nowuid,
            'password' => md5(time() . rand(1, 5000)),
            'status' => 1,
            'add_time' => $now,
            'last_login_time' => $now,
            'token' => $token,
            'tokenvip' => $tokenvip,
            'expire_time' => $now,
        ];
        try {
            $retid = Db::name('accounts')->insertGetId($newdata);
        } catch (\Exception $e) {
            $retid = false;
        }

        if ($retid) {
            if (empty($appuid)) {
                return json(['code' => 1, 'data' => ['id' => $retid, 'name' => $nowuid, 'token' => $token], 'msg' => '注册成功', 'tutype' => -1]);
            } else {
                return json(['code' => 1, 'data' => ['id' => $retid, 'name' => $nowuid, 'token' => $tokenvip], 'msg' => '注册成功', 'tutype' => 2]);
            }
        } else {
            return json(['code' => 0, 'data' => [], 'msg' => '注册用户失败']);
        }

    }

}
