Sfoglia il codice sorgente

增加消息模块

黄宗昌 2 anni fa
parent
commit
80ff55b1f8

+ 15 - 0
app/Admin/Controllers/ProductController.php

xqd xqd
@@ -4,6 +4,7 @@ namespace App\Admin\Controllers;
 
 use App\Admin\Repositories\Product;
 use App\Admin\Repositories\ProductType;
+use App\Models\Msg;
 use App\Models\User;
 use Dcat\Admin\Form;
 use Dcat\Admin\Grid;
@@ -94,6 +95,20 @@ class ProductController extends AdminController
             $form->image('image')->autoUpload()->uniqueName()->required()->width(4);
             $form->url('url')->width(4);
             $form->switch('status')->default(1);
+            $form->saving(function (Form $form) {
+                if($form->status == "0" && $form->isEditing()){
+                    $id = $form->getKey();
+                    $product = \App\Models\Product::query()->where('id',$id)->first();
+                    $msg = [
+                        'type' => 4,//下架通知
+                        'title' => "下架通知",
+                        'content' => "很抱歉的告诉您,您上传的内容涉及敏感内容,我们已处理下架",
+                        'user_id' => $product->user_id,
+                        'to_user_id' => $product->user_id,
+                    ];
+                    Msg::query()->create($msg); // 添加通知消息
+                }
+            });
         });
     }
 }

+ 67 - 0
app/Http/Controllers/V1/MsgController.php

xqd
@@ -0,0 +1,67 @@
+<?php
+
+namespace App\Http\Controllers\V1;
+
+use App\Models\Msg;
+use App\Models\MsgRead;
+use Illuminate\Http\Request;
+
+class MsgController extends Controller
+{
+
+    public function __construct()
+    {
+        $this->user = auth('api')->user();
+        $this->userId = $this->user ? $this->user->id : 0;
+
+        //如果用户被删除,会自动退出登录
+        if (!empty($this->user->deleted_at)) {
+            $this->user->online = 0;
+            $this->user->save();
+            auth('api')->logout();
+        }
+    }
+
+    /**
+     * @return void
+     * 消息列表
+     */
+    public function msgList(Request $request){
+
+        $limit = $request->get('limit',10);
+
+        $list = Msg::query()->with('user:id,name,avatar')->where('to_user_id',$this->userId)->paginate($limit);
+
+        foreach ($list as $v){
+            if($v){
+                $v['is_read'] = MsgRead::query()->where('user_id',$this->userId)->where('msg_id',$v['id'])->count();
+            }
+        }
+
+        return $this->success($this->page($list));
+
+    }
+
+    /**
+     * @param Request $request
+     * @return void
+     * 消息详情
+     */
+    public function msgDetail(Request $request){
+        $id = $request->get('id');
+        if(!$id){
+            return $this->error("缺少参数ID!");
+        }
+        $msg = Msg::query()->with('user:id,name,avatar')->where('id',$id)->first();
+        $MsgRead = MsgRead::query()->where('user_id',$this->userId)->where('msg_id',$msg['id'])->count();
+        if(!$MsgRead){
+            $data =[
+                'user_id'=>$this->userId,
+                'msg_id'=>$msg['id']
+            ];
+            MsgRead::query()->create($data); // 已读
+        }
+        return $this->success($msg);
+    }
+
+}

+ 54 - 16
app/Http/Controllers/V1/ProductController.php

xqd xqd xqd xqd xqd
@@ -152,6 +152,20 @@ class ProductController extends Controller
         if(!$res){
             return $this->error("上传失败!");
         }
+        $follow = UserFollow::query()->where('to_user_id',$this->userId)->pluck('user_id')->toArray();
+        if(!empty($follow)){
+            $user = User::query()->where('id',$this->userId)->first();
+            foreach ($follow as $v){
+                $msg = [
+                    'type' => 5,//关注的人发了新作品
+                    'title' => "关注的人发了新作品",
+                    'content' => " 您关注的".$user->name."上传了1张图片",
+                    'user_id' => $this->userId,
+                    'to_user_id' => $v,
+                ];
+                Msg::query()->create($msg); // 添加通知消息
+            }
+        }
         return $this->success($res);
 
     }
