移除渠道ChannelManage

This commit is contained in:
2026-03-10 10:45:23 +08:00
parent a6d87d5c0d
commit 7716929447
11 changed files with 31 additions and 1201 deletions

View File

@@ -1,327 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\channel\controller\manage;
use plugin\saiadmin\basic\BaseController;
use app\channel\logic\manage\ChannelManageLogic;
use app\channel\validate\manage\ChannelManageValidate;
use plugin\saiadmin\app\model\system\SystemUser;
use plugin\saiadmin\app\model\system\SystemDept;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\service\Permission;
use support\Request;
use support\Response;
/**
* 渠道控制器
*/
class ChannelManageController extends BaseController
{
/**
* 构造函数
*/
public function __construct()
{
$this->logic = new ChannelManageLogic();
$this->validate = new ChannelManageValidate;
parent::__construct();
}
/**
* 数据列表
* @param Request $request
* @return Response
*/
#[Permission('渠道列表', 'channel:manage:index:index')]
public function index(Request $request): Response
{
$where = $request->more([
['name', ''],
['title', ''],
['status', ''],
['department_id', ''],
['total_recharge', ''],
['total_withdrawal', ''],
['total_profit', ''],
]);
$query = $this->logic->search($where);
// 仅渠道管理员和超级管理员可查看:超管看全部,渠道管理员只看自己管理的渠道
if (isset($this->adminInfo['id']) && (int) $this->adminInfo['id'] > 1) {
$query->where('admin_id', $this->adminInfo['id']);
}
$data = $this->logic->getList($query);
$data['data'] = $this->attachAdminToRows($data['data'] ?? []);
$data['data'] = $this->attachDepartmentToRows($data['data'] ?? []);
$data['data'] = $this->attachCanOperate($data['data'] ?? []);
return $this->success($data);
}
/**
* 读取数据
* @param Request $request
* @return Response
*/
#[Permission('渠道读取', 'channel:manage:index:read')]
public function read(Request $request): Response
{
$id = $request->input('id', '');
$model = $this->logic->read($id);
if ($model) {
$data = is_array($model) ? $model : $model->toArray();
if (!$this->logic->canOperateChannel($data)) {
throw new ApiException('没有权限查看该渠道');
}
return $this->success($data);
} else {
return $this->fail('未查找到信息');
}
}
/**
* 保存数据
* @param Request $request
* @return Response
*/
#[Permission('渠道添加', 'channel:manage:index:save')]
public function save(Request $request): Response
{
$data = $request->post();
$data['agent'] = md5((string)($data['name'] ?? '') . (string)($data['admin_id'] ?? ''));
$this->validate('save', $data);
$result = $this->logic->add($data);
if ($result) {
return $this->success('添加成功');
} else {
return $this->fail('添加失败');
}
}
/**
* 更新数据
* @param Request $request
* @return Response
*/
#[Permission('渠道修改', 'channel:manage:index:update')]
public function update(Request $request): Response
{
$data = $request->post();
$channel = $this->logic->read($data['id'] ?? 0);
if (!$channel || !$this->logic->canOperateChannel($channel)) {
throw new ApiException('没有权限修改该渠道');
}
$data['agent'] = md5((string)($data['name'] ?? '') . (string)($data['admin_id'] ?? ''));
$this->validate('update', $data);
$result = $this->logic->edit($data['id'], $data);
if ($result) {
return $this->success('修改成功');
} else {
return $this->fail('修改失败');
}
}
/**
* 仅更新状态(列表内开关用)
* @param Request $request
* @return Response
*/
#[Permission('渠道管理-更新状态', 'channel:manage:index:update')]
public function updateStatus(Request $request): Response
{
$id = $request->input('id');
$status = $request->input('status');
if ($id === null || $id === '') {
return $this->fail('缺少 id');
}
if ($status === null || $status === '') {
return $this->fail('缺少 status');
}
$channel = $this->logic->read($id);
if (!$channel || !$this->logic->canOperateChannel($channel)) {
throw new ApiException('没有权限修改该渠道');
}
$this->logic->edit($id, ['status' => (int) $status]);
return $this->success('修改成功');
}
/**
* 删除数据
* @param Request $request
* @return Response
*/
#[Permission('渠道删除', 'channel:manage:index:destroy')]
public function destroy(Request $request): Response
{
$ids = $request->post('ids', '');
if (empty($ids)) {
return $this->fail('请选择要删除的数据');
}
$idList = is_array($ids) ? $ids : (array) $ids;
foreach ($idList as $id) {
$channel = $this->logic->read($id);
if (!$channel || !$this->logic->canOperateChannel($channel)) {
throw new ApiException('没有权限删除该渠道');
}
}
$result = $this->logic->destroy($ids);
if ($result) {
return $this->success('删除成功');
} else {
return $this->fail('删除失败');
}
}
/**
* 获取可操作部门列表(树形,用于渠道所属部门选择)
* @param Request $request
* @return Response
*/
#[Permission('渠道部门列表', 'channel:manage:index:index')]
public function getDeptList(Request $request): Response
{
$this->ensureAdminInfo();
$logic = new \plugin\saiadmin\app\logic\system\SystemDeptLogic();
$logic->init($this->adminInfo ?? []);
$data = $logic->accessDept(['status' => 1]);
return $this->success($data);
}
/**
* 获取管理员下拉列表(远程关联 SystemUservalue=id, label=name
* @param Request $request
* @return Response
*/
#[Permission('渠道管理员列表', 'channel:manage:index:index')]
public function getAdminList(Request $request): Response
{
$list = SystemUser::where('status', 1)
->field('id, username, realname')
->select();
$rows = $list->toArray();
$data = array_map(function ($row) {
$name = !empty($row['realname']) && trim($row['realname']) !== ''
? trim($row['realname'])
: ($row['username'] ?? '#' . $row['id']);
return [
'id' => $row['id'],
'name' => $name,
];
}, $rows);
return $this->success($data);
}
/**
* 为列表行附加管理员信息(避免 Eloquent with(Think 模型) 触发 getConnectionName 报错)
* @param array $rows 列表行,可为 Eloquent 模型或数组
* @return array 已附加 admin 的列表
*/
protected function attachAdminToRows(array $rows): array
{
if (empty($rows)) {
return $rows;
}
$adminIds = [];
foreach ($rows as $row) {
$id = is_array($row) ? ($row['admin_id'] ?? null) : $row->admin_id;
if ($id !== null && $id !== '') {
$adminIds[(string) $id] = true;
}
}
$adminIds = array_keys($adminIds);
if (empty($adminIds)) {
return $rows;
}
$admins = SystemUser::whereIn('id', $adminIds)
->field('id, username, realname')
->select()
->toArray();
$map = [];
foreach ($admins as $a) {
$map[(string) ($a['id'] ?? '')] = $a;
}
foreach ($rows as &$row) {
$aid = is_array($row) ? ($row['admin_id'] ?? null) : $row->admin_id;
$admin = $map[(string) ($aid ?? '')] ?? null;
if (is_array($row)) {
$row['admin'] = $admin;
} else {
$row->setAttribute('admin', $admin);
}
}
unset($row);
return $rows;
}
/**
* 为列表行附加部门信息department_id 关联 SystemDept
* @param array $rows 列表行,可为 Eloquent 模型或数组
* @return array 已附加 department 的列表
*/
protected function attachDepartmentToRows(array $rows): array
{
if (empty($rows)) {
return $rows;
}
$deptIds = [];
foreach ($rows as $row) {
$id = is_array($row) ? ($row['department_id'] ?? null) : ($row->department_id ?? null);
if ($id !== null && $id !== '') {
$deptIds[(string) $id] = true;
}
}
$deptIds = array_keys($deptIds);
if (empty($deptIds)) {
foreach ($rows as &$row) {
if (is_array($row)) {
$row['department'] = null;
} else {
$row->setAttribute('department', null);
}
}
unset($row);
return $rows;
}
$depts = SystemDept::whereIn('id', $deptIds)
->field('id, name')
->select()
->toArray();
$map = [];
foreach ($depts as $d) {
$map[(string) ($d['id'] ?? '')] = $d;
}
foreach ($rows as &$row) {
$did = is_array($row) ? ($row['department_id'] ?? null) : ($row->department_id ?? null);
$dept = $map[(string) ($did ?? '')] ?? null;
if (is_array($row)) {
$row['department'] = $dept;
} else {
$row->setAttribute('department', $dept);
}
}
unset($row);
return $rows;
}
/**
* 为列表行附加是否可操作标记(仅渠道管理员和超级管理员可操作)
* @param array $rows 列表行
* @return array
*/
protected function attachCanOperate(array $rows): array
{
foreach ($rows as &$row) {
$canOperate = $this->logic->canOperateChannel($row);
if (is_array($row)) {
$row['canOperate'] = $canOperate;
} else {
$row->setAttribute('canOperate', $canOperate);
}
}
unset($row);
return $rows;
}
}

