xieruidong 2 年之前
父節點
當前提交
57ff8b762a
共有 2 個文件被更改,包括 21 次插入4 次删除
  1. 8 0
      application/common/model/Goods.php
  2. 13 4
      application/common/model/SiteMsg.php

+ 8 - 0
application/common/model/Goods.php

@@ -102,5 +102,13 @@ class Goods Extends Model
         self::afterUpdate(function (self $goods){
             //$goods->setAmount();
         });
+
+        self::afterInsert(function (self $goods){
+            SiteMsg::sendMsg(
+                SiteMsg::TYPE_NEW_GOODS,
+                0,
+                compact('goods')
+            );
+        });
     }
 }

+ 13 - 4
application/common/model/SiteMsg.php

@@ -4,6 +4,7 @@ namespace app\common\model;
 
 use think\db\Query;
 use think\Model;
+use Yansongda\Supports\Arr;
 
 /**
  * @method static Query|static hot($hot=1)
@@ -21,6 +22,9 @@ class SiteMsg extends Model
     public function read(){
         return $this->hasMany(SiteMsgRead::class);
     }
+    protected $type=[
+        'data'=>'json',
+    ];
     protected static function init()
     {
         self::afterDelete(function (self $msg){
@@ -29,19 +33,20 @@ class SiteMsg extends Model
     }
 
     public static function sendMsg($msgType,$user,$extend=[]){
-        list($title,$content)=self::getTitleByType($msgType,$extend);
+        list($title,$content,$data)=self::getTitleByType($msgType,$extend);
         if($title && $content){
             return self::create([
                 'msg_type'=>$msgType,
                 'user_id'=>$user instanceof User?$user['id']:$user,
                 'title'=>$title,
                 'content'=>$content,
+                'data'=>$data,
             ]);
         }
     }
 
     public static function getTitleByType($type,$extend=[]){
-        $arr=['',''];
+        $arr=['','',null];
         switch ($type){
             case self::TYPE_COUPON:
                 $arr[0]='您获得了新的优惠券';
@@ -64,8 +69,12 @@ class SiteMsg extends Model
                 $arr[1]='您申请的退货退款未通过,请注意查看';
                 break;
             case self::TYPE_NEW_GOODS:
-                $arr[0]='有新产品上新啦';
-                $arr[1]='有新产品上新啦,请注意查看';
+                $goods=$extend['goods'];
+                $arr[0]="新产品【{$goods['name']}】全新上市";
+                $arr[1]="有新产品上新啦,请注意查看";
+                $arr[2]=[
+                    'id'=>$goods['id'],
+                ];
                 break;
         }
         return $arr;