<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace app\service\admin\auth;

use app\service\admin\BaseAdminService;
use app\service\domain\admin\CoreAdminAuthService;
use think\Request;


/**
 * Auth Service
 * @package app\service\admin\auth
 */
class AuthService extends BaseAdminService
{

    /**
     * Admin login
     * @param Request $request HTTP request with username and password
     * @return array
     */
    public function login(Request $request): array
    {
        return $this->core()->login(
            trim((string)$request->post('username', '')),
            (string)$request->post('password', '')
        );
    }


    /**
     * Get current admin user profile
     * @param Request $request HTTP request with uid
     * @return array
     */
    public function profile(Request $request): array
    {
        return $this->core()->profile((int)($request->uid ?? 0));
    }


    /**
     * Change admin user password
     * @param Request $request HTTP request with old_password and new_password
     * @return array
     */
    public function changePassword(Request $request): array
    {
        return $this->core()->changePassword(
            (int)($request->uid ?? 0),
            (string)$request->post('old_password', ''),
            (string)$request->post('new_password', '')
        );
    }

    /**
     * 获取管理员认证核心服务实例
     * @return CoreAdminAuthService
     */
    private function core(): CoreAdminAuthService
    {
        return $this->app->make(CoreAdminAuthService::class);
    }
}