初始化

main
gzydong 2020-11-29 17:39:24 +08:00
parent 8ce33cada3
commit 84b9900f7d
15 changed files with 139 additions and 69 deletions

View File

@ -18,6 +18,7 @@
- JSON
- PDO
- Redis >= 5.0.0
- AMQP
## 4、项目安装
[接口文档](https://docs.apipost.cn/view/9c75130d7006e6e5#3184466)

View File

@ -103,7 +103,7 @@ class ArticleController extends CController
public function getArticleDetail()
{
$this->validate($this->request->all(), [
'article_id' => 'required|integer',
'article_id' => 'required|integer'
]);
return $this->response->success(
@ -124,7 +124,7 @@ class ArticleController extends CController
$params = $this->request->all();
$this->validate($params, [
'class_id' => 'required|integer',
'class_name' => 'required',
'class_name' => 'required'
]);
$class_id = $this->articleService->editArticleClass($this->uid(), $params['class_id'], $params['class_name']);
@ -260,7 +260,7 @@ class ArticleController extends CController
'class_id' => 'required|integer|min:0',
'title' => 'required|max:255',
'content' => 'required',
'md_content' => 'required',
'md_content' => 'required'
]);
$id = $this->articleService->editArticle($this->uid(), (int)$params['article_id'], [
@ -309,6 +309,7 @@ class ArticleController extends CController
]);
$isTrue = $this->articleService->updateArticleStatus($this->uid(), (int)$params['article_id'], 1);
return $isTrue
? $this->response->success([], '笔记恢复成功...')
: $this->response->fail('笔记恢复失败...');
@ -406,6 +407,7 @@ class ArticleController extends CController
]);
$isTrue = $this->articleService->updateArticleTag($this->uid(), (int)$params['article_id'], $params['tags']);
return $isTrue
? $this->response->success([], 'success...')
: $this->response->fail('编辑失败...');

View File

@ -7,7 +7,6 @@ use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\JWTAuthMiddleware;
use Phper666\JWTAuth\JWT;
use App\Constants\ResponseCode;
use App\Model\User;
use App\Service\UserService;
@ -26,12 +25,6 @@ class AuthController extends CController
*/
private $userService;
/**
* @Inject
* @var JWT
*/
private $jwt;
/**
* @Inject
* @var SmsCodeService
@ -43,7 +36,6 @@ class AuthController extends CController
*
* @RequestMapping(path="login", methods="post")
*
* @param JWT $jwt
* @return \Psr\Http\Message\ResponseInterface
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
@ -61,7 +53,7 @@ class AuthController extends CController
);
if (!$userInfo) {
return $this->response->fail('账号不存在或密码填写错误...', ResponseCode::FAIL);
return $this->response->fail('账号不存在或密码填写错误...');
}
try {
@ -85,7 +77,7 @@ class AuthController extends CController
'motto' => $userInfo['motto'],
'email' => $userInfo['email'],
]
], '登录成功...');
]);
}
/**
@ -154,12 +146,12 @@ class AuthController extends CController
]);
if (!$this->smsCodeService->check('forget_password', $params['mobile'], $params['sms_code'])) {
return $this->response->fail('验证码填写错误...', ResponseCode::FAIL);
return $this->response->fail('验证码填写错误');
}
$isTrue = $this->userService->resetPassword($params['mobile'], $params['password']);
if (!$isTrue) {
return $this->response->fail('重置密码失败...', ResponseCode::FAIL);
return $this->response->fail('重置密码失败');
}
// 删除验证码缓存
@ -217,6 +209,7 @@ class AuthController extends CController
[$isTrue, $result] = $this->smsCodeService->send($params['type'], $params['mobile']);
if (!$isTrue) {
// ... 处理发送失败逻辑,当前默认发送成功
return $this->response->fail('验证码发送失败');
}
// 测试环境下直接返回验证码

View File

@ -21,14 +21,21 @@ class CController extends AbstractController
*/
protected $response;
/**
* @Inject
* @var JWT
*/
protected $jwt;
/**
* 获取当前登录用户ID
*
* @return int
*/
public function uid(){
public function uid()
{
$token = request()->getQueryParams()['token'] ?? null;
$data = container()->get(JWT::class)->getParserData($token);
$data = $this->jwt->getParserData($token);
return $data['user_id'];
}
}

View File

