소스 검색

feat(message): 添加消息通知功能- 新增消息通知数据库迁移- 创建消息通知实体类、枚举类和模型类
- 实现消息发送钩子和测试用例
- 更新扩展类以注册钩子

runphp 6 달 전
부모
커밋
7179cc4800

+ 44 - 0
database/migrations/20250917093821_notifications_timstamp.php

@@ -0,0 +1,44 @@
+<?php
+
+use think\migration\Migrator;
+use think\migration\db\Column;
+
+class NotificationsTimstamp extends Migrator
+{
+    /**
+     * Change Method.
+     *
+     * Write your reversible migrations using this method.
+     *
+     * More information on writing migrations is available here:
+     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
+     *
+     * The following commands can be used in this method and Phinx will
+     * automatically reverse them when rolling back:
+     *
+     *    createTable
+     *    renameTable
+     *    addColumn
+     *    renameColumn
+     *    addIndex
+     *    addForeignKey
+     *
+     * Remember to call "create()" or "update()" and NOT "save()" when working
+     * with the Table class.
+     */
+    public function up()
+    {
+        $table = $this->table('message_notifications');
+        $table->removeColumn('create_time')
+            ->removeColumn('update_time')
+            ->removeColumn('delete_time')
+            ->update();
+        $table->addTimestamps()
+            ->addSoftDelete()
+            ->update();
+    }
+    public function down()
+    {
+
+    }
+}

+ 14 - 0
src/Entity/MessageNotificationsEntity.php

@@ -0,0 +1,14 @@
+<?php
+declare(strict_types=1);
+namespace SixShop\Message\Entity;
+
+use SixShop\Core\Entity\BaseEntity;
+use SixShop\Message\Model\MessageNotificationsModel;
+
+/**
+ * @mixin MessageNotificationsModel
+ */
+class MessageNotificationsEntity extends BaseEntity
+{
+
+}

+ 24 - 0
src/Enum/NotificationsTypeEnum.php

@@ -0,0 +1,24 @@
+<?php
+declare(strict_types=1);
+namespace SixShop\Message\Enum;
+
+enum NotificationsTypeEnum:int
+{
+    //类型:1-系统通知,2-活动通知,3-订单通知,4-物流通知,5-其他
+    case SYSTEM = 1;
+    case ACTIVITY = 2;
+    case ORDER = 3;
+    case LOGISTICS = 4;
+    case OTHER = 5;
+
+    public function toString(): string
+    {
+        return match ($this) {
+            self::SYSTEM => '系统通知',
+            self::ACTIVITY => '活动通知',
+            self::ORDER => '订单通知',
+            self::LOGISTICS => '物流通知',
+            self::OTHER => '其他通知',
+        };
+    }
+}

+ 8 - 0
src/Extension.php

@@ -4,6 +4,7 @@ declare(strict_types=1);
 namespace SixShop\Message;
 
 use SixShop\Core\ExtensionAbstract;
+use SixShop\Message\Hook\SendNotificationsHook;
 
 class Extension extends ExtensionAbstract
 {
@@ -11,4 +12,11 @@ class Extension extends ExtensionAbstract
     {
         return dirname(__DIR__);
     }
+
+    public function getHooks(): array
+    {
+        return [
+            SendNotificationsHook::class
+        ];
+    }
 }

+ 31 - 0
src/Hook/SendNotificationsHook.php

@@ -0,0 +1,31 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\Message\Hook;
+
+use SixShop\Core\Attribute\Hook;
+use SixShop\Message\Entity\MessageNotificationsEntity;
+use SixShop\Message\Enum\NotificationsTypeEnum;
+
+readonly class SendNotificationsHook
+{
+    public function __construct(private MessageNotificationsEntity $messageNotificationsEntity)
+    {
+    }
+
+    #[Hook('message.send_notifications')]
+    public function handle(array $params): void
+    {
+        $msgList = [];
+        foreach ($params as $msg) {
+            $msgList[] = [
+                'user_id' => $msg['user_id'],
+                'title' => $msg['title'],
+                'content' => $msg['content'],
+                'type' => NotificationsTypeEnum::from((int)$msg['type']),
+            ];
+
+        }
+        $msgList && $this->messageNotificationsEntity->insertAll($msgList);
+    }
+}

+ 19 - 0
src/Model/MessageNotificationsModel.php

@@ -0,0 +1,19 @@
+<?php
+declare(strict_types=1);
+namespace SixShop\Message\Model;
+
+use SixShop\Message\Enum\NotificationsTypeEnum;
+use think\Model;
+
+class MessageNotificationsModel extends Model
+{
+    protected function getOptions(): array
+    {
+        return [
+            'name' => 'message_notifications',
+            'type' => [
+                'type' => NotificationsTypeEnum::class,
+            ]
+        ];
+    }
+}

+ 44 - 0
test/Hook/SendNotificationsHookTest.php

@@ -0,0 +1,44 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\Message\Hook;
+
+
+use PHPUnit\Framework\TestCase;
+use think\Event;
+
+class SendNotificationsHookTest extends TestCase
+{
+    protected SendNotificationsHook $sendNotifications;
+    protected Event $event;
+
+    protected function setUp(): void
+    {
+        $this->event = app()->get('event');
+        $this->sendNotifications = app(SendNotificationsHook::class);
+
+    }
+
+    public function testHandle(): void
+    {
+        $this->event->trigger('message.send_notifications', [
+            [
+                'user_id' => 1,
+                'title' => 'test',
+                'content' => 'test',
+                'type' => 2,
+            ], [
+                'user_id' => 1,
+                'title' => 'test',
+                'content' => 'test',
+                'type' => 1,
+            ],
+            [
+                'user_id' => 1,
+                'title' => 'test',
+                'content' => 'test',
+                'type' => 5,
+            ]
+        ]);
+    }
+}