MessageController.php 2.18 KB
<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\Controller;
use App\Models\Feedback;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Jiannei\Response\Laravel\Support\Facades\Response;

class MessageController extends Controller
{
    //
    public function insertFeedback(Request $request)
    {
        $user_id = Auth::user()->getAuthIdentifier();
        $type = $request->post("type", 1);
        $content = $request->post("content");

        if (!$content) return Response::fail('内容不可为空');

        $insert = [
            [
                'from_user_id' => $user_id,
                'to_user_id' => 1,
                'type' => $type,
                'content' => $content,
                'is_from_user' => 1,
                'state' => 1,
                'send_time' => Carbon::now()
            ],
            [
                'from_user_id' => 1,
                'to_user_id' => $user_id,
                'type' => $type,
                'content' => $content,
                'is_from_user' => 0,
                'state' => 0,
                'send_time' => Carbon::now()
            ]
        ];
        Feedback::query()->insert($insert);

        return Response::success();
    }

    public function feedbackList()
    {
        $user_id = Auth::user()->getAuthIdentifier();
        $data = Feedback::query()
            ->where('from_user_id', $user_id)
            ->where('to_user_id', 1)
            ->orderBy('send_time')
            ->limit(10)
            ->get();

        return Response::success($data);
    }

    public function unreadFeedbackCount()
    {
        $user_id = Auth::user()->getAuthIdentifier();
        $count = Feedback::query()
            ->where('from_user_id', $user_id)
            ->where('is_from_user', 0)
            ->count();
        return Response::success(['count' => $count]);
    }

    public function readFeedback()
    {
        $user_id = Auth::user()->getAuthIdentifier();
        $count = Feedback::query()
            ->where('from_user_id', $user_id)
            ->where('is_from_user', 0)
            ->update(['state' => 1]);
        return Response::success(['count' => $count]);
    }
}