[QUESTION] 一个示例问题
zds-s opened this issue · comments
Before you submit this issue, you has been search all existed issues and search the documentation
- I've been search all existed issues
- I've been read all documentation
Describe your question
请问在 MineAdmin 本仓库中怎么获取当前用户id。它的实现原理是怎么实现的。如果可以,请用中文1回复
在MineAdmin中获取当前用户ID,可以通过SystemUserService
服务的getInfo()
方法实现。这个方法主要通过user()->getId()
来获取当前登录用户的ID。如果用户已登录,此方法会返回用户的ID;否则,会抛出异常提示无法获取用户信息。这里是关键的实现代码示例:
public function getInfo(): array
{
if ($uid = user()->getId()) {
return $this->getCacheInfo((int) $uid);
}
throw new MineException(t('system.unable_get_userinfo'), 500);
}
getInfo()
方法进一步调用getCacheInfo()
,传入用户ID以获取包含用户信息的数组。这种方式确保了能够有效且安全地获取当前用户的ID及其它信息。
Sources
- app/System/Controller/LoginController.php
- app/System/Service/SystemUserService.php
- app/System/Controller/Permission/UserController.php
About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
If you want to continue the conversation, start your reply with @dosu-bot.