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/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 3345c6b0..0cf0b180 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) @@ -89,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 e12ed1a4..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(); @@ -103,6 +107,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/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/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 @@ + 'open_id = ?1 AND provider = ?2', + 'bind' => [1 => $openId, 2 => $provider], + ]); + } + /** * @param int $userId * @param int $provider diff --git a/app/Services/Logic/Chapter/ChapterInfo.php b/app/Services/Logic/Chapter/ChapterInfo.php index e6846397..85209a9e 100644 --- a/app/Services/Logic/Chapter/ChapterInfo.php +++ b/app/Services/Logic/Chapter/ChapterInfo.php @@ -53,7 +53,11 @@ class ChapterInfo extends LogicService $this->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) 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/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/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); 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'] ?: ''; 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 */