@@ -174,7 +188,7 @@ class ProductController extends Controller
     /**
      * @param Request $request
      * @return \Illuminate\Http\JsonResponse
-     * 产品列
+     * 产品列
      */
     public function productList(Request $request){
         $limit = $request->get('limit',10);
@@ -263,6 +277,30 @@ class ProductController extends Controller
         if(!$res){
             return $this->error("收藏失败!");
         }
+        $user = User::query()->where('id',$this->userId)->first();
+        $product_type = ProductType::query()->whereIn('id',$product['type'])->select('id','zh_name','ko_name')->first();
+        $type_name = '';
+        if(!empty($product_type)){
+            $type_name = $product_type['zh_name'];
+        }
+        $msg = [
+            'type' => 2,//喜欢通知
+            'title' => "下载通知",
+            'content' => "您收藏了".$type_name."中的1张图片",
+            'user_id' => $this->userId,
+            'to_user_id' => $this->userId,
+        ];
+        Msg::query()->create($msg); // 添加通知消息
+
+        $msg1 = [
+            'type' => 3,//喜欢通知
+            'title' => "保存通知",
+            'content' => $user->name."收藏了您".$type_name."中的1张图片",
+            'user_id' => $this->userId,
+            'to_user_id' => $product['user_id'],
+        ];
+        Msg::query()->create($msg1); // 添加通知消息
+
         return $this->success();
     }
 
@@ -289,7 +327,7 @@ class ProductController extends Controller
     public function addLike(Request $request){
         $product = Product::query()->where('id',$request->get('product_id'))->first();
         if(!$product){
-            return $this->error("商品不存在!");
+            return $this->error("数据不存在!");
         }
         $collect = UserLike::query()->where('product_id',$product->id)->where('user_id',$this->userId)->first();
         if($collect){
@@ -303,20 +341,20 @@ class ProductController extends Controller
         if(!$res){
             return $this->error("操作失败!");
         }
-//        dd($product['type']);
-//        $user = User::query()->where('id',$this->userId)->first();
-//        $product_type = ProductType::query()->whereIn('id',$product['type'])->value('name');
-//        dd($product_type);
-//        $msg = [
-//            'type' => 1,//喜欢通知
-//            'title' => "喜欢通知",
-////            'content' => $user->name."点赞了您".12."中的xxx(数量)张图片",
-//            'user_id' => $this->userId,
-//            'to_user_id' => $product['user_id']
-//        ];
-//
-//        dd($product);
-//        $msg = Msg::query()->create();
+        $user = User::query()->where('id',$this->userId)->first();
+        $product_type = ProductType::query()->whereIn('id',$product['type'])->select('id','zh_name','ko_name')->first();
+        $type_name = '';
+        if(!empty($product_type)){
+            $type_name = $product_type['zh_name'];
+        }
+        $msg = [
+            'type' => 1,//喜欢通知
+            'title' => "喜欢通知",
+            'content' => $user->name."点赞了您".$type_name."中的1张图片",
+            'user_id' => $this->userId,
+            'to_user_id' => $product['user_id']
+        ];
+        Msg::query()->create($msg); // 添加通知消息
 
         return $this->success();
     }

+ 29 - 0
app/Models/Msg.php

xqd
@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Models;
+use Dcat\Admin\Traits\HasDateTimeFormatter;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+class Msg extends Model
+{
+    use HasDateTimeFormatter;
+    use HasFactory;
+
+    protected $table='msg';
+
+    protected $fillable = [
+        'type',
+        'user_id',
+        'to_user_id',
+        'title',
+        'content',
+    ];
+
+    public function type(){
+        return [ 1 => '喜欢通知', 2 => '下载通知', 3 => '保存通知', 4 => '下架通知', 5 => '关注的人发了新作品', 6 => '系统通知'];
+    }
+
+    public function user(){
+        return $this->hasOne(User::class,'id','to_user_id');
+    }
+}

+ 18 - 0
app/Models/MsgRead.php

xqd
@@ -0,0 +1,18 @@
+<?php
+
+namespace App\Models;
+use Dcat\Admin\Traits\HasDateTimeFormatter;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+class MsgRead extends Model
+{
+    use HasDateTimeFormatter;
+    use HasFactory;
+
+    protected $table='msg_read';
+
+    protected $fillable = [
+        'user_id',
+        'msg_id',
+    ];
+}

+ 5 - 1
routes/api.php

xqd
@@ -114,7 +114,11 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\V1'], function ($api)
             $api->get('report_list', 'ProductController@reportList');  //举报列表
             $api->post('report', 'ProductController@report');  //举报图片
         });
-
+        // 消息
+        $api->group(['prefix' => 'msg'], function($api){
+            $api->get('msg_list', 'MsgController@msgList');  //消息列表
+            $api->get('msg_detail', 'MsgController@msgDetail');  //消息详情
+        });
         // 会员
         $api->group(['prefix' => 'member'], function($api){
             $api->get('member_list', 'MemberController@memberList');  //会员价格