View File

@@ -1,44 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\channel\logic\manage;
use plugin\saiadmin\basic\eloquent\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use app\channel\model\manage\ChannelManage;
/**
* 渠道逻辑层
*/
class ChannelManageLogic extends BaseLogic
{
/**
* 构造函数
*/
public function __construct()
{
$this->model = new ChannelManage();
}
/**
* 判断当前用户是否可操作该渠道(仅渠道管理员或超级管理员可操作)
* @param object|array $channel 渠道数据,需包含 admin_id
* @return bool
*/
public function canOperateChannel($channel): bool
{
if (empty($this->adminInfo) || !isset($this->adminInfo['id'])) {
return false;
}
$adminId = (int) ($this->adminInfo['id']);
if ($adminId === 1) {
return true;
}
$channelAdminId = is_array($channel) ? ($channel['admin_id'] ?? null) : ($channel->admin_id ?? null);
return $channelAdminId !== null && (int) $channelAdminId === $adminId;
}
}

View File

@@ -1,114 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\channel\model\manage;
use plugin\saiadmin\app\model\system\SystemUser;
use plugin\saiadmin\basic\eloquent\BaseModel;
/**
* 渠道模型
*
* channel_manage 渠道
* @property $id ID
* @property $name 名称
* @property $title 标题
* @property $status 状态
* @property $admin_id 管理员
* @property $department_id 所属部门ID关联sa_system_dept
* @property $game_url 游戏地址
* @property $image 图标
* @property $agent 代理agent
* @property $ip_white IP白名单
* @property $total_recharge 总充值
* @property $total_withdrawal 总提现
* @property $total_profit 总盈利
* @property $player_count 玩家数量
* @property $create_time 创建时间
* @property $update_time 更新时间
*/
class ChannelManage extends BaseModel
{
/**
* 数据表主键
* @var string
*/
protected $primaryKey = 'id';
/**
* 数据库表名称
* @var string
*/
protected $table = 'channel_manage';
/**
* 属性转换
*/
protected function casts(): array
{
return array_merge(parent::casts(), [
]);
}
/**
* 名称 搜索
*/
public function searchNameAttr($query, $value)
{
$query->where('name', 'like', '%'.$value.'%');
}
/**
* 标题 搜索
*/
public function searchTitleAttr($query, $value)
{
$query->where('title', 'like', '%'.$value.'%');
}
/**
* 部门 搜索
*/
public function searchDepartmentIdAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('department_id', $value);
}
}
/**
* 总充值 搜索
*/
public function searchTotalRechargeAttr($query, $value)
{
$query->where('total_recharge', '>=', $value);
}
/**
* 总提现 搜索
*/
public function searchTotalWithdrawalAttr($query, $value)
{
$query->where('total_withdrawal', '>=', $value);
}
/**
* 总盈利 搜索
*/
public function searchTotalProfitAttr($query, $value)
{
$query->where('total_profit', '>=', $value);
}
/**
* 管理员
* 关联模型 systemUser
*/
public function admin(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(SystemUser::class, 'admin_id', 'id');
}
}

View File

@@ -1,58 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\channel\validate\manage;
use plugin\saiadmin\basic\BaseValidate;
/**
* 渠道验证器
*/
class ChannelManageValidate extends BaseValidate
{
/**
* 定义验证规则
*/
protected $rule = [
'name' => 'require',
'title' => 'require',
'admin_id' => 'require',
'game_url' => 'require',
'image' => 'require',
];
/**
* 定义错误信息
*/
protected $message = [
'name' => '名称必须填写',
'title' => '标题必须填写',
'admin_id' => '管理员必须填写',
'game_url' => '游戏地址必须填写',
'image' => '图标必须填写',
];
/**
* 定义场景agent 由后端按 md5(name.admin_id) 自动生成,无需校验)
*/
protected $scene = [
'save' => [
'name',
'title',
'admin_id',
'game_url',
'image',
],
'update' => [
'name',
'title',
'admin_id',
'game_url',
'image',
],
];
}