Skip to content

Commit

Permalink
增加用户状态
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Jul 14, 2023
1 parent 7ff5de5 commit 1ba66b0
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ imi-ai 是一个 ChatGPT 开源项目,你可以用它方便地部署和使用
* [x] 界面主题
* [x] 用户邮箱注册和登录
* [ ] 用户手机号注册和登录
* [ ] 计费系统
* [x] Tokens 计费系统
* [ ] Tokens 充值

更多功能计划中……

Expand Down
10 changes: 10 additions & 0 deletions server/Enum/ApiStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ abstract class ApiStatus extends BaseEnum
*/
public const VCODE_ERROR = 10003;

/**
* @EnumItem("用户已封禁")
*/
public const MEMBER_BANDED = 10004;

/**
* @EnumItem("用户状态异常")
*/
public const MEMBER_STATUS_ANOMALY = 10005;

/**
* @EnumItem("积分不足")
*/
Expand Down
26 changes: 26 additions & 0 deletions server/Exception/MemberBandedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace app\Exception;

use app\Enum\ApiStatus;

/**
* 用户已封禁.
*/
class MemberBandedException extends BaseException
{
public function __construct(string $message = '用户已封禁', int $code = 0, \Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}

/**
* 获取状态码
*/
public function getStatusCode(): int
{
return ApiStatus::MEMBER_BANDED;
}
}
26 changes: 26 additions & 0 deletions server/Exception/MemberStatusAnomalyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace app\Exception;

use app\Enum\ApiStatus;

/**
* 用户状态异常.
*/
class MemberStatusAnomalyException extends BaseException
{
public function __construct(string $message = '用户状态异常', int $code = 0, \Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}

/**
* 获取状态码
*/
public function getStatusCode(): int
{
return ApiStatus::MEMBER_STATUS_ANOMALY;
}
}
44 changes: 35 additions & 9 deletions server/Module/Embedding/ApiController/OpenAIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use app\Module\Embedding\Service\EmbeddingService;
use app\Module\Embedding\Service\OpenAIService;
use app\Module\Member\Annotation\LoginRequired;
use app\Module\Member\Util\MemberUtil;
use app\Util\IPUtil;
use Imi\Aop\Annotation\Inject;
Expand All @@ -29,7 +30,8 @@ class OpenAIController extends HttpController

#[
Action(),
Route(method: RequestMethod::POST)
Route(method: RequestMethod::POST),
LoginRequired()
]
public function upload(UploadedFile $file): array
{
Expand All @@ -40,7 +42,10 @@ public function upload(UploadedFile $file): array
];
}

#[Action]
#[
Action,
LoginRequired()
]
public function getProject(string $id): array
{
$memberSession = MemberUtil::getMemberSession();
Expand All @@ -50,7 +55,10 @@ public function getProject(string $id): array
];
}

#[Action]
#[
Action,
LoginRequired()
]
public function projectList(int $page = 1, int $limit = 15): array
{
$memberSession = MemberUtil::getMemberSession();
Expand All @@ -63,7 +71,8 @@ public function projectList(int $page = 1, int $limit = 15): array
*/
#[
Action(),
Route(method: RequestMethod::POST)
Route(method: RequestMethod::POST),
LoginRequired()
]
public function updateProject(string $id, string $name)
{
Expand All @@ -74,14 +83,21 @@ public function updateProject(string $id, string $name)
/**
* @return mixed
*/
#[Action]
#[
Action,
Route(method: RequestMethod::POST),
LoginRequired()
]
public function deleteProject(string $id)
{
$memberSession = MemberUtil::getMemberSession();
$this->embeddingService->deleteProject($id, $memberSession->getIntMemberId());
}

#[Action]
#[
Action,
LoginRequired()
]
public function fileList(string $projectId): array
{
$memberSession = MemberUtil::getMemberSession();
Expand All @@ -91,7 +107,10 @@ public function fileList(string $projectId): array
];
}

#[Action]
#[
Action,
LoginRequired()
]
public function assocFileList(string $projectId): array
{
$memberSession = MemberUtil::getMemberSession();
Expand All @@ -101,7 +120,10 @@ public function assocFileList(string $projectId): array
];
}

#[Action]
#[
Action,
LoginRequired()
]
public function sectionList(string $projectId, string $fileId): array
{
$memberSession = MemberUtil::getMemberSession();
Expand All @@ -114,6 +136,7 @@ public function sectionList(string $projectId, string $fileId): array
#[
Action,
Route(method: RequestMethod::POST),
LoginRequired()
]
public function sendMessage(string $question, string $projectId, array|object $config = []): array
{
Expand Down Expand Up @@ -149,7 +172,10 @@ protected function task(): void
});
}

#[Action]
#[
Action,
LoginRequired()
]
public function chatList(string $id, int $page = 1, int $limit = 15): array
{
$memberSession = MemberUtil::getMemberSession();
Expand Down
3 changes: 2 additions & 1 deletion server/Module/Member/ApiController/EmailAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public function register(string $email, string $vcodeToken, string $vcode): arra
}

