<?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()
    {
        if (!$this->verifyApiToken()) {
            return json(['code' => -6, 'data' => [], 'msg' => 'require false']);
        }

        $appid = input("post.appid/s");
        $appuid = input("post.appuid/s");
        $token = trim(input("post.token/s"));
        $nowuid = (empty($appuid) || empty($appuid)) ? uniqid('anon_') : $appid . '_' . $appuid;
        $avatar = "http://wx2.sinaimg.cn/mw690/5db11ff4gy1flxmew7edlj203d03wt8n.jpg";  //默认头像

        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'], 'avatar' => $avatar], 'msg' => '注册成功']);
            }
        }


        $old = Db::name('accounts')->where(['account_name' => $nowuid])->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['token'], 'avatar' => $avatar], 'msg' => '注册成功']);
        }

        $now = time();
        $token = md5(uniqid() . rand(10000, 50000));
        $newdata = [
            'account_name' => $nowuid,
            'password' => md5(time() . rand(1, 5000)),
            'status' => 1,
            'add_time' => $now,
            'last_login_time' => $now,
            'token' => $token,
            'expire_time' => $now,
        ];
        $retid = Db::name('accounts')->insertGetId($newdata);
        if ($retid) {
            return json(['code' => 1, 'data' => ['id' => $retid, 'name' => $nowuid, 'token' => $token, 'avatar' => $avatar], 'msg' => '注册成功']);
        } else {
            return json(['code' => 0, 'data' => [], 'msg' => '注册用户失败']);
        }

    }

}