gamebackend/application/admin/controller/GameGmController.php
2025-05-08 11:27:49 +08:00

232 lines
9.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\admin\controller;
// 引入使用到的模型和验证类
use app\common\model\GameConfiguration;
use app\common\model\GameGm;
use app\common\validate\GameGmValidate;
use think\Db;
use think\facade\Config;
use think\Request;
class GameGmController extends Controller
{
/**
* GM工具主入口
* 处理各种 GM 操作请求,如封号、加经验、发公告等
*/
public function index(Request $request, GameGm $model, GameGmValidate $validate)
{
// 如果是 POST 请求则表示执行 GM 操作
if ($request->isPost()) {
$param = $request->param(); // 获取请求参数
// 校验密码场景,验证管理员输入的 GM 操作密码是否正确
//$validate->scene('password')->check($param) or admin_error($validate->getError());
// 验证密码是否匹配配置项中的加密密码
//if (!password_verify($param['password'], base64_decode(Config::get('game.gm.password')))) {
// admin_error(lang('gm_password_error'));
//}
// 根据前端传入的 type 参数判断要执行的 GM 操作类型
switch ($param['type']) {
case 'create_robot':
// 验证机器人参数
$validate->scene('robot')->check($param) or admin_error($validate->getError());
try {
$result = $model::create_robot((int)$param['server_id'], (int)$param['robot_num']);
} catch (\Exception $e) {
admin_error($e->getMessage());
}
break;
case 'clear_robot':
$validate->scene('robot')->check($param) or admin_error($validate->getError());
try {
$result = $model::clear_robot((int)$param['server_id'], (int)$param['robot_num']);
} catch (\Exception $e) {
admin_error($e->getMessage());
}
break;
case 'open_gold':
// 开启金矿功能
$validate->scene('open_gold')->check($param) or admin_error($validate->getError());
try {
$result = GameConfiguration::configuration(1, (int)$param['server_id'], (int)$param['quota'], (int)$param['maxQuota']);
} catch (\Exception $e) {
admin_error($e->getMessage());
}
break;
case 'close_gold':
// 关闭金矿功能
$validate->scene('close_gold')->check($param) or admin_error($validate->getError());
try {
$result = GameConfiguration::configuration(0, (int)$param['server_id']);
} catch (\Exception $e) {
admin_error($e->getMessage());
}
break;
case 'not_speek':
// 禁言角色
$validate->scene('speek')->check($param) or admin_error($validate->getError());
try {
$result = $model::not_speak((int)$param['role_id'], (int)$param['server_id']);
} catch (\Exception $e) {
admin_error($e->getMessage());
}
break;
case 'can_speek':
// 解除角色禁言
$validate->scene('speek')->check($param) or admin_error($validate->getError());
try {
$result = $model::can_speak((int)$param['role_id'], (int)$param['server_id']);
} catch (\Exception $e) {
admin_error($e->getMessage());
}
break;
case 'add_jade':
// 增加玉石
$validate->scene('add_jade')->check($param) or admin_error($validate->getError());
try {
$result = $model::add_jade((int)$param['role_id'], (int)$param['jade_num']);
} catch (\Exception $e) {
admin_error($e->getMessage());
}
break;
case 'add_exp':
// 增加经验值
$validate->scene('add_exp')->check($param) or admin_error($validate->getError());
try {
$result = $model::add_exp((int)$param['role_id'], (int)$param['exp_num']);
} catch(\Exception $e) {
admin_error($e->getMessage());
}
break;
case 'add_item':
// 增加道具
$validate->scene('add_item')->check($param) or admin_error($validate->getError());
try {
$result = $model::add_item((int)$param['role_id'], (int)$param['item_id'], (int)$param['item_num']);
} catch(\Exception $e) {
admin_error($e->getMessage());
}
break;
case 'frozen_ip':
// 冻结IP地址
$validate->scene('frozen_ip')->check($param) or admin_error($validate->getError());
try {
$result = $model::frozen_ip($param['ip_address']);
} catch (\Exception $e) {
admin_error($e->getMessage());
}
break;
case 'unfrozen_ip':
// 解冻IP地址
$validate->scene('frozen_ip')->check($param) or admin_error($validate->getError());
try {
$result = $model::unfrozen_ip($param['ip_address']);
} catch (\Exception $e) {
admin_error($e->getMessage());
}
break;
case 'frozen_mac':
// 冻结设备账号MAC
$validate->scene('frozen_mac')->check($param) or admin_error($validate->getError());
try {
$result = $model::frozen_mac($param['account']);
} catch (\Exception $e) {
admin_error($e->getMessage());
}
break;
// 下面这个接口注释掉了,表示目前暂不支持解冻 MAC
case 'unfrozen_mac':
$validate->scene('frozen_mac')->check($param) or admin_error($validate->getError());
try {
$result = $model::unfrozen_mac($param['account']);
} catch (\Exception $e) {
admin_error($e->getMessage());
}
break;
case 'inform':
// 系统全服公告
$validate->scene('inform')->check($param) or admin_error($validate->getError());
try {
$result = $model::sys_notice($param);
} catch (\Exception $e) {
admin_error($e->getMessage());
}
break;
case 'set_comment':
// 设置公告栏留言
$validate->scene('set_comment')->check($param) or admin_error($validate->getError());
try {
$result = $model::set_comment($param['server_id'], $param['notice_content']);
} catch (\Exception $e) {
admin_error($e->getMessage());
}
break;
case 'server_mail':
// 发放全服邮件
$validate->scene('server_mail')->check($param) or admin_error($validate->getError());
try {
$result = $model::send_mail($param, 'S'); // S 表示服务器邮件
} catch (\Exception $e) {
admin_error($e->getMessage());
}
break;
case 'role_mail':
// 发放角色邮件
$validate->scene('role_mail')->check($param) or admin_error($validate->getError());
try {
$result = $model::send_mail($param, 'R'); // R 表示角色邮件
} catch (\Exception $e) {
admin_error($e->getMessage());
}
break;
case 'closeserver':
//设置关服时间
try {
$result = $model::closeserver($param['times']); // 标识多少分钟后关闭
} catch (\Exception $e) {
admin_error($e->getMessage());
}
break;
}
// 操作成功,统一返回成功提示;否则返回 GM 接口失败
if (isset($result) && array_key_exists('code', $result) && $result['code'] == 0) {
admin_success($result['msg'] ?? lang('success'));
} else {
admin_error(lang('gm_api_error'));
}
}
// 非 POST 请求,展示 GM 操作页面,同时传递道具列表供选择
$this->assign([
'item_list' => $model::get_item_list(),
]);
return $this->fetch();
}
}