Open
Description
首先第一步, 注释规范化. . 建议使用apidocjs的方案. 比如下面是我的代码:
/**
* @api {post} /auth/register/email 邮箱注册
* @apiDescription 注册时, 请求体将不可避免地用明文传参.所以建议开启SSL.
* @apiVersion 2.0.0
* @apiPermission none
* @apiName registerByEmail
* @apiGroup Auth
*
* @apiParam {String{3..16}} username 用户名.
* @apiParam {String{..32}} email 用户邮箱.
* @apiParam {String{6}} captch 验证码.
* @apiParam {String} password 密码.
*
* @apiSuccessExample 成功响应:
{
"ret": 200,
"data": {},
"msg": ""
}
*/
public function registerByEmail()
{
$domain = new Domain();
/** ============== 格式检查 ============== */
/** ------- 用户名检查 ------- */
$this->username = trim($this->username);
if (!\App\Helper\Validator::checkUsernameFormat($this->username)) {
throw new BadRequestException('用户名格式错误, 请检查用户名格式是否正确. 只可以含有?.@_所有中英文和emoji.');
}
/** ------- 邮箱检查 ------- */
$this->email = trim($this->email);
if (!\App\Helper\Validator::checkEmailFormat($this->email)) {
throw new BadRequestException('邮箱格式错误, 必须形如 [email protected] 的格式');
}
/** ============== 正式检查 ============== */
// 检查可用性(重复)
if (!$domain->isEmailAvailable($this->email)) {
throw new BadRequestException('邮箱已被使用');
}
/** ------- captch validating ------- */
$this->captch = trim($this->captch);
$correctCaptch = $domain->getLastCaptch($this->email);
if ($correctCaptch == null) {
throw new BadRequestException('未发送验证码');
}
if ($this->captch != $correctCaptch) {
throw new BadRequestException('验证码错误或过期');
}
/** ------- password validating ------- */
// 密码不进行 trim()
/** ------- 完成注册 ------- */
$domain->registerUserByEmail($this->username, $this->email, $this->password, 1);
// 目前规定, 失败才返回消息
//\PhalApi\DI()->response->setMsg("注册成功");
return array();
}
- 在目前的设计中, getRules 和 API 实现距离太远, 无论是查找和修改都麻烦, 降低了开发效率. 建议直接从注释中读取规则.
- 建议能够从注释直接生成fastroute的配置. 比如上面这个就是post方式, 地址为/auth/register/email
- 建议直接从注释中判断执行权限, 比如这里 @apiPermission none, 就表示任何人都有权限.
apidoc 已经能够直接生成json, 读取json再生成相应所需php文件, 并不困难.
Metadata
Metadata
Assignees
Labels
No labels