diff --git a/app/Controller/Api/V1/Article/AnnexController.php b/app/Controller/Api/V1/Article/AnnexController.php index e364429..8b7fcde 100644 --- a/app/Controller/Api/V1/Article/AnnexController.php +++ b/app/Controller/Api/V1/Article/AnnexController.php @@ -53,7 +53,6 @@ class AnnexController extends CController $annex = [ 'suffix' => pathinfo($file->getClientFilename(), PATHINFO_EXTENSION), 'size' => $file->getSize(), - 'path' => '', 'drive' => FileDriveConstant::Local, 'original_name' => $file->getClientFilename() ]; diff --git a/app/Controller/Api/V1/Article/ClassController.php b/app/Controller/Api/V1/Article/ClassController.php index e451cf3..d29c00b 100644 --- a/app/Controller/Api/V1/Article/ClassController.php +++ b/app/Controller/Api/V1/Article/ClassController.php @@ -5,6 +5,7 @@ namespace App\Controller\Api\V1\Article; use App\Cache\Repository\LockRedis; use App\Controller\Api\V1\CController; +use App\Repository\Article\ArticleClassRepository; use App\Service\ArticleService; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; @@ -36,11 +37,14 @@ class ClassController extends CController */ 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]); } diff --git a/app/Controller/Api/V1/CommonController.php b/app/Controller/Api/V1/CommonController.php index 08ec716..6461b6e 100644 --- a/app/Controller/Api/V1/CommonController.php +++ b/app/Controller/Api/V1/CommonController.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace App\Controller\Api\V1; +use App\Repository\UserRepository; use App\Support\SendEmailCode; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; @@ -35,7 +36,7 @@ class CommonController extends CController * * @RequestMapping(path="sms-code", methods="post") */ - public function SmsCode() + public function SmsCode(UserRepository $userRepository) { $params = $this->request->all(); @@ -47,7 +48,7 @@ class CommonController extends CController switch ($params['channel']) { case SmsConstant::SmsLoginChannel: case SmsConstant::SmsForgetAccountChannel: - if (!$this->userService->isMobileExist($params['mobile'])) { + if (!$userRepository->isExistMobile($params['mobile'])) { return $this->response->fail("账号不存在或密码错误!"); } @@ -55,7 +56,7 @@ class CommonController extends CController case SmsConstant::SmsRegisterChannel: case SmsConstant::SmsChangeAccountChannel: - if ($this->userService->isMobileExist($params['mobile'])) { + if ($userRepository->isExistMobile($params['mobile'])) { return $this->response->fail("账号已被他(她)人使用!"); } diff --git a/app/Controller/Api/V1/Contact/ContactApplyController.php b/app/Controller/Api/V1/Contact/ContactApplyController.php index 973e10d..ccac3a3 100644 --- a/app/Controller/Api/V1/Contact/ContactApplyController.php +++ b/app/Controller/Api/V1/Contact/ContactApplyController.php @@ -6,13 +6,13 @@ namespace App\Controller\Api\V1\Contact; use App\Cache\FriendApply; use App\Cache\Repository\LockRedis; 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\Middleware; -use App\Middleware\JWTAuthMiddleware; use Hyperf\HttpServer\Annotation\RequestMapping; -use Hyperf\Di\Annotation\Inject; -use App\Service\ContactApplyService; use Psr\Http\Message\ResponseInterface; /** @@ -34,10 +34,10 @@ class ContactApplyController extends CController * 添加联系人申请接口 * * @RequestMapping(path="create", methods="post") - * @param UserService $userService + * * @return ResponseInterface */ - public function create(UserService $userService): ResponseInterface + public function create(): ResponseInterface { $params = $this->request->inputs(['friend_id', 'remark']); $this->validate($params, [ @@ -47,12 +47,15 @@ class ContactApplyController extends CController $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(); $key = "{$user_id}_{$params['friend_id']}"; + if (LockRedis::getInstance()->lock($key, 10)) { $isTrue = $this->service->create($user_id, $params['friend_id'], $params['remark']); if ($isTrue) { diff --git a/app/Controller/Api/V1/Contact/ContactController.php b/app/Controller/Api/V1/Contact/ContactController.php index 71d9c1b..7d9b5f1 100644 --- a/app/Controller/Api/V1/Contact/ContactController.php +++ b/app/Controller/Api/V1/Contact/ContactController.php @@ -2,20 +2,22 @@ namespace App\Controller\Api\V1\Contact; +use App\Cache\FriendRemark; +use App\Cache\ServerRunID; use App\Constants\TalkModeConstant; 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 Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; -use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\Middleware; -use App\Middleware\JWTAuthMiddleware; +use Hyperf\HttpServer\Annotation\RequestMapping; use Psr\Http\Message\ResponseInterface; -use App\Service\ContactsService; -use App\Service\SocketClientService; -use App\Cache\FriendRemark; -use App\Cache\ServerRunID; /** * Class ContactsController @@ -38,6 +40,11 @@ class ContactController extends CController */ private $userService; + /** + * @var UserRepository + */ + private $userRepository; + /** * 获取用户联系人列表 * @@ -45,7 +52,12 @@ class ContactController extends CController */ 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) { $runArr = ServerRunID::getInstance()->getServerRunIdAll(); foreach ($rows as $k => $row) { @@ -73,7 +85,7 @@ class ContactController extends CController 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([], '好友关系解除成功...'); } @@ -91,7 +103,9 @@ class ContactController extends CController '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); } @@ -131,6 +145,7 @@ class ContactController extends CController public function detail(): ResponseInterface { $params = $this->request->inputs(['user_id']); + $this->validate($params, ['user_id' => 'required|integer']); if ($data = $this->userService->getUserCard($params['user_id'], $this->uid())) { diff --git a/app/Controller/Api/V1/Group/GroupController.php b/app/Controller/Api/V1/Group/GroupController.php index 228477b..4c15232 100644 --- a/app/Controller/Api/V1/Group/GroupController.php +++ b/app/Controller/Api/V1/Group/GroupController.php @@ -9,7 +9,7 @@ use App\Model\Group\Group; use App\Model\Group\GroupNotice; use App\Service\Group\GroupMemberService; use App\Service\Group\GroupService; -use App\Service\TalkListService; +use App\Service\TalkSessionService; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; @@ -138,7 +138,7 @@ class GroupController extends CController * 获取群信息接口 * @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); $user_id = $this->uid(); diff --git a/app/Controller/Api/V1/Group/MemberController.php b/app/Controller/Api/V1/Group/MemberController.php index 49ed922..8ceb771 100644 --- a/app/Controller/Api/V1/Group/MemberController.php +++ b/app/Controller/Api/V1/Group/MemberController.php @@ -5,9 +5,9 @@ namespace App\Controller\Api\V1\Group; use App\Controller\Api\V1\CController; use App\Model\Group\GroupMember; +use App\Repository\Contact\ContactRepository; use App\Service\Group\GroupMemberService; use App\Service\Group\GroupService; -use App\Service\UserService; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; @@ -75,12 +75,11 @@ class MemberController extends CController * * @RequestMapping(path="invites", methods="get") */ - public function invites(UserService $service): ResponseInterface + public function invites(): ResponseInterface { $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 ($ids = $this->groupMemberService->getMemberIds($group_id)) { foreach ($friends as $k => $item) { diff --git a/app/Controller/Api/V1/Talk/RecordsController.php b/app/Controller/Api/V1/Talk/RecordsController.php index c6ee349..7dfc833 100644 --- a/app/Controller/Api/V1/Talk/RecordsController.php +++ b/app/Controller/Api/V1/Talk/RecordsController.php @@ -10,7 +10,7 @@ use App\Controller\Api\V1\CController; use App\Model\Talk\TalkRecords; use App\Model\Talk\TalkRecordsFile; use App\Service\Group\GroupMemberService; -use App\Service\TalkListService; +use App\Service\TalkSessionService; use App\Service\TalkService; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; @@ -38,7 +38,7 @@ class RecordsController extends CController /** * @Inject - * @var TalkListService + * @var TalkSessionService */ public $talkListService; diff --git a/app/Controller/Api/V1/Talk/TalkController.php b/app/Controller/Api/V1/Talk/TalkController.php index b305ff3..8535c32 100644 --- a/app/Controller/Api/V1/Talk/TalkController.php +++ b/app/Controller/Api/V1/Talk/TalkController.php @@ -11,7 +11,7 @@ use App\Controller\Api\V1\CController; use App\Model\Group\Group; use App\Model\Talk\TalkSession; use App\Model\User; -use App\Service\TalkListService; +use App\Service\TalkSessionService; use App\Service\TalkService; use App\Service\UserFriendService; use App\Support\UserRelation; @@ -39,7 +39,7 @@ class TalkController extends CController /** * @Inject - * @var TalkListService + * @var TalkSessionService */ public $talkListService; diff --git a/app/Model/Contact/Contact.php b/app/Model/Contact/Contact.php index 587950b..5f28f0c 100644 --- a/app/Model/Contact/Contact.php +++ b/app/Model/Contact/Contact.php @@ -22,6 +22,8 @@ class Contact extends BaseModel { protected $table = 'contact'; + public $timestamps = true; + protected $fillable = [ 'user_id', 'friend_id', diff --git a/app/Model/Talk/TalkRecords.php b/app/Model/Talk/TalkRecords.php index 33dcbeb..d6108f5 100644 --- a/app/Model/Talk/TalkRecords.php +++ b/app/Model/Talk/TalkRecords.php @@ -28,6 +28,8 @@ class TalkRecords extends BaseModel { protected $table = 'talk_records'; + public $timestamps = true; + protected $fillable = [ 'talk_type', 'msg_type', diff --git a/app/Model/Talk/TalkRecordsVote.php b/app/Model/Talk/TalkRecordsVote.php index b9e9f7d..ea70276 100644 --- a/app/Model/Talk/TalkRecordsVote.php +++ b/app/Model/Talk/TalkRecordsVote.php @@ -26,6 +26,8 @@ class TalkRecordsVote extends BaseModel { protected $table = 'talk_records_vote'; + public $timestamps = true; + protected $fillable = [ 'record_id', 'user_id', diff --git a/app/Repository/Article/ArticleClassRepository.php b/app/Repository/Article/ArticleClassRepository.php new file mode 100644 index 0000000..7d20cb2 --- /dev/null +++ b/app/Repository/Article/ArticleClassRepository.php @@ -0,0 +1,39 @@ +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; + } +} diff --git a/app/Repository/BaseRepository.php b/app/Repository/BaseRepository.php index 9c3254e..e103746 100644 --- a/app/Repository/BaseRepository.php +++ b/app/Repository/BaseRepository.php @@ -37,7 +37,7 @@ use Hyperf\Utils\Collection; * @method bool exists(array $where) 判断是否存在相关数据 * @method bool doesntExist(array $where) 判断是否不存在相关数据 * - * @todo 待完善,请勿使用 + * @todo 待完善 * * @package App\Repository */ diff --git a/app/Repository/Contact/ContactRepository.php b/app/Repository/Contact/ContactRepository.php new file mode 100644 index 0000000..3b5a537 --- /dev/null +++ b/app/Repository/Contact/ContactRepository.php @@ -0,0 +1,52 @@ +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', + ]); + } +} diff --git a/app/Repository/Talk/TalkSessionRepository.php b/app/Repository/Talk/TalkSessionRepository.php new file mode 100644 index 0000000..93ac7ec --- /dev/null +++ b/app/Repository/Talk/TalkSessionRepository.php @@ -0,0 +1,15 @@ +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]); + } +} diff --git a/app/Service/ArticleService.php b/app/Service/ArticleService.php index 7a39b7a..9c5f1da 100644 --- a/app/Service/ArticleService.php +++ b/app/Service/ArticleService.php @@ -21,27 +21,6 @@ class ArticleService extends BaseService { 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(); - } - /** * 获取用户文章标签列表 * diff --git a/app/Service/ContactApplyService.php b/app/Service/Contact/ContactApplyService.php similarity index 97% rename from app/Service/ContactApplyService.php rename to app/Service/Contact/ContactApplyService.php index 28b1ad8..ac83476 100644 --- a/app/Service/ContactApplyService.php +++ b/app/Service/Contact/ContactApplyService.php @@ -1,15 +1,18 @@ repository = $repository; + } + /** * 删除联系人 * @@ -33,16 +46,15 @@ class ContactsService extends BaseService */ 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([ - 'status' => 0, - 'updated_at' => date('Y-m-d H:i:s') - ]); + $isTrue = (bool)$this->repository->update([ + "user_id" => $user_id, + "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 $res; + return $isTrue; } /** @@ -55,22 +67,9 @@ class ContactsService extends BaseService */ 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([ - 'remark' => $remark, - 'updated_at' => date('Y-m-d H:i:s') - ]); - } - - /** - * 搜索联系人 - * - * @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() : []; + return (bool)$this->repository->update([ + "user_id" => $user_id, + "friend_id" => $friend_id, + ], ['remark' => $remark]); } } diff --git a/app/Service/Message/SubscribeHandleService.php b/app/Service/Message/SubscribeHandleService.php index ab663e4..56787f9 100644 --- a/app/Service/Message/SubscribeHandleService.php +++ b/app/Service/Message/SubscribeHandleService.php @@ -10,6 +10,7 @@ use App\Constants\TalkModeConstant; use App\Model\Talk\TalkRecords; use App\Model\User; use App\Model\Contact\ContactApply; +use App\Repository\Contact\ContactRepository; use App\Service\SocketClientService; use App\Service\UserService; @@ -154,7 +155,7 @@ class SubscribeHandleService $user_id = (int)$data['data']['user_id']; $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; diff --git a/app/Service/TalkMessageService.php b/app/Service/TalkMessageService.php index fe0e35a..390f3f2 100644 --- a/app/Service/TalkMessageService.php +++ b/app/Service/TalkMessageService.php @@ -35,10 +35,8 @@ class TalkMessageService */ public function insertText(array $message): bool { - $message['msg_type'] = TalkMessageType::TEXT_MESSAGE; - $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'); + $message['msg_type'] = TalkMessageType::TEXT_MESSAGE; + $message['content'] = htmlspecialchars($message['content']); $result = TalkRecords::create($message); @@ -58,9 +56,7 @@ class TalkMessageService { Db::beginTransaction(); try { - $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'); + $message['msg_type'] = TalkMessageType::CODE_MESSAGE; $insert = TalkRecords::create($message); if (!$insert) { @@ -95,9 +91,7 @@ class TalkMessageService { Db::beginTransaction(); try { - $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'); + $message['msg_type'] = TalkMessageType::FILE_MESSAGE; $insert = TalkRecords::create($message); if (!$insert) { @@ -137,11 +131,9 @@ class TalkMessageService Db::beginTransaction(); try { - $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'); + $message['msg_type'] = TalkMessageType::VOTE_MESSAGE; + $insert = TalkRecords::create($message); - $insert = TalkRecords::create($message); $options = []; foreach ($vote['answer_option'] as $k => $option) { $options[chr(65 + $k)] = $option; @@ -150,8 +142,6 @@ class TalkMessageService $vote['record_id'] = $insert->id; $vote['answer_option'] = $options; $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)) { throw new Exception('插入聊天记录(投票消息)失败...'); @@ -259,11 +249,9 @@ class TalkMessageService Db::beginTransaction(); try { - $message['user_id'] = $user_id; - $message['talk_type'] = TalkModeConstant::PRIVATE_CHAT; - $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'); + $message['user_id'] = $user_id; + $message['talk_type'] = TalkModeConstant::PRIVATE_CHAT; + $message['msg_type'] = TalkMessageType::USER_LOGIN_MESSAGE; $insert = TalkRecords::create($message); 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' => '[登录提醒]']); diff --git a/app/Service/TalkListService.php b/app/Service/TalkSessionService.php similarity index 72% rename from app/Service/TalkListService.php rename to app/Service/TalkSessionService.php index 720bf2f..7b41e59 100644 --- a/app/Service/TalkListService.php +++ b/app/Service/TalkSessionService.php @@ -8,10 +8,21 @@ use App\Cache\ServerRunID; use App\Cache\UnreadTalkCache; use App\Constants\TalkModeConstant; use App\Model\Talk\TalkSession; +use App\Repository\Talk\TalkSessionRepository; 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 { - $result = TalkSession::updateOrCreate([ + $result = $this->repository->updateOrCreate([ 'talk_type' => $talk_type, 'user_id' => $user_id, 'receiver_id' => $receiver_id, @@ -32,8 +43,6 @@ class TalkListService 'is_delete' => 0, 'is_disturb' => 0, 'is_robot' => intval($is_robot), - 'created_at' => date('Y-m-d H:i:s'), - 'updated_at' => date('Y-m-d H:i:s') ]); return [ @@ -46,38 +55,26 @@ class TalkListService /** * 聊天对话列表置顶操作 * - * @param int $user_id 用户ID - * @param int $list_id 会话列表ID - * @param bool $is_top 是否置顶(true:是 false:否) + * @param int $user_id 用户ID + * @param int $session_id 会话列表ID + * @param bool $is_top 是否置顶(true:是 false:否) * @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([ - ['id', '=', $list_id], - ['user_id', '=', $user_id], - ])->update([ - 'is_top' => $is_top ? 1 : 0, - 'updated_at' => date('Y-m-d H:i:s') - ]); + return (bool)$this->repository->update(['id' => $session_id, 'user_id' => $user_id], ['is_top' => $is_top ? 1 : 0]); } /** * 删除会话列表 * - * @param int $user_id 用户ID - * @param int $list_id 会话列表ID + * @param int $user_id 用户ID + * @param int $session_id 会话列表ID * @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([ - ['id', '=', $list_id], - ['user_id', '=', $user_id], - ])->update([ - 'is_delete' => 1, - 'updated_at' => date('Y-m-d H:i:s') - ]); + return (bool)$this->repository->update(['id' => $session_id, 'user_id' => $user_id], ['is_delete' => 1]); } /** @@ -90,14 +87,7 @@ class TalkListService */ public function deleteByType(int $user_id, int $receiver_id, int $talk_type): bool { - return (bool)TalkSession::query()->where([ - ['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') - ]); + return (bool)$this->repository->update(['user_id' => $user_id, 'talk_type' => $talk_type, 'receiver_id' => $receiver_id], ['is_delete' => 1]); } /** @@ -173,20 +163,17 @@ class TalkListService */ public function disturb(int $user_id, int $receiver_id, int $talk_type, int $is_disturb): bool { - $result = TalkSession::query()->where([ - ['user_id', '=', $user_id], - ['talk_type', '=', $talk_type], - ['receiver_id', '=', $receiver_id], - ])->first(['id', 'is_disturb']); + $result = $this->repository->first([ + 'user_id' => $user_id, + 'talk_type' => $talk_type, + 'receiver_id' => $receiver_id, + ], ['id', 'is_disturb']); if (!$result || $is_disturb == $result->is_disturb) { return false; } - return (bool)TalkSession::query()->where('id', $result->id)->update([ - 'is_disturb' => $is_disturb, - 'updated_at' => date('Y-m-d H:i:s') - ]); + return (bool)$this->repository->update(['id' => $result->id], ['is_disturb' => $is_disturb]); } /** @@ -199,10 +186,10 @@ class TalkListService */ public function isDisturb(int $user_id, int $receiver_id, int $talk_type): bool { - return (bool)TalkSession::query()->where([ - ['user_id', '=', $user_id], - ['talk_type', '=', $talk_type], - ['receiver_id', '=', $receiver_id], - ])->value('is_disturb'); + return (bool)$this->repository->value([ + 'user_id' => $user_id, + 'talk_type' => $talk_type, + 'receiver_id' => $receiver_id, + ], "is_disturb"); } } diff --git a/app/Service/UserService.php b/app/Service/UserService.php index ac27b48..df67875 100644 --- a/app/Service/UserService.php +++ b/app/Service/UserService.php @@ -3,30 +3,27 @@ namespace App\Service; 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\Repository\Article\ArticleClassRepository; +use App\Repository\UserRepository; use Hyperf\DbConnection\Db; class UserService extends BaseService { - - public function isMobileExist(string $mobile): bool - { - return User::where('mobile', $mobile)->exists(); - } + /** + * @var UserRepository + */ + private $userRepository; /** - * 获取用户信息 - * - * @param int $user_id 用户ID - * @param array $field 查询字段 - * @return User|object|null + * @var ArticleClassRepository */ - 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 $password 登录密码 - * @return false|User */ public function login(string $mobile, string $password) { - if (!$user = User::where('mobile', $mobile)->first()) { + $user = $this->userRepository->first(["mobile" => $mobile]); + if (!$user) { return false; } @@ -59,19 +56,15 @@ class UserService extends BaseService { Db::beginTransaction(); try { - $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'); + $data['password'] = HashHelper::make($data['password']); - $result = User::create($data); + $result = $this->userRepository->create($data); - // 创建用户的默认笔记分类 - ArticleClass::create([ + $this->articleClassRepository->create([ 'user_id' => $result->id, 'class_name' => '我的笔记', 'is_default' => 1, 'sort' => 1, - 'created_at' => time() ]); Db::commit(); @@ -86,13 +79,15 @@ class UserService extends BaseService /** * 账号重置密码 * - * @param string $mobile 用户手机好 + * @param string $mobile 用户手机号 * @param string $password 新密码 * @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) { - if (User::where('mobile', $mobile)->value('id')) { + if ($this->userRepository->isExistMobile($mobile)) { return [false, '手机号已被他人绑定']; } - $isTrue = (bool)User::where('id', $user_id)->update(['mobile' => $mobile]); - return [$isTrue, null]; + $this->userRepository->update(["id" => $user_id], [ + 'mobile' => $mobile + ]); + + return [true, null]; } /** @@ -119,9 +117,12 @@ class UserService extends BaseService * @param int $me_user_id 当前登录用户的ID * @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 []; $info = $info->toArray(); @@ -148,35 +149,4 @@ class UserService extends BaseService 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(); - } } diff --git a/seeders/initialize.php b/seeders/initialize.php index 18528d7..f359c02 100644 --- a/seeders/initialize.php +++ b/seeders/initialize.php @@ -67,7 +67,7 @@ class Initialize extends Seeder Contact::insert($friends); - $service = new \App\Service\TalkListService(); + $service = new \App\Service\TalkSessionService(); foreach ($list as $item) { $service->create($item->user_id, $item->friend_id, \App\Constants\TalkModeConstant::PRIVATE_CHAT); }