#[
Action
Action,
Route(method: RequestMethod::POST)
]
public function verifyFromEmail(string $email, string $token, string $verifyToken): array
{
Expand Down
17 changes: 17 additions & 0 deletions server/Module/Member/Enum/MemberStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace app\Module\Member\Enum;

use Imi\Enum\Annotation\EnumItem;
use Imi\Enum\BaseEnum;

class MemberStatus extends BaseEnum
{
#[EnumItem(text: '正常')]
public const NORMAL = 1;

#[EnumItem(text: '封禁')]
public const BANDED = 2;
}
33 changes: 32 additions & 1 deletion server/Module/Member/Model/Base/MemberBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
*
* @Table(name=@ConfigValue(name="@app.models.app\Module\Member\Model\Member.name", default="tb_member"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.app\Module\Member\Model\Member.poolName"))
*
* @DDL(sql="CREATE TABLE `tb_member` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '邮箱地址', `email_hash` int(10) unsigned NOT NULL COMMENT '邮箱哈希(crc32)', `phone` bigint(20) unsigned NOT NULL COMMENT '手机号码', `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '密码', `nickname` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '昵称', `register_time` int(10) unsigned NOT NULL COMMENT '注册时间', `last_login_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最后登录时间', PRIMARY KEY (`id`), KEY `phone` (`phone`), KEY `email_hash` (`email_hash`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户'")
* @DDL(sql="CREATE TABLE `tb_member` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `status` tinyint(3) unsigned NOT NULL COMMENT '状态', `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '邮箱地址', `email_hash` int(10) unsigned NOT NULL COMMENT '邮箱哈希(crc32)', `phone` bigint(20) unsigned NOT NULL COMMENT '手机号码', `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '密码', `nickname` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '昵称', `register_time` int(10) unsigned NOT NULL COMMENT '注册时间', `last_login_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最后登录时间', PRIMARY KEY (`id`), KEY `phone` (`phone`), KEY `email_hash` (`email_hash`), KEY `status` (`status`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户'")
*
* @property int|null $id
* @property int|null $status 状态
* @property string|null $email 邮箱地址
* @property int|null $emailHash 邮箱哈希(crc32)
* @property int|null $phone 手机号码
Expand Down Expand Up @@ -70,6 +71,36 @@ public function setId($id)
return $this;
}

/**
* 状态.
* status.
*
* @Column(name="status", type="tinyint", length=3, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false, unsigned=true, virtual=false)
*/
protected ?int $status = null;

/**
* 获取 status - 状态.
*/
public function getStatus(): ?int
{
return $this->status;
}

/**
* 赋值 status - 状态.
*
* @param int|null $status status
*
* @return static
*/
public function setStatus($status)
{
$this->status = null === $status ? null : (int) $status;

return $this;
}

/**
* 邮箱地址.
* email.
Expand Down
12 changes: 12 additions & 0 deletions server/Module/Member/Model/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace app\Module\Member\Model;

use app\Module\Common\Model\Traits\TRecordId;
use app\Module\Member\Enum\MemberStatus;
use app\Module\Member\Model\Base\MemberBase;
use Imi\Bean\Annotation\Inherit;
use Imi\Model\Annotation\Column;
Expand All @@ -27,4 +28,15 @@ class Member extends MemberBase
* @Column(name="register_time", type="int", length=10, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false, unsigned=true, virtual=false, createTime=true)
*/
protected ?int $registerTime = null;

/**
* 状态
*/
#[Column(virtual: true)]
protected ?string $statusText = null;

public function getStatusText(): ?string
{
return MemberStatus::getText($this->status);
}
}
4 changes: 3 additions & 1 deletion server/Module/Member/Service/MemberService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace app\Module\Member\Service;

use app\Exception\NotFoundException;
use app\Module\Member\Enum\MemberStatus;
use app\Module\Member\Model\Member;
use Imi\Aop\Annotation\Inject;
use Imi\Db\Annotation\Transaction;
Expand All @@ -15,9 +16,10 @@ class MemberService
protected EmailAuthService $emailAuthService;

#[Transaction()]
public function create(string $email = '', int $phone = 0, string $password = '', string $nickname = ''): Member
public function create(string $email = '', int $phone = 0, string $password = '', string $nickname = '', int $status = MemberStatus::NORMAL): Member
{
$record = Member::newInstance();
$record->status = $status;
$record->email = $email;
$record->emailHash = $this->emailAuthService->hash($email);
$record->phone = $phone;
Expand Down
12 changes: 12 additions & 0 deletions server/Module/Member/Service/MemberSessionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace app\Module\Member\Service;

use app\Exception\MemberBandedException;
use app\Exception\MemberNoLoginException;
use app\Exception\MemberStatusAnomalyException;
use app\Module\Member\Enum\MemberStatus;
use app\Module\Member\Model\Member;
use Imi\Aop\Annotation\Inject;
use Imi\JWT\Exception\InvalidAuthorizationException;
Expand Down Expand Up @@ -99,6 +102,15 @@ public function checkLogin(): void
{
throw new MemberNoLoginException();
}
switch($this->getMemberInfo()->status)
{
case MemberStatus::NORMAL:
break;
case MemberStatus::BANDED:
throw new MemberBandedException();
default:
throw new MemberStatusAnomalyException();
}
}

/**
Expand Down

0 comments on commit 1ba66b0

Please sign in to comment.