hyperf-chat/app/Controller/Api/V1/UsersController.php

490 lines
15 KiB
PHP
Raw Normal View History

2020-11-04 11:57:16 +08:00
<?php
namespace App\Controller\Api\V1;
2020-11-13 23:09:56 +08:00
use App\Cache\ApplyNumCache;
2020-11-07 22:57:10 +08:00
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Phper666\JWTAuth\Middleware\JWTAuthMiddleware;
2020-11-09 17:41:22 +08:00
use App\Constants\ResponseCode;
use App\Helper\Hash;
use App\Model\User;
use App\Model\UsersChatList;
use App\Model\UsersFriend;
use App\Service\SmsCodeService;
use App\Support\SendEmailCode;
2020-11-07 22:57:10 +08:00
use App\Service\FriendService;
use App\Service\UserService;
2020-11-09 17:41:22 +08:00
use App\Service\SocketFDService;
/**
* Class UsersController
*
* @Controller(path="/api/v1/users")
* @Middleware(JWTAuthMiddleware::class)
*
* @package App\Controller\Api\V1
*/
2020-11-04 11:57:16 +08:00
class UsersController extends CController
{
2020-11-07 22:57:10 +08:00
/**
* @Inject
* @var FriendService
*/
protected $friendService;
2020-11-04 11:57:16 +08:00
2020-11-07 22:57:10 +08:00
/**
* @Inject
* @var UserService
*/
protected $userService;
/**
2020-11-09 17:41:22 +08:00
* @inject
* @var SocketFDService
*/
private $socketFDService;
/**
* 获取我的好友列表
*
2020-11-07 22:57:10 +08:00
* @RequestMapping(path="friends", methods="get")
*/
public function getUserFriends()
{
2020-11-09 17:41:22 +08:00
$rows = UsersFriend::getUserFriends($this->uid());
2020-11-07 22:57:10 +08:00
2020-11-09 17:41:22 +08:00
$runArr = $this->socketFDService->getServerRunIdAll();
foreach ($rows as $k => $row) {
$rows[$k]['online'] = $this->socketFDService->isOnlineAll($row['id'], $runArr);
}
return $this->response->success($rows);
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 解除好友关系
*
* @RequestMapping(path="remove-friend", methods="post")
2020-11-07 22:57:10 +08:00
*/
public function removeFriend()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->all();
$this->validate($params, [
'friend_id' => 'required|integer'
]);
$user_id = $this->uid();
if (!$this->friendService->removeFriend($user_id, $params['friend_id'])) {
return $this->response->fail('好友关系解除成功...');
}
//删除好友会话列表
UsersChatList::delItem($user_id, $params['friend_id'], 2);
UsersChatList::delItem($params['friend_id'], $user_id, 2);
2020-11-07 22:57:10 +08:00
2020-11-09 17:41:22 +08:00
return $this->response->success([], '好友关系解除成功...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 获取用户群聊列表
*
2020-11-07 22:57:10 +08:00
* @RequestMapping(path="user-groups", methods="get")
*/
public function getUserGroups()
{
2020-11-09 17:41:22 +08:00
return $this->response->success(
$this->userService->getUserChatGroups($this->uid())
);
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 获取我的信息
*
2020-11-07 22:57:10 +08:00
* @RequestMapping(path="detail", methods="get")
*/
public function getUserDetail()
{
2020-11-09 17:41:22 +08:00
$userInfo = $this->userService->findById($this->uid(), ['mobile', 'nickname', 'avatar', 'motto', 'email', 'gender']);
return $this->response->success([
'mobile' => $userInfo->mobile,
'nickname' => $userInfo->nickname,
'avatar' => $userInfo->avatar,
'motto' => $userInfo->motto,
'email' => $userInfo->email,
'gender' => $userInfo->gender
]);
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 用户相关设置
*
2020-11-07 22:57:10 +08:00
* @RequestMapping(path="setting", methods="get")
*/
public function getUserSetting()
{
2020-11-09 17:41:22 +08:00
$userInfo = $this->userService->findById($this->uid(), ['id', 'nickname', 'avatar', 'motto', 'gender']);
return $this->response->success([
'user_info' => [
'uid' => $userInfo->id,
'nickname' => $userInfo->nickname,
'avatar' => $userInfo->avatar,
'motto' => $userInfo->motto,
'gender' => $userInfo->gender,
],
'setting' => [
'theme_mode' => '',
'theme_bag_img' => '',
'theme_color' => '',
'notify_cue_tone' => '',
'keyboard_event_notify' => ''
]
]);
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 编辑我的信息
*
* @RequestMapping(path="edit-user-detail", methods="post")
2020-11-07 22:57:10 +08:00
*/
public function editUserDetail()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['nickname', 'avatar', 'motto', 'gender']);
$this->validate($params, [
'nickname' => 'required',
2020-11-14 17:37:55 +08:00
'motto' => 'present',
'gender' => 'required|in:0,1,2',
'avatar' => 'present|url'
2020-11-09 17:41:22 +08:00
]);
$isTrue = User::where('id', $this->uid())->update($params);
return $isTrue
? $this->response->success([], '个人信息修改成功...')
: $this->response->fail('个人信息修改失败...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 修改用户头像
*
* @RequestMapping(path="edit-avatar", methods="post")
2020-11-07 22:57:10 +08:00
*/
public function editAvatar()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['avatar']);
$this->validate($params, [
'avatar' => 'required|url'
]);
$isTrue = User::where('id', $this->uid())->update(['avatar' => $params['avatar']]);
return $isTrue
? $this->response->success([], '头像修改成功...')
: $this->response->fail('头像修改失败...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 通过手机号查找用户
*
2020-11-13 23:09:56 +08:00
* @RequestMapping(path="search-user", methods="post")
2020-11-07 22:57:10 +08:00
*/
public function searchUserInfo()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['user_id', 'mobile']);
2020-11-14 17:37:55 +08:00
if (isset($params['user_id']) && !empty($params['user_id'])) {
2020-11-13 23:09:56 +08:00
$this->validate($params, ['user_id' => 'present|integer']);
2020-11-09 17:41:22 +08:00
$where['uid'] = $params['user_id'];
2020-11-14 17:37:55 +08:00
} else if (isset($params['mobile']) && !empty($params['mobile'])) {
2020-11-13 23:09:56 +08:00
$this->validate($params, ['mobile' => "present|regex:/^1[345789][0-9]{9}$/"]);
2020-11-09 17:41:22 +08:00
$where['mobile'] = $params['mobile'];
} else {
return $this->response->fail('请求参数不正确...', [], ResponseCode::VALIDATION_ERROR);
}
if ($data = $this->userService->searchUserInfo($where, $this->uid())) {
return $this->response->success($data);
}
return $this->response->fail('查询失败...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 编辑好友备注信息
*
* @RequestMapping(path="edit-friend-remark", methods="post")
2020-11-07 22:57:10 +08:00
*/
public function editFriendRemark()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['friend_id', 'remarks']);
$this->validate($params, [
'friend_id' => 'required|integer',
'remarks' => "required",
]);
$isTrue = $this->friendService->editFriendRemark($this->uid(), $params['friend_id'], $params['remarks']);
return $isTrue
? $this->response->success([], '备注修改成功...')
: $this->response->fail('备注修改失败...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 发送添加好友申请
*
* @RequestMapping(path="send-friend-apply", methods="post")
2020-11-07 22:57:10 +08:00
*/
public function sendFriendApply()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['friend_id', 'remarks']);
$this->validate($params, [
'friend_id' => 'required|integer',
'remarks' => 'present',
]);
$user = $this->userService->findById($params['friend_id']);
if (!$user) {
return $this->response->fail('用户不存在...');
}
if (!$this->friendService->addFriendApply($this->uid(), $params['friend_id'], $params['remarks'])) {
return $this->response->fail('发送好友申请失败...');
}
2020-11-07 22:57:10 +08:00
2020-11-09 17:41:22 +08:00
//判断对方是否在线。如果在线发送消息通知
// ...
return $this->response->success([], '发送好友申请成功...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 处理好友的申请
*
* @RequestMapping(path="handle-friend-apply", methods="post")
2020-11-07 22:57:10 +08:00
*/
public function handleFriendApply()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['apply_id', 'remarks']);
$this->validate($params, [
'apply_id' => 'required|integer',
'remarks' => 'present',
]);
$isTrue = $this->friendService->handleFriendApply($this->uid(), $params['apply_id'], $params['remarks']);
//判断是否是同意添加好友
if ($isTrue) {
//... 推送处理消息
}
return $isTrue
? $this->response->success([], '处理成功...')
: $this->response->fail('处理失败...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 删除好友申请记录
*
* @RequestMapping(path="delete-friend-apply", methods="post")
2020-11-07 22:57:10 +08:00
*/
public function deleteFriendApply()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['apply_id']);
$this->validate($params, [
'apply_id' => 'required|integer',
]);
$isTrue = $this->friendService->delFriendApply($this->uid(), $params['apply_id']);
return $isTrue
? $this->response->success([], '删除成功...')
: $this->response->fail('删除失败...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 获取我的好友申请记录
*
2020-11-07 22:57:10 +08:00
* @RequestMapping(path="friend-apply-records", methods="get")
*/
public function getFriendApplyRecords()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['page', 'page_size']);
$this->validate($params, [
'page' => 'present|integer',
'page_size' => 'present|integer',
]);
$page = $this->request->input('page', 1);
$page_size = $this->request->input('page_size', 10);
$user_id = $this->uid();
$data = $this->friendService->findApplyRecords($user_id, $page, $page_size);
return $this->response->success($data);
2020-11-07 22:57:10 +08:00
}
/**
2020-11-13 23:09:56 +08:00
* 获取好友申请未读数
*
2020-11-07 22:57:10 +08:00
* @RequestMapping(path="friend-apply-num", methods="get")
*/
public function getApplyUnreadNum()
{
2020-11-13 23:09:56 +08:00
$num = ApplyNumCache::get($this->uid());
return $this->response->success([
'unread_num' => $num ? $num : 0
]);
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 修改我的密码
*
* @RequestMapping(path="change-password", methods="post")
2020-11-07 22:57:10 +08:00
*/
public function editUserPassword()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['old_password', 'new_password']);
$this->validate($params, [
'old_password' => 'required',
'new_password' => 'required',
]);
$userInfo = $this->userService->findById($this->uid(), ['id', 'password', 'mobile']);
// 验证密码是否正确
if (!Hash::check($this->request->post('old_password'), $userInfo->password)) {
return $this->response->fail('旧密码验证失败...');
}
$isTrue = $this->userService->resetPassword($userInfo->mobile, $params['new_password']);
return $isTrue
? $this->response->success([], '密码修改成功...')
: $this->response->fail('密码修改失败...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 更换用户手机号
*
* @RequestMapping(path="change-mobile", methods="post")
*
* @param SmsCodeService $smsCodeService
* @return \Psr\Http\Message\ResponseInterface
2020-11-07 22:57:10 +08:00
*/
2020-11-09 17:41:22 +08:00
public function editUserMobile(SmsCodeService $smsCodeService)
2020-11-07 22:57:10 +08:00
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['mobile', 'password', 'sms_code']);
$this->validate($params, [
2020-11-14 17:37:55 +08:00
'mobile' => "required|regex:/^1[345789][0-9]{9}$/",
2020-11-09 17:41:22 +08:00
'password' => 'required',
2020-11-14 17:37:55 +08:00
'sms_code' => 'required|digits:6',
2020-11-09 17:41:22 +08:00
]);
if (!$smsCodeService->check('change_mobile', $params['mobile'], $params['sms_code'])) {
return $this->response->fail('验证码填写错误...');
}
$user_id = $this->uid();
if (!Hash::check($params['password'], User::where('id', $user_id)->value('password'))) {
return $this->response->fail('账号密码验证失败...');
}
[$isTrue,] = $this->userService->changeMobile($user_id, $params['mobile']);
if (!$isTrue) {
return $this->response->fail('手机号更换失败...');
}
// 清除缓存信息
$smsCodeService->delCode('change_mobile', $params['mobile']);
return $this->response->success([], '手机号更换成功...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 修改用户邮箱接口
*
* @RequestMapping(path="change-email", methods="post")
2020-11-07 22:57:10 +08:00
*/
public function editUserEmail()
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['email', 'password', 'email_code']);
$this->validate($params, [
2020-11-14 17:37:55 +08:00
'email' => 'required|email',
2020-11-09 17:41:22 +08:00
'password' => 'required',
2020-11-14 17:37:55 +08:00
'email_code' => 'required|digits:6',
2020-11-09 17:41:22 +08:00
]);
$sendEmailCode = new SendEmailCode();
if (!$sendEmailCode->check(SendEmailCode::CHANGE_EMAIL, $params['email'], $params['email_code'])) {
return $this->response->fail('验证码填写错误...');
}
$uid = $this->uid();
$user_password = User::where('id', $uid)->value('password');
if (!Hash::check($params['password'], $user_password)) {
return $this->response->fail('账号密码验证失败...');
}
$isTrue = User::where('id', $uid)->update(['email' => $params['email']]);
if (!$isTrue) {
return $this->response->fail('邮箱设置失败...');
}
$sendEmailCode->delCode(SendEmailCode::CHANGE_EMAIL, $params['email']);
return $this->response->success([], '邮箱设置成功...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 修改手机号发送验证码
*
* @RequestMapping(path="send-mobile-code", methods="post")
*
* @param SmsCodeService $smsCodeService
* @return \Psr\Http\Message\ResponseInterface
2020-11-07 22:57:10 +08:00
*/
2020-11-09 17:41:22 +08:00
public function sendMobileCode(SmsCodeService $smsCodeService)
2020-11-07 22:57:10 +08:00
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['mobile']);
$this->validate($params, [
'mobile' => "present|regex:/^1[345789][0-9]{9}$/",
]);
$user_id = $this->uid();
if (in_array($user_id, [2054, 2055])) {
return $this->response->fail('测试账号不支持修改手机号...');
}
if (User::where('mobile', $params['mobile'])->exists()) {
return $this->response->fail('手机号已被他人注册...');
}
$data = ['is_debug' => true];
[$isTrue, $result] = $smsCodeService->send('change_mobile', $params['mobile']);
if ($isTrue) {
$data['sms_code'] = $result['data']['code'];
} else {
// ... 处理发送失败逻辑,当前默认发送成功
}
return $this->response->success($data, '验证码发送成功...');
2020-11-07 22:57:10 +08:00
}
/**
2020-11-09 17:41:22 +08:00
* 发送绑定邮箱的验证码
*
* @RequestMapping(path="send-change-email-code", methods="post")
*
* @param SendEmailCode $sendEmailCode
* @return \Psr\Http\Message\ResponseInterface
2020-11-07 22:57:10 +08:00
*/
2020-11-09 17:41:22 +08:00
public function sendChangeEmailCode(SendEmailCode $sendEmailCode)
2020-11-07 22:57:10 +08:00
{
2020-11-09 17:41:22 +08:00
$params = $this->request->inputs(['email']);
$this->validate($params, [
'email' => "required|email",
]);
$isTrue = $sendEmailCode->send(SendEmailCode::CHANGE_EMAIL, '绑定邮箱', $params['email']);
if (!$isTrue) {
return $this->response->fail('验证码发送失败...');
}
2020-11-07 22:57:10 +08:00
2020-11-09 17:41:22 +08:00
return $this->response->success([], '验证码发送成功...');
2020-11-07 22:57:10 +08:00
}
2020-11-09 17:41:22 +08:00
}