<?php
// +----------------------------------------------------------------------
// | 登录控制器
// +----------------------------------------------------------------------
// | Copyright (c) 2018 https://blog.junphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小黄牛 <1731223728@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\admin\login;
use app\controller\SuperClass\Admin;

/**
 * @Controller(prefix="admin")
*/
class Index extends Admin
{

    /**
     * @RequestMapping(route="/login", method="GET", title="登录页面")
    */
    public function run() {
        return $this->display('admin/login/index');
    }
    
    /**
     * @RequestMapping(route="/login/send", method="POST", title="提交登录处理")
     * @Param(name="name", type="string", empty="true", tips="请输入账号")
     * @Param(name="pwd", type="string", empty="true", tips="请输入密码")
    */
    public function send() {
        $param = \x\Request::post();

        $httpClient = new \x\Client();
        $res = $httpClient->http()
                ->domain('https://blog.junphp.com/api/geetest_captcha/php/v1/ajax_vif.php')
                ->body([
                    'appid' => 'blog.junphp.com',
                    'junphp_session_id' => $param['junphp_session_id'],
                    'junphp_appkey' => $param['junphp_appkey'],
                    'junphp_sign' => $param['junphp_sign'],
                    'junphp_time' => $param['junphp_time'],
                    'junphp_geetest' => $param['junphp_geetest'],
                    'junphp_yes' => $param['junphp_yes'],
                ])
                ->post();
        $arr = json_decode($res, true);
        if ($arr['code'] != '00') {
            return $this->fetch($res);
        }
        $this->Db = new \x\Db();

        $where = [];
        $where[] = ['A.name', '=', $param['name']];
        $info = $this->Db->name('auth_admin')->alias('A')
                ->join('auth_role B', 'A.role = B.id', 'LEFT')
                ->field('A.*, B.menu_list, B.status as role_status')
                ->where($where)
                ->find();
        if (!$info) {
            $this->Db->return();
            return $this->returnJson('01', '账号 或 密码错误');
        }
        if ($info['status'] == 0) {
            $this->Db->return();
            return $this->returnJson('01', '该账号已被禁用！');
        }
        if ($info['role'] !=0 && $info['role_status'] == 0) {
            $this->Db->return();
            return $this->returnJson('01', '该账号已被禁用！');
        }
        if (md5($param['pwd'].$info['create_time']) != $info['pwd']) {
            $this->Db->return();
            return $this->returnJson('01', '账号 或 密码错误');
        }
        
        # 记住登录状态
        \x\Session::set('admin', $info);

        // 记录登录日志
        $this->_login_log($info['id']);
        
        $this->Db->return();
        return $this->returnJson('00', '登录成功');
    }

    // 登录日志写入
    private function _login_log($id) {
		$data  = [
            'ip'   => \x\Request::ip(),
            'admin' => $id,
            'province' => '无',
            'city' => '无',
            'area' => '无',
			'create_time' => time(),
		];
		$this->Db->name('auth_admin_login')->insert($data);
    }
}