@ -37,7 +37,7 @@ class DownloadController extends CController
{
$params = $this->request->inputs(['cr_id']);
$this->validate($params, [
'cr_id' => 'required|integer',
'cr_id' => 'required|integer'
]);
$recordsInfo = ChatRecord::select(['msg_type', 'source', 'user_id', 'receive_id'])->where('id', $params['cr_id'])->first();
@ -81,7 +81,7 @@ class DownloadController extends CController
{
$params = $this->request->inputs(['annex_id']);
$this->validate($params, [
'annex_id' => 'required|integer',
'annex_id' => 'required|integer'
]);
$info = ArticleAnnex::select(['save_dir', 'original_name'])

View File

@ -11,6 +11,7 @@ use App\Constants\ResponseCode;
use App\Model\Emoticon;
use App\Model\EmoticonDetail;
use App\Service\EmoticonService;
use App\Service\UploadService;
/**
* Class EmoticonController
@ -26,7 +27,7 @@ class EmoticonController extends CController
* @Inject
* @var EmoticonService
*/
public $emoticonService;
private $emoticonService;
/**
* 获取用户表情包列表
@ -91,7 +92,7 @@ class EmoticonController extends CController
$params = $this->request->all();
$this->validate($params, [
'emoticon_id' => 'required|integer',
'type' => 'required|in:1,2',
'type' => 'required|in:1,2'
]);
$user_id = $this->uid();
@ -131,8 +132,11 @@ class EmoticonController extends CController
* 自定义上传表情包
*
* @RequestMapping(path="upload-emoticon", methods="post")
*
* @param UploadService $uploadService
* @return \Psr\Http\Message\ResponseInterface
*/
public function uploadEmoticon()
public function uploadEmoticon(UploadService $uploadService)
{
$file = $this->request->file('emoticon');
if (!$file->isValid()) {
@ -150,14 +154,16 @@ class EmoticonController extends CController
);
}
$save_path = '';
$user_id = $this->uid();
// 读取图片信息
$imgInfo = getimagesize($file->getPath());
$filename = create_image_name($ext, $imgInfo[0], $imgInfo[1]);
$save_path = $uploadService->media($file, 'media/images/emoticon', create_image_name($ext, $imgInfo[0], $imgInfo[1]));
if (!$save_path) {
return $this->response->fail('图片上传失败');
}
$result = EmoticonDetail::create([
'user_id' => $user_id,
'user_id' => $this->uid(),
'url' => $save_path,
'file_suffix' => $ext,
'file_size' => $file->getSize(),
@ -181,7 +187,7 @@ class EmoticonController extends CController
*/
public function collectEmoticon()
{
$params = $this->request->all();
$params = $this->request->inputs(['record_id']);
$this->validate($params, [
'record_id' => 'required|integer'
]);
@ -204,15 +210,13 @@ class EmoticonController extends CController
*/
public function delCollectEmoticon()
{
$params = $this->request->all();
$params = $this->request->inputs(['ids']);
$this->validate($params, [
'ids' => 'required'
'ids' => 'required|ids'
]);
$ids = explode(',', trim($params['ids']));
return $this->emoticonService->deleteCollect($this->uid(), $ids) ?
$this->response->success([], 'success') :
$this->response->fail('fail');
return $this->emoticonService->deleteCollect($this->uid(), parse_ids($params['ids'])) ?
$this->response->success([]) :
$this->response->fail();
}
}

View File

@ -31,7 +31,7 @@ class GroupController extends CController
* @Inject
* @var GroupService
*/
public $groupService;
private $groupService;
/**
* @Inject
@ -57,11 +57,10 @@ class GroupController extends CController
$params = $this->request->inputs(['group_name', 'uids']);
$this->validate($params, [
'group_name' => 'required',
'uids' => 'required',
'uids' => 'required|ids'
]);
$friend_ids = array_filter(explode(',', $params['uids']));
$friend_ids = array_unique($friend_ids);
$friend_ids = parse_ids($params['uids']);
$user_id = $this->uid();
[$isTrue, $data] = $this->groupService->create($user_id, [
@ -92,7 +91,7 @@ class GroupController extends CController
return $this->response->success([
'group_id' => $data['group_id']
], '群聊创建成功...');
]);
}
/**
@ -104,7 +103,7 @@ class GroupController extends CController
{
$params = $this->request->inputs(['group_id']);
$this->validate($params, [
'group_id' => 'required|integer',
'group_id' => 'required|integer'
]);
$isTrue = $this->groupService->dismiss($params['group_id'], $this->uid());
@ -129,11 +128,10 @@ class GroupController extends CController
$params = $this->request->inputs(['group_id', 'uids']);
$this->validate($params, [
'group_id' => 'required|integer',
'uids' => 'required',
'uids' => 'required|ids'
]);
$uids = array_filter(explode(',', $params['uids']));
$uids = array_unique($uids);
$uids = parse_ids($params['uids']);
$user_id = $this->uid();
[$isTrue, $record_id] = $this->groupService->invite($user_id, $params['group_id'], $uids);
@ -205,7 +203,7 @@ class GroupController extends CController
'group_id' => 'required|integer',
'group_name' => 'required',
'group_profile' => 'required',
'avatar' => 'required',
'avatar' => 'required'
]);
$result = UsersGroup::where('id', $params['group_id'])->where('user_id', $this->uid())->update([
@ -421,7 +419,7 @@ class GroupController extends CController
'group_id' => 'required|integer',
'notice_id' => 'required|integer',
'title' => 'required',
'content' => 'required',
'content' => 'required'
]);
$user_id = $this->uid();

View File

@ -39,7 +39,6 @@ class UploadController extends CController
* 图片文件流上传接口
*
* @RequestMapping(path="file-stream", methods="post")
*
*/
public function fileStream()
{
@ -56,7 +55,6 @@ class UploadController extends CController
* 获取拆分文件信息
*
* @RequestMapping(path="get-file-split-info", methods="get")
*
*/
public function getFileSplitInfo()
{
@ -72,6 +70,7 @@ class UploadController extends CController
}
/**
* 文件拆分上传接口
*
* @RequestMapping(path="file-subarea-upload", methods="post")
*

View File

@ -35,13 +35,13 @@ class UsersController extends CController
* @Inject
* @var FriendService
*/
protected $friendService;
private $friendService;
/**
* @Inject
* @var UserService
*/
protected $userService;
private $userService;
/**
* @inject
@ -140,7 +140,7 @@ class UsersController extends CController
'nickname' => $userInfo->nickname,
'avatar' => $userInfo->avatar,
'motto' => $userInfo->motto,
'gender' => $userInfo->gender,
'gender' => $userInfo->gender
],
'setting' => [
'theme_mode' => '',
@ -187,6 +187,7 @@ class UsersController extends CController
]);
$isTrue = User::where('id', $this->uid())->update(['avatar' => $params['avatar']]);
return $isTrue
? $this->response->success([], '头像修改成功...')
: $this->response->fail('头像修改失败...');
@ -228,10 +229,11 @@ class UsersController extends CController
$params = $this->request->inputs(['friend_id', 'remarks']);
$this->validate($params, [
'friend_id' => 'required|integer',
'remarks' => "required",
'remarks' => "required"
]);
$isTrue = $this->friendService->editFriendRemark($this->uid(), $params['friend_id'], $params['remarks']);
return $isTrue
? $this->response->success([], '备注修改成功...')
: $this->response->fail('备注修改失败...');
@ -247,7 +249,7 @@ class UsersController extends CController
$params = $this->request->inputs(['friend_id', 'remarks']);
$this->validate($params, [
'friend_id' => 'required|integer',
'remarks' => 'present',
'remarks' => 'present'
]);
$user = $this->userService->findById($params['friend_id']);
@ -276,7 +278,6 @@ class UsersController extends CController
);
}
return $this->response->success([], '发送好友申请成功...');
}
@ -290,7 +291,7 @@ class UsersController extends CController
$params = $this->request->inputs(['apply_id', 'remarks']);
$this->validate($params, [
'apply_id' => 'required|integer',
'remarks' => 'present',
'remarks' => 'present'
]);
$isTrue = $this->friendService->handleFriendApply($this->uid(), (int)$params['apply_id'], $params['remarks']);
@ -323,7 +324,7 @@ class UsersController extends CController
{
$params = $this->request->inputs(['apply_id']);
$this->validate($params, [
'apply_id' => 'required|integer',
'apply_id' => 'required|integer'
]);
$isTrue = $this->friendService->delFriendApply($this->uid(), (int)$params['apply_id']);
@ -342,7 +343,7 @@ class UsersController extends CController
$params = $this->request->inputs(['page', 'page_size']);
$this->validate($params, [
'page' => 'present|integer',
'page_size' => 'present|integer',
'page_size' => 'present|integer'
]);
$page = $this->request->input('page', 1);
@ -376,7 +377,7 @@ class UsersController extends CController
$params = $this->request->inputs(['old_password', 'new_password']);
$this->validate($params, [
'old_password' => 'required',
'new_password' => 'required',
'new_password' => 'required'
]);
$userInfo = $this->userService->findById($this->uid(), ['id', 'password', 'mobile']);
@ -406,7 +407,7 @@ class UsersController extends CController
$this->validate($params, [
'mobile' => "required|regex:/^1[345789][0-9]{9}$/",
'password' => 'required',
'sms_code' => 'required|digits:6',
'sms_code' => 'required|digits:6'
]);
if (!$smsCodeService->check('change_mobile', $params['mobile'], $params['sms_code'])) {
@ -425,6 +426,7 @@ class UsersController extends CController
// 清除缓存信息
$smsCodeService->delCode('change_mobile', $params['mobile']);
return $this->response->success([], '手机号更换成功...');
}
@ -439,7 +441,7 @@ class UsersController extends CController
$this->validate($params, [
'email' => 'required|email',
'password' => 'required',
'email_code' => 'required|digits:6',
'email_code' => 'required|digits:6'
]);
$sendEmailCode = new SendEmailCode();
@ -459,6 +461,7 @@ class UsersController extends CController
}
$sendEmailCode->delCode(SendEmailCode::CHANGE_EMAIL, $params['email']);
return $this->response->success([], '邮箱设置成功...');
}
@ -474,7 +477,7 @@ class UsersController extends CController
{
$params = $this->request->inputs(['mobile']);
$this->validate($params, [
'mobile' => "present|regex:/^1[345789][0-9]{9}$/",
'mobile' => "present|regex:/^1[345789][0-9]{9}$/"
]);
$user_id = $this->uid();
@ -488,12 +491,14 @@ class UsersController extends CController
$data = ['is_debug' => true];
[$isTrue, $result] = $smsCodeService->send('change_mobile', $params['mobile']);
if ($isTrue) {
$data['sms_code'] = $result['data']['code'];
} else {
if (!$isTrue) {
// ... 处理发送失败逻辑,当前默认发送成功
return $this->response->fail('验证码发送失败');
}
// 测试环境下直接返回验证码
$data['sms_code'] = $result['data']['code'];
return $this->response->success($data, '验证码发送成功...');
}
@ -509,7 +514,7 @@ class UsersController extends CController
{
$params = $this->request->inputs(['email']);
$this->validate($params, [
'email' => "required|email",
'email' => "required|email"
]);
$isTrue = $sendEmailCode->send(SendEmailCode::CHANGE_EMAIL, '绑定邮箱', $params['email']);

View File

@ -0,0 +1,38 @@
<?php
namespace App\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Hyperf\Validation\Event\ValidatorFactoryResolved;
use Hyperf\Event\Annotation\Listener;
/**
* @Listener
*/
class ValidatorFactoryResolvedListener implements ListenerInterface
{
public function listen(): array
{
return [
ValidatorFactoryResolved::class,
];
}
public function process(object $event)
{
/** @var ValidatorFactoryInterface $validatorFactory */
$validatorFactory = $event->validatorFactory;
// 注册了 ids 验证器(验证英文逗号拼接的整形数字字符串 例如:[1,2,3,4,5])
$validatorFactory->extend('ids', function ($attribute, $value, $parameters, $validator) {
$arr = explode(',', $value);
foreach ($arr as $id) {
if (!check_int($id)) return false;
}
return true;
});
}
}

View File

@ -46,7 +46,7 @@ class Response
*
* @return PsrResponseInterface
*/
public function fail($message = 'FAIL', $data = [], $code = ResponseCode::FAIL)
public function fail($message = 'fail', $data = [], $code = ResponseCode::FAIL)
{
return $this->response->json(compact('code', 'message', 'data'));
}

View File

@ -198,3 +198,22 @@ function arraysSort(array $array, $field, $sort = SORT_DESC)
array_multisort(array_column($array, $field), $sort, $array);
return $array;
}
/**
* 判断0或正整数
*
* @param string $int 验证字符串
* @param bool $isZero 判断是否可为0
* @return bool
*/
function check_int($int, $isZero = false)
{
$reg = $isZero ? '/^[+]{0,1}(\d+)$/' : '/^[1-9]\d*$/';
return is_numeric($int) && preg_match($reg, $int);
}
function parse_ids($ids)
{
return array_unique(explode(',', trim($ids)));
}

View File

@ -174,4 +174,6 @@ return [
'chinese_word' => 'The :attribute must contain valid characters(chinese/english character, number, underscore)',
'sequential_array' => 'The :attribute must be sequential array',
'ids' => ' :attribute 字段 ids 格式不正确',
];

View File

@ -174,4 +174,6 @@ return [
'chinese_word' => ':attribute 必须包含以下有效字符 (中文/英文,数字, 下划线)',
'sequential_array' => ':attribute 必须是一个有序数组',
'ids' => ' :attribute 字段 ids 格式不正确',
];