feat:兼容开发

main
gzydong 2022-01-22 16:04:54 +08:00
parent 706d1a5387
commit 6f2569593a
25 changed files with 334 additions and 222 deletions

View File

@ -53,7 +53,6 @@ class AnnexController extends CController
$annex = [ $annex = [
'suffix' => pathinfo($file->getClientFilename(), PATHINFO_EXTENSION), 'suffix' => pathinfo($file->getClientFilename(), PATHINFO_EXTENSION),
'size' => $file->getSize(), 'size' => $file->getSize(),
'path' => '',
'drive' => FileDriveConstant::Local, 'drive' => FileDriveConstant::Local,
'original_name' => $file->getClientFilename() 'original_name' => $file->getClientFilename()
]; ];

View File

@ -5,6 +5,7 @@ namespace App\Controller\Api\V1\Article;
use App\Cache\Repository\LockRedis; use App\Cache\Repository\LockRedis;
use App\Controller\Api\V1\CController; use App\Controller\Api\V1\CController;
use App\Repository\Article\ArticleClassRepository;
use App\Service\ArticleService; use App\Service\ArticleService;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
@ -36,11 +37,14 @@ class ClassController extends CController
*/ */
public function list(): ResponseInterface public function list(): ResponseInterface
{ {
$rows = $this->articleService->getUserClass($this->uid()); // $rows = $this->articleService->getUserClass($this->uid());
//
// foreach ($rows as &$row) {
// $row['count'] = is_null($row['count']) ? 0 : $row['count'];
// }
foreach ($rows as &$row) {
$row['count'] = is_null($row['count']) ? 0 : $row['count']; $rows = di()->get(ArticleClassRepository::class)->getUserClass($this->uid());
}
return $this->response->success(['rows' => $rows]); return $this->response->success(['rows' => $rows]);
} }

View File

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace App\Controller\Api\V1; namespace App\Controller\Api\V1;
use App\Repository\UserRepository;
use App\Support\SendEmailCode; use App\Support\SendEmailCode;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
@ -35,7 +36,7 @@ class CommonController extends CController
* *
* @RequestMapping(path="sms-code", methods="post") * @RequestMapping(path="sms-code", methods="post")
*/ */
public function SmsCode() public function SmsCode(UserRepository $userRepository)
{ {
$params = $this->request->all(); $params = $this->request->all();
@ -47,7 +48,7 @@ class CommonController extends CController
switch ($params['channel']) { switch ($params['channel']) {
case SmsConstant::SmsLoginChannel: case SmsConstant::SmsLoginChannel:
case SmsConstant::SmsForgetAccountChannel: case SmsConstant::SmsForgetAccountChannel:
if (!$this->userService->isMobileExist($params['mobile'])) { if (!$userRepository->isExistMobile($params['mobile'])) {
return $this->response->fail("账号不存在或密码错误!"); return $this->response->fail("账号不存在或密码错误!");
} }
@ -55,7 +56,7 @@ class CommonController extends CController
case SmsConstant::SmsRegisterChannel: case SmsConstant::SmsRegisterChannel:
case SmsConstant::SmsChangeAccountChannel: case SmsConstant::SmsChangeAccountChannel:
if ($this->userService->isMobileExist($params['mobile'])) { if ($userRepository->isExistMobile($params['mobile'])) {
return $this->response->fail("账号已被他(她)人使用!"); return $this->response->fail("账号已被他(她)人使用!");
} }

View File

@ -6,13 +6,13 @@ namespace App\Controller\Api\V1\Contact;
use App\Cache\FriendApply; use App\Cache\FriendApply;
use App\Cache\Repository\LockRedis; use App\Cache\Repository\LockRedis;
use App\Controller\Api\V1\CController; use App\Controller\Api\V1\CController;
use App\Service\UserService; use App\Middleware\JWTAuthMiddleware;
use App\Repository\UserRepository;
use App\Service\Contact\ContactApplyService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware; use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\JWTAuthMiddleware;
use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Di\Annotation\Inject;
use App\Service\ContactApplyService;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
/** /**
@ -34,10 +34,10 @@ class ContactApplyController extends CController
* 添加联系人申请接口 * 添加联系人申请接口
* *
* @RequestMapping(path="create", methods="post") * @RequestMapping(path="create", methods="post")
* @param UserService $userService *
* @return ResponseInterface * @return ResponseInterface
*/ */
public function create(UserService $userService): ResponseInterface public function create(): ResponseInterface
{ {
$params = $this->request->inputs(['friend_id', 'remark']); $params = $this->request->inputs(['friend_id', 'remark']);
$this->validate($params, [ $this->validate($params, [
@ -47,12 +47,15 @@ class ContactApplyController extends CController
$params['friend_id'] = (int)$params['friend_id']; $params['friend_id'] = (int)$params['friend_id'];
$user = $userService->findById($params['friend_id']); $user = di()->get(UserRepository::class)->findById($params['friend_id']);
if (!$user) return $this->response->fail('用户不存在!'); if (!$user) {
return $this->response->fail('用户不存在!');
}
$user_id = $this->uid(); $user_id = $this->uid();
$key = "{$user_id}_{$params['friend_id']}"; $key = "{$user_id}_{$params['friend_id']}";
if (LockRedis::getInstance()->lock($key, 10)) { if (LockRedis::getInstance()->lock($key, 10)) {
$isTrue = $this->service->create($user_id, $params['friend_id'], $params['remark']); $isTrue = $this->service->create($user_id, $params['friend_id'], $params['remark']);
if ($isTrue) { if ($isTrue) {

View File

@ -2,20 +2,22 @@
namespace App\Controller\Api\V1\Contact; namespace App\Controller\Api\V1\Contact;
use App\Cache\FriendRemark;
use App\Cache\ServerRunID;
use App\Constants\TalkModeConstant; use App\Constants\TalkModeConstant;
use App\Controller\Api\V1\CController; use App\Controller\Api\V1\CController;
use App\Service\TalkListService; use App\Middleware\JWTAuthMiddleware;
use App\Repository\Contact\ContactRepository;
use App\Repository\UserRepository;
use App\Service\Contact\ContactsService;
use App\Service\SocketClientService;
use App\Service\TalkSessionService;
use App\Service\UserService; use App\Service\UserService;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware; use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\JWTAuthMiddleware; use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use App\Service\ContactsService;
use App\Service\SocketClientService;
use App\Cache\FriendRemark;
use App\Cache\ServerRunID;
/** /**
* Class ContactsController * Class ContactsController
@ -38,6 +40,11 @@ class ContactController extends CController
*/ */
private $userService; private $userService;
/**
* @var UserRepository
*/
private $userRepository;
/** /**
* 获取用户联系人列表 * 获取用户联系人列表
* *
@ -45,7 +52,12 @@ class ContactController extends CController
*/ */
public function getContacts(UserService $service): ResponseInterface public function getContacts(UserService $service): ResponseInterface
{ {
$rows = $service->getUserFriends($this->uid());
$rows = di()->get(ContactRepository::class)->friends($this->uid());
// $rows = $service->getUserFriends($this->uid());
if ($rows) { if ($rows) {
$runArr = ServerRunID::getInstance()->getServerRunIdAll(); $runArr = ServerRunID::getInstance()->getServerRunIdAll();
foreach ($rows as $k => $row) { foreach ($rows as $k => $row) {
@ -73,7 +85,7 @@ class ContactController extends CController
return $this->response->fail('好友关系解除失败!'); return $this->response->fail('好友关系解除失败!');
} }
di()->get(TalkListService::class)->deleteByType($user_id, $params['friend_id'], TalkModeConstant::PRIVATE_CHAT); di()->get(TalkSessionService::class)->deleteByType($user_id, $params['friend_id'], TalkModeConstant::PRIVATE_CHAT);
return $this->response->success([], '好友关系解除成功...'); return $this->response->success([], '好友关系解除成功...');
} }
@ -91,7 +103,9 @@ class ContactController extends CController
'mobile' => "present|regex:/^1[3456789][0-9]{9}$/" 'mobile' => "present|regex:/^1[3456789][0-9]{9}$/"
]); ]);
$result = $this->service->findContact($params['mobile']); $result = $this->userRepository->findByMobile($params['mobile'], [
'id', 'nickname', 'mobile', 'avatar', 'gender'
]);
return $this->response->success($result); return $this->response->success($result);
} }
@ -131,6 +145,7 @@ class ContactController extends CController
public function detail(): ResponseInterface public function detail(): ResponseInterface
{ {
$params = $this->request->inputs(['user_id']); $params = $this->request->inputs(['user_id']);
$this->validate($params, ['user_id' => 'required|integer']); $this->validate($params, ['user_id' => 'required|integer']);
if ($data = $this->userService->getUserCard($params['user_id'], $this->uid())) { if ($data = $this->userService->getUserCard($params['user_id'], $this->uid())) {

View File

@ -9,7 +9,7 @@ use App\Model\Group\Group;
use App\Model\Group\GroupNotice; use App\Model\Group\GroupNotice;
use App\Service\Group\GroupMemberService; use App\Service\Group\GroupMemberService;
use App\Service\Group\GroupService; use App\Service\Group\GroupService;
use App\Service\TalkListService; use App\Service\TalkSessionService;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\RequestMapping;
@ -138,7 +138,7 @@ class GroupController extends CController
* 获取群信息接口 * 获取群信息接口
* @RequestMapping(path="detail", methods="get") * @RequestMapping(path="detail", methods="get")
*/ */
public function detail(TalkListService $service): ResponseInterface public function detail(TalkSessionService $service): ResponseInterface
{ {
$group_id = (int)$this->request->input('group_id', 0); $group_id = (int)$this->request->input('group_id', 0);
$user_id = $this->uid(); $user_id = $this->uid();

View File

@ -5,9 +5,9 @@ namespace App\Controller\Api\V1\Group;
use App\Controller\Api\V1\CController; use App\Controller\Api\V1\CController;
use App\Model\Group\GroupMember; use App\Model\Group\GroupMember;
use App\Repository\Contact\ContactRepository;
use App\Service\Group\GroupMemberService; use App\Service\Group\GroupMemberService;
use App\Service\Group\GroupService; use App\Service\Group\GroupService;
use App\Service\UserService;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\RequestMapping;
@ -75,12 +75,11 @@ class MemberController extends CController
* *
* @RequestMapping(path="invites", methods="get") * @RequestMapping(path="invites", methods="get")
*/ */
public function invites(UserService $service): ResponseInterface public function invites(): ResponseInterface
{ {
$group_id = (int)$this->request->input('group_id', 0); $group_id = (int)$this->request->input('group_id', 0);
$friends = $service->getUserFriends($this->uid()); $friends = di()->get(ContactRepository::class)->friends($this->uid());
if ($group_id > 0 && $friends) { if ($group_id > 0 && $friends) {
if ($ids = $this->groupMemberService->getMemberIds($group_id)) { if ($ids = $this->groupMemberService->getMemberIds($group_id)) {
foreach ($friends as $k => $item) { foreach ($friends as $k => $item) {

View File

@ -10,7 +10,7 @@ use App\Controller\Api\V1\CController;
use App\Model\Talk\TalkRecords; use App\Model\Talk\TalkRecords;
use App\Model\Talk\TalkRecordsFile; use App\Model\Talk\TalkRecordsFile;
use App\Service\Group\GroupMemberService; use App\Service\Group\GroupMemberService;
use App\Service\TalkListService; use App\Service\TalkSessionService;
use App\Service\TalkService; use App\Service\TalkService;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
@ -38,7 +38,7 @@ class RecordsController extends CController
/** /**
* @Inject * @Inject
* @var TalkListService * @var TalkSessionService
*/ */
public $talkListService; public $talkListService;

View File

@ -11,7 +11,7 @@ use App\Controller\Api\V1\CController;
use App\Model\Group\Group; use App\Model\Group\Group;
use App\Model\Talk\TalkSession; use App\Model\Talk\TalkSession;
use App\Model\User; use App\Model\User;
use App\Service\TalkListService; use App\Service\TalkSessionService;
use App\Service\TalkService; use App\Service\TalkService;
use App\Service\UserFriendService; use App\Service\UserFriendService;
use App\Support\UserRelation; use App\Support\UserRelation;
@ -39,7 +39,7 @@ class TalkController extends CController
/** /**
* @Inject * @Inject
* @var TalkListService * @var TalkSessionService
*/ */
public $talkListService; public $talkListService;

View File

@ -22,6 +22,8 @@ class Contact extends BaseModel
{ {
protected $table = 'contact'; protected $table = 'contact';
public $timestamps = true;
protected $fillable = [ protected $fillable = [
'user_id', 'user_id',
'friend_id', 'friend_id',

View File

@ -28,6 +28,8 @@ class TalkRecords extends BaseModel
{ {
protected $table = 'talk_records'; protected $table = 'talk_records';
public $timestamps = true;
protected $fillable = [ protected $fillable = [
'talk_type', 'talk_type',
'msg_type', 'msg_type',

View File

@ -26,6 +26,8 @@ class TalkRecordsVote extends BaseModel
{ {
protected $table = 'talk_records_vote'; protected $table = 'talk_records_vote';
public $timestamps = true;
protected $fillable = [ protected $fillable = [
'record_id', 'record_id',
'user_id', 'user_id',

View File

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace App\Repository\Article;
use App\Model\Article\Article;
use App\Model\Article\ArticleClass;
use App\Repository\BaseRepository;
use Hyperf\DbConnection\Db;
class ArticleClassRepository extends BaseRepository
{
public function __construct(ArticleClass $articleClass)
{
parent::__construct($articleClass);
}
/**
* 获取用户分类
*
* @param int $user_id 用户ID
*
* @return array
*/
public function getUserClass(int $user_id): array
{
$items = $this->get(["user_id" => $user_id, 'order by' => ['sort' => 'asc']], ['id', 'class_name', 'is_default']);
if (empty($items)) {
return [];
}
$rows = Article::select(['class_id', Db::raw('count(class_id) as count')])->where('user_id', $user_id)->where('status', 1)->groupBy(['class_id'])->get()->keyBy("class_id")->toArray();
foreach ($items as $k => $val) {
$items[$k]['count'] = isset($rows[$val['id']]) ? $rows[$val['id']]['count'] : 0;
}
return $items;
}
}

View File

@ -37,7 +37,7 @@ use Hyperf\Utils\Collection;
* @method bool exists(array $where) 判断是否存在相关数据 * @method bool exists(array $where) 判断是否存在相关数据
* @method bool doesntExist(array $where) 判断是否不存在相关数据 * @method bool doesntExist(array $where) 判断是否不存在相关数据
* *
* @todo 待完善,请勿使用 * @todo 待完善
* *
* @package App\Repository * @package App\Repository
*/ */

View File

@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace App\Repository\Contact;
use App\Model\Contact\Contact;
use App\Repository\BaseRepository;
class ContactRepository extends BaseRepository
{
public function __construct(Contact $model)
{
parent::__construct($model);
}
/**
* 获取所有朋友ID
*
* @param int $user_id 用户ID
*
* @return array
*/
public function findAllFriendIds(int $user_id)
{
return $this->pluck(["user_id" => $user_id, "status" => 1], 'friend_id')->toArray();
}
/**
* 获取联系人列表
*
* @param int $user_id 用户ID
*
* @return array
*/
public function friends(int $user_id): array
{
return $this->get([
'contact.user_id' => $user_id,
'contact.status' => 1,
'join table' => [
['users', 'users.id', '=', 'contact.friend_id', 'left'],
]
], [
'users.id',
'users.nickname',
'users.avatar',
'users.motto',
'users.gender',
'contact.remark as friend_remark',
]);
}
}

View File

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Repository\Talk;
use App\Model\Talk\TalkSession;
use App\Repository\BaseRepository;
class TalkSessionRepository extends BaseRepository
{
public function __construct(TalkSession $model)
{
parent::__construct($model);
}
}

View File

@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace App\Repository;
use App\Model\User;
/**
* User 数据层
*/
class UserRepository extends BaseRepository
{
public function __construct(User $model)
{
parent::__construct($model);
}
/**
* 根据主键查询
*
* @param int $user_id
* @return \Hyperf\Database\Model\Model
*/
public function findById(int $user_id)
{
return $this->find($user_id);
}
/**
* 根据手机号查询下用户信息
*
* @param string $mobile 手机号
* @param array $fields 查询字段
* @return array
*/
public function findByMobile(string $mobile, array $fields = ["*"]): array
{
return $this->first(["mobile" => $mobile], $fields, true);
}
/**
* 查询手机号是否存在
*
* @param string $mobile 手机号
*
* @return bool
*/
public function isExistMobile(string $mobile): bool
{
return $this->exists(["mobile" => $mobile]);
}
}

View File

@ -21,27 +21,6 @@ class ArticleService extends BaseService
{ {
use PagingTrait; use PagingTrait;
/**
* 获取用户文章分类列表
*
* @param int $user_id 用户ID
* @return array
*/
public function getUserClass(int $user_id): array
{
$fields = [
'article_class.id', 'article_class.class_name',
'article_class.is_default',
'sub_join.count',
];
$subJoin = Article::select(['class_id', Db::raw('count(class_id) as count')])->where('user_id', $user_id)->where('status', 1)->groupBy(['class_id']);
return ArticleClass::leftJoinSub($subJoin, 'sub_join', function ($join) {
$join->on('article_class.id', '=', 'sub_join.class_id');
})->where('article_class.user_id', $user_id)->orderBy('article_class.sort')->get($fields)->toArray();
}
/** /**
* 获取用户文章标签列表 * 获取用户文章标签列表
* *

View File

@ -1,15 +1,18 @@
<?php <?php
namespace App\Service; namespace App\Service\Contact;
use App\Cache\FriendApply; use App\Cache\FriendApply;
use App\Constants\TalkEventConstant; use App\Constants\TalkEventConstant;
use App\Event\TalkEvent; use App\Event\TalkEvent;
use App\Model\User;
use App\Model\Contact\Contact; use App\Model\Contact\Contact;
use App\Model\Contact\ContactApply; use App\Model\Contact\ContactApply;
use App\Model\User;
use App\Service\SocketClientService;
use App\Traits\PagingTrait; use App\Traits\PagingTrait;
use Hyperf\DbConnection\Db; use Hyperf\DbConnection\Db;
use function di;
use function event;
class ContactApplyService class ContactApplyService
{ {

View File

@ -8,10 +8,10 @@
* @link https://github.com/gzydong/hyperf-chat * @link https://github.com/gzydong/hyperf-chat
*/ */
namespace App\Service; namespace App\Service\Contact;
use App\Model\User; use App\Repository\Contact\ContactRepository;
use App\Model\Contact\Contact; use App\Service\BaseService;
use App\Traits\PagingTrait; use App\Traits\PagingTrait;
/** /**
@ -24,6 +24,19 @@ class ContactsService extends BaseService
{ {
use PagingTrait; use PagingTrait;
/**
* @var ContactRepository
*/
private $repository;
/**
* @param ContactRepository $repository
*/
public function __construct(ContactRepository $repository)
{
$this->repository = $repository;
}
/** /**
* 删除联系人 * 删除联系人
* *
@ -33,16 +46,15 @@ class ContactsService extends BaseService
*/ */
public function delete(int $user_id, int $friend_id): bool public function delete(int $user_id, int $friend_id): bool
{ {
$res = (bool)Contact::where('user_id', $user_id)->where('friend_id', $friend_id)->where('status', 1)->update([ $isTrue = (bool)$this->repository->update([
'status' => 0, "user_id" => $user_id,
'updated_at' => date('Y-m-d H:i:s') "friend_id" => $friend_id,
]); "status" => 1,
], ['status' => 0]);
if ($res) redis()->del("good_friends:{$user_id}_{$friend_id}"); if ($isTrue) redis()->del("good_friends:{$user_id}_{$friend_id}");
return $isTrue;
return $res;
} }
/** /**
@ -55,22 +67,9 @@ class ContactsService extends BaseService
*/ */
public function editRemark(int $user_id, int $friend_id, string $remark): bool public function editRemark(int $user_id, int $friend_id, string $remark): bool
{ {
return (bool)Contact::where('user_id', $user_id)->where('friend_id', $friend_id)->update([ return (bool)$this->repository->update([
'remark' => $remark, "user_id" => $user_id,
'updated_at' => date('Y-m-d H:i:s') "friend_id" => $friend_id,
]); ], ['remark' => $remark]);
}
/**
* 搜索联系人
*
* @param string $mobile 用户手机号/登录账号
* @return array
*/
public function findContact(string $mobile): array
{
$user = User::where('mobile', $mobile)->first(['id', 'nickname', 'mobile', 'avatar', 'gender']);
return $user ? $user->toArray() : [];
} }
} }

View File

@ -10,6 +10,7 @@ use App\Constants\TalkModeConstant;
use App\Model\Talk\TalkRecords; use App\Model\Talk\TalkRecords;
use App\Model\User; use App\Model\User;
use App\Model\Contact\ContactApply; use App\Model\Contact\ContactApply;
use App\Repository\Contact\ContactRepository;
use App\Service\SocketClientService; use App\Service\SocketClientService;
use App\Service\UserService; use App\Service\UserService;
@ -154,7 +155,7 @@ class SubscribeHandleService
$user_id = (int)$data['data']['user_id']; $user_id = (int)$data['data']['user_id'];
$status = (int)$data['data']['status']; $status = (int)$data['data']['status'];
$ids = di()->get(UserService::class)->getFriendIds($user_id); $ids = di()->get(ContactRepository::class)->findAllFriendIds($user_id);
if (empty($ids)) return; if (empty($ids)) return;

View File

@ -37,8 +37,6 @@ class TalkMessageService
{ {
$message['msg_type'] = TalkMessageType::TEXT_MESSAGE; $message['msg_type'] = TalkMessageType::TEXT_MESSAGE;
$message['content'] = htmlspecialchars($message['content']); $message['content'] = htmlspecialchars($message['content']);
$message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s');
$result = TalkRecords::create($message); $result = TalkRecords::create($message);
@ -59,8 +57,6 @@ class TalkMessageService
Db::beginTransaction(); Db::beginTransaction();
try { try {
$message['msg_type'] = TalkMessageType::CODE_MESSAGE; $message['msg_type'] = TalkMessageType::CODE_MESSAGE;
$message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s');
$insert = TalkRecords::create($message); $insert = TalkRecords::create($message);
if (!$insert) { if (!$insert) {
@ -96,8 +92,6 @@ class TalkMessageService
Db::beginTransaction(); Db::beginTransaction();
try { try {
$message['msg_type'] = TalkMessageType::FILE_MESSAGE; $message['msg_type'] = TalkMessageType::FILE_MESSAGE;
$message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s');
$insert = TalkRecords::create($message); $insert = TalkRecords::create($message);
if (!$insert) { if (!$insert) {
@ -138,10 +132,8 @@ class TalkMessageService
Db::beginTransaction(); Db::beginTransaction();
try { try {
$message['msg_type'] = TalkMessageType::VOTE_MESSAGE; $message['msg_type'] = TalkMessageType::VOTE_MESSAGE;
$message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s');
$insert = TalkRecords::create($message); $insert = TalkRecords::create($message);
$options = []; $options = [];
foreach ($vote['answer_option'] as $k => $option) { foreach ($vote['answer_option'] as $k => $option) {
$options[chr(65 + $k)] = $option; $options[chr(65 + $k)] = $option;
@ -150,8 +142,6 @@ class TalkMessageService
$vote['record_id'] = $insert->id; $vote['record_id'] = $insert->id;
$vote['answer_option'] = $options; $vote['answer_option'] = $options;
$vote['answer_num'] = $answer_num; $vote['answer_num'] = $answer_num;
$vote['created_at'] = date('Y-m-d H:i:s');
$vote['updated_at'] = $vote['created_at'];
if (!TalkRecordsVote::create($vote)) { if (!TalkRecordsVote::create($vote)) {
throw new Exception('插入聊天记录(投票消息)失败...'); throw new Exception('插入聊天记录(投票消息)失败...');
@ -262,8 +252,6 @@ class TalkMessageService
$message['user_id'] = $user_id; $message['user_id'] = $user_id;
$message['talk_type'] = TalkModeConstant::PRIVATE_CHAT; $message['talk_type'] = TalkModeConstant::PRIVATE_CHAT;
$message['msg_type'] = TalkMessageType::USER_LOGIN_MESSAGE; $message['msg_type'] = TalkMessageType::USER_LOGIN_MESSAGE;
$message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s');
$insert = TalkRecords::create($message); $insert = TalkRecords::create($message);
if (!$insert) { if (!$insert) {
@ -284,7 +272,7 @@ class TalkMessageService
} }
// 创建对话列表 // 创建对话列表
di()->get(TalkListService::class)->create($insert->receiver_id, $insert->user_id, $insert->talk_type, true); di()->get(TalkSessionService::class)->create($insert->receiver_id, $insert->user_id, $insert->talk_type, true);
$this->handle($insert, ['text' => '[登录提醒]']); $this->handle($insert, ['text' => '[登录提醒]']);

View File

@ -8,10 +8,21 @@ use App\Cache\ServerRunID;
use App\Cache\UnreadTalkCache; use App\Cache\UnreadTalkCache;
use App\Constants\TalkModeConstant; use App\Constants\TalkModeConstant;
use App\Model\Talk\TalkSession; use App\Model\Talk\TalkSession;
use App\Repository\Talk\TalkSessionRepository;
use Carbon\Carbon; use Carbon\Carbon;
class TalkListService class TalkSessionService extends BaseService
{ {
/**
* @var TalkSessionRepository
*/
private $repository;
public function __construct(TalkSessionRepository $repository)
{
$this->repository = $repository;
}
/** /**
* 创建聊天列表记录 * 创建聊天列表记录
* *
@ -23,7 +34,7 @@ class TalkListService
*/ */
public function create(int $user_id, int $receiver_id, int $talk_type, bool $is_robot = false): array public function create(int $user_id, int $receiver_id, int $talk_type, bool $is_robot = false): array
{ {
$result = TalkSession::updateOrCreate([ $result = $this->repository->updateOrCreate([
'talk_type' => $talk_type, 'talk_type' => $talk_type,
'user_id' => $user_id, 'user_id' => $user_id,
'receiver_id' => $receiver_id, 'receiver_id' => $receiver_id,
@ -32,8 +43,6 @@ class TalkListService
'is_delete' => 0, 'is_delete' => 0,
'is_disturb' => 0, 'is_disturb' => 0,
'is_robot' => intval($is_robot), 'is_robot' => intval($is_robot),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
]); ]);
return [ return [
@ -47,37 +56,25 @@ class TalkListService
* 聊天对话列表置顶操作 * 聊天对话列表置顶操作
* *
* @param int $user_id 用户ID * @param int $user_id 用户ID
* @param int $list_id 会话列表ID * @param int $session_id 会话列表ID
* @param bool $is_top 是否置顶true: false:否) * @param bool $is_top 是否置顶true: false:否)
* @return bool * @return bool
*/ */
public function top(int $user_id, int $list_id, $is_top = true): bool public function top(int $user_id, int $session_id, bool $is_top = true): bool
{ {
return (bool)TalkSession::query()->where([ return (bool)$this->repository->update(['id' => $session_id, 'user_id' => $user_id], ['is_top' => $is_top ? 1 : 0]);
['id', '=', $list_id],
['user_id', '=', $user_id],
])->update([
'is_top' => $is_top ? 1 : 0,
'updated_at' => date('Y-m-d H:i:s')
]);
} }
/** /**
* 删除会话列表 * 删除会话列表
* *
* @param int $user_id 用户ID * @param int $user_id 用户ID
* @param int $list_id 会话列表ID * @param int $session_id 会话列表ID
* @return bool * @return bool
*/ */
public function delete(int $user_id, int $list_id): bool public function delete(int $user_id, int $session_id): bool
{ {
return (bool)TalkSession::query()->where([ return (bool)$this->repository->update(['id' => $session_id, 'user_id' => $user_id], ['is_delete' => 1]);
['id', '=', $list_id],
['user_id', '=', $user_id],
])->update([
'is_delete' => 1,
'updated_at' => date('Y-m-d H:i:s')
]);
} }
/** /**
@ -90,14 +87,7 @@ class TalkListService
*/ */
public function deleteByType(int $user_id, int $receiver_id, int $talk_type): bool public function deleteByType(int $user_id, int $receiver_id, int $talk_type): bool
{ {
return (bool)TalkSession::query()->where([ return (bool)$this->repository->update(['user_id' => $user_id, 'talk_type' => $talk_type, 'receiver_id' => $receiver_id], ['is_delete' => 1]);
['user_id', '=', $user_id],
['talk_type', '=', $talk_type],
['receiver_id', '=', $receiver_id],
])->update([
'is_delete' => 1,
'updated_at' => date('Y-m-d H:i:s')
]);
} }
/** /**
@ -173,20 +163,17 @@ class TalkListService
*/ */
public function disturb(int $user_id, int $receiver_id, int $talk_type, int $is_disturb): bool public function disturb(int $user_id, int $receiver_id, int $talk_type, int $is_disturb): bool
{ {
$result = TalkSession::query()->where([ $result = $this->repository->first([
['user_id', '=', $user_id], 'user_id' => $user_id,
['talk_type', '=', $talk_type], 'talk_type' => $talk_type,
['receiver_id', '=', $receiver_id], 'receiver_id' => $receiver_id,
])->first(['id', 'is_disturb']); ], ['id', 'is_disturb']);
if (!$result || $is_disturb == $result->is_disturb) { if (!$result || $is_disturb == $result->is_disturb) {
return false; return false;
} }
return (bool)TalkSession::query()->where('id', $result->id)->update([ return (bool)$this->repository->update(['id' => $result->id], ['is_disturb' => $is_disturb]);
'is_disturb' => $is_disturb,
'updated_at' => date('Y-m-d H:i:s')
]);
} }
/** /**
@ -199,10 +186,10 @@ class TalkListService
*/ */
public function isDisturb(int $user_id, int $receiver_id, int $talk_type): bool public function isDisturb(int $user_id, int $receiver_id, int $talk_type): bool
{ {
return (bool)TalkSession::query()->where([ return (bool)$this->repository->value([
['user_id', '=', $user_id], 'user_id' => $user_id,
['talk_type', '=', $talk_type], 'talk_type' => $talk_type,
['receiver_id', '=', $receiver_id], 'receiver_id' => $receiver_id,
])->value('is_disturb'); ], "is_disturb");
} }
} }

View File

@ -3,30 +3,27 @@
namespace App\Service; namespace App\Service;
use App\Helpers\HashHelper; use App\Helpers\HashHelper;
use App\Model\User;
use App\Model\Article\ArticleClass;
use App\Model\Contact\Contact;
use App\Model\Contact\ContactApply; use App\Model\Contact\ContactApply;
use App\Repository\Article\ArticleClassRepository;
use App\Repository\UserRepository;
use Hyperf\DbConnection\Db; use Hyperf\DbConnection\Db;
class UserService extends BaseService class UserService extends BaseService
{ {
/**
public function isMobileExist(string $mobile): bool * @var UserRepository
{ */
return User::where('mobile', $mobile)->exists(); private $userRepository;
}
/** /**
* 获取用户信息 * @var ArticleClassRepository
*
* @param int $user_id 用户ID
* @param array $field 查询字段
* @return User|object|null
*/ */
public function findById(int $user_id, $field = ['*']): ?User private $articleClassRepository;
public function __construct(UserRepository $userRepository, ArticleClassRepository $articleClassRepository)
{ {
return User::where('id', $user_id)->first($field); $this->userRepository = $userRepository;
$this->articleClassRepository = $articleClassRepository;
} }
/** /**
@ -34,11 +31,11 @@ class UserService extends BaseService
* *
* @param string $mobile 手机号 * @param string $mobile 手机号
* @param string $password 登录密码 * @param string $password 登录密码
* @return false|User
*/ */
public function login(string $mobile, string $password) public function login(string $mobile, string $password)
{ {
if (!$user = User::where('mobile', $mobile)->first()) { $user = $this->userRepository->first(["mobile" => $mobile]);
if (!$user) {
return false; return false;
} }
@ -60,18 +57,14 @@ class UserService extends BaseService
Db::beginTransaction(); Db::beginTransaction();
try { try {
$data['password'] = HashHelper::make($data['password']); $data['password'] = HashHelper::make($data['password']);
$data['created_at'] = date('Y-m-d H:i:s');
$data['updated_at'] = date('Y-m-d H:i:s');
$result = User::create($data); $result = $this->userRepository->create($data);
// 创建用户的默认笔记分类 $this->articleClassRepository->create([
ArticleClass::create([
'user_id' => $result->id, 'user_id' => $result->id,
'class_name' => '我的笔记', 'class_name' => '我的笔记',
'is_default' => 1, 'is_default' => 1,
'sort' => 1, 'sort' => 1,
'created_at' => time()
]); ]);
Db::commit(); Db::commit();
@ -86,13 +79,15 @@ class UserService extends BaseService
/** /**
* 账号重置密码 * 账号重置密码
* *
* @param string $mobile 用户手机 * @param string $mobile 用户手机
* @param string $password 新密码 * @param string $password 新密码
* @return bool * @return bool
*/ */
public function resetPassword(string $mobile, string $password) public function resetPassword(string $mobile, string $password): bool
{ {
return (bool)User::where('mobile', $mobile)->update(['password' => HashHelper::make($password)]); return (bool)$this->userRepository->update(["mobile" => $mobile], [
'password' => HashHelper::make($password)
]);
} }
/** /**
@ -104,12 +99,15 @@ class UserService extends BaseService
*/ */
public function changeMobile(int $user_id, string $mobile) public function changeMobile(int $user_id, string $mobile)
{ {
if (User::where('mobile', $mobile)->value('id')) { if ($this->userRepository->isExistMobile($mobile)) {
return [false, '手机号已被他人绑定']; return [false, '手机号已被他人绑定'];
} }
$isTrue = (bool)User::where('id', $user_id)->update(['mobile' => $mobile]); $this->userRepository->update(["id" => $user_id], [
return [$isTrue, null]; 'mobile' => $mobile
]);
return [true, null];
} }
/** /**
@ -119,9 +117,12 @@ class UserService extends BaseService
* @param int $me_user_id 当前登录用户的ID * @param int $me_user_id 当前登录用户的ID
* @return array * @return array
*/ */
public function getUserCard(int $friend_id, int $me_user_id) public function getUserCard(int $friend_id, int $me_user_id): array
{ {
$info = User::select(['id', 'mobile', 'nickname', 'avatar', 'gender', 'motto'])->where('id', $friend_id)->first(); $info = $this->userRepository->find($friend_id, [
'id', 'mobile', 'nickname', 'avatar', 'gender', 'motto'
]);
if (!$info) return []; if (!$info) return [];
$info = $info->toArray(); $info = $info->toArray();
@ -148,35 +149,4 @@ class UserService extends BaseService
return $info; return $info;
} }
/**
* 获取用户好友列表
*
* @param int $user_id 用户ID
* @return array
*/
public function getUserFriends(int $user_id): array
{
return Contact::Join('users', 'users.id', '=', 'contact.friend_id')
->where('user_id', $user_id)->where('contact.status', 1)
->get([
'users.id',
'users.nickname',
'users.avatar',
'users.motto',
'users.gender',
'contact.remark as friend_remark',
])->toArray();
}
/**
* 获取指定用户的所有朋友的用户ID
*
* @param int $user_id 指定用户ID
* @return array
*/
public function getFriendIds(int $user_id): array
{
return Contact::where('user_id', $user_id)->where('status', 1)->pluck('friend_id')->toArray();
}
} }

View File

@ -67,7 +67,7 @@ class Initialize extends Seeder
Contact::insert($friends); Contact::insert($friends);
$service = new \App\Service\TalkListService(); $service = new \App\Service\TalkSessionService();
foreach ($list as $item) { foreach ($list as $item) {
$service->create($item->user_id, $item->friend_id, \App\Constants\TalkModeConstant::PRIVATE_CHAT); $service->create($item->user_id, $item->friend_id, \App\Constants\TalkModeConstant::PRIVATE_CHAT);
} }