From 0aa07da41b722645759fffd302abedff908e6287 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Sun, 23 Jun 2024 08:16:20 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=95=B4=E7=90=86=E6=96=87=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E5=8E=BB=E9=99=A4=E6=97=A0=E7=94=A8=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Caches/ModerationStat.php | 44 ------------------------------ app/Console/Tasks/MaintainTask.php | 32 ++-------------------- app/Validators/Account.php | 2 +- app/Validators/Category.php | 2 +- app/Validators/Comment.php | 13 --------- app/Validators/CourseUser.php | 13 --------- app/Validators/Nav.php | 2 +- config/errors.php | 1 - 8 files changed, 5 insertions(+), 104 deletions(-) delete mode 100644 app/Caches/ModerationStat.php diff --git a/app/Caches/ModerationStat.php b/app/Caches/ModerationStat.php deleted file mode 100644 index 55fa8112..00000000 --- a/app/Caches/ModerationStat.php +++ /dev/null @@ -1,44 +0,0 @@ -lifetime; - } - - public function getKey($id = null) - { - return 'moderation_stat'; - } - - public function getContent($id = null) - { - $statRepo = new StatRepo(); - - $articleCount = $statRepo->countPendingArticles(); - $questionCount = $statRepo->countPendingQuestions(); - $answerCount = $statRepo->countPendingAnswers(); - $commentCount = $statRepo->countPendingComments(); - - return [ - 'article_count' => $articleCount, - 'question_count' => $questionCount, - 'answer_count' => $answerCount, - 'comment_count' => $commentCount, - ]; - } - -} diff --git a/app/Console/Tasks/MaintainTask.php b/app/Console/Tasks/MaintainTask.php index dc67e75f..1c767d06 100644 --- a/app/Console/Tasks/MaintainTask.php +++ b/app/Console/Tasks/MaintainTask.php @@ -10,7 +10,7 @@ namespace App\Console\Tasks; use App\Http\Admin\Services\Setting as SettingService; use App\Library\Utils\Password as PasswordUtil; use App\Models\ChapterVod as ChapterVodModel; -use App\Services\Utils\IndexCourseCache as IndexCourseCacheUtil; +use App\Services\Utils\IndexPageCache as IndexPageCacheUtil; use App\Validators\Account as AccountValidator; class MaintainTask extends Task @@ -26,7 +26,7 @@ class MaintainTask extends Task { $section = $params[0] ?? null; - $util = new IndexCourseCacheUtil(); + $util = new IndexPageCacheUtil(); $util->rebuild($section); @@ -64,34 +64,6 @@ class MaintainTask extends Task echo '------ reset password success ------' . PHP_EOL; } - /** - * 关闭验证码 - * - * @command: php console.php maintain disable_captcha - */ - public function disableCaptchaAction() - { - $service = new SettingService(); - - $service->updateSettings('captcha', ['enabled' => 0]); - - echo '------ disable captcha success ------' . PHP_EOL; - } - - /** - * 启用验证码 - * - * @command: php console.php maintain enable_captcha - */ - public function enableCaptchaAction() - { - $service = new SettingService(); - - $service->updateSettings('captcha', ['enabled' => 1]); - - echo '------ enable captcha success ------' . PHP_EOL; - } - /** * 关闭站点 * diff --git a/app/Validators/Account.php b/app/Validators/Account.php index 7daec3fd..4725f2c2 100644 --- a/app/Validators/Account.php +++ b/app/Validators/Account.php @@ -39,7 +39,7 @@ class Account extends Validator $account = $accountRepo->findById($name); } - if (!$account || $account->deleted == 1) { + if (!$account) { throw new BadRequestException('account.not_found'); } diff --git a/app/Validators/Category.php b/app/Validators/Category.php index d2caebe0..662844aa 100644 --- a/app/Validators/Category.php +++ b/app/Validators/Category.php @@ -137,7 +137,7 @@ class Category extends Validator return $status; } - public function checkDeleteAbility($category) + public function checkDeleteAbility(CategoryModel $category) { $categoryRepo = new CategoryRepo(); diff --git a/app/Validators/Comment.php b/app/Validators/Comment.php index 0026cbb9..8d353ebf 100644 --- a/app/Validators/Comment.php +++ b/app/Validators/Comment.php @@ -42,19 +42,6 @@ class Comment extends Validator return $comment; } - public function checkToUser($userId) - { - $userRepo = new UserRepo(); - - $user = $userRepo->findById($userId); - - if (!$user) { - throw new BadRequestException('comment.to_user_not_found'); - } - - return $user; - } - public function checkItem($itemId, $itemType) { if (!array_key_exists($itemType, CommentModel::itemTypes())) { diff --git a/app/Validators/CourseUser.php b/app/Validators/CourseUser.php index 1642733e..53a6a97f 100644 --- a/app/Validators/CourseUser.php +++ b/app/Validators/CourseUser.php @@ -15,19 +15,6 @@ use App\Repos\CourseUser as CourseUserRepo; class CourseUser extends Validator { - public function checkRelation($id) - { - $courseUserRepo = new CourseUserRepo(); - - $courseUser = $courseUserRepo->findById($id); - - if (!$courseUser) { - throw new BadRequestException('course_user.not_found'); - } - - return $courseUser; - } - public function checkCourseUser($courseId, $userId) { $repo = new CourseUserRepo(); diff --git a/app/Validators/Nav.php b/app/Validators/Nav.php index bea1b5cc..1d34ad3f 100644 --- a/app/Validators/Nav.php +++ b/app/Validators/Nav.php @@ -116,7 +116,7 @@ class Nav extends Validator return $status; } - public function checkDeleteAbility($nav) + public function checkDeleteAbility(NavModel $nav) { $navRepo = new NavRepo(); diff --git a/config/errors.php b/config/errors.php index cc7c3359..7b94647c 100644 --- a/config/errors.php +++ b/config/errors.php @@ -160,7 +160,6 @@ $error['answer.delete_not_allowed'] = '当前不允许删除回答'; */ $error['comment.not_found'] = '评论不存在'; $error['comment.parent_not_found'] = '上级评论不存在'; -$error['comment.to_user_not_found'] = '回复用户不存在'; $error['comment.invalid_item_type'] = '无效的条目类型'; $error['comment.invalid_publish_status'] = '无效的发布状态'; $error['comment.invalid_reject_reason'] = '无效的拒绝理由'; From f04c23e90ff931261d6f7c37871d5bd7f5d2f91f Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Fri, 28 Jun 2024 08:26:28 +0800 Subject: [PATCH 2/5] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E8=AF=BE=E7=A8=8B?= =?UTF-8?q?=E5=92=8C=E7=AB=A0=E8=8A=82=E4=BA=8B=E4=BB=B6=E7=9B=91=E5=90=AC?= =?UTF-8?q?=202.=E4=BC=98=E5=8C=96SEO=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Api/Module.php | 4 +- app/Http/Home/Services/Connect.php | 2 + .../Home/Services/WeChatOfficialAccount.php | 2 + app/Library/Seo.php | 14 +++++ app/Listeners/Chapter.php | 51 +++++++++++++++++++ app/Listeners/Course.php | 51 +++++++++++++++++++ app/Services/Logic/Chapter/ChapterInfo.php | 6 ++- app/Services/Logic/Chapter/ChapterLike.php | 4 ++ app/Services/Logic/Course/CourseFavorite.php | 4 ++ app/Services/Logic/Course/CourseInfo.php | 6 ++- 10 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 app/Listeners/Chapter.php create mode 100644 app/Listeners/Course.php diff --git a/app/Http/Api/Module.php b/app/Http/Api/Module.php index 2efcc423..87146a69 100644 --- a/app/Http/Api/Module.php +++ b/app/Http/Api/Module.php @@ -7,7 +7,7 @@ namespace App\Http\Api; -use App\Services\Auth\Api as AppAuth; +use App\Services\Auth\Api as ApiAuth; use Phalcon\DiInterface; use Phalcon\Mvc\ModuleDefinitionInterface; use Phalcon\Mvc\View; @@ -29,7 +29,7 @@ class Module implements ModuleDefinitionInterface }); $dependencyInjector->setShared('auth', function () { - return new AppAuth(); + return new ApiAuth(); }); } diff --git a/app/Http/Home/Services/Connect.php b/app/Http/Home/Services/Connect.php index 3345c6b0..0d96ddaf 100644 --- a/app/Http/Home/Services/Connect.php +++ b/app/Http/Home/Services/Connect.php @@ -70,6 +70,8 @@ class Connect extends Service $auth = $this->getAppAuth(); $auth->saveAuthInfo($user); + + $this->eventsManager->fire('Account:afterRegister', $this, $user); } public function bindUser(array $openUser) diff --git a/app/Http/Home/Services/WeChatOfficialAccount.php b/app/Http/Home/Services/WeChatOfficialAccount.php index e12ed1a4..1126665d 100644 --- a/app/Http/Home/Services/WeChatOfficialAccount.php +++ b/app/Http/Home/Services/WeChatOfficialAccount.php @@ -103,6 +103,8 @@ class WeChatOfficialAccount extends Service $auth = $this->getAppAuth(); $auth->saveAuthInfo($user); + + $this->eventsManager->fire('Account:afterRegister', $this, $user); } protected function getAppAuth() diff --git a/app/Library/Seo.php b/app/Library/Seo.php index 9976b433..88f617f7 100644 --- a/app/Library/Seo.php +++ b/app/Library/Seo.php @@ -30,6 +30,11 @@ class Seo */ protected $titleSeparator = ' - '; + /** + * @var string 关键字分隔符 + */ + protected $keywordSeparator = ','; + public function setTitle($title) { $this->title = $title; @@ -37,6 +42,10 @@ class Seo public function setKeywords($keywords) { + if (is_array($keywords)) { + $keywords = implode($this->keywordSeparator, $keywords); + } + $this->keywords = $keywords; } @@ -84,4 +93,9 @@ class Seo return $this->titleSeparator; } + public function getKeywordSeparator() + { + return $this->keywordSeparator; + } + } diff --git a/app/Listeners/Chapter.php b/app/Listeners/Chapter.php new file mode 100644 index 00000000..291916f3 --- /dev/null +++ b/app/Listeners/Chapter.php @@ -0,0 +1,51 @@ +setChapterUser($chapter, $user); $this->handleChapterUser($chapter, $user); - return $this->handleChapter($chapter, $user); + $result = $this->handleChapter($chapter, $user); + + $this->eventsManager->fire('Chapter:afterView', $this, $chapter); + + return $result; } protected function handleChapter(ChapterModel $chapter, UserModel $user) diff --git a/app/Services/Logic/Chapter/ChapterLike.php b/app/Services/Logic/Chapter/ChapterLike.php index d9a97774..c4aa84c5 100644 --- a/app/Services/Logic/Chapter/ChapterLike.php +++ b/app/Services/Logic/Chapter/ChapterLike.php @@ -58,11 +58,15 @@ class ChapterLike extends LogicService $this->incrChapterLikeCount($chapter); + $this->eventsManager->fire('Chapter:afterLike', $this, $chapter); + } else { $action = 'undo'; $this->decrChapterLikeCount($chapter); + + $this->eventsManager->fire('Chapter:afterUndoLike', $this, $chapter); } return [ diff --git a/app/Services/Logic/Course/CourseFavorite.php b/app/Services/Logic/Course/CourseFavorite.php index 493a7a5c..021233cf 100644 --- a/app/Services/Logic/Course/CourseFavorite.php +++ b/app/Services/Logic/Course/CourseFavorite.php @@ -57,12 +57,16 @@ class CourseFavorite extends LogicService $this->incrCourseFavoriteCount($course); $this->incrUserFavoriteCount($user); + $this->eventsManager->fire('Course:afterFavorite', $this, $course); + } else { $action = 'undo'; $this->decrCourseFavoriteCount($course); $this->decrUserFavoriteCount($user); + + $this->eventsManager->fire('Course:afterUndoFavorite', $this, $course); } return [ diff --git a/app/Services/Logic/Course/CourseInfo.php b/app/Services/Logic/Course/CourseInfo.php index 45245f27..518755fd 100644 --- a/app/Services/Logic/Course/CourseInfo.php +++ b/app/Services/Logic/Course/CourseInfo.php @@ -27,7 +27,11 @@ class CourseInfo extends LogicService $this->setCourseUser($course, $user); - return $this->handleCourse($course, $user); + $result = $this->handleCourse($course, $user); + + $this->eventsManager->fire('Course:afterView', $this, $course); + + return $result; } protected function handleCourse(CourseModel $course, UserModel $user) From 703d5cbc3dae05d066d0a8188fac38dbccab6980 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Fri, 28 Jun 2024 20:41:45 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E7=9F=AD=E4=BF=A1=E5=8F=91=E9=80=81?= =?UTF-8?q?=E5=A4=84=E7=90=86=E4=B8=AD=E5=A2=9E=E5=8A=A0=E6=8F=8F=E8=BF=B0?= =?UTF-8?q?=E5=AF=B9=E7=85=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/Logic/Notice/External/ConsultReply.php | 2 +- app/Services/Logic/Notice/External/OrderFinish.php | 2 +- app/Services/Logic/Notice/External/PointGoodsDeliver.php | 2 +- app/Services/Logic/Notice/External/RefundFinish.php | 2 +- app/Services/Logic/Notice/External/Sms/ConsultReply.php | 3 +++ app/Services/Logic/Notice/External/Sms/GoodsDeliver.php | 3 +++ app/Services/Logic/Notice/External/Sms/LiveBegin.php | 3 +++ app/Services/Logic/Notice/External/Sms/OrderFinish.php | 3 +++ app/Services/Logic/Notice/External/Sms/RefundFinish.php | 3 +++ app/Services/Logic/Notice/External/Sms/Verify.php | 3 +++ 10 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/Services/Logic/Notice/External/ConsultReply.php b/app/Services/Logic/Notice/External/ConsultReply.php index 991d4920..4df0f7ef 100644 --- a/app/Services/Logic/Notice/External/ConsultReply.php +++ b/app/Services/Logic/Notice/External/ConsultReply.php @@ -24,7 +24,7 @@ class ConsultReply extends LogicService $wechatNoticeEnabled = $this->wechatNoticeEnabled(); $smsNoticeEnabled = $this->smsNoticeEnabled(); - $consultId = $task->item_info['consult']['id']; + $consultId = $task->item_id; $consultRepo = new ConsultRepo(); diff --git a/app/Services/Logic/Notice/External/OrderFinish.php b/app/Services/Logic/Notice/External/OrderFinish.php index 789138d0..566b3995 100644 --- a/app/Services/Logic/Notice/External/OrderFinish.php +++ b/app/Services/Logic/Notice/External/OrderFinish.php @@ -23,7 +23,7 @@ class OrderFinish extends LogicService $wechatNoticeEnabled = $this->wechatNoticeEnabled(); $smsNoticeEnabled = $this->smsNoticeEnabled(); - $orderId = $task->item_info['order']['id']; + $orderId = $task->item_id; $orderRepo = new OrderRepo(); diff --git a/app/Services/Logic/Notice/External/PointGoodsDeliver.php b/app/Services/Logic/Notice/External/PointGoodsDeliver.php index f299cd21..ab104179 100644 --- a/app/Services/Logic/Notice/External/PointGoodsDeliver.php +++ b/app/Services/Logic/Notice/External/PointGoodsDeliver.php @@ -23,7 +23,7 @@ class PointGoodsDeliver extends LogicService $wechatNoticeEnabled = $this->wechatNoticeEnabled(); $smsNoticeEnabled = $this->smsNoticeEnabled(); - $redeemId = $task->item_info['redeem']['id']; + $redeemId = $task->item_id; $redeemRepo = new PointGiftRedeemRepo(); diff --git a/app/Services/Logic/Notice/External/RefundFinish.php b/app/Services/Logic/Notice/External/RefundFinish.php index 104c4720..ac9b8a21 100644 --- a/app/Services/Logic/Notice/External/RefundFinish.php +++ b/app/Services/Logic/Notice/External/RefundFinish.php @@ -23,7 +23,7 @@ class RefundFinish extends LogicService $wechatNoticeEnabled = $this->wechatNoticeEnabled(); $smsNoticeEnabled = $this->smsNoticeEnabled(); - $refundId = $task->item_info['refund']['id']; + $refundId = $task->item_id; $refundRepo = new RefundRepo(); diff --git a/app/Services/Logic/Notice/External/Sms/ConsultReply.php b/app/Services/Logic/Notice/External/Sms/ConsultReply.php index ab75219c..b82aa2b3 100644 --- a/app/Services/Logic/Notice/External/Sms/ConsultReply.php +++ b/app/Services/Logic/Notice/External/Sms/ConsultReply.php @@ -29,6 +29,9 @@ class ConsultReply extends Smser $templateId = $this->getTemplateId($this->templateCode); + /** + * {1} 回复了你的咨询,课程名称:{2},请登录系统查看详情。 + */ $params = [ $params['replier']['name'], $params['course']['title'], diff --git a/app/Services/Logic/Notice/External/Sms/GoodsDeliver.php b/app/Services/Logic/Notice/External/Sms/GoodsDeliver.php index a83e9fc8..d4a8557e 100644 --- a/app/Services/Logic/Notice/External/Sms/GoodsDeliver.php +++ b/app/Services/Logic/Notice/External/Sms/GoodsDeliver.php @@ -31,6 +31,9 @@ class GoodsDeliver extends Smser $params['deliver_time'] = date('Y-m-d H:i', $params['deliver_time']); + /** + * 发货成功,商品名称:{1},订单序号:{2},发货时间:{3},请注意查收。 + */ $params = [ $params['goods_name'], $params['order_sn'], diff --git a/app/Services/Logic/Notice/External/Sms/LiveBegin.php b/app/Services/Logic/Notice/External/Sms/LiveBegin.php index fef22b23..2b427428 100644 --- a/app/Services/Logic/Notice/External/Sms/LiveBegin.php +++ b/app/Services/Logic/Notice/External/Sms/LiveBegin.php @@ -29,6 +29,9 @@ class LiveBegin extends Smser $params['live']['start_time'] = date('H:i', $params['live']['start_time']); + /** + * 直播预告,课程名称:{1},章节名称:{2},开播时间:{3} + */ $params = [ $params['course']['title'], $params['chapter']['title'], diff --git a/app/Services/Logic/Notice/External/Sms/OrderFinish.php b/app/Services/Logic/Notice/External/Sms/OrderFinish.php index 881e1c76..324df5f6 100644 --- a/app/Services/Logic/Notice/External/Sms/OrderFinish.php +++ b/app/Services/Logic/Notice/External/Sms/OrderFinish.php @@ -29,6 +29,9 @@ class OrderFinish extends Smser $templateId = $this->getTemplateId($this->templateCode); + /** + * 下单成功,商品名称:{1},订单序号:{2},订单金额:{3}元 + */ $params = [ $params['order']['subject'], $params['order']['sn'], diff --git a/app/Services/Logic/Notice/External/Sms/RefundFinish.php b/app/Services/Logic/Notice/External/Sms/RefundFinish.php index a059a623..bfae0093 100644 --- a/app/Services/Logic/Notice/External/Sms/RefundFinish.php +++ b/app/Services/Logic/Notice/External/Sms/RefundFinish.php @@ -29,6 +29,9 @@ class RefundFinish extends Smser $templateId = $this->getTemplateId($this->templateCode); + /** + * 退款成功,商品名称:{1},退款序号:{2},退款金额:{3}元 + */ $params = [ $params['refund']['subject'], $params['refund']['sn'], diff --git a/app/Services/Logic/Notice/External/Sms/Verify.php b/app/Services/Logic/Notice/External/Sms/Verify.php index a4c983ef..b852f4b6 100644 --- a/app/Services/Logic/Notice/External/Sms/Verify.php +++ b/app/Services/Logic/Notice/External/Sms/Verify.php @@ -29,6 +29,9 @@ class Verify extends SmserService $templateId = $this->getTemplateId($this->templateCode); + /** + * 验证码:{1},{2} 分钟内有效,如非本人操作请忽略。 + */ $params = [$code, $minutes]; return $this->send($phone, $templateId, $params); From f0470c027d207650b48dcfe579ff3526bec04893 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Sat, 29 Jun 2024 21:26:53 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=82=AE=E4=BB=B6?= =?UTF-8?q?=E5=8F=91=E9=80=81=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Logic/Notice/External/Mail/Test.php | 26 ++--------- .../Logic/Notice/External/Mail/Verify.php | 42 ++++-------------- app/Services/Mailer.php | 43 +++++++++++++++++++ 3 files changed, 56 insertions(+), 55 deletions(-) diff --git a/app/Services/Logic/Notice/External/Mail/Test.php b/app/Services/Logic/Notice/External/Mail/Test.php index 313a5e55..08b57e48 100644 --- a/app/Services/Logic/Notice/External/Mail/Test.php +++ b/app/Services/Logic/Notice/External/Mail/Test.php @@ -14,32 +14,14 @@ class Test extends Mailer /** * @param string $email - * @return bool + * @return bool|null */ public function handle($email) { - try { + $subject = $this->formatSubject('测试邮件'); + $content = $this->formatContent('东风快递,使命必达'); - $message = $this->manager->createMessage(); - - $count = $message->to($email) - ->subject('测试邮件') - ->content('东风快递,使命必达') - ->send(); - - $result = $count > 0; - - } catch (\Exception $e) { - - $this->logger->error('Send Test Mail Exception ' . kg_json_encode([ - 'code' => $e->getCode(), - 'message' => $e->getMessage(), - ])); - - $result = false; - } - - return $result; + return $this->send($email, $subject, $content); } } diff --git a/app/Services/Logic/Notice/External/Mail/Verify.php b/app/Services/Logic/Notice/External/Mail/Verify.php index b760afaa..0f0b4ace 100644 --- a/app/Services/Logic/Notice/External/Mail/Verify.php +++ b/app/Services/Logic/Notice/External/Mail/Verify.php @@ -15,47 +15,23 @@ class Verify extends MailerService /** * @param string $email - * @return bool + * @return bool|null */ public function handle($email) { - try { + $minutes = 5; - $message = $this->manager->createMessage(); + $verify = new VerifyService(); - $verify = new VerifyService(); + $code = $verify->getMailCode($email, 60 * $minutes); - $minutes = 5; + $subject = '邮件验证码'; + $content = sprintf('验证码:%s,%s 分钟内有效,如非本人操作请忽略。', $code, $minutes); - $code = $verify->getMailCode($email, 60 * $minutes); + $subject = $this->formatSubject($subject); + $content = $this->formatSubject($content); - $subject = '邮件验证码'; - - $content = $this->formatContent($code, $minutes); - - $count = $message->to($email) - ->subject($subject) - ->content($content) - ->send(); - - $result = $count > 0; - - } catch (\Exception $e) { - - $this->logger->error('Send Verify Mail Exception ' . kg_json_encode([ - 'code' => $e->getCode(), - 'message' => $e->getMessage(), - ])); - - $result = false; - } - - return $result; - } - - protected function formatContent($code, $minutes) - { - return sprintf('验证码:%s,%s 分钟内有效,如非本人操作请忽略。', $code, $minutes); + return $this->send($email, $subject, $content); } } diff --git a/app/Services/Mailer.php b/app/Services/Mailer.php index 1dd9853c..9c7a2499 100644 --- a/app/Services/Mailer.php +++ b/app/Services/Mailer.php @@ -30,6 +30,49 @@ abstract class Mailer extends Service $this->logger = $this->getLogger('mail'); } + public function send($email, $subject, $content, $attachment = null) + { + try { + + $message = $this->manager->createMessage(); + + $message->to($email); + $message->subject($subject); + $message->content($content); + + if ($attachment) { + $message->attachment($attachment); + } + + $count = $message->send(); + + $result = $count > 0; + + } catch (\Exception $e) { + + $this->logger->error('Send Mail Exception ' . kg_json_encode([ + 'code' => $e->getCode(), + 'message' => $e->getMessage(), + ])); + + $result = false; + } + + return $result; + } + + protected function formatSubject($subject) + { + $site = $this->getSettings('site'); + + return sprintf('【%s】%s', $site['title'], $subject); + } + + protected function formatContent($content) + { + return $content; + } + /** * 获取 Manager */ From 4a5cc68915052163e0e741badf70378a5a05e86e Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Mon, 1 Jul 2024 07:21:37 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=85=AC=E4=BC=97?= =?UTF-8?q?=E5=8F=B7=E5=85=B3=E6=B3=A8=E8=AE=A2=E9=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Api/Services/Connect.php | 6 ++- app/Http/Home/Services/Connect.php | 4 ++ .../Home/Services/WeChatOfficialAccount.php | 4 ++ .../Home/Views/user/console/account_info.volt | 2 +- app/Repos/Connect.php | 13 +++++ app/Services/Logic/WeChat/OfficialAccount.php | 54 +++++++++++++++++++ 6 files changed, 81 insertions(+), 2 deletions(-) diff --git a/app/Http/Api/Services/Connect.php b/app/Http/Api/Services/Connect.php index 5d334be4..e15e23ce 100644 --- a/app/Http/Api/Services/Connect.php +++ b/app/Http/Api/Services/Connect.php @@ -56,6 +56,10 @@ class Connect extends Service if ($relation) { + $relation->update_time = time(); + + $relation->update(); + $userRepo = new UserRepo(); $user = $userRepo->findById($relation->user_id); @@ -111,7 +115,7 @@ class Connect extends Service case ConnectModel::PROVIDER_WEIBO: $auth = $this->getWeiBoAuth(); break; - case ConnectModel::PROVIDER_WECHAT: + case ConnectModel::PROVIDER_WECHAT_OA: $auth = $this->getWeChatAuth(); break; } diff --git a/app/Http/Home/Services/Connect.php b/app/Http/Home/Services/Connect.php index 0d96ddaf..0cf0b180 100644 --- a/app/Http/Home/Services/Connect.php +++ b/app/Http/Home/Services/Connect.php @@ -91,6 +91,10 @@ class Connect extends Service $validator->checkIfAllowLogin($user); + $connect->update_time = time(); + + $connect->update(); + $this->handleLoginNotice($user); $auth = $this->getAppAuth(); diff --git a/app/Http/Home/Services/WeChatOfficialAccount.php b/app/Http/Home/Services/WeChatOfficialAccount.php index 1126665d..b923ea91 100644 --- a/app/Http/Home/Services/WeChatOfficialAccount.php +++ b/app/Http/Home/Services/WeChatOfficialAccount.php @@ -40,6 +40,10 @@ class WeChatOfficialAccount extends Service $validator->checkIfAllowLogin($user); + $connect->update_time = time(); + + $connect->update(); + $this->handleLoginNotice($user); $auth = $this->getAppAuth(); diff --git a/app/Http/Home/Views/user/console/account_info.volt b/app/Http/Home/Views/user/console/account_info.volt index f7c7d254..55325b13 100644 --- a/app/Http/Home/Views/user/console/account_info.volt +++ b/app/Http/Home/Views/user/console/account_info.volt @@ -98,7 +98,7 @@ 提供方 用户信息 - 更新日期 + 最后登录 操作 {% for connect in connects %} diff --git a/app/Repos/Connect.php b/app/Repos/Connect.php index 82a56b5b..bd687a9c 100644 --- a/app/Repos/Connect.php +++ b/app/Repos/Connect.php @@ -67,6 +67,19 @@ class Connect extends Repository ]); } + /** + * @param string $openId + * @param int $provider + * @return ConnectModel|Model|bool + */ + public function findByOpenIdShallow($openId, $provider) + { + return ConnectModel::findFirst([ + 'conditions' => 'open_id = ?1 AND provider = ?2', + 'bind' => [1 => $openId, 2 => $provider], + ]); + } + /** * @param int $userId * @param int $provider diff --git a/app/Services/Logic/WeChat/OfficialAccount.php b/app/Services/Logic/WeChat/OfficialAccount.php index 5f3c6cd4..6038b560 100644 --- a/app/Services/Logic/WeChat/OfficialAccount.php +++ b/app/Services/Logic/WeChat/OfficialAccount.php @@ -9,6 +9,7 @@ namespace App\Services\Logic\WeChat; use App\Models\Connect as ConnectModel; use App\Repos\Connect as ConnectRepo; +use App\Repos\User as UserRepo; use App\Services\Service as AppService; use App\Services\WeChat as WeChatService; use EasyWeChat\Kernel\Messages\Text as TextMessage; @@ -150,9 +151,56 @@ class OfficialAccount extends AppService protected function handleSubscribeEvent($message) { $openId = $message['FromUserName'] ?? ''; + $eventKey = $message['EventKey'] ?? ''; if (empty($openId)) return null; + $connectRepo = new ConnectRepo(); + + $connect = $connectRepo->findByOpenId($openId, ConnectModel::PROVIDER_WECHAT_OA); + + if ($connect) return null; + + /** + * 尼玛不知道为什么又多了个"qrscene_"前缀,SCAN事件里面又不带这个前缀 + */ + $subscribeScene = sprintf('qrscene_%s', self::QR_SCENE_SUBSCRIBE); + + $userId = 0; + + if (Text::startsWith($eventKey, $subscribeScene)) { + + $userId = str_replace($subscribeScene, '', $eventKey); + + } else { + + $connect = $connectRepo->findByOpenIdShallow($openId, ConnectModel::PROVIDER_WECHAT_OA); + + if ($connect) $userId = $connect->user_id; + } + + if ($userId > 0) { + + $userRepo = new UserRepo(); + + $user = $userRepo->findById($userId); + + if (!$user) return null; + + $userInfo = $this->getUserInfo($openId); + + $unionId = $userInfo['unionid'] ?: ''; + + $connect = new ConnectModel(); + + $connect->user_id = $userId; + $connect->open_id = $openId; + $connect->union_id = $unionId; + $connect->provider = ConnectModel::PROVIDER_WECHAT_OA; + + $connect->create(); + } + return new TextMessage('开心呀,我们又多了一个小伙伴!'); } @@ -218,6 +266,12 @@ class OfficialAccount extends AppService if (empty($userId) || empty($openId)) return null; + $userRepo = new UserRepo(); + + $user = $userRepo->findById($userId); + + if (!$user) return null; + $userInfo = $this->getUserInfo($openId); $unionId = $userInfo['unionid'] ?: '';