<?php

namespace app\api\controller;

use app\common\arw\adjfut\src\Validate;
use app\BaseController;
use app\common\logic\Login as LogicLogin;
use app\Request;
use think\captcha\facade\Captcha;
use think\middleware\SessionInit;
use think\Response;

class Login extends BaseController
{
    /**
     * 初始化
     *
     * @date 2022-03-15
     * @example
     * @author admin
     * @since 1.0.0
     */
    protected function initialize(): void
    {
        $this->middleware[SessionInit::class] = [
            'only' => ['accountLogin', 'getCaptcha']
        ];
    }

    /**
     * 验证token
     *
     * @param Request $request
     * @return void
     */
    public function validateToken(Request $request)
    {
        $token = $request->getCurrentToken();
        return [
            'code' => 0,
            'data' => [
                'exp_time' => $token->token_exp_time,
            ],
            'msg' => 'ok'
        ];
    }

    /**
     * 西北政法大学单点登陆
     *
     * @param Request $request
     * @return Response
     * @date 2023-01-04
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function casOauthLogin(Request $request): Response
    {
        $url = $request->param('url');
        return LogicLogin::casOauthLogin(
            LogicLogin::casOauthLoginHandle($url)
        );
    }

    /**
     * 西北政法大学单点登出
     *
     * @param Request $request
     * @return Response
     * @date 2023-01-04
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function casOauthLogout(Request $request): Response
    {
        $url = $request->param('url');
        return LogicLogin::casOauthLogout($url);
    }

    /**
     * 用户账号登录
     *
     * @param Request $request
     * @date 2022-03-05
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function accountLogin(Request $request): array
    {
        $param = Validate::param([
            'account|账号' => 'require',
            'password|密码' => 'require',
            'captcha|验证码' => $request->isProd() ? 'require|captcha'  : false
        ]);
        $token = LogicLogin::accountLogin(
            $param['account'],
            $param['password']
        );

        return [
            'code' => 0,
            'data' => [
                'token' => $token->token_content,
                'exp_time' => $token->token_exp_time,
            ],
            'msg' => 'ok'
        ];
    }

    /**
     * 生成验证码
     *
     * @date 2022-03-05
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function getCaptcha()
    {
        return Captcha::create();
    }

    /**
     * 用户登出
     *
     * @param Request $request
     * @date 2022-03-09
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function userLogout(Request $request): array
    {
        $token = $request->getCurrentToken();
        $token->logout();
        return [
            'code' => 0,
            'msg' => '登出成功'
        ];
    }
}
