[菜单]渠道管理
This commit is contained in:
217
server/app/channel/controller/manage/ChannelManageController.php
Normal file
217
server/app/channel/controller/manage/ChannelManageController.php
Normal file
@@ -0,0 +1,217 @@
|
||||
<?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\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', ''],
|
||||
['total_recharge', ''],
|
||||
['total_withdrawal', ''],
|
||||
['total_profit', ''],
|
||||
]);
|
||||
$query = $this->logic->search($where);
|
||||
// 不使用 with('admin'):SystemUser 为 Think 模型,与 Eloquent 混用会触发 getConnectionName 报错,改为手动填充
|
||||
$data = $this->logic->getList($query);
|
||||
$data['data'] = $this->attachAdminToRows($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();
|
||||
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();
|
||||
$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');
|
||||
}
|
||||
$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('请选择要删除的数据');
|
||||
}
|
||||
$result = $this->logic->destroy($ids);
|
||||
if ($result) {
|
||||
return $this->success('删除成功');
|
||||
} else {
|
||||
return $this->fail('删除失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取管理员下拉列表(远程关联 SystemUser:value=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;
|
||||
}
|
||||
|
||||
}
|
||||
27
server/app/channel/logic/manage/ChannelManageLogic.php
Normal file
27
server/app/channel/logic/manage/ChannelManageLogic.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: your name
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\channel\logic\manage;
|
||||
|
||||
use plugin\saiadmin\basic\eloquent\BaseLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
use plugin\saiadmin\utils\Helper;
|
||||
use app\channel\model\manage\ChannelManage;
|
||||
|
||||
/**
|
||||
* 渠道逻辑层
|
||||
*/
|
||||
class ChannelManageLogic extends BaseLogic
|
||||
{
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->model = new ChannelManage();
|
||||
}
|
||||
|
||||
}
|
||||
103
server/app/channel/model/manage/ChannelManage.php
Normal file
103
server/app/channel/model/manage/ChannelManage.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?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 $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 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');
|
||||
}
|
||||
|
||||
}
|
||||
58
server/app/channel/validate/manage/ChannelManageValidate.php
Normal file
58
server/app/channel/validate/manage/ChannelManageValidate.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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',
|
||||
],
|
||||
];
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user