优化代码

main
gzydong 2021-09-04 20:18:20 +08:00
parent 900b5fefdb
commit d0697ac658
18 changed files with 80 additions and 77 deletions

View File

@ -1,5 +1,4 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace App\Command; namespace App\Command;

View File

@ -1,5 +1,4 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace App\Command; namespace App\Command;

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
namespace App\Exception; namespace App\Exception;

View File

@ -1,6 +1,6 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
/** /**
* This file is part of Hyperf. * This file is part of Hyperf.
* *

View File

@ -1,6 +1,6 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
/** /**
* This file is part of Hyperf. * This file is part of Hyperf.
* *

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
namespace App\Listener; namespace App\Listener;

View File

@ -1,5 +1,6 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
/** /**
* This is my open source code, please do not use it for commercial applications. * This is my open source code, please do not use it for commercial applications.
* For the full copyright and license information, * For the full copyright and license information,

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
namespace App\Service; namespace App\Service;
@ -26,7 +27,7 @@ class ArticleService extends BaseService
* @param int $user_id 用户ID * @param int $user_id 用户ID
* @return array * @return array
*/ */
public function getUserClass(int $user_id) public function getUserClass(int $user_id): array
{ {
$fields = [ $fields = [
'article_class.id', 'article_class.class_name', 'article_class.id', 'article_class.class_name',
@ -35,12 +36,10 @@ class ArticleService extends BaseService
]; ];
$subJoin = Article::select(['class_id', Db::raw('count(class_id) as count')])->where('user_id', $user_id)->where('status', 1)->groupBy(['class_id']); $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) { return ArticleClass::leftJoinSub($subJoin, 'sub_join', function ($join) {
$join->on('article_class.id', '=', 'sub_join.class_id'); $join->on('article_class.id', '=', 'sub_join.class_id');
})->where('article_class.user_id', $user_id) })->where('article_class.user_id', $user_id)->orderBy('article_class.sort')->get($fields)->toArray();
->orderBy('article_class.sort')
->get($fields)
->toArray();
} }
/** /**
@ -49,7 +48,7 @@ class ArticleService extends BaseService
* @param int $user_id 用户ID * @param int $user_id 用户ID
* @return array * @return array
*/ */
public function getUserTags(int $user_id) public function getUserTags(int $user_id): array
{ {
$items = ArticleTag::where('user_id', $user_id)->orderBy('id', 'desc')->get(['id', 'tag_name', Db::raw('0 as count')])->toArray(); $items = ArticleTag::where('user_id', $user_id)->orderBy('id', 'desc')->get(['id', 'tag_name', Db::raw('0 as count')])->toArray();
foreach ($items as $k => $item) { foreach ($items as $k => $item) {
@ -68,47 +67,40 @@ class ArticleService extends BaseService
* @param array $params 查询参数 * @param array $params 查询参数
* @return array * @return array
*/ */
public function getUserArticleList(int $user_id, int $page, int $page_size, $params = []) public function getUserArticleList(int $user_id, int $page, int $page_size, $params = []): array
{ {
$filed = ['article.id', 'article.class_id', 'article.title', 'article.image', 'article.abstract', 'article.updated_at', 'article_class.class_name', 'article.status']; $filed = ['article.id', 'article.class_id', 'article.title', 'article.image', 'article.abstract', 'article.updated_at', 'article_class.class_name', 'article.status'];
$countSqlObj = Article::select(); $model = Article::select($filed)
$rowsSqlObj = Article::select($filed)
->leftJoin('article_class', 'article_class.id', '=', 'article.class_id'); ->leftJoin('article_class', 'article_class.id', '=', 'article.class_id');
$countSqlObj->where('article.user_id', $user_id); $model->where('article.user_id', $user_id);
$rowsSqlObj->where('article.user_id', $user_id);
if ($params['find_type'] == 3) { if ($params['find_type'] == 3) {
$countSqlObj->where('article.class_id', $params['class_id']); $model->where('article.class_id', $params['class_id']);
$rowsSqlObj->where('article.class_id', $params['class_id']);
} else if ($params['find_type'] == 4) { } else if ($params['find_type'] == 4) {
$countSqlObj->whereRaw("FIND_IN_SET({$params['class_id']},tags_id)"); $model->whereRaw("FIND_IN_SET({$params['class_id']},tags_id)");
$rowsSqlObj->whereRaw("FIND_IN_SET({$params['class_id']},tags_id)");
} else if ($params['find_type'] == 2) { } else if ($params['find_type'] == 2) {
$countSqlObj->where('article.is_asterisk', 1); $model->where('article.is_asterisk', 1);
$rowsSqlObj->where('article.is_asterisk', 1);
} }
$countSqlObj->where('article.status', $params['find_type'] == 5 ? 2 : 1); $model->where('article.status', $params['find_type'] == 5 ? 2 : 1);
$rowsSqlObj->where('article.status', $params['find_type'] == 5 ? 2 : 1);
if (isset($params['keyword'])) { if (isset($params['keyword']) && !empty($params['keyword'])) {
$countSqlObj->where('article.title', 'like', "%{$params['keyword']}%"); $model->where('article.title', 'like', "%{$params['keyword']}%");
$rowsSqlObj->where('article.title', 'like', "%{$params['keyword']}%");
} }
$count = $countSqlObj->count(); $count = $model->count();
$rows = []; $rows = [];
if ($count > 0) { if ($count > 0) {
if ($params['find_type'] == 1) { if ($params['find_type'] == 1) {
$rowsSqlObj->orderBy('updated_at', 'desc'); $model->orderBy('updated_at', 'desc');
$page_size = 20; $page_size = 20;
} else { } else {
$rowsSqlObj->orderBy('id', 'desc'); $model->orderBy('id', 'desc');
} }
$rows = $rowsSqlObj->forPage($page, $page_size)->get()->toArray(); $rows = $model->forPage($page, $page_size)->get()->toArray();
} }
return $this->getPagingRows($rows, $count, $page, $page_size); return $this->getPagingRows($rows, $count, $page, $page_size);
@ -121,18 +113,15 @@ class ArticleService extends BaseService
* @param int $user_id 用户ID * @param int $user_id 用户ID
* @return array * @return array
*/ */
public function getArticleDetail(int $article_id, $user_id = 0) public function getArticleDetail(int $article_id, $user_id = 0): array
{ {
$info = Article::where('id', $article_id)->where('user_id', $user_id)->first(['id', 'class_id', 'tags_id', 'title', 'status', 'is_asterisk', 'created_at', 'updated_at']); $info = Article::where('id', $article_id)->where('user_id', $user_id)->first(['id', 'class_id', 'tags_id', 'title', 'status', 'is_asterisk', 'created_at', 'updated_at']);
if (!$info) {
return []; if (!$info) return [];
}
// 关联文章详情 // 关联文章详情
$detail = $info->detail; $detail = $info->detail;
if (!$detail) { if (!$detail) return [];
return [];
}
$tags = []; $tags = [];
if ($info->tags_id) { if ($info->tags_id) {
@ -159,9 +148,9 @@ class ArticleService extends BaseService
* *
* @param int $user_id 用户ID * @param int $user_id 用户ID
* @param int $article_id 笔记ID * @param int $article_id 笔记ID
* @return mixed * @return array
*/ */
public function findArticleAnnexAll(int $user_id, int $article_id) public function findArticleAnnexAll(int $user_id, int $article_id): array
{ {
return ArticleAnnex::where([ return ArticleAnnex::where([
['user_id', '=', $user_id], ['user_id', '=', $user_id],
@ -220,7 +209,7 @@ class ArticleService extends BaseService
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function delArticleClass(int $user_id, int $class_id) public function delArticleClass(int $user_id, int $class_id): bool
{ {
$result = ArticleClass::where('id', $class_id)->where('user_id', $user_id)->first(['id', 'sort']); $result = ArticleClass::where('id', $class_id)->where('user_id', $user_id)->first(['id', 'sort']);
if (!$result) return false; if (!$result) return false;
@ -244,7 +233,7 @@ class ArticleService extends BaseService
* @param int $sort_type 排序方式 * @param int $sort_type 排序方式
* @return bool * @return bool
*/ */
public function articleClassSort(int $user_id, int $class_id, int $sort_type) public function articleClassSort(int $user_id, int $class_id, int $sort_type): bool
{ {
if (!$info = ArticleClass::select(['id', 'sort'])->where('id', $class_id)->where('user_id', $user_id)->first()) { if (!$info = ArticleClass::select(['id', 'sort'])->where('id', $class_id)->where('user_id', $user_id)->first()) {
return false; return false;
@ -308,7 +297,7 @@ class ArticleService extends BaseService
* @param int $to_class_id 笔记分类ID * @param int $to_class_id 笔记分类ID
* @return bool * @return bool
*/ */
public function mergeArticleClass(int $user_id, int $class_id, int $to_class_id) public function mergeArticleClass(int $user_id, int $class_id, int $to_class_id): bool
{ {
$count = ArticleClass::whereIn('id', [$class_id, $to_class_id])->where('user_id', $user_id)->count(); $count = ArticleClass::whereIn('id', [$class_id, $to_class_id])->where('user_id', $user_id)->count();
if ($count < 2) { if ($count < 2) {
@ -316,7 +305,8 @@ class ArticleService extends BaseService
} }
return (bool)Article::where('class_id', $class_id)->where('user_id', $user_id)->update([ return (bool)Article::where('class_id', $class_id)->where('user_id', $user_id)->update([
'class_id' => $to_class_id 'class_id' => $to_class_id,
'updated_at' => date('Y-m-d H:i:s')
]); ]);
} }
@ -357,7 +347,7 @@ class ArticleService extends BaseService
* @param int $tag_id 标签ID * @param int $tag_id 标签ID
* @return bool * @return bool
*/ */
public function delArticleTags(int $uid, int $tag_id) public function delArticleTags(int $uid, int $tag_id): bool
{ {
if (!ArticleTag::where('id', $tag_id)->where('user_id', $uid)->exists()) { if (!ArticleTag::where('id', $tag_id)->where('user_id', $uid)->exists()) {
return false; return false;
@ -446,14 +436,18 @@ class ArticleService extends BaseService
* @param int $status 笔记状态 1:正常 2:已删除 * @param int $status 笔记状态 1:正常 2:已删除
* @return bool * @return bool
*/ */
public function updateArticleStatus(int $user_id, int $article_id, int $status) public function updateArticleStatus(int $user_id, int $article_id, int $status): bool
{ {
$data = ['status' => $status]; $data = [
'status' => $status,
'updated_at' => date('Y-m-d H:i:s')
];
if ($status == 2) { if ($status == 2) {
$data['deleted_at'] = date('Y-m-d H:i:s'); $data['deleted_at'] = date('Y-m-d H:i:s');
} }
return Article::where('id', $article_id)->where('user_id', $user_id)->update($data); return (bool)Article::where('id', $article_id)->where('user_id', $user_id)->update($data);
} }
/** /**
@ -464,9 +458,12 @@ class ArticleService extends BaseService
* @param int $class_id 笔记分类ID * @param int $class_id 笔记分类ID
* @return bool * @return bool
*/ */
public function moveArticle(int $user_id, int $article_id, int $class_id) public function moveArticle(int $user_id, int $article_id, int $class_id): bool
{ {
return (bool)Article::where('id', $article_id)->where('user_id', $user_id)->update(['class_id' => $class_id]); return (bool)Article::where('id', $article_id)->where('user_id', $user_id)->update([
'class_id' => $class_id,
'updated_at' => date('Y-m-d H:i:s')
]);
} }
/** /**
@ -477,10 +474,11 @@ class ArticleService extends BaseService
* @param int $type 1:标记星号 2:取消星号标记 * @param int $type 1:标记星号 2:取消星号标记
* @return bool * @return bool
*/ */
public function setAsteriskArticle(int $user_id, int $article_id, int $type) public function setAsteriskArticle(int $user_id, int $article_id, int $type): bool
{ {
return (bool)Article::where('id', $article_id)->where('user_id', $user_id)->update([ return (bool)Article::where('id', $article_id)->where('user_id', $user_id)->update([
'is_asterisk' => $type == 1 ? 1 : 0 'is_asterisk' => $type == 1 ? 1 : 0,
'updated_at' => date('Y-m-d H:i:s')
]); ]);
} }
@ -492,9 +490,12 @@ class ArticleService extends BaseService
* @param array $tags 关联标签ID * @param array $tags 关联标签ID
* @return bool * @return bool
*/ */
public function updateArticleTag(int $uid, int $article_id, array $tags) public function updateArticleTag(int $uid, int $article_id, array $tags): bool
{ {
return (bool)Article::where('id', $article_id)->where('user_id', $uid)->update(['tags_id' => implode(',', $tags), 'updated_at' => date('Y-m-d H:i:s')]); return (bool)Article::where('id', $article_id)->where('user_id', $uid)->update([
'tags_id' => implode(',', $tags),
'updated_at' => date('Y-m-d H:i:s')
]);
} }
/** /**
@ -553,7 +554,7 @@ class ArticleService extends BaseService
* @param int $status 附件状态 1:正常 2:已删除 * @param int $status 附件状态 1:正常 2:已删除
* @return bool * @return bool
*/ */
public function updateArticleAnnexStatus(int $user_id, int $annex_id, int $status) public function updateArticleAnnexStatus(int $user_id, int $annex_id, int $status): bool
{ {
$data = ['status' => $status]; $data = ['status' => $status];
if ($status == 2) { if ($status == 2) {
@ -569,7 +570,7 @@ class ArticleService extends BaseService
* @param int $uid 用户ID * @param int $uid 用户ID
* @return array * @return array
*/ */
public function recoverAnnexList(int $uid) public function recoverAnnexList(int $uid): array
{ {
return ArticleAnnex::join('article', 'article.id', '=', 'article_annex.article_id') return ArticleAnnex::join('article', 'article.id', '=', 'article_annex.article_id')
->where('article_annex.user_id', $uid) ->where('article_annex.user_id', $uid)
@ -598,11 +599,6 @@ class ArticleService extends BaseService
return false; return false;
} }
// 将文件从磁盘中删除
//if (!Storage::disk('uploads')->delete($info->save_dir)) {
// return false;
//}
return $info->delete(); return $info->delete();
} }
@ -612,7 +608,7 @@ class ArticleService extends BaseService
* @param int $user_id 用户id * @param int $user_id 用户id
* @param int $article_id 笔记ID * @param int $article_id 笔记ID
* @param array $annex 笔记附件信息 * @param array $annex 笔记附件信息
* @return bool * @return bool|int
*/ */
public function insertArticleAnnex(int $user_id, int $article_id, array $annex) public function insertArticleAnnex(int $user_id, int $article_id, array $annex)
{ {

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
namespace App\Service; namespace App\Service;
@ -25,7 +26,7 @@ class EmoticonService extends BaseService
* @param int $emoticon_id 表情包ID * @param int $emoticon_id 表情包ID
* @return bool * @return bool
*/ */
public function installSysEmoticon(int $user_id, int $emoticon_id) public function installSysEmoticon(int $user_id, int $emoticon_id): bool
{ {
$info = UsersEmoticon::select(['id', 'user_id', 'emoticon_ids'])->where('user_id', $user_id)->first(); $info = UsersEmoticon::select(['id', 'user_id', 'emoticon_ids'])->where('user_id', $user_id)->first();
if (!$info) { if (!$info) {
@ -51,7 +52,7 @@ class EmoticonService extends BaseService
* @param int $emoticon_id 表情包ID * @param int $emoticon_id 表情包ID
* @return bool * @return bool
*/ */
public function removeSysEmoticon(int $user_id, int $emoticon_id) public function removeSysEmoticon(int $user_id, int $emoticon_id): bool
{ {
$info = UsersEmoticon::select(['id', 'user_id', 'emoticon_ids'])->where('user_id', $user_id)->first(); $info = UsersEmoticon::select(['id', 'user_id', 'emoticon_ids'])->where('user_id', $user_id)->first();
if (!$info || !in_array($emoticon_id, $info->emoticon_ids)) { if (!$info || !in_array($emoticon_id, $info->emoticon_ids)) {
@ -78,9 +79,10 @@ class EmoticonService extends BaseService
* @param int $user_id 用户ID * @param int $user_id 用户ID
* @return array * @return array
*/ */
public function getInstallIds(int $user_id) public function getInstallIds(int $user_id): array
{ {
$result = UsersEmoticon::where('user_id', $user_id)->value('emoticon_ids'); $result = UsersEmoticon::where('user_id', $user_id)->value('emoticon_ids');
return $result ? array_filter($result) : []; return $result ? array_filter($result) : [];
} }
@ -91,7 +93,7 @@ class EmoticonService extends BaseService
* @param int $record_id 聊天消息ID * @param int $record_id 聊天消息ID
* @return array * @return array
*/ */
public function collect(int $user_id, int $record_id) public function collect(int $user_id, int $record_id): array
{ {
$result = TalkRecords::where([ $result = TalkRecords::where([
['id', '=', $record_id], ['id', '=', $record_id],
@ -153,7 +155,7 @@ class EmoticonService extends BaseService
* @param array $where * @param array $where
* @return array * @return array
*/ */
public function getDetailsAll(array $where = []) public function getDetailsAll(array $where = []): array
{ {
$items = EmoticonItem::where($where)->get(['id as media_id', 'url as src'])->toArray(); $items = EmoticonItem::where($where)->get(['id as media_id', 'url as src'])->toArray();
foreach ($items as $k => $item) { foreach ($items as $k => $item) {

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
namespace App\Service; namespace App\Service;
@ -27,7 +28,7 @@ class MailerService
* @param string $template 对应邮件模板 * @param string $template 对应邮件模板
* @return bool * @return bool
*/ */
public function send(string $email, string $subject, string $template) public function send(string $email, string $subject, string $template): bool
{ {
if ($this->queueSwitch) { if ($this->queueSwitch) {
@ -44,7 +45,7 @@ class MailerService
* @param string $template 对应邮件模板 * @param string $template 对应邮件模板
* @return bool * @return bool
*/ */
public function realSend(string $email, string $subject, string $template) public function realSend(string $email, string $subject, string $template): bool
{ {
try { try {
return $this->mail($email, $subject, $template); return $this->mail($email, $subject, $template);
@ -67,10 +68,10 @@ class MailerService
$config = config('mail'); $config = config('mail');
$mail = new PHPMailer(); $mail = new PHPMailer();
$mail->CharSet = 'UTF-8'; $mail->CharSet = 'UTF-8';
$mail->IsSMTP(); // 设定使用SMTP服务 $mail->IsSMTP(); // 设定使用SMTP服务
$mail->SMTPDebug = 0; // 关闭SMTP调试功能 $mail->SMTPDebug = 0; // 关闭SMTP调试功能
$mail->SMTPAuth = true; // 启用 SMTP 验证功能 $mail->SMTPAuth = true; // 启用 SMTP 验证功能
$mail->SMTPSecure = 'ssl'; // 使用安全协议 $mail->SMTPSecure = 'ssl'; // 使用安全协议
$mail->Host = $config['host']; $mail->Host = $config['host'];
$mail->Port = $config['port']; $mail->Port = $config['port'];
$mail->Username = $config['username']; $mail->Username = $config['username'];

View File

@ -181,7 +181,7 @@ class FormatMessageService
]; ];
if ($rows[$k]['invite']['type'] == 1 || $rows[$k]['invite']['type'] == 3) { if ($rows[$k]['invite']['type'] == 1 || $rows[$k]['invite']['type'] == 3) {
$rows[$k]['invite']['users'] = User::select('id', 'nickname')->whereIn('id', parse_ids($invites[$row['id']]['user_ids']))->get()->toArray(); $rows[$k]['invite']['users'] = User::select(['id', 'nickname'])->whereIn('id', parse_ids($invites[$row['id']]['user_ids']))->get()->toArray();
} else { } else {
$rows[$k]['invite']['users'] = $rows[$k]['invite']['operate_user']; $rows[$k]['invite']['users'] = $rows[$k]['invite']['operate_user'];
} }

View File

@ -10,7 +10,7 @@ use PHPMailer\PHPMailer\PHPMailer;
* *
* @package App\Support * @package App\Support
*/ */
class Mail class Mailer
{ {
/** /**
* @param string $address * @param string $address

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
namespace App\Support; namespace App\Support;

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
namespace App\Support; namespace App\Support;
@ -16,7 +17,7 @@ class UserRelation
* @param int $talk_type 对话类型 * @param int $talk_type 对话类型
* @return bool * @return bool
*/ */
public static function isFriendOrGroupMember(int $user_id, int $receiver_id, int $talk_type) public static function isFriendOrGroupMember(int $user_id, int $receiver_id, int $talk_type): bool
{ {
if ($talk_type == TalkModeConstant::PRIVATE_CHAT) { if ($talk_type == TalkModeConstant::PRIVATE_CHAT) {
return di()->get(UserFriendService::class)->isFriend($user_id, $receiver_id, true); return di()->get(UserFriendService::class)->isFriend($user_id, $receiver_id, true);

View File

@ -132,11 +132,11 @@ function response()
/** /**
* 获取邮件助手 * 获取邮件助手
* *
* @return \App\Support\Mail|mixed * @return \App\Support\Mailer|mixed
*/ */
function email() function email()
{ {
return di()->get(App\Support\Mail::class); return di()->get(App\Support\Mailer::class);
} }
/** /**

1
doc/README.md Normal file
View File

@ -0,0 +1 @@
# 文档说明

View File