<?php

/**
 * 用户基础信息
 * @author benzhan
 */
class User extends Singleton {

    private static $userInfo = null;

    public static function checkLogin() {
        $userInfo = self::getInfo();
        return $userInfo;
    }

    public static function getUserId() {
        $userInfo = self::getInfo();
        return $userInfo['user_id'] ?: 0;
    }

    public static function getUserName() {
        $userName = self::$userInfo['nick_name'] ?: self::$userInfo['user_name'];
        return $userName;
    }

    public static function getInfo() {
        if (self::$userInfo) {
            return self::$userInfo;
        }

        $userId = $_REQUEST['user_id'] ?: $_COOKIE['user_id'];
        $accessToken = $_REQUEST['access_token'] ?: $_COOKIE['access_token'];

        $info = ThirdApi::getMeeUserInfo($userId, $accessToken);
        if ($info) {
            self::$userInfo = $info;
            return self::$userInfo;
        } else {
            return [];
        }
    }


}
