<?php

namespace App\Http\Controllers\API\User;

use App\Exceptions\UserException;
use App\Http\Controllers\Controller;
use App\Models\UseUser;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;

class AuthController extends Controller
{
    public function login(Request $request)
    {
        $data = $request->input();
        if (empty($data['address'])) {
            return $this->error('缺少数据', 400);
        }
        # 登录
        $result = self::LoginEvent($data);
        return $this->success('success', $result);
    }

    public function logout()
    {
        return '';
    }

    public function LoginEvent(array $data = [])
    {
        $credentials = Arr::only($data, ['address']);
        $address = $credentials['address'] ?? '';
        $UserInfo = UseUser::where('user_address', $address)->first();
        if (blank($UserInfo)) {
            throw new UserException(1, '账号不存在');
        }
        $token = auth('api')->login($UserInfo);
        return [
            'access_token' => $token,
            'token_type' => 'bearer',
            'expires_in' => auth('api')->factory()->getTTL() * 60
        ];
    }

    public function getlist()
    {
        return 123;
    }
}
