完善职务礼包红包默认配置

This commit is contained in:
2026-04-24 23:09:32 +08:00
parent 4486a87326
commit 5273b4ee4b
12 changed files with 394 additions and 93 deletions
@@ -70,6 +70,8 @@ class PositionController extends Controller
'max_reward' => 'nullable|integer|min:0',
'daily_reward_limit' => 'nullable|integer|min:0',
'recipient_daily_limit' => 'nullable|integer|min:0',
'red_packet_amount' => 'nullable|integer|min:1|max:999999999|gte:red_packet_count',
'red_packet_count' => 'nullable|integer|min:1|max:100',
'sort_order' => 'required|integer|min:0',
'appointable_ids' => 'nullable|array',
'appointable_ids.*' => 'exists:positions,id',
@@ -80,6 +82,8 @@ class PositionController extends Controller
$appointableIds = $data['appointable_ids'] ?? [];
unset($data['appointable_ids']);
$data['permissions'] = array_values(array_unique($data['permissions'] ?? []));
$data['red_packet_amount'] = (int) ($data['red_packet_amount'] ?? 8888);
$data['red_packet_count'] = (int) ($data['red_packet_count'] ?? 10);
$position = Position::create($data);
@@ -161,6 +165,8 @@ class PositionController extends Controller
'max_reward' => 'nullable|integer|min:0',
'daily_reward_limit' => 'nullable|integer|min:0',
'recipient_daily_limit' => 'nullable|integer|min:0',
'red_packet_amount' => 'nullable|integer|min:1|max:999999999|gte:red_packet_count',
'red_packet_count' => 'nullable|integer|min:1|max:100',
'sort_order' => 'required|integer|min:0',
'appointable_ids' => 'nullable|array',
'appointable_ids.*' => 'exists:positions,id',
@@ -171,6 +177,8 @@ class PositionController extends Controller
$appointableIds = $data['appointable_ids'] ?? [];
unset($data['appointable_ids']);
$data['permissions'] = array_values(array_unique($data['permissions'] ?? []));
$data['red_packet_amount'] = (int) ($data['red_packet_amount'] ?? 8888);
$data['red_packet_count'] = (int) ($data['red_packet_count'] ?? 10);
$position->update($data);
$position->appointablePositions()->sync($appointableIds);
+77 -17
View File
@@ -4,7 +4,8 @@
* 文件功能:聊天室礼包(红包)控制器
*
* 提供两个核心接口:
* - send() :拥有权限的职务用户凭空发出 8888 数量 10 份礼包(金币 or 经验)
* - config():读取当前职务的默认礼包数量与份数
* - send() :拥有权限的职务用户按职务配置发出礼包(金币 or 经验)
* - claim() :在线用户抢礼包(先到先得,每人一份)
*
* 接入 UserCurrencyService 记录所有货币变动流水。
@@ -23,6 +24,7 @@ use App\Events\RedPacketSent;
use App\Jobs\SaveMessageJob;
use App\Models\RedPacketClaim;
use App\Models\RedPacketEnvelope;
use App\Models\User;
use App\Services\ChatStateService;
use App\Services\PositionPermissionService;
use App\Services\UserCurrencyService;
@@ -40,11 +42,11 @@ use Illuminate\Support\Facades\DB;
*/
class RedPacketController extends Controller
{
/** 礼包固定总数量 */
private const TOTAL_AMOUNT = 8888;
/** 礼包默认总数量 */
private const DEFAULT_TOTAL_AMOUNT = 8888;
/** 礼包固定份数 */
private const TOTAL_COUNT = 10;
/** 礼包默认份数 */
private const DEFAULT_TOTAL_COUNT = 10;
/** 礼包有效期(秒) */
private const EXPIRE_SECONDS = 300;
@@ -58,10 +60,34 @@ class RedPacketController extends Controller
private readonly PositionPermissionService $positionPermissionService,
) {}
/**
* 获取当前用户可发出的礼包默认配置。
*
* 聊天室发包弹窗打开时调用,确保页面展示与最终发包数量同源。
*/
public function config(): JsonResponse
{
$user = Auth::user();
// 仅拥有礼包红包权限的在职职务可以读取发包配置。
if (! $this->positionPermissionService->hasPermission($user, PositionPermissionRegistry::ROOM_RED_PACKET)) {
return response()->json(['status' => 'error', 'message' => '当前职务无权发礼包红包'], 403);
}
$redPacketConfig = $this->redPacketConfigForUser($user);
return response()->json([
'status' => 'success',
'amount' => $redPacketConfig['amount'],
'count' => $redPacketConfig['count'],
'expire_seconds' => self::EXPIRE_SECONDS,
]);
}
/**
* 拥有权限的职务用户凭空发出礼包。
*
* 不扣发包人自身货币,888 数量凭空发出分 10
* 不扣发包人自身货币,礼包总量和份数读取当前在职职务配置
* type 参数决定本次发出的是金币(gold)还是经验(exp)。
*
* @param Request $request 需包含 room_id typegold / exp
@@ -82,6 +108,10 @@ class RedPacketController extends Controller
return response()->json(['status' => 'error', 'message' => '当前职务无权发礼包红包'], 403);
}
$redPacketConfig = $this->redPacketConfigForUser($user);
$totalAmount = $redPacketConfig['amount'];
$totalCount = $redPacketConfig['count'];
// 检查该用户在此房间是否有进行中的红包(防止刷包)
$activeExists = RedPacketEnvelope::query()
->where('sender_id', $user->id)
@@ -94,8 +124,8 @@ class RedPacketController extends Controller
return response()->json(['status' => 'error', 'message' => '您有一个礼包尚未领完,请稍后再发!'], 422);
}
// 随机拆分数量(二倍均值法,保证每份至少 1,总额精确等于 TOTAL_AMOUNT
$amounts = $this->splitAmount(self::TOTAL_AMOUNT, self::TOTAL_COUNT);
// 随机拆分数量(二倍均值法,保证每份至少 1,总额精确等于职务配置总量
$amounts = $this->splitAmount($totalAmount, $totalCount);
// 货币展示文案
$typeLabel = $type === 'exp' ? '经验' : '金币';
@@ -105,15 +135,15 @@ class RedPacketController extends Controller
: 'linear-gradient(135deg,#dc2626,#ea580c)';
// 事务:创建红包记录 + Redis 写入分额
$envelope = DB::transaction(function () use ($user, $roomId, $type, $amounts): RedPacketEnvelope {
$envelope = DB::transaction(function () use ($user, $roomId, $type, $amounts, $totalAmount, $totalCount): RedPacketEnvelope {
// 创建红包主记录(凭空发出,不扣发包人货币)
$envelope = RedPacketEnvelope::create([
'sender_id' => $user->id,
'sender_username' => $user->username,
'room_id' => $roomId,
'type' => $type,
'total_amount' => self::TOTAL_AMOUNT,
'total_count' => self::TOTAL_COUNT,
'total_amount' => $totalAmount,
'total_count' => $totalCount,
'claimed_count' => 0,
'claimed_amount' => 0,
'status' => 'active',
@@ -138,8 +168,8 @@ class RedPacketController extends Controller
$btnHtml = '<button data-sent-at="'.time().'" onclick="showRedPacketModal('
.$envelope->id
.',\''.$user->username.'\','
.self::TOTAL_AMOUNT.','
.self::TOTAL_COUNT.','
.$totalAmount.','
.$totalCount.','
.self::EXPIRE_SECONDS
.',\''.$type.'\''
.')" style="margin-left:8px;padding:2px 10px;background:'.$btnBg.';'
@@ -151,7 +181,7 @@ class RedPacketController extends Controller
'room_id' => $roomId,
'from_user' => '系统公告',
'to_user' => '',
'content' => "🧧 <b>{$user->username}</b> 发出了一个 <b>".self::TOTAL_AMOUNT."</b> {$typeLabel}的礼包!共 ".self::TOTAL_COUNT." 份,先到先得,快去抢!{$btnHtml}",
'content' => "🧧 <b>{$user->username}</b> 发出了一个 <b>{$totalAmount}</b> {$typeLabel}的礼包!共 {$totalCount} 份,先到先得,快去抢!{$btnHtml}",
'is_secret' => false,
'font_color' => $type === 'exp' ? '#6d28d9' : '#b91c1c',
'action' => '',
@@ -166,15 +196,15 @@ class RedPacketController extends Controller
roomId: $roomId,
envelopeId: $envelope->id,
senderUsername: $user->username,
totalAmount: self::TOTAL_AMOUNT,
totalCount: self::TOTAL_COUNT,
totalAmount: $totalAmount,
totalCount: $totalCount,
expireSeconds: self::EXPIRE_SECONDS,
type: $type,
));
return response()->json([
'status' => 'success',
'message' => "🧧 {$typeLabel}礼包已发出!".self::TOTAL_AMOUNT." {$typeLabel} · ".self::TOTAL_COUNT.' 份',
'message' => "🧧 {$typeLabel}礼包已发出!{$totalAmount} {$typeLabel} · {$totalCount}",
]);
}
@@ -389,4 +419,34 @@ class RedPacketController extends Controller
return $amounts;
}
/**
* 按当前在职职务解析礼包红包配置。
*
* @return array{amount: int, count: int}
*/
private function redPacketConfigForUser(User $user): array
{
$position = $user->activePosition?->position;
$amount = (int) ($position?->red_packet_amount ?? self::DEFAULT_TOTAL_AMOUNT);
$count = (int) ($position?->red_packet_count ?? self::DEFAULT_TOTAL_COUNT);
if ($amount < 1) {
$amount = self::DEFAULT_TOTAL_AMOUNT;
}
if ($count < 1 || $count > 100) {
$count = self::DEFAULT_TOTAL_COUNT;
}
if ($amount < $count) {
$amount = self::DEFAULT_TOTAL_AMOUNT;
$count = self::DEFAULT_TOTAL_COUNT;
}
return [
'amount' => $amount,
'count' => $count,
];
}
}
+4
View File
@@ -37,6 +37,8 @@ class Position extends Model
'recipient_daily_limit',
'sort_order',
'permissions',
'red_packet_amount',
'red_packet_count',
];
/**
@@ -53,6 +55,8 @@ class Position extends Model
'recipient_daily_limit' => 'integer',
'sort_order' => 'integer',
'permissions' => 'array',
'red_packet_amount' => 'integer',
'red_packet_count' => 'integer',
];
}
+1 -7
View File
@@ -19,9 +19,7 @@ class PositionPermissionService
/**
* 返回当前用户拥有的全部聊天室权限码。
*
* 规则:
* - id=1 站长始终拥有全部权限
* - 其他用户仅按当前在职职务的 permissions 生效
* 规则:所有用户都仅按当前在职职务的 permissions 生效。
*
* @return list<string>
*/
@@ -31,10 +29,6 @@ class PositionPermissionService
return [];
}
if ($user->id === 1) {
return PositionPermissionRegistry::codes();
}
$position = $user->activePosition?->position;
if (! $position) {
return [];
@@ -0,0 +1,44 @@
<?php
/**
* 文件功能:为职务表增加礼包红包默认配置。
*
* 每个职务可独立设置礼包红包总量和份数,发金币礼包与经验礼包时共用这组配置。
*/
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* 类功能:维护 positions 表的礼包红包默认总量与份数字段。
*/
return new class extends Migration
{
/**
* 方法功能:新增职务礼包红包总量和份数字段。
*/
public function up(): void
{
Schema::table('positions', function (Blueprint $table) {
$table->unsignedInteger('red_packet_amount')
->default(8888)
->after('permissions')
->comment('礼包红包默认总量');
$table->unsignedSmallInteger('red_packet_count')
->default(10)
->after('red_packet_amount')
->comment('礼包红包默认份数');
});
}
/**
* 方法功能:删除职务礼包红包配置字段。
*/
public function down(): void
{
Schema::table('positions', function (Blueprint $table) {
$table->dropColumn(['red_packet_amount', 'red_packet_count']);
});
}
};
@@ -27,6 +27,8 @@
max_reward: '',
daily_reward_limit: '',
recipient_daily_limit: '',
red_packet_amount: 8888,
red_packet_count: 10,
sort_order: 0
},
@@ -34,7 +36,7 @@
this.editing = null;
this.selectedIds = [];
this.selectedPermissions = [];
this.form = { department_id: '', name: '', icon: '🎖️', rank: 50, level: 60, max_persons: 1, max_reward: '', daily_reward_limit: '', recipient_daily_limit: '', sort_order: 0 };
this.form = { department_id: '', name: '', icon: '🎖️', rank: 50, level: 60, max_persons: 1, max_reward: '', daily_reward_limit: '', recipient_daily_limit: '', red_packet_amount: 8888, red_packet_count: 10, sort_order: 0 };
this.showForm = true;
},
openEdit(pos, appointableIds, permissions) {
@@ -51,6 +53,8 @@
max_reward: pos.max_reward !== null && pos.max_reward !== undefined ? pos.max_reward : '',
daily_reward_limit: pos.daily_reward_limit !== null && pos.daily_reward_limit !== undefined ? pos.daily_reward_limit : '',
recipient_daily_limit: pos.recipient_daily_limit !== null && pos.recipient_daily_limit !== undefined ? pos.recipient_daily_limit : '',
red_packet_amount: pos.red_packet_amount || 8888,
red_packet_count: pos.red_packet_count || 10,
sort_order: pos.sort_order,
};
this.showForm = true;
@@ -185,6 +189,7 @@
<th class="px-4 py-3 text-center">单次上限</th>
<th class="px-4 py-3 text-center">单日上限</th>
<th class="px-4 py-3 text-center">任命权</th>
<th class="px-4 py-3 text-center">礼包默认</th>
<th class="px-4 py-3 text-center">聊天室权限</th>
@php $superLvl = (int) \App\Models\Sysparam::getValue('superlevel', '100'); @endphp
@if (Auth::user()->user_level >= $superLvl)
@@ -254,6 +259,12 @@
<span class="text-xs text-gray-400"></span>
@endif
</td>
<td class="px-4 py-3 text-center">
<div class="text-xs leading-5 text-red-700">
<div class="font-bold">{{ number_format((int) ($pos->red_packet_amount ?? 8888)) }}</div>
<div class="text-gray-400">{{ (int) ($pos->red_packet_count ?? 10) }} </div>
</div>
</td>
<td class="px-4 py-3">
@if (! empty($pos->permissions))
@php
@@ -297,6 +308,8 @@
max_reward: {{ $pos->max_reward ?? 'null' }},
daily_reward_limit: {{ $pos->daily_reward_limit ?? 'null' }},
recipient_daily_limit: {{ $pos->recipient_daily_limit ?? 'null' }},
red_packet_amount: {{ $pos->red_packet_amount ?? 8888 }},
red_packet_count: {{ $pos->red_packet_count ?? 10 }},
sort_order: {{ $pos->sort_order }},
requestUrl: '{{ route('admin.positions.update', $pos->id) }}'
}, {{ json_encode($appointableIds) }}, {{ json_encode($pos->permissions ?? []) }})"
@@ -447,6 +460,30 @@
<span class="min-w-0">
<span class="block font-bold text-gray-700">{{ $permissionMeta['label'] }}</span>
<span class="block text-xs text-gray-500">{{ $permissionMeta['description'] }}</span>
@if ($permissionCode === \App\Support\PositionPermissionRegistry::ROOM_RED_PACKET)
<span class="mt-3 grid grid-cols-2 gap-2 rounded-lg border border-red-100 bg-red-50/70 p-2"
@click.stop>
<span>
<span class="mb-1 block text-[11px] font-bold text-red-700">默认礼包总量</span>
<input type="number" name="red_packet_amount"
x-model="form.red_packet_amount" required min="1"
max="999999999"
class="w-full rounded-md border border-red-200 bg-white p-1.5 text-xs text-red-800"
placeholder="金币/经验共用">
</span>
<span>
<span class="mb-1 block text-[11px] font-bold text-red-700">默认礼包份数</span>
<input type="number" name="red_packet_count"
x-model="form.red_packet_count" required min="1"
max="100"
class="w-full rounded-md border border-red-200 bg-white p-1.5 text-xs text-red-800"
placeholder="拆成几份">
</span>
<span class="col-span-2 text-[11px] leading-4 text-red-600">
勾选后发金币/经验礼包都使用这组默认值;取消权限不会清空配置。
</span>
</span>
@endif
</span>
</label>
@endforeach
@@ -296,42 +296,82 @@
/**
* superlevel 点击「礼包」按钮,弹出 chatBanner 三按钮选择类型后发包。
*/
window.sendRedPacket = function() {
window.chatBanner.show({
icon: '🧧',
title: '发出礼包',
name: '选择礼包类型',
body: '将发出 <b>8888</b> 数量共 <b>10</b> 份的礼包,系统凭空发放,房间成员先到先得!',
gradient: ['#991b1b', '#dc2626', '#ea580c'],
titleColor: '#fde68a',
autoClose: 0,
buttons: [{
label: '💰 金币礼包',
color: '#d97706',
onClick(btn, close) {
close();
doSendRedPacket('gold');
window.sendRedPacket = async function() {
const btn = document.getElementById('red-packet-btn');
if (btn) {
btn.disabled = true;
btn.textContent = '读取中…';
}
try {
const config = await fetchRedPacketConfig();
const amountText = Number(config.amount || 0).toLocaleString('zh-CN');
const countText = Number(config.count || 0).toLocaleString('zh-CN');
window.chatBanner.show({
icon: '🧧',
title: '发出礼包',
name: '选择礼包类型',
body: `将发出 ${amountText} 数量共 ${countText} 份的礼包,系统凭空发放,房间成员先到先得!`,
gradient: ['#991b1b', '#dc2626', '#ea580c'],
titleColor: '#fde68a',
autoClose: 0,
buttons: [{
label: '💰 金币礼包',
color: '#d97706',
onClick(button, close) {
close();
doSendRedPacket('gold');
},
},
},
{
label: '✨ 经验礼包',
color: '#7c3aed',
onClick(btn, close) {
close();
doSendRedPacket('exp');
{
label: '✨ 经验礼包',
color: '#7c3aed',
onClick(button, close) {
close();
doSendRedPacket('exp');
},
},
},
{
label: '取消',
color: 'rgba(255,255,255,0.15)',
onClick(btn, close) {
close();
{
label: '取消',
color: 'rgba(255,255,255,0.15)',
onClick(button, close) {
close();
},
},
},
],
});
],
});
} catch (e) {
await window.chatDialog.alert(e.message || '读取礼包配置失败', '操作失败', '#cc4444');
} finally {
if (btn) {
btn.disabled = false;
btn.innerHTML = '🧧 礼包';
}
}
};
/**
* 读取当前职务的礼包红包默认配置。
*
* @returns {Promise<{amount:number,count:number,expire_seconds:number}>}
*/
async function fetchRedPacketConfig() {
const res = await fetch('/command/red-packet/config', {
headers: {
'Accept': 'application/json',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
},
});
const data = await res.json();
if (!res.ok || data.status !== 'success') {
throw new Error(data.message || '读取礼包配置失败');
}
return data;
}
/**
* 实际发包请求(由 chatBanner 按钮回调触发)。
*
@@ -624,7 +664,7 @@
// 弹出全局 Toast
window.chatToast.show({
title: '🧧 礼包到账',
message: `恭喜您抢到了礼包 <b>${data.amount}</b> ${typeLabel}`,
message: `恭喜您抢到了礼包 ${data.amount} ${typeLabel}`,
icon: '🧧',
color: (_rpType === 'exp') ? '#7c3aed' : '#dc2626',
duration: 8000,
+1
View File
@@ -338,6 +338,7 @@ Route::middleware(['chat.auth'])->group(function () {
Route::post('/command/baccarat-loss-cover/{event}/close', [\App\Http\Controllers\Admin\BaccaratLossCoverEventController::class, 'close'])->name('command.baccarat_loss_cover.close');
// ---- 礼包红包(superlevel 发包 / 所有登录用户可抢)----
Route::get('/command/red-packet/config', [\App\Http\Controllers\RedPacketController::class, 'config'])->name('command.red_packet.config');
Route::post('/command/red-packet/send', [\App\Http\Controllers\RedPacketController::class, 'send'])->name('command.red_packet.send');
Route::get('/red-packet/{envelopeId}/status', [\App\Http\Controllers\RedPacketController::class, 'status'])->name('red_packet.status');
Route::post('/red-packet/{envelopeId}/claim', [\App\Http\Controllers\RedPacketController::class, 'claim'])->name('red_packet.claim');
+13 -12
View File
@@ -336,15 +336,14 @@ class ChatControllerTest extends TestCase
}
/**
* 测试站长即使没有在职职务,也能看到管理菜单中的刷新全员按钮。
* 测试站长有在职职务权限时能看到管理菜单中的刷新全员按钮。
*/
public function test_room_view_shows_refresh_all_button_for_site_owner(): void
public function test_room_view_shows_refresh_all_button_for_positioned_site_owner(): void
{
$room = Room::create(['room_name' => 'owner-rf']);
$user = User::factory()->create([
'id' => 1,
'user_level' => 100,
]);
$user = $this->createUserWithPositionPermissions([
PositionPermissionRegistry::ROOM_CLEAR_SCREEN,
], ['id' => 1, 'user_level' => 100]);
$response = $this->actingAs($user)->get(route('chat.room', $room->id));
@@ -804,11 +803,13 @@ class ChatControllerTest extends TestCase
}
/**
* 测试管理员可以设置房间公告。
* 测试拥有公告权限的职务用户可以设置房间公告。
*/
public function test_site_owner_can_set_announcement()
public function test_position_user_can_set_announcement()
{
$user = User::factory()->create(['id' => 1, 'user_level' => 100]);
$user = $this->createUserWithPositionPermissions([
PositionPermissionRegistry::ROOM_ANNOUNCEMENT,
]);
$room = Room::create(['room_name' => 'test_ann', 'room_owner' => 'someone']);
$response = $this->actingAs($user)->postJson(route('chat.announcement', $room->id), [
@@ -873,11 +874,11 @@ class ChatControllerTest extends TestCase
*
* @param list<string> $permissions
*/
private function createUserWithPositionPermissions(array $permissions): User
private function createUserWithPositionPermissions(array $permissions, array $attributes = []): User
{
$user = User::factory()->create([
$user = User::factory()->create(array_merge([
'user_level' => 70,
]);
], $attributes));
$department = Department::create([
'name' => '聊天室测试部门'.$user->id,
@@ -39,13 +39,12 @@ class AdminCommandControllerTest extends TestCase
}
/**
* 测试站长可以触发全部新增全屏特效。
* 测试拥有全屏特效权限的职务用户可以触发全部新增全屏特效。
*/
public function test_super_admin_can_trigger_all_new_effect_types(): void
public function test_position_user_can_trigger_all_new_effect_types(): void
{
$admin = User::factory()->create([
'id' => 1,
'user_level' => 100,
$admin = $this->createPositionedManager([
PositionPermissionRegistry::ROOM_FULLSCREEN_EFFECT,
]);
$room = Room::create([
'room_name' => '特效房',
@@ -297,7 +296,8 @@ class AdminCommandControllerTest extends TestCase
'message' => "已警告 {$target->username}",
]);
$privateMessage = $this->findPrivateSystemMessage($room->id, $target->username, '站长</b> <b>'.$admin->username.'</b> 警告了你');
$identityText = $admin->activePosition->position->department->name.'·'.$admin->activePosition->position->name;
$privateMessage = $this->findPrivateSystemMessage($room->id, $target->username, "{$identityText}</b> <b>{$admin->username}</b> 警告了你");
$this->assertNotNull($privateMessage);
$this->assertSame('⚠️ 收到警告', $privateMessage['toast_notification']['title'] ?? null);
@@ -521,10 +521,13 @@ class AdminCommandControllerTest extends TestCase
*/
private function createAdminCommandActors(): array
{
$admin = User::factory()->create([
'id' => 1,
'user_level' => 100,
$admin = $this->createPositionedManager([
PositionPermissionRegistry::USER_WARN,
PositionPermissionRegistry::USER_MUTE,
PositionPermissionRegistry::USER_KICK,
PositionPermissionRegistry::USER_FREEZE,
]);
$admin->load('activePosition.position.department');
$target = User::factory()->create([
'user_level' => 1,
]);
@@ -41,6 +41,8 @@ class AdminPositionPermissionTest extends TestCase
'max_reward' => 100,
'daily_reward_limit' => 300,
'recipient_daily_limit' => 2,
'red_packet_amount' => 12000,
'red_packet_count' => 12,
'sort_order' => 8,
'permissions' => [
PositionPermissionRegistry::ROOM_ANNOUNCEMENT,
@@ -52,6 +54,8 @@ class AdminPositionPermissionTest extends TestCase
$position = Position::query()->where('name', '值班主持')->firstOrFail();
$this->assertSame(12000, (int) $position->red_packet_amount);
$this->assertSame(12, (int) $position->red_packet_count);
$this->assertSame([
PositionPermissionRegistry::ROOM_ANNOUNCEMENT,
PositionPermissionRegistry::ROOM_PUBLIC_BROADCAST,
@@ -113,6 +117,8 @@ class AdminPositionPermissionTest extends TestCase
'max_reward' => null,
'daily_reward_limit' => null,
'recipient_daily_limit' => null,
'red_packet_amount' => 6600,
'red_packet_count' => 6,
'sort_order' => 1,
'permissions' => [
PositionPermissionRegistry::ROOM_RED_PACKET,
@@ -124,12 +130,41 @@ class AdminPositionPermissionTest extends TestCase
$position->refresh();
$this->assertSame(6600, (int) $position->red_packet_amount);
$this->assertSame(6, (int) $position->red_packet_count);
$this->assertSame([
PositionPermissionRegistry::ROOM_RED_PACKET,
PositionPermissionRegistry::ROOM_FULLSCREEN_EFFECT,
], $position->permissions);
}
/**
* 验证礼包总量不能小于礼包份数。
*/
public function test_red_packet_amount_must_cover_packet_count(): void
{
$owner = $this->createSiteOwner();
$department = $this->createDepartment();
$response = $this->from(route('admin.positions.index'))->actingAs($owner)->post(route('admin.positions.store'), [
'department_id' => $department->id,
'name' => '礼包主持',
'icon' => '🧧',
'rank' => 55,
'level' => 55,
'max_persons' => 1,
'red_packet_amount' => 5,
'red_packet_count' => 10,
'sort_order' => 2,
'permissions' => [
PositionPermissionRegistry::ROOM_RED_PACKET,
],
]);
$response->assertRedirect(route('admin.positions.index'));
$response->assertSessionHasErrors('red_packet_amount');
}
/**
* 验证职务管理页面会渲染权限配置与摘要文案。
*/
@@ -157,6 +192,8 @@ class AdminPositionPermissionTest extends TestCase
$response->assertSee('权限管理');
$response->assertSee('设置公告');
$response->assertSee('礼包红包');
$response->assertSee('默认礼包总量');
$response->assertSee('默认礼包份数');
$response->assertSee('警告用户');
$response->assertSee('冻结用户');
}
+87 -15
View File
@@ -64,9 +64,9 @@ class RedPacketControllerTest extends TestCase
}
/**
* 方法功能:验证站长可以成功发出礼包并写入 Redis 拆包结果
* 方法功能:验证站长没有在职职务时也不能绕过礼包权限
*/
public function test_superadmin_can_send_red_packet(): void
public function test_site_owner_without_position_cannot_send_red_packet(): void
{
$admin = $this->createSiteOwner();
@@ -75,19 +75,72 @@ class RedPacketControllerTest extends TestCase
'type' => 'gold',
]);
$response->assertStatus(200);
$response->assertJson(['status' => 'success']);
$response->assertStatus(403);
}
$this->assertDatabaseHas('red_packet_envelopes', [
'sender_id' => $admin->id,
/**
* 方法功能:验证礼包弹窗配置接口会读取当前职务的数据库配置。
*/
public function test_red_packet_config_uses_position_packet_config(): void
{
$user = $this->createUserWithPermissions([
PositionPermissionRegistry::ROOM_RED_PACKET,
], redPacketAmount: 2468, redPacketCount: 8);
$response = $this->actingAs($user)->getJson(route('command.red_packet.config'));
$response->assertOk()
->assertJson([
'status' => 'success',
'amount' => 2468,
'count' => 8,
'expire_seconds' => 300,
]);
}
/**
* 方法功能:验证没有礼包权限时不能读取发包弹窗配置。
*/
public function test_user_without_red_packet_permission_cannot_read_packet_config(): void
{
$user = $this->createUserWithPermissions([]);
$response = $this->actingAs($user)->getJson(route('command.red_packet.config'));
$response->assertStatus(403)
->assertJson(['status' => 'error']);
}
/**
* 方法功能:验证拥有礼包权限的职务用户会按职务配置发出礼包。
*/
public function test_position_user_with_red_packet_permission_uses_position_packet_config(): void
{
$user = $this->createUserWithPermissions([
PositionPermissionRegistry::ROOM_RED_PACKET,
], redPacketAmount: 1234, redPacketCount: 7);
$response = $this->actingAs($user)->postJson(route('command.red_packet.send'), [
'room_id' => 1,
'type' => 'gold',
]);
$response->assertOk()
->assertJsonPath('status', 'success');
$this->assertDatabaseHas('red_packet_envelopes', [
'sender_id' => $user->id,
'room_id' => 1,
'type' => 'gold',
'total_amount' => 1234,
'total_count' => 7,
'status' => 'active',
]);
$envelope = RedPacketEnvelope::first();
// Check Redis for parts
$this->assertEquals(10, Redis::llen("red_packet:{$envelope->id}:amounts"));
$amounts = array_map('intval', Redis::lrange("red_packet:{$envelope->id}:amounts", 0, -1));
$this->assertCount(7, $amounts);
$this->assertSame(1234, array_sum($amounts));
}
/**
@@ -97,7 +150,7 @@ class RedPacketControllerTest extends TestCase
{
$user = $this->createUserWithPermissions([
PositionPermissionRegistry::ROOM_RED_PACKET,
]);
], redPacketAmount: 4321, redPacketCount: 6);
$response = $this->actingAs($user)->postJson(route('command.red_packet.send'), [
'room_id' => 1,
@@ -107,6 +160,13 @@ class RedPacketControllerTest extends TestCase
$response->assertOk()->assertJson([
'status' => 'success',
]);
$this->assertDatabaseHas('red_packet_envelopes', [
'sender_id' => $user->id,
'type' => 'exp',
'total_amount' => 4321,
'total_count' => 6,
]);
}
/**
@@ -114,7 +174,9 @@ class RedPacketControllerTest extends TestCase
*/
public function test_cannot_send_multiple_active_packets_in_same_room(): void
{
$admin = $this->createSiteOwner();
$admin = $this->createUserWithPermissions([
PositionPermissionRegistry::ROOM_RED_PACKET,
]);
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
'room_id' => 1,
@@ -134,7 +196,9 @@ class RedPacketControllerTest extends TestCase
*/
public function test_user_can_claim_red_packet(): void
{
$admin = $this->createSiteOwner();
$admin = $this->createUserWithPermissions([
PositionPermissionRegistry::ROOM_RED_PACKET,
]);
$user = User::factory()->create(['jjb' => 100]);
// Send packet
@@ -169,7 +233,9 @@ class RedPacketControllerTest extends TestCase
{
Event::fake([RedPacketClaimed::class]);
$admin = $this->createSiteOwner();
$admin = $this->createUserWithPermissions([
PositionPermissionRegistry::ROOM_RED_PACKET,
]);
$user = User::factory()->create(['jjb' => 100]);
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
@@ -203,7 +269,9 @@ class RedPacketControllerTest extends TestCase
*/
public function test_user_cannot_claim_same_packet_twice(): void
{
$admin = $this->createSiteOwner();
$admin = $this->createUserWithPermissions([
PositionPermissionRegistry::ROOM_RED_PACKET,
]);
$user = User::factory()->create();
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
@@ -232,7 +300,9 @@ class RedPacketControllerTest extends TestCase
*/
public function test_can_check_packet_status(): void
{
$admin = $this->createSiteOwner();
$admin = $this->createUserWithPermissions([
PositionPermissionRegistry::ROOM_RED_PACKET,
]);
$user = User::factory()->create();
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
@@ -279,7 +349,7 @@ class RedPacketControllerTest extends TestCase
*
* @param list<string> $permissions
*/
private function createUserWithPermissions(array $permissions): User
private function createUserWithPermissions(array $permissions, int $redPacketAmount = 8888, int $redPacketCount = 10): User
{
$user = User::factory()->create([
'user_level' => 80,
@@ -301,6 +371,8 @@ class RedPacketControllerTest extends TestCase
'level' => 80,
'sort_order' => 1,
'permissions' => $permissions,
'red_packet_amount' => $redPacketAmount,
'red_packet_count' => $redPacketCount,
]);
UserPosition::create([