hyperf-chat/app/Service/UserService.php

153 lines
4.1 KiB
PHP
Raw Normal View History

2021-01-28 20:07:14 +08:00
<?php
namespace App\Service;
2022-01-22 20:08:19 +08:00
use App\Helper\HashHelper;
2022-01-17 21:27:27 +08:00
use App\Model\Contact\ContactApply;
2022-01-22 16:04:54 +08:00
use App\Repository\Article\ArticleClassRepository;
use App\Repository\UserRepository;
2021-01-28 20:07:14 +08:00
use Hyperf\DbConnection\Db;
class UserService extends BaseService
{
2022-01-22 16:04:54 +08:00
/**
* @var UserRepository
*/
private $userRepository;
2022-01-21 21:46:17 +08:00
2021-01-28 20:07:14 +08:00
/**
2022-01-22 16:04:54 +08:00
* @var ArticleClassRepository
2021-01-28 20:07:14 +08:00
*/
2022-01-22 16:04:54 +08:00
private $articleClassRepository;
public function __construct(UserRepository $userRepository, ArticleClassRepository $articleClassRepository)
2021-01-28 20:07:14 +08:00
{
2022-01-22 16:04:54 +08:00
$this->userRepository = $userRepository;
$this->articleClassRepository = $articleClassRepository;
2021-01-28 20:07:14 +08:00
}
/**
* 登录逻辑
*
2021-04-20 16:30:57 +08:00
* @param string $mobile 手机号
2021-01-28 20:07:14 +08:00
* @param string $password 登录密码
*/
public function login(string $mobile, string $password)
{
2022-01-22 16:04:54 +08:00
$user = $this->userRepository->first(["mobile" => $mobile]);
if (!$user) {
2021-01-28 20:07:14 +08:00
return false;
}
if (!password_verify($password, $user->password)) {
return false;
}
2021-05-23 16:52:01 +08:00
return $user;
2021-01-28 20:07:14 +08:00
}
/**
* 账号注册逻辑
*
* @param array $data 用户数据
* @return bool
*/
public function register(array $data)
{
Db::beginTransaction();
try {
2022-01-22 16:04:54 +08:00
$data['password'] = HashHelper::make($data['password']);
2021-01-28 20:07:14 +08:00
2022-01-22 16:04:54 +08:00
$result = $this->userRepository->create($data);
2021-01-28 20:07:14 +08:00
2022-01-22 16:04:54 +08:00
$this->articleClassRepository->create([
2021-04-20 16:30:57 +08:00
'user_id' => $result->id,
2021-01-28 20:07:14 +08:00
'class_name' => '我的笔记',
'is_default' => 1,
2021-04-20 16:30:57 +08:00
'sort' => 1,
2021-01-28 20:07:14 +08:00
]);
Db::commit();
} catch (\Exception $e) {
Db::rollBack();
2021-04-22 16:14:34 +08:00
return false;
2021-01-28 20:07:14 +08:00
}
2021-07-10 00:18:40 +08:00
return true;
2021-01-28 20:07:14 +08:00
}
/**
* 账号重置密码
*
2022-01-22 16:04:54 +08:00
* @param string $mobile 用户手机号
2021-01-28 20:07:14 +08:00
* @param string $password 新密码
2021-04-22 16:14:34 +08:00
* @return bool
2021-01-28 20:07:14 +08:00
*/
2022-01-22 16:04:54 +08:00
public function resetPassword(string $mobile, string $password): bool
2021-01-28 20:07:14 +08:00
{
2022-01-22 16:04:54 +08:00
return (bool)$this->userRepository->update(["mobile" => $mobile], [
'password' => HashHelper::make($password)
]);
2021-01-28 20:07:14 +08:00
}
/**
* 修改绑定的手机号
*
2021-04-20 16:30:57 +08:00
* @param int $user_id 用户ID
* @param string $mobile 换绑手机号
2021-04-22 16:14:34 +08:00
* @return array
2021-01-28 20:07:14 +08:00
*/
public function changeMobile(int $user_id, string $mobile)
{
2022-01-22 16:04:54 +08:00
if ($this->userRepository->isExistMobile($mobile)) {
2021-01-28 20:07:14 +08:00
return [false, '手机号已被他人绑定'];
}
2022-01-22 16:04:54 +08:00
$this->userRepository->update(["id" => $user_id], [
'mobile' => $mobile
]);
return [true, null];
2021-01-28 20:07:14 +08:00
}
/**
* 通过手机号查找用户
*
2021-04-20 16:30:57 +08:00
* @param int $friend_id 用户ID
2021-01-28 20:07:14 +08:00
* @param int $me_user_id 当前登录用户的ID
* @return array
*/
2022-01-22 16:04:54 +08:00
public function getUserCard(int $friend_id, int $me_user_id): array
2021-01-28 20:07:14 +08:00
{
2022-01-22 16:04:54 +08:00
$info = $this->userRepository->find($friend_id, [
'id', 'mobile', 'nickname', 'avatar', 'gender', 'motto'
]);
2021-04-20 16:30:57 +08:00
if (!$info) return [];
2021-01-28 20:07:14 +08:00
2021-04-20 16:30:57 +08:00
$info = $info->toArray();
2021-07-10 00:18:40 +08:00
$info['friend_status'] = 0;//朋友关系[0:本人;1:陌生人;2:朋友;]
2021-01-28 20:07:14 +08:00
$info['nickname_remark'] = '';
2021-04-20 16:30:57 +08:00
$info['friend_apply'] = 0;
2021-01-28 20:07:14 +08:00
// 判断查询信息是否是自己
if ($friend_id != $me_user_id) {
2021-07-23 21:34:29 +08:00
$is_friend = di()->get(UserFriendService::class)->isFriend($me_user_id, $friend_id, true);
2021-07-07 19:43:09 +08:00
$info['friend_status'] = $is_friend ? 2 : 1;
if ($is_friend) {
2021-07-23 21:34:29 +08:00
$info['nickname_remark'] = di()->get(UserFriendService::class)->getFriendRemark($me_user_id, $friend_id);
2021-01-28 20:07:14 +08:00
} else {
2022-01-16 10:29:16 +08:00
$res = ContactApply::where('user_id', $me_user_id)
2021-01-28 20:07:14 +08:00
->where('friend_id', $friend_id)
->orderBy('id', 'desc')
->exists();
$info['friend_apply'] = $res ? 1 : 0;
}
}
return $info;
}
}