<?php
namespace Admin\Controller;

use User\Api\UserApi;
use Com\Wechat;
use Com\WechatAuth;

/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class PublicController extends \Think\Controller
{

    /**
     * 后台用户登录
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function login($username = null, $password = null, $verify = null)
    {
        if (IS_POST) {
            /* 检测验证码 TODO: */
            if (!check_verify($verify)) {
                $this->error('验证码输入错误!');
            }

            /* 调用UC登录接口登录 */
            $User = new UserApi;
            $uid = $User->login($username, $password);
            if (0 < $uid) { //UC登录成功
                /* 登录用户 */
                $Member = D('Member');
                if ($Member->login($uid)) { //登录用户
                    //TODO:跳转到登录前页面
                    $this->success('登录成功!', U('Index/index'));
                } else {

                    $this->error($Member->getError());
                }

            } else { //登录失败
                switch ($uid) {
                    case -1:
                        $error = '用户不存在或被禁用!';
                        break; //系统级别禁用
                    case -2:
                        $error = '密码错误!';
                        break;
                    default:
                        $error = '未知错误!';
                        break; // 0-接口参数错误(调试阶段使用)
                }
                $this->error($error);
            }
        } else {
            if (is_login()) {
                $this->redirect('Index/index');
            } else {
                /* 读取数据库中的配置 */
                $config = S('DB_CONFIG_DATA');
                if (!$config) {
                    $config = D('Config')->lists();
                    S('DB_CONFIG_DATA', $config);
                }
                C($config); //添加配置

                $this->display();
            }
        }
    }

    /* 退出登录 */
    public function logout()
    {
        if (is_login()) {
            D('Member')->logout();
            session('[destroy]');
            session('user_auth_promote_ids');
            cookie('think_language', Null);
            // $this->success('退出成功!', U('login'));
            $this->ajaxReturn(array('status' => 1, 'msg' => '退出成功!'));
        } else {
            $this->redirect('login');
        }
    }

    public function checkVerify()
    {
        $verify = $_POST['verify'];
        if (!check_verify($verify)) {
            $this->ajaxReturn(array('status' => 0, 'msg' => '验证码输入错误!'));
        }
    }

    public function verify()
    {
        $config = array(
            'seKey' => 'ThinkPHP.CN',    //验证码加密密钥
            'fontSize' => 22,            // 验证码字体大小(px)
            'imageH' => 50,              // 验证码图片高度
            'imageW' => 180,             // 验证码图片宽度
            'length' => 4,               // 验证码位数
            'fontttf' => '4.ttf',        // 验证码字体,不设置随机获取
        );
        ob_clean();
        $verify = new \Think\Verify($config);
        $verify->codeSet = '0123456789';
        $verify->entry(1);
    }

    public function get_openid()
    {
        $appid = C('wechat.appid');
        $appsecret = C('wechat.appsecret');
        $result = auto_get_access_token(RUNTIME_PATH . '/access_token_validity.txt');
        if ($result['is_validity']) {
            session('token', $result['access_token']);
            $auth = new WechatAuth($appid, $appsecret, $result['access_token']);
        } else {
            $auth = new WechatAuth($appid, $appsecret);
            $token = $auth->getAccessToken();
            $token['expires_in_validity'] = time() + $token['expires_in'];
            wite_text(json_encode($token), RUNTIME_PATH . '/access_token_validity.txt');
            session('token', $token['access_token']);
        }
        $scene_id = sp_random_num(4) . '0';
        $ticket = $auth->qrcodeCreate($scene_id, 120);//10分钟
        if ($ticket['errcode']) {
            $return = array('status' => 0, 'data' => '获取ticket失败!');
        } else {
            $qrcode = $auth->showqrcode($ticket['ticket']);
            $return = array('status' => 1, 'data' => $qrcode, 'token' => $scene_id);
        }
        $this->ajaxReturn($return);
    }

    public function wite_token()
    {
        $appid = C('wechat.appid');
        $appsecret = C('wechat.appsecret');
        $auth = new WechatAuth($appid, $appsecret);
        $token = $auth->getAccessToken();
        $token['expires_in_validity'] = time() + $token['expires_in'];
        wite_text(json_encode($token), RUNTIME_PATH . '/access_token_validity.txt');
        session('token', $token['access_token']);
        $this->get_openid();
    }

    /** * 第三方微信扫码登陆 * */
    public function wechat_qrcode_login($state = 1)
    {
        if (empty(session("user_auth.user_id")) && !is_weixin()) {
            $appid = C('weixin_login.appid');
            $appsecret = C('weixin_login.appsecret');
            $auth = new WechatAuth($appid, $appsecret);
            $result = auto_get_access_token(RUNTIME_PATH . '/qr_access_token_validity.txt');
            if ($result['is_validity']) {
                session('token', $result['access_token']);
            } else {
                $token = $auth->getAccessToken();
                $token['expires_in_validity'] = time() + $token['expires_in'];
                wite_text(json_encode($token), RUNTIME_PATH . '/qr_access_token_validity.txt');
                session('token', $token['access_token']);
            }
            $redirect_uri = "http://" . $_SERVER['HTTP_HOST'] . "/admin.php/Public/wechat_login_callback";
            redirect($auth->getQrconnectURL($redirect_uri, $state));
        }
    }

    public function wechat_login_callback()
    {
        if ($host && $_GET['state'] != $_SERVER['HTTP_HOST']) {
            $url = 'http://' . $_GET['state'] . '/admin.php/Public/wechat_login_callback?' . http_build_query($_GET);
            Header("Location: $url");
            exit;
        }
        if (is_weixin()) {
            $appid = C('wechat.appid');
            $appsecret = C('wechat.appsecret');
        } else {
            $appid = C('weixin_login.appid');
            $appsecret = C('weixin_login.appsecret');
        }
        $auth = new WechatAuth($appid, $appsecret);
        $token = $auth->getAccessToken("code", $_GET['code']);
        if (isset($_GET['auto_get_openid'])) {
            if (base64_decode($_GET['auto_get_openid']) != 'auto_get_openid') {
                die('非法操作!');
            } else {
                session('admin_wechat_token', array('openid' => $token['openid']));
                session('admin_openid', $token['openid']);
            }
        }
        $Member = D('UcenterMember');
        $admin = $Member->where(array('admin_openid' => $token['openid']))->find();
        if ($admin == '') {
            $this->error("微信未绑定管理员账号!");
        } else {
            $User = new UserApi;
            $uid = $User->login($admin['username'], $admin['password']);
            if (0 < $uid) { //UC登录成功
                /* 登录用户 */
                $Member = D('Member');
                if ($Member->login($uid)) { //登录用户
                    //TODO:跳转到登录前页面
                    $this->success('登录成功!', U('Index/index'));
                } else {
                    $this->error($Member->getError());
                }

            } else { //登录失败
                switch ($uid) {
                    case -1:
                        $error = '用户不存在或被禁用!';
                        break; //系统级别禁用
                    case -2:
                        $error = '密码错误!';
                        break;
                    default:
                        $error = '未知错误!';
                        break; // 0-接口参数错误(调试阶段使用)
                }
                $this->error($error);
            }
        }
    }


    public function zh_cn()
    {
        cookie('think_language', 'zh-cn');
        $this->ajaxReturn(['status' => 1]);
    }


    public function en_us()
    {
        cookie('think_language', 'en-us');
        $this->ajaxReturn(['status' => 1]);
    }
}