hyperf-chat/app/Service/UserService.php

149 lines
4.3 KiB
PHP
Raw Normal View History

2021-01-28 20:07:14 +08:00
<?php
namespace App\Service;
use App\Helper\Hash;
use App\Model\User;
use App\Model\Article\ArticleClass;
use App\Model\UsersFriend;
use App\Model\UsersFriendsApply;
use Hyperf\DbConnection\Db;
class UserService extends BaseService
{
/**
* 获取用户信息
*
2021-04-20 16:30:57 +08:00
* @param int $user_id 用户ID
* @param array $field 查询字段
2021-04-22 16:14:34 +08:00
* @return User
2021-01-28 20:07:14 +08:00
*/
public function findById(int $user_id, $field = ['*'])
{
return User::where('id', $user_id)->first($field);
}
/**
* 登录逻辑
*
2021-04-20 16:30:57 +08:00
* @param string $mobile 手机号
2021-01-28 20:07:14 +08:00
* @param string $password 登录密码
* @return array|bool
*/
public function login(string $mobile, string $password)
{
if (!$user = User::where('mobile', $mobile)->first()) {
return false;
}
if (!password_verify($password, $user->password)) {
return false;
}
return $user->toArray();
}
/**
* 账号注册逻辑
*
* @param array $data 用户数据
* @return bool
*/
public function register(array $data)
{
Db::beginTransaction();
try {
2021-04-20 16:30:57 +08:00
$data['password'] = Hash::make($data['password']);
2021-01-28 20:07:14 +08:00
$data['created_at'] = date('Y-m-d H:i:s');
$result = User::create($data);
// 创建用户的默认笔记分类
ArticleClass::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
'created_at' => time()
]);
Db::commit();
2021-04-22 16:14:34 +08:00
return true;
2021-01-28 20:07:14 +08:00
} catch (\Exception $e) {
Db::rollBack();
2021-04-22 16:14:34 +08:00
return false;
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 新密码
2021-04-22 16:14:34 +08:00
* @return bool
2021-01-28 20:07:14 +08:00
*/
public function resetPassword(string $mobile, string $password)
{
2021-04-22 16:14:34 +08:00
return (bool)User::where('mobile', $mobile)->update(['password' => Hash::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)
{
if (User::where('mobile', $mobile)->value('id')) {
return [false, '手机号已被他人绑定'];
}
$isTrue = (bool)User::where('id', $user_id)->update(['mobile' => $mobile]);
return [$isTrue, null];
}
/**
* 通过手机号查找用户
*
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
*/
2021-04-20 16:30:57 +08:00
public function getUserCard(int $friend_id, int $me_user_id)
2021-01-28 20:07:14 +08:00
{
$info = User::select(['id', 'mobile', 'nickname', 'avatar', 'gender', 'motto'])->where('id', $friend_id)->first();
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();
$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) {
$friendInfo = UsersFriend::
2021-04-20 16:30:57 +08:00
where('user1', '=', $friend_id > $me_user_id ? $me_user_id : $friend_id)
->where('user2', '=', $friend_id < $me_user_id ? $me_user_id : $friend_id)
2021-01-28 20:07:14 +08:00
->where('status', 1)
->first(['id', 'user1', 'user2', 'active', 'user1_remark', 'user2_remark']);
$info['friend_status'] = $friendInfo ? 2 : 1;
if ($friendInfo) {
$info['nickname_remark'] = $friendInfo->user1 == $friend_id ? $friendInfo->user2_remark : $friendInfo->user1_remark;
} else {
$res = UsersFriendsApply::where('user_id', $me_user_id)
->where('friend_id', $friend_id)
->where('status', 0)
->orderBy('id', 'desc')
->exists();
$info['friend_apply'] = $res ? 1 : 0;
}
}
return $info;
}
}