功能:送花/礼物系统完整开发
- 新增 Gift 模型和 gifts 数据表(7种默认花卉,各有图片/金币/魅力配置) - 7张花卉图片生成并存放于 public/images/gifts/ - 名片弹窗新增送礼物 UI:图片选择列表、金币/魅力标注、数量选择 - sendFlower 控制器方法:按 gift_id 查找礼物、扣金币、加魅力、广播消息 - 聊天消息渲染支持显示礼物图片(含弹跳动画效果) - 后台可在 gifts 表中管理花卉类型(名称、图标、图片、金币、魅力、排序、启禁用)
This commit is contained in:
@@ -17,8 +17,10 @@ use App\Events\UserLeft;
|
||||
use App\Http\Requests\SendMessageRequest;
|
||||
use App\Jobs\SaveMessageJob;
|
||||
use App\Models\Autoact;
|
||||
use App\Models\Gift;
|
||||
use App\Models\Room;
|
||||
use App\Models\Sysparam;
|
||||
use App\Models\User;
|
||||
use App\Services\ChatStateService;
|
||||
use App\Services\MessageFilterService;
|
||||
use App\Services\VipService;
|
||||
@@ -433,6 +435,99 @@ class ChatController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 送花/礼物:消耗金币给目标用户增加魅力值
|
||||
*
|
||||
* 根据 gift_id 查找 gifts 表中的礼物类型,读取对应的金币消耗和魅力增量。
|
||||
* 送花成功后在聊天室广播带图片的消息。
|
||||
*/
|
||||
public function sendFlower(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'to_user' => 'required|string',
|
||||
'room_id' => 'required|integer',
|
||||
'gift_id' => 'required|integer',
|
||||
'count' => 'sometimes|integer|min:1|max:99',
|
||||
]);
|
||||
|
||||
$user = Auth::user();
|
||||
if (! $user) {
|
||||
return response()->json(['status' => 'error', 'message' => '请先登录'], 401);
|
||||
}
|
||||
|
||||
$toUsername = $request->input('to_user');
|
||||
$roomId = $request->input('room_id');
|
||||
$giftId = $request->integer('gift_id');
|
||||
$count = $request->integer('count', 1);
|
||||
|
||||
// 不能给自己送花
|
||||
if ($toUsername === $user->username) {
|
||||
return response()->json(['status' => 'error', 'message' => '不能给自己送花哦~']);
|
||||
}
|
||||
|
||||
// 查找礼物类型
|
||||
$gift = Gift::where('id', $giftId)->where('is_active', true)->first();
|
||||
if (! $gift) {
|
||||
return response()->json(['status' => 'error', 'message' => '礼物不存在或已下架']);
|
||||
}
|
||||
|
||||
// 查找目标用户
|
||||
$toUser = User::where('username', $toUsername)->first();
|
||||
if (! $toUser) {
|
||||
return response()->json(['status' => 'error', 'message' => '用户不存在']);
|
||||
}
|
||||
|
||||
$totalCost = $gift->cost * $count;
|
||||
$totalCharm = $gift->charm * $count;
|
||||
|
||||
// 检查金币余额
|
||||
if (($user->jjb ?? 0) < $totalCost) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => "金币不足!送 {$count} 份【{$gift->name}】需要 {$totalCost} 金币,您当前有 ".($user->jjb ?? 0).' 枚。',
|
||||
]);
|
||||
}
|
||||
|
||||
// 扣除金币、增加对方魅力
|
||||
$user->jjb = ($user->jjb ?? 0) - $totalCost;
|
||||
$user->save();
|
||||
|
||||
$toUser->meili = ($toUser->meili ?? 0) + $totalCharm;
|
||||
$toUser->save();
|
||||
|
||||
// 构建礼物图片 URL
|
||||
$giftImageUrl = $gift->image ? "/images/gifts/{$gift->image}" : '';
|
||||
|
||||
// 广播送花消息(含图片标记,前端识别后渲染图片)
|
||||
$countText = $count > 1 ? " {$count} 份" : '';
|
||||
$sysMsg = [
|
||||
'id' => $this->chatState->nextMessageId($roomId),
|
||||
'room_id' => $roomId,
|
||||
'from_user' => '系统传音',
|
||||
'to_user' => '大家',
|
||||
'content' => "{$gift->emoji} {$user->username} 向 {$toUsername} 送出了{$countText}【{$gift->name}】!魅力 +{$totalCharm}!",
|
||||
'is_secret' => false,
|
||||
'font_color' => '#e91e8f',
|
||||
'action' => '',
|
||||
'sent_at' => now()->toDateTimeString(),
|
||||
'gift_image' => $giftImageUrl,
|
||||
'gift_name' => $gift->name,
|
||||
];
|
||||
|
||||
$this->chatState->pushMessage($roomId, $sysMsg);
|
||||
broadcast(new MessageSent($roomId, $sysMsg));
|
||||
SaveMessageJob::dispatch($sysMsg);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => "送花成功!花费 {$totalCost} 金币,{$toUsername} 魅力 +{$totalCharm}",
|
||||
'data' => [
|
||||
'my_jjb' => $user->jjb,
|
||||
'target_charm' => $toUser->meili,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析奖励数值配置(支持固定值或范围格式)
|
||||
*
|
||||
|
||||
53
app/Models/Gift.php
Normal file
53
app/Models/Gift.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:礼物/鲜花模型
|
||||
* 存储各种礼物的名称、图标、金币消耗、魅力增量等信息。
|
||||
* 后台可完整 CRUD 管理,前端名片弹窗中展示可送的礼物列表。
|
||||
*
|
||||
* @author ChatRoom Laravel
|
||||
*
|
||||
* @version 1.0.0
|
||||
*/
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Gift extends Model
|
||||
{
|
||||
/** @var string 表名 */
|
||||
protected $table = 'gifts';
|
||||
|
||||
/** @var array 可批量赋值字段 */
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'emoji',
|
||||
'image',
|
||||
'cost',
|
||||
'charm',
|
||||
'sort_order',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
/** @var array 类型转换 */
|
||||
protected $casts = [
|
||||
'cost' => 'integer',
|
||||
'charm' => 'integer',
|
||||
'sort_order' => 'integer',
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
|
||||
/**
|
||||
* 获取所有启用的礼物列表(按排序字段排列)
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public static function activeList()
|
||||
{
|
||||
return static::where('is_active', true)
|
||||
->orderBy('sort_order')
|
||||
->orderBy('cost')
|
||||
->get();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user