diff --git a/app/Controller/Api/V1/AuthController.php b/app/Controller/Api/V1/AuthController.php index 9e3abea..e727fa2 100644 --- a/app/Controller/Api/V1/AuthController.php +++ b/app/Controller/Api/V1/AuthController.php @@ -8,6 +8,7 @@ use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\Middleware; use App\Service\UserService; +use App\Service\SmsCodeService; use Phper666\JWTAuth\JWT; use Phper666\JWTAuth\Middleware\JWTAuthMiddleware; @@ -45,8 +46,8 @@ class AuthController extends CController 'mobile' => "required|regex:/^1[345789][0-9]{9}$/", 'password' => 'required', 'platform' => 'required|in:h5,ios,windows,mac', - ],[ - 'mobile.regex'=>'mobile 格式不正确' + ], [ + 'mobile.regex' => 'mobile 格式不正确' ]); $userInfo = $this->userService->login( @@ -84,7 +85,7 @@ class AuthController extends CController /** * 退出登录接口 * - * @RequestMapping(path="logout", methods="get,post") + * @RequestMapping(path="logout", methods="post") * @Middleware(JWTAuthMiddleware::class) */ public function logout() @@ -96,10 +97,38 @@ class AuthController extends CController /** * 账号注册接口 + * + * @RequestMapping(path="register", methods="post") + * + * @param SmsCodeService $smsCodeService + * @return \Psr\Http\Message\ResponseInterface */ - public function register() + public function register(SmsCodeService $smsCodeService) { + $params = $this->request->all(); + $this->validate($params, [ + 'nickname' => "required", + 'mobile' => "required|regex:/^1[345789][0-9]{9}$/", + 'password' => 'required', + 'sms_code' => 'required|integer|max:999999', + 'platform' => 'required|in:h5,ios,windows,mac', + ]); + if (!$smsCodeService->check('user_register', $params['mobile'], $params['sms_code'])) { + return $this->response->fail('验证码填写错误...'); + } + + $isTrue = $this->userService->register([ + 'mobile' => $params['mobile'], + 'password' => $params['password'], + 'nickname' => strip_tags($params['nickname']), + ]); + + if ($isTrue) { + $smsCodeService->delCode('user_register', $params['mobile']); + } + + return $this->response->success([], 'Successfully logged out'); } /** diff --git a/app/Model/Article.php b/app/Model/Article.php new file mode 100644 index 0000000..53e6669 --- /dev/null +++ b/app/Model/Article.php @@ -0,0 +1,44 @@ + 'integer', 'user_id' => 'integer', 'class_id' => 'integer', 'is_asterisk' => 'integer', 'status' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime']; +} diff --git a/app/Model/ArticleAnnex.php b/app/Model/ArticleAnnex.php new file mode 100644 index 0000000..2768cb6 --- /dev/null +++ b/app/Model/ArticleAnnex.php @@ -0,0 +1,41 @@ + 'integer', 'user_id' => 'integer', 'article_id' => 'integer', 'file_size' => 'integer', 'status' => 'integer', 'created_at' => 'datetime']; +} diff --git a/app/Model/ArticleClass.php b/app/Model/ArticleClass.php new file mode 100644 index 0000000..96565fe --- /dev/null +++ b/app/Model/ArticleClass.php @@ -0,0 +1,37 @@ + 'integer', 'user_id' => 'integer', 'sort' => 'integer', 'is_default' => 'integer', 'created_at' => 'datetime']; +} diff --git a/app/Model/ArticleDetail.php b/app/Model/ArticleDetail.php new file mode 100644 index 0000000..a6899e4 --- /dev/null +++ b/app/Model/ArticleDetail.php @@ -0,0 +1,35 @@ + 'integer', 'article_id' => 'integer']; +} diff --git a/app/Model/ArticleTag.php b/app/Model/ArticleTag.php new file mode 100644 index 0000000..daf04cc --- /dev/null +++ b/app/Model/ArticleTag.php @@ -0,0 +1,37 @@ + 'integer', 'user_id' => 'integer', 'sort' => 'integer', 'created_at' => 'datetime']; +} diff --git a/app/Model/ChatRecord.php b/app/Model/ChatRecord.php new file mode 100644 index 0000000..d9f82be --- /dev/null +++ b/app/Model/ChatRecord.php @@ -0,0 +1,39 @@ + 'integer', 'source' => 'integer', 'msg_type' => 'integer', 'user_id' => 'integer', 'receive_id' => 'integer', 'is_revoke' => 'integer', 'created_at' => 'datetime']; +} diff --git a/app/Model/ChatRecordsCode.php b/app/Model/ChatRecordsCode.php new file mode 100644 index 0000000..e16f62d --- /dev/null +++ b/app/Model/ChatRecordsCode.php @@ -0,0 +1,37 @@ + 'integer', 'record_id' => 'integer', 'user_id' => 'integer', 'created_at' => 'datetime']; +} diff --git a/app/Model/ChatRecordsDelete.php b/app/Model/ChatRecordsDelete.php new file mode 100644 index 0000000..630a0a5 --- /dev/null +++ b/app/Model/ChatRecordsDelete.php @@ -0,0 +1,35 @@ + 'integer', 'record_id' => 'integer', 'user_id' => 'integer', 'created_at' => 'datetime']; +} diff --git a/app/Model/ChatRecordsFile.php b/app/Model/ChatRecordsFile.php new file mode 100644 index 0000000..b20b3e3 --- /dev/null +++ b/app/Model/ChatRecordsFile.php @@ -0,0 +1,43 @@ + 'integer', 'record_id' => 'integer', 'user_id' => 'integer', 'file_source' => 'integer', 'file_type' => 'integer', 'save_type' => 'integer', 'file_size' => 'integer', 'is_delete' => 'integer', 'created_at' => 'datetime']; +} diff --git a/app/Model/ChatRecordsForward.php b/app/Model/ChatRecordsForward.php new file mode 100644 index 0000000..85262cb --- /dev/null +++ b/app/Model/ChatRecordsForward.php @@ -0,0 +1,37 @@ + 'integer', 'record_id' => 'integer', 'user_id' => 'integer', 'created_at' => 'datetime']; +} diff --git a/app/Model/ChatRecordsInvite.php b/app/Model/ChatRecordsInvite.php new file mode 100644 index 0000000..a554f1c --- /dev/null +++ b/app/Model/ChatRecordsInvite.php @@ -0,0 +1,36 @@ + 'integer', 'record_id' => 'integer', 'type' => 'integer', 'operate_user_id' => 'integer']; +} diff --git a/app/Model/Emoticon.php b/app/Model/Emoticon.php new file mode 100644 index 0000000..f5f91d7 --- /dev/null +++ b/app/Model/Emoticon.php @@ -0,0 +1,35 @@ + 'integer', 'created_at' => 'datetime']; +} diff --git a/app/Model/EmoticonDetail.php b/app/Model/EmoticonDetail.php new file mode 100644 index 0000000..7b7b64c --- /dev/null +++ b/app/Model/EmoticonDetail.php @@ -0,0 +1,37 @@ + 'integer', 'emoticon_id' => 'integer', 'user_id' => 'integer', 'file_size' => 'integer', 'created_at' => 'datetime']; +} diff --git a/app/Model/FileSplitUpload.php b/app/Model/FileSplitUpload.php new file mode 100644 index 0000000..74d670c --- /dev/null +++ b/app/Model/FileSplitUpload.php @@ -0,0 +1,41 @@ + 'integer', 'file_type' => 'integer', 'user_id' => 'integer', 'split_index' => 'integer', 'split_num' => 'integer', 'file_size' => 'integer', 'is_delete' => 'integer', 'upload_at' => 'integer']; +} diff --git a/app/Model/Model.php b/app/Model/Model.php index 06d375e..ab0c466 100644 --- a/app/Model/Model.php +++ b/app/Model/Model.php @@ -10,6 +10,7 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace App\Model; use Hyperf\DbConnection\Model\Model as BaseModel; diff --git a/app/Model/UsersChatList.php b/app/Model/UsersChatList.php new file mode 100644 index 0000000..4f4eb7f --- /dev/null +++ b/app/Model/UsersChatList.php @@ -0,0 +1,40 @@ + 'integer', 'type' => 'integer', 'uid' => 'integer', 'friend_id' => 'integer', 'group_id' => 'integer', 'status' => 'integer', 'is_top' => 'integer', 'not_disturb' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime']; +} diff --git a/app/Model/UsersEmoticon.php b/app/Model/UsersEmoticon.php new file mode 100644 index 0000000..aabf4a5 --- /dev/null +++ b/app/Model/UsersEmoticon.php @@ -0,0 +1,33 @@ + 'integer', 'user_id' => 'integer']; +} diff --git a/app/Model/UsersFriend.php b/app/Model/UsersFriend.php new file mode 100644 index 0000000..e6036d3 --- /dev/null +++ b/app/Model/UsersFriend.php @@ -0,0 +1,39 @@ + 'integer', 'user1' => 'integer', 'user2' => 'integer', 'active' => 'integer', 'status' => 'integer', 'created_at' => 'datetime']; +} diff --git a/app/Model/UsersFriendsApply.php b/app/Model/UsersFriendsApply.php new file mode 100644 index 0000000..f15c603 --- /dev/null +++ b/app/Model/UsersFriendsApply.php @@ -0,0 +1,37 @@ + 'integer', 'user_id' => 'integer', 'friend_id' => 'integer', 'status' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime']; +} diff --git a/app/Model/UsersGroup.php b/app/Model/UsersGroup.php new file mode 100644 index 0000000..d9079d5 --- /dev/null +++ b/app/Model/UsersGroup.php @@ -0,0 +1,38 @@ + 'integer', 'user_id' => 'integer', 'status' => 'integer', 'created_at' => 'datetime']; +} diff --git a/app/Model/UsersGroupMember.php b/app/Model/UsersGroupMember.php new file mode 100644 index 0000000..2d8848e --- /dev/null +++ b/app/Model/UsersGroupMember.php @@ -0,0 +1,37 @@ + 'integer', 'group_id' => 'integer', 'user_id' => 'integer', 'group_owner' => 'integer', 'status' => 'integer', 'created_at' => 'datetime']; +} diff --git a/app/Model/UsersGroupNotice.php b/app/Model/UsersGroupNotice.php new file mode 100644 index 0000000..a2b6ea3 --- /dev/null +++ b/app/Model/UsersGroupNotice.php @@ -0,0 +1,40 @@ + 'integer', 'group_id' => 'integer', 'user_id' => 'integer', 'is_delete' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime']; +} diff --git a/app/Service/SmsCodeService.php b/app/Service/SmsCodeService.php new file mode 100644 index 0000000..b1c63cf --- /dev/null +++ b/app/Service/SmsCodeService.php @@ -0,0 +1,167 @@ +redis()->get($this->getKey($type, $mobile)); + + return $sms_code == $code; + } + + /** + * 发送验证码 + * + * @param string $usage 验证码用途 + * @param string $mobile 手机号 + * @return array|bool + */ + public function send(string $usage, string $mobile) + { + if (!$this->isUsages($usage)) { + return [false, [ + 'msg' => "[{$usage}]:此类短信验证码不支持发送", + 'data' => [] + ]]; + } + + $key = $this->getKey($usage, $mobile); + + // 为防止刷短信行为,此处可进行过滤处理 + [$isTrue, $data] = $this->filter($usage, $mobile); + if (!$isTrue) { + return [false, $data]; + } + + if (!$sms_code = $this->getCode($key)) { + $sms_code = mt_rand(100000, 999999); + } + + // 设置短信验证码 + $this->setCode($key, $sms_code); + + // ... 调取短信接口,建议异步任务执行 (暂无短信接口,省略处理) + + return [true, [ + 'msg' => 'success', + 'data' => ['type' => $usage, 'code' => $sms_code] + ]]; + } + + /** + * 获取缓存的验证码 + * + * @param string $key + * @return mixed + */ + public function getCode(string $key) + { + return $this->redis()->get($key); + } + + /** + * 设置验证码缓存 + * + * @param string $key 缓存key + * @param string $sms_code 验证码 + * @param float|int $exp 过期时间(默认15分钟) + * @return mixed + */ + public function setCode(string $key, string $sms_code, $exp = 60 * 15) + { + return $this->redis()->setex($key, $exp, $sms_code); + } + + /** + * 删除验证码缓存 + * + * @param string $usage 验证码用途 + * @param string $mobile 手机号 + * @return mixed + */ + public function delCode(string $usage, string $mobile) + { + return $this->redis()->del($this->getKey($usage, $mobile)); + } + + /** + * 短信发送过滤验证 + * + * @param string $usage 验证码用途 + * @param string $mobile 手机号 + * @return array + */ + public function filter(string $usage, string $mobile) + { + // ... 省略处理 + if (false) { + return [false, [ + 'msg' => '过滤原因...', + 'data' => [] + ]]; + } + + return [true, [ + 'msg' => 'ok', + 'data' => [] + ]]; + } + + /** + * 判断验证码用途渠道是否注册 + * + * @param string $usage + * @return bool + */ + public function isUsages(string $usage) + { + return in_array($usage, self::SMS_USAGE); + } +} diff --git a/app/Service/UserService.php b/app/Service/UserService.php index d406e22..9535597 100644 --- a/app/Service/UserService.php +++ b/app/Service/UserService.php @@ -3,6 +3,7 @@ namespace App\Service; use App\Model\User; +use App\Model\ArticleClass; class UserService extends BaseService { @@ -26,4 +27,33 @@ class UserService extends BaseService return $user->toArray(); } + + /** + * 账号注册逻辑 + * + * @param array $data 用户数据 + * @return bool + */ + public function register(array $data) + { + try { + $data['password'] = Hash::make($data['password']); + $data['created_at'] = date('Y-m-d H:i:s'); + $result = User::create($data); + + // 创建用户的默认笔记分类 + ArticleClass::create([ + 'user_id' => $result->id, + 'class_name' => '我的笔记', + 'is_default' => 1, + 'sort' => 1, + 'created_at' => time() + ]); + } catch (Exception $e) { + $result = false; + DB::rollBack(); + } + + return $result ? true : false; + } }