<?php

declare(strict_types=1);

namespace App\Controller\Wechat;

use App\Controller\BaseController;
use App\Exception\BusinessException;
use app\Constants\Wechat\AuthErrorCode;
use App\Service\Wechat\AuthService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;

/**
 * 微信授权处理控制器
 */
#[Controller]
class AuthController extends BaseController
{
    #[Inject]
    protected AuthService $service;

    /**
     * 通过微信授权新建用户
     *
     * @return array
     */
    #[PostMapping(path: "auth/code2OpenID")]
    public function code2OpenID() : array
    {
        $code = $this->request->input('code', '');

        if (empty($code)) {
            throw new BusinessException(AuthErrorCode::CODE_EMPTY_ERROR);
        }

        return $this->service->codeToOpenID($code);
    }
}