初始化提交

This commit is contained in:
maticarmy
2025-02-10 10:39:00 +08:00
commit 59cd2c19d1
491 changed files with 54545 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace app\manager\controller;
use think\admin\Controller;
/**
* 后台管理基础控制器
*/
class Base extends Controller
{
/**
* 初始化方法
*/
protected function initialize()
{
parent::initialize();
// 直接使用 admin 的权限验证
}
}

View File

@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace app\manager\controller;
class Error extends Base
{
public function forbidden()
{
return $this->fetch();
}
}

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace app\manager\controller;
use app\manager\sys\RedisManager;
use think\admin\Controller;
use think\admin\service\AdminService;
/**
* 后台首页控制器
*/
class Index extends Controller
{
/**
* 显示后台首页
*/
public function index()
{
$userid = AdminService::getUserId();
// 获取实例
$redis = RedisManager::getInstance();
$this->assign("userid", $userid);
$this->fetch();
}
}

View File

@@ -0,0 +1,216 @@
<?php
declare (strict_types = 1);
namespace app\manager\controller;
use app\manager\model\Member as MemberModel;
use think\admin\Controller;
use think\facade\Db;
/**
* 会员管理
* @auth true
* @menu true
*/
class Member extends Controller
{
/**
* 会员列表
* @auth true
* @menu true
*/
public function index()
{
$this->title = '会员管理';
if ($this->request->isGet() && $this->request->get('output') === 'layui.table') {
$where = [];
// 搜索条件
$keyword = input('keyword', '', 'trim');
if (!empty($keyword)) {
$where[] = ['email|order_id', 'like', "%{$keyword}%"];
}
// 状态筛选
$status = input('status', '', 'trim');
if ($status !== '') {
$where[] = ['status', '=', intval($status)];
}
// 查询数据
$query = MemberModel::where($where)->order('id desc');
$result = $query->paginate([
'list_rows' => input('limit', 20),
'page' => input('page', 1),
]);
return json([
'code' => 0,
'msg' => '',
'count' => $result->total(),
'data' => $result->items()
]);
}
return $this->fetch();
}
/**
* 添加会员
* @auth true
*/
public function add()
{
if ($this->request->isPost()) {
$data = $this->_vali([
'email.require' => '邮箱不能为空!',
'email.email' => '邮箱格式错误!',
'order_id.require' => '闲鱼订单号不能为空!',
'expire_time.require' => '有效期不能为空!',
'usage_limit.require' => '可用次数不能为空!',
'usage_limit.number' => '可用次数必须为数字!',
]);
// 检查唯一性
$map = [
['email', '=', $data['email']]
];
if (MemberModel::where($map)->count() > 0) {
$this->error('邮箱已存在!');
}
$map = [
['order_id', '=', $data['order_id']]
];
if (MemberModel::where($map)->count() > 0) {
$this->error('闲鱼订单号已存在!');
}
// 设置默认值
$data['status'] = isset($data['status']) ? 1 : 0;
$data['used_count'] = 0;
// 保存数据
if (MemberModel::create($data) !== false) {
$this->success('添加成功!');
} else {
$this->error('添加失败!');
}
}
$this->title = '添加会员';
return $this->fetch('form');
}
/**
* 编辑会员
* @auth true
*/
public function edit()
{
$id = input('id');
if (empty($id)) $this->error('参数错误!');
if ($this->request->isPost()) {
$data = $this->_vali([
'id.require' => 'ID不能为空',
'email.require' => '邮箱不能为空!',
'email.email' => '邮箱格式错误!',
'order_id.require' => '闲鱼订单号不能为空!',
'expire_time.require' => '有效期不能为空!',
'usage_limit.require' => '可用次数不能为空!',
'usage_limit.number' => '可用次数必须为数字!',
]);
// 检查唯一性
$map = [
['email', '=', $data['email']],
['id', '<>', $id]
];
if (MemberModel::where($map)->count() > 0) {
$this->error('邮箱已存在!');
}
$map = [
['order_id', '=', $data['order_id']],
['id', '<>', $id]
];
if (MemberModel::where($map)->count() > 0) {
$this->error('闲鱼订单号已存在!');
}
// 设置默认值
$data['status'] = isset($data['status']) ? 1 : 0;
$data['used_count'] = isset($data['used_count']) ? intval($data['used_count']) : 0;
// 保存数据
if (MemberModel::update($data) !== false) {
$this->success('编辑成功!');
} else {
$this->error('编辑失败!');
}
}
$this->title = '编辑会员';
$this->member = MemberModel::find($id);
return $this->fetch('form');
}
/**
* 修改状态
* @auth true
*/
public function state()
{
MemberModel::mSave($this->_vali([
'status.in:0,1' => '状态值范围异常!',
'status.require' => '状态值不能为空!',
]));
}
/**
* 删除会员
* @auth true
*/
public function remove()
{
MemberModel::mDelete();
}
/**
* 表单数据处理
* @param array $data
*/
protected function _form_filter(array &$data)
{
if ($this->request->isPost()) {
// 检查数据完整性
empty($data['username']) && $this->error('用户名不能为空!');
empty($data['order_id']) && $this->error('闲鱼订单号不能为空!');
empty($data['expire_time']) && $this->error('有效期不能为空!');
// 检查唯一性
$map = [];
if (empty($data['id'])) {
$map[] = ['username', '=', $data['username']];
$map[] = ['order_id', '=', $data['order_id']];
if (MemberModel::where($map)->count() > 0) {
$this->error('用户名或闲鱼订单号已存在!');
}
} else {
$map[] = ['id', '<>', $data['id']];
$map[] = ['username|order_id', '=', $data['username']];
if (MemberModel::where($map)->count() > 0) {
$this->error('用户名或闲鱼订单号已存在!');
}
}
// 设置默认值
$data['status'] = isset($data['status']) ? 1 : 0;
$data['used_count'] = isset($data['used_count']) ? intval($data['used_count']) : 0;
$data['balance'] = isset($data['balance']) ? floatval($data['balance']) : 0;
$data['points'] = isset($data['points']) ? intval($data['points']) : 0;
$data['level'] = isset($data['level']) ? intval($data['level']) : 1;
}
}
}

View File

@@ -0,0 +1,503 @@
<?php
declare(strict_types=1);
namespace app\manager\controller;
use think\admin\Controller;
use think\admin\service\AdminService;
/**
* 包名管理
* @auth true # 表示需要验证权限
* @menu true # 表示可以生成菜单
*/
class Package extends Controller
{
/**
* 绑定数据表
* @var string
*/
protected $table = 'offer_package';
/**
* 检查包名权限
* @param int $packageId 包名ID
* @return bool
*/
protected function checkPackageAuth($packageId)
{
// 超级管理员直接放行
if (AdminService::instance()->isSuper()) {
return true;
}
// 检查用户是否有包名管理模块的权限
if (!AdminService::instance()->check('package/index')) {
return false;
}
// 检查具体包名权限
$userId = AdminService::instance()->getUserId();
$exists = $this->app->db->name('offer_package_auth')
->where(['package_id' => $packageId, 'user_id' => $userId])
->find();
return !empty($exists);
}
/**
* 获取当前用户可访问的包名ID列表
*/
protected function getAuthPackageIds()
{
// 超级管理员可以访问所有
if (AdminService::instance()->isSuper()) {
return [];
}
// 如果没有包名管理模块权限,则不能访问任何包名
if (!AdminService::instance()->check('package/index')) {
return [-1];
}
// 获取用户被授权的包名列表
$userId = AdminService::instance()->getUserId();
$packageIds = $this->app->db->name('offer_package_auth')
->where(['user_id' => $userId])
->column('package_id');
return $packageIds ?: [-1];
}
/**
* 包名列表
* @auth true # 需要验证权限
* @menu true # 菜单可见
*/
public function index()
{
if ($this->request->isPost()) {
try {
// 获取请求参数
$page = $this->request->post('page/d', 1);
$limit = $this->request->post('limit/d', 15);
$where = [];
// 包名搜索
if ($package_name = $this->request->post('package_name/s', '')) {
$where[] = ['package_name', 'like', "%{$package_name}%"];
}
// 应用名称搜索
if ($name = $this->request->post('name/s', '')) {
$where[] = ['name', 'like', "%{$name}%"];
}
// 状态筛选
if ($status = $this->request->post('status/s', '')) {
$where[] = ['status', '=', $status];
}
// 创建人搜索
if ($username = $this->request->post('username/s', '')) {
$where[] = ['username', 'like', "%{$username}%"];
}
// 时间范围处理
if ($add_time = $this->request->post('add_time/s', '')) {
$times = explode(' - ', $add_time);
if (count($times) == 2) {
$start_time = strtotime($times[0] . ' 00:00:00');
$end_time = strtotime($times[1] . ' 23:59:59');
$where[] = ['add_time', 'between', [$start_time, $end_time]];
}
}
// 查询数据
$query = $this->app->db->name('offer_package')->alias('p');
// 非超级管理员需要进行权限过滤
if (!AdminService::instance()->isSuper()) {
// 检查包名管理模块权限
if (!AdminService::instance()->check('package/index')) {
return json([
'code' => 0,
'msg' => '',
'count' => 0,
'data' => []
]);
}
$userId = AdminService::instance()->getUserId();
// 通过左连接查询有权限的包名
$query->leftJoin('offer_package_auth pa', 'p.id = pa.package_id')
->where('pa.user_id', $userId);
}
// 先获取总数
$total = $query->where($where)->count();
// 分页查询数据
$list = $query->where($where)
->field('p.*') // 只查询包名表的字段
->order('p.id desc')
->group('p.id') // 防止重复数据
->page($page, $limit)
->select()
->toArray();
return json([
'code' => 0,
'msg' => '',
'count' => $total,
'data' => $list
]);
} catch (\Exception $e) {
trace("获取包名列表异常:" . $e->getMessage());
return json([
'code' => 1,
'msg' => '获取数据失败,请稍后重试',
'count' => 0,
'data' => []
]);
}
}
$this->fetch();
}
/**
* 添加包名
* @auth true
* @menu true
*/
public function add()
{
if ($this->request->isPost()) {
// 开启事务
$this->app->db->startTrans();
try {
$data = $this->_vali([
'package_name.require' => '包名不能为空!',
'name.require' => '应用名称不能为空!',
'status.require' => '状态不能为空!',
'status.in:0,1' => '状态值不正确!'
]);
// 检查包名是否已存在
$exists = $this->app->db->name('offer_package')
->where(['package_name' => $data['package_name']])
->find();
if ($exists) {
$this->app->db->rollback();
return json(['code' => 0, 'info' => '包名已存在,请更换!']);
}
// 添加时间字段
$now = time();
$data['add_time'] = $now;
$data['update_time'] = $now;
$data['username'] = session('user.username') ?? '';
// 插入包名数据
$result = $this->app->db->name('offer_package')->insertGetId($data);
if ($result === false) {
$this->app->db->rollback();
return json(['code' => 0, 'info' => '添加失败,请重试!']);
}
// 非超级管理员需要添加权限记录
if (!AdminService::instance()->isSuper()) {
$authData = [
'package_id' => $result,
'user_id' => AdminService::instance()->getUserId(),
'create_at' => date('Y-m-d H:i:s')
];
$authResult = $this->app->db->name('offer_package_auth')->insert($authData);
if ($authResult === false) {
$this->app->db->rollback();
return json(['code' => 0, 'info' => '添加权限记录失败!']);
}
}
$this->app->db->commit();
sysoplog('积分墙列表', '添加成功');
return json(['code' => 1, 'info' => '添加成功!']);
} catch (\Exception $e) {
$this->app->db->rollback();
trace("添加包名异常:" . $e->getMessage());
return json(['code' => 0, 'info' => '系统异常,请稍后重试!']);
}
}
}
/**
* 编辑包名
* @auth true
* @menu true
*/
public function edit()
{
if ($this->request->isPost()) {
try {
$data = $this->_vali([
'id.require' => '记录ID不能为空',
'package_name.require' => '包名不能为空!',
'name.require' => '应用名称不能为空!',
'status.require' => '状态不能为空!',
'status.in:0,1' => '状态值不正确!'
]);
// 检查权限
if (!$this->checkPackageAuth($data['id'])) {
return json(['code' => 0, 'info' => '您没有操作此包名的权限!']);
}
// 检查包名是否已存在(排除当前记录)
$exists = $this->app->db->name('offer_package')
->where('package_name', $data['package_name'])
->where('id', '<>', $data['id'])
->find();
if ($exists) {
return json(['code' => 0, 'info' => '包名已存在,请更换!']);
}
// 更新时间
$data['update_time'] = time();
$data['username'] = session('user.username') ?? '';
$result = $this->app->db->name('offer_package')
->where(['id' => $data['id']])
->update($data);
if ($result === false) {
return json(['code' => 0, 'info' => '更新失败,请重试!']);
}
sysoplog('积分墙列表', '更新成功');
return json(['code' => 1, 'info' => '更新成功!']);
} catch (\Exception $e) {
trace("编辑包名异常:" . $e->getMessage());
return json(['code' => 0, 'info' => '系统异常,请稍后重试!']);
}
}
}
/**
* 删除包名
* @auth true
* @menu true
*/
public function remove()
{
if ($this->request->isPost()) {
// 开启事务
$this->app->db->startTrans();
try {
$id = $this->request->param('id');
if (empty($id)) {
return json(['code' => 0, 'info' => '参数错误,请刷新重试!']);
}
// 检查权限
if (!$this->checkPackageAuth($id)) {
return json(['code' => 0, 'info' => '您没有操作此包名的权限!']);
}
// 删除包名记录
$result = $this->app->db->name('offer_package')
->where(['id' => $id])
->delete();
if ($result === false) {
$this->app->db->rollback();
return json(['code' => 0, 'info' => '删除失败,请重试!']);
}
// 同步删除权限表中的相关记录
$this->app->db->name('offer_package_auth')
->where(['package_id' => $id])
->delete();
$this->app->db->commit();
sysoplog('积分墙列表', '删除成功');
return json(['code' => 1, 'info' => '删除成功!']);
} catch (\Exception $e) {
$this->app->db->rollback();
trace("删除包名异常:" . $e->getMessage());
return json(['code' => 0, 'info' => '系统异常,请稍后重试!']);
}
}
}
/**
* 切换状态
* @auth true
* @menu true
*/
public function state()
{
if ($this->request->isPost()) {
try {
$data = $this->_vali([
'id.require' => '记录ID不能为空',
'status.in:0,1' => '状态值不正确!'
]);
// 检查权限
if (!$this->checkPackageAuth($data['id'])) {
return json(['code' => 0, 'info' => '您没有操作此包名的权限!']);
}
// 检查记录是否存在
$exists = $this->app->db->name('offer_package')
->where(['id' => $data['id']])
->find();
if (empty($exists)) {
return json(['code' => 0, 'info' => '记录不存在!']);
}
// 更新状态
$updateData = [
'status' => $data['status'],
'update_time' => time(),
'username' => session('user.username') ?? ''
];
$result = $this->app->db->name('offer_package')
->where(['id' => $data['id']])
->update($updateData);
if ($result === false) {
return json(['code' => 0, 'info' => '状态更新失败,请重试!']);
}
sysoplog('积分墙列表', '状态更新成功');
return json(['code' => 1, 'info' => '状态更新成功!']);
} catch (\Exception $e) {
trace("状态切换异常:" . $e->getMessage() . "\n" . $e->getTraceAsString());
return json(['code' => 0, 'info' => '系统异常,请稍后重试!']);
}
}
}
/**
* 生成测试数据
* @auth true
*/
public function generateTestData()
{
try {
// 设置执行时间为无限制
set_time_limit(0);
ini_set('memory_limit', '1024M');
// 开启事务
$this->app->db->startTrans();
try {
$insertData = [];
$existPackages = [];
$total = 8000; // 总记录数
$batchSize = 500; // 每批插入数量
$startTime = time();
// 获取已存在的包名,避免重复
$exists = $this->app->db->name($this->table)
->column('package_name');
$existPackages = array_flip($exists);
// 预生成一些常用词,提高应用名称的真实性
$prefixes = ['Super', 'My', 'Fast', 'Smart', 'Easy', 'Pro', 'Best', 'Quick'];
$suffixes = ['Browser', 'Player', 'Cleaner', 'Manager', 'Tool', 'Helper', 'App', 'Game'];
$domains = ['com', 'net', 'org', 'app', 'tech', 'io'];
// 生成记录
for ($i = 0; $i < $total; $i++) {
// 生成唯一包名
do {
// 生成更真实的包名 xxx.xxx.xxx.xxx
$domain = $domains[array_rand($domains)];
$company = $this->generateRandomString(rand(3, 8));
$product = $this->generateRandomString(rand(3, 8));
$module = $this->generateRandomString(rand(3, 8));
$packageName = "{$domain}.{$company}.{$product}.{$module}";
} while (isset($existPackages[$packageName]));
$existPackages[$packageName] = 1;
// 生成更真实的应用名称
$prefix = $prefixes[array_rand($prefixes)];
$suffix = $suffixes[array_rand($suffixes)];
$appName = $prefix . ' ' . $suffix . ' ' . ($i + 1);
// 生成数据
$insertData[] = [
'package_name' => $packageName,
'name' => $appName,
'status' => rand(0, 1), // 随机状态
'add_time' => time() - rand(0, 86400 * 30), // 随机时间最近30天内
'update_time' => time(),
'username' => session('user.username') ?? 'system'
];
// 批量插入
if (count($insertData) >= $batchSize) {
$this->app->db->name($this->table)->insertAll($insertData);
$insertData = [];
// 输出进度
$progress = round(($i + 1) / $total * 100, 2);
trace("数据生成进度:{$progress}%");
}
}
// 插入剩余数据
if (!empty($insertData)) {
$this->app->db->name($this->table)->insertAll($insertData);
}
$endTime = time();
$timeUsed = $endTime - $startTime;
$this->app->db->commit();
return json([
'code' => 1,
'info' => "测试数据生成成功!共生成 {$total} 条记录,耗时 {$timeUsed}"
]);
} catch (\Exception $e) {
$this->app->db->rollback();
throw $e;
}
} catch (\Exception $e) {
trace("生成测试数据异常:" . $e->getMessage() . "\n" . $e->getTraceAsString());
return json(['code' => 0, 'info' => '系统异常:' . $e->getMessage()]);
}
}
/**
* 生成随机字符串
* @param int $length 长度
* @return string
*/
private function generateRandomString($length = 5)
{
// 添加数字,使包名更真实
$characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
$result = '';
// 第一个字符必须是字母
$result .= $characters[rand(0, 25)];
// 生成剩余字符
for ($i = 1; $i < $length; $i++) {
$result .= $characters[rand(0, strlen($characters) - 1)];
}
return $result;
}
}

View File

@@ -0,0 +1,918 @@
<?php
declare(strict_types=1);
namespace app\manager\controller;
use think\admin\Controller;
use think\admin\service\AdminService;
class PackageAuth extends Controller
{
/**
* 绑定数据表
* @var string
*/
protected $table = 'offer_package_auth';
/**
* 包名权限列表
* @auth true
* @menu true
*/
public function index()
{
if ($this->request->isGet()) {
// 获取所有管理员(排除超级管理员)
$users = $this->app->db->name('system_user')
->where([
['status', '=', 1], // 状态正常的用户
['authorize', 'not like', '%admin%'], // 排除admin权限组的用户
['id', '<>', '10000'] // 排除系统超管账号
])
->field('id,username,authorize')
->order('id asc')
->select()
->filter(function($user) {
// 只显示有包名管理权限的用户
return AdminService::instance()->check('package/index', $user['id']);
})
->toArray();
// 获取现有权限配置
$auths = $this->app->db->name($this->table)
->select()
->toArray();
// 整理权限数据
$authMap = [];
foreach ($auths as $auth) {
$authMap[$auth['user_id']][] = $auth['package_id'];
}
$this->assign([
'users' => $users,
'authMap' => $authMap
]);
$this->fetch();
}
}
/**
* 获取包名列表(分页)
* @auth true
*/
public function getPackageList()
{
if ($this->request->isPost()) {
try {
$page = $this->request->post('page/d', 1);
$limit = $this->request->post('limit/d', 20);
$keyword = $this->request->post('keyword/s', '');
$startTime = $this->request->post('start_time/s', '');
$endTime = $this->request->post('end_time/s', '');
$where = [];
// 只显示启用的包名
$where[] = ['status', '=', 1];
if ($keyword !== '') {
$where[] = ['package_name|name', 'like', "%{$keyword}%"];
}
// 修改时间筛选条件处理方式
if ($startTime && $endTime) {
$where[] = ['add_time', 'between', [
strtotime($startTime),
strtotime($endTime)
]];
}
// 使用正确的表名
$query = $this->app->db->name('offer_package');
// 先获取总数
$total = $query->where($where)->count();
// 获取列表数据
$list = $query->where($where)
->field('id,package_name,name,add_time,update_time,status') // 修改字段名
->order('id desc')
->page($page, $limit)
->select()
->toArray();
// 格式化时间戳
foreach ($list as &$item) {
$item['add_time'] = date('Y-m-d H:i:s', intval($item['add_time']));
$item['update_time'] = date('Y-m-d H:i:s', intval($item['update_time']));
}
// 记录调试信息
trace("包名列表查询:" . json_encode([
'where' => $where,
'page' => $page,
'limit' => $limit,
'total' => $total,
'list_count' => count($list),
'sql' => $query->getLastSql()
], JSON_UNESCAPED_UNICODE));
return json([
'code' => 0,
'msg' => '',
'count' => $total,
'data' => $list
]);
} catch (\Exception $e) {
// 详细记录异常信息
trace("获取包名列表异常:" . $e->getMessage() . "\n" . $e->getTraceAsString());
return json(['code' => 1, 'msg' => '系统异常,请稍后重试!']);
}
}
}
/**
* 获取用户包名权限
* @auth true
* @api true
*/
public function getUserAuth()
{
if ($this->request->isPost()) {
try {
$userId = $this->request->post('user_id/d', 0);
if (empty($userId)) {
return json(['code' => 0, 'info' => '请选择管理员!']);
}
// 只获取包名ID列表
$packageIds = $this->app->db->name($this->table)
->where('user_id', $userId)
->column('package_id'); // 直接返回ID数组
return json([
'code' => 1,
'info' => '获取成功!',
'data' => $packageIds // 直接返回ID数组供 getPackageDetails 使用
]);
} catch (\Exception $e) {
trace("获取用户权限异常:{$e->getMessage()}\n" . $e->getTraceAsString());
return json(['code' => 0, 'info' => '系统异常,请稍后重试!']);
}
}
}
/**
* 批量授权
* @auth true
*/
public function batchAuth()
{
if ($this->request->isPost()) {
try {
$data = $this->_vali([
'user_ids.require' => '请选择管理员!',
'package_ids.require' => '请选择包名!'
]);
// 验证所选用户是否都有包名管理权限
foreach ($data['user_ids'] as $userId) {
if (!AdminService::instance()->check('package/index', $userId)) {
return json(['code' => 0, 'info' => '选中的用户中有人没有包名管理权限!']);
}
}
// 开启事务
$this->app->db->startTrans();
try {
foreach ($data['user_ids'] as $userId) {
// 删除原有权限
$this->app->db->name($this->table)
->where('user_id', $userId)
->delete();
// 添加新权限
$insertData = [];
foreach ($data['package_ids'] as $packageId) {
$insertData[] = [
'user_id' => $userId,
'package_id' => $packageId,
'create_at' => date('Y-m-d H:i:s')
];
}
if (!empty($insertData)) {
$this->app->db->name($this->table)->insertAll($insertData);
}
}
$this->app->db->commit();
sysoplog('积分墙权限', '批量授权成功');
return json(['code' => 1, 'info' => '批量授权成功!']);
} catch (\Exception $e) {
$this->app->db->rollback();
throw $e;
}
} catch (\Exception $e) {
trace("批量授权异常:" . $e->getMessage());
return json(['code' => 0, 'info' => '系统异常请稍后重<E5908E><E9878D><EFBFBD>']);
}
}
}
/**
* 清空
* @auth true
*/
public function clearAuth()
{
if ($this->request->isPost()) {
try {
$post = $this->request->post();
// 验证用户ID
if (empty($post['user_ids']) || !is_array($post['user_ids'])) {
return json(['code' => 0, 'info' => '选择管理员!']);
}
// 开启事务
$this->app->db->startTrans();
try {
foreach ($post['user_ids'] as $userId) {
// 删除用户的所有权限
$this->app->db->name($this->table)
->where('user_id', $userId)
->delete();
}
$this->app->db->commit();
sysoplog('积分墙权限', '权限清空成功');
return json(['code' => 1, 'info' => '权限清空成功!']);
} catch (\Exception $e) {
$this->app->db->rollback();
trace('清空权限事务异常:' . $e->getMessage());
throw $e;
}
} catch (\Exception $e) {
trace("清空权限异常:" . $e->getMessage());
return json(['code' => 0, 'info' => '系统异常,请稍后重试!']);
}
}
}
/**
* 获取包名详情
*/
public function getPackageDetails()
{
if ($this->request->isPost()) {
try {
$packageIds = $this->request->post('package_ids/a', []);
// 记录请求参数
trace("getPackageDetails 请求参数:" . json_encode([
'package_ids' => $packageIds
], JSON_UNESCAPED_UNICODE));
// 如果没有包名ID返回空数组
if (empty($packageIds)) {
trace("getPackageDetails包名ID为空");
return json([
'code' => 1,
'info' => '获取成功!',
'data' => []
]);
}
// 获取包名详情
$packages = $this->app->db->name('offer_package')
->whereIn('id', $packageIds)
->field('id,package_name,name,status')
->select()
->toArray();
return json([
'code' => 1,
'info' => '获取成功!',
'data' => $packages
]);
} catch (\Exception $e) {
trace("getPackageDetails 异常:" . $e->getMessage());
trace("getPackageDetails 异常堆栈:" . $e->getTraceAsString());
return json(['code' => 0, 'info' => '系统异常,请稍后重试!#3']);
}
}
}
/**
* 获取未授权的包名列表
* @auth true
*/
public function getUnauthorizedPackages()
{
// 同时支持 GET 和 POST 请求
$userId = $this->request->param('user_id/d', 0);
$keyword = $this->request->param('keyword/s', '');
$page = $this->request->param('page/d', 1);
$limit = $this->request->param('limit/d', 10);
try {
if (empty($userId)) {
return json(['code' => 0, 'info' => '参数错误!']);
}
// 获取已授权的包名ID
$authorizedIds = $this->app->db->name($this->table)
->where('user_id', $userId)
->column('package_id');
// 构建查询条件
$where = [];
// 排除已授权的包名
if (!empty($authorizedIds)) {
$where[] = ['id', 'not in', $authorizedIds];
}
// 搜索条件
if ($keyword !== '') {
$where[] = ['package_name|name', 'like', "%{$keyword}%"];
}
// 只显示启用的包名
$where[] = ['status', '=', 1];
// 查询数据
$query = $this->app->db->name('offer_package');
// 获取总数
$total = $query->where($where)->count();
// 获取列表
$list = $query->where($where)
->field('id,package_name,name,status')
->order('status desc,id desc')
->limit(($page - 1) * $limit, $limit)
->select()
->toArray();
// 记录调试信息
trace('未授权包名查询:' . json_encode([
'user_id' => $userId,
'keyword' => $keyword,
'where' => $where,
'sql' => $query->getLastSql(),
'total' => $total,
'list_count' => count($list)
], JSON_UNESCAPED_UNICODE));
return json([
'code' => 0, // layui table 要求成功码为 0
'msg' => '', // layui table 使用 msg 而不是 info
'count' => $total,
'data' => $list
]);
} catch (\Exception $e) {
trace("获取未授权包名异常:" . $e->getMessage() . "\n" . $e->getTraceAsString());
return json(['code' => 1, 'msg' => '系统异常,请稍后重试!']); // layui table 要求失败码为非 0
}
}
/**
* 批量添加授权
* @auth true
*/
public function addAuth()
{
if ($this->request->isPost()) {
try {
$userId = $this->request->post('user_id/d', 0);
$packageIds = $this->request->post('package_ids/a');
$isAll = $this->request->post('is_all/d', 0); // 是否全部授权
if (empty($userId)) {
return json(['code' => 0, 'info' => '参数错误!']);
}
// 开启事务
$this->app->db->startTrans();
try {
if ($isAll) {
// 优化: 使用INSERT INTO SELECT语法直接插入
$sql = "INSERT INTO {$this->table} (user_id, package_id, create_at)
SELECT :user_id, id, :create_at
FROM offer_package
WHERE status = 1
AND id NOT IN (
SELECT package_id
FROM {$this->table}
WHERE user_id = :user_id2
)";
$this->app->db->execute($sql, [
'user_id' => $userId,
'user_id2' => $userId,
'create_at' => date('Y-m-d H:i:s')
]);
} else {
if (empty($packageIds)) {
return json(['code' => 0, 'info' => '请选择要授权的包名!']);
}
// 过滤掉已授权的包名
$existIds = $this->app->db->name($this->table)
->where('user_id', $userId)
->whereIn('package_id', $packageIds)
->column('package_id');
$newPackageIds = array_diff($packageIds, $existIds);
if (!empty($newPackageIds)) {
$insertData = array_map(function($packageId) use ($userId) {
return [
'user_id' => $userId,
'package_id' => $packageId,
'create_at' => date('Y-m-d H:i:s')
];
}, $newPackageIds);
$this->app->db->name($this->table)->insertAll($insertData);
}
}
$this->app->db->commit();
sysoplog('积分墙权限', '授权添加成功!');
return json(['code' => 1, 'info' => '授权添加成功!']);
} catch (\Exception $e) {
$this->app->db->rollback();
throw $e;
}
} catch (\Exception $e) {
trace("添加授权异常:" . $e->getMessage());
return json(['code' => 0, 'info' => '系统异常,请稍后重试!']);
}
}
}
/**
* 移除单个包名权限
* @auth true
*/
public function removeAuth()
{
if ($this->request->isPost()) {
try {
$userId = $this->request->post('user_id/d', 0);
$packageId = $this->request->post('package_id/d', 0);
if (empty($userId) || empty($packageId)) {
return json(['code' => 0, 'info' => '参数错误!']);
}
$result = $this->app->db->name($this->table)
->where([
'user_id' => $userId,
'package_id' => $packageId
])
->delete();
if ($result !== false) {
sysoplog('积分墙权限', '权限移除成功!');
return json(['code' => 1, 'info' => '权限移除成功!']);
} else {
return json(['code' => 0, 'info' => '权限移除失败!']);
}
} catch (\Exception $e) {
trace("移除权限异常:" . $e->getMessage());
return json(['code' => 0, 'info' => '系统异常,请稍后重试!']);
}
}
}
/**
* 首页批量授权保存
* @auth true
*/
public function batchSaveAuth()
{
if ($this->request->isPost()) {
try {
$data = $this->_vali([
'user_ids.require' => '请选择管理员!',
'package_ids.require' => '请选择包名!'
]);
// 开启事务
$this->app->db->startTrans();
try {
foreach ($data['user_ids'] as $userId) {
// 删除原有权限
$this->app->db->name($this->table)
->where('user_id', $userId)
->delete();
// 添加新权限
$insertData = [];
foreach ($data['package_ids'] as $packageId) {
$insertData[] = [
'user_id' => $userId,
'package_id' => $packageId,
'create_at' => date('Y-m-d H:i:s')
];
}
if (!empty($insertData)) {
$this->app->db->name($this->table)->insertAll($insertData);
}
}
$this->app->db->commit();
sysoplog('积分墙权限', '批量授权成功!');
return json(['code' => 1, 'info' => '批量授权成功!']);
} catch (\Exception $e) {
$this->app->db->rollback();
throw $e;
}
} catch (\Exception $e) {
trace("首页批量授权异常:" . $e->getMessage());
return json(['code' => 0, 'info' => '系统异常,请稍后重试!']);
}
}
}
/**
* 批量移除包名权限
* @auth true
*/
public function batchRemoveAuth()
{
if ($this->request->isPost()) {
try {
$userId = $this->request->post('user_id/d', 0);
$packageIds = $this->request->post('package_ids/a');
if (empty($userId) || empty($packageIds)) {
return json(['code' => 0, 'info' => '参数错误!']);
}
// 开启事务
$this->app->db->startTrans();
try {
// 批量删除权限
$result = $this->app->db->name($this->table)
->where('user_id', $userId)
->whereIn('package_id', $packageIds)
->delete();
if ($result !== false) {
$this->app->db->commit();
sysoplog('积分墙权限', '批量移除成功!');
return json(['code' => 1, 'info' => '批量移除成功!']);
} else {
$this->app->db->rollback();
return json(['code' => 0, 'info' => '批量移除失败!']);
}
} catch (\Exception $e) {
$this->app->db->rollback();
throw $e;
}
} catch (\Exception $e) {
trace("批移除权限异:" . $e->getMessage());
return json(['code' => 0, 'info' => '系统异常,请稍后重试!']);
}
}
}
/**
* 显示权限详情页面
* @auth true
*/
public function authDetail()
{
try {
$userId = input('user_id/d');
if (!$userId) {
$this->error('参数错误');
}
// 获取用户基本信息
$user = $this->app->db->name('system_user')
->where('id', $userId)
->field('id,username,status,create_at')
->find();
if (!$user) {
$this->error('用户不存在');
}
// 获取用户权限统计
$stats = [
'total' => $this->app->db->name($this->table)
->where('user_id', $userId)
->count(),
'active' => $this->app->db->name('offer_package')
->alias('p')
->join("{$this->table} a", 'p.id = a.package_id')
->where([
'a.user_id' => $userId,
'p.status' => 1
])
->count()
];
$this->assign([
'user' => $user,
'stats' => $stats,
'title' => '授权包名管理'
]);
return $this->fetch();
} catch (\Exception $e) {
$this->app->log->error("显示权限详情页面异常:{$e->getMessage()}");
$this->error('系统异常,请稍后重试!');
}
}
/**
* 获取权限详情数据
* @auth true
* @api true
*/
public function getAuthDetailData()
{
try {
$get = $this->_vali([
'user_id.require' => '用户ID不能为空',
'type.require' => '类型不能为空',
'page.default' => 1,
'limit.default' => 20,
'keyword.default' => '',
'sort.default' => 'id',
'order.default' => 'desc'
]);
// 确保分页参数为整数
$page = intval($get['page']);
$limit = intval($get['limit']);
// 构建基础查询
$query = $this->app->db->name('offer_package')
->alias('p');
if ($get['type'] === 'authorized') {
// 已授权包名查询
$query->join("{$this->table} a", 'p.id = a.package_id')
->where('a.user_id', $get['user_id']);
} else {
// 未授权包名查询
$query->whereNotExists(function($query) use ($get) {
$query->table($this->table)
->where('package_id=p.id')
->where('user_id', $get['user_id']);
})
->where('p.status', 1);
}
// 关键词搜索
if ($get['keyword'] !== '') {
$query->whereLike('p.package_name|p.name', "%{$get['keyword']}%");
}
// 获取总数
$total = $query->count();
// 获取列表数据
$list = $query->field([
'p.id',
'p.package_name',
'p.name',
'p.status',
$get['type'] === 'authorized' ? 'a.create_at' : 'p.add_time as create_at'
])
->order("{$get['sort']} {$get['order']}")
->page($page, $limit) // 使用转换后的整数值
->select()
->toArray();
// 格式化时间
foreach ($list as &$item) {
$item['create_at'] = format_datetime($item['create_at']);
$item['status_text'] = $item['status'] ? '启用' : '停用';
}
return json([
'code' => 0,
'msg' => '',
'count' => $total,
'data' => $list
]);
} catch (\Exception $e) {
trace("获取权限详情数据异常:" . $e->getMessage() . "\n" . $e->getTraceAsString());
return json(['code' => 1, 'msg' => '系统异常,请稍后重试!']);
}
}
/**
* 获取已授权的包名列表
* @auth true
*/
public function getAuthorizedPackages()
{
try {
$userId = $this->request->param('user_id/d', 0);
$keyword = $this->request->param('keyword/s', '');
$page = $this->request->param('page/d', 1);
$limit = $this->request->param('limit/d', 10);
if (empty($userId)) {
return json(['code' => 1, 'msg' => '参数错误']);
}
// 优化1: 使用子查询优化 JOIN
$query = $this->app->db->name('offer_package')
->whereExists(function($query) use ($userId) {
$query->table($this->table)
->where('package_id=offer_package.id')
->where('user_id', $userId);
})
->where('status', 1);
// 优化2: 添加索引字段的索条件
if ($keyword !== '') {
$query->where(function($query) use ($keyword) {
$query->whereOr([
['package_name', 'like', "%{$keyword}%"],
['name', 'like', "%{$keyword}%"]
]);
});
}
// 优化3: 使用子查询获取总数,避免重复JOIN
$total = $query->count();
// 优化4: 只查询需要的字段
$list = $query->field([
'id',
'package_name',
'name',
'status',
"(SELECT create_at FROM {$this->table} WHERE package_id=offer_package.id AND user_id={$userId} LIMIT 1) as create_at"
])
->order('id desc')
->page($page, $limit)
->select()
->toArray();
// 优化5: 添加缓存
$cacheKey = "auth_packages_{$userId}_{$page}_{$limit}_" . md5($keyword);
cache($cacheKey, $list, 300); // 缓存5分钟
return json([
'code' => 0,
'msg' => '',
'count' => $total,
'data' => $list
]);
} catch (\Exception $e) {
trace("获取已授权包名列表异常:" . $e->getMessage() . "\n" . $e->getTraceAsString());
return json(['code' => 1, 'msg' => '系统异常,请稍后重试!']);
}
}
/**
* 获取授权包名列表(分页)
* @auth true
* @api true
*/
public function getAuthPackageList()
{
try {
$get = $this->_vali([
'user_id.require' => '用户ID不能为空',
'page.default' => 1,
'limit.default' => 10,
'keyword.default' => '',
'status.default' => 1,
'sort.default' => 'id',
'order.default' => 'desc'
]);
// 构建基础查询
$query = $this->app->db->name('offer_package')
->alias('p')
->join("{$this->table} a", 'p.id = a.package_id')
->where([
'a.user_id' => $get['user_id'],
'p.status' => $get['status']
]);
// 关键词搜索
if ($get['keyword'] !== '') {
$query->whereLike('p.package_name|p.name', "%{$get['keyword']}%");
}
// 获取总数
$total = $query->count();
// 获取分页数据
$list = $query->field([
'p.id',
'p.package_name',
'p.name',
'p.status',
'a.create_at',
'p.update_time'
])
->order("{$get['sort']} {$get['order']}")
->limit(($get['page'] - 1) * $get['limit'], $get['limit'])
->select()
->toArray();
// 格式化时间
foreach ($list as &$item) {
$item['create_at'] = format_datetime($item['create_at']);
$item['update_time'] = format_datetime($item['update_time']);
}
// 使用缓存
$cacheKey = "auth_package_list_{$get['user_id']}_{$get['page']}_{$get['limit']}_" . md5($get['keyword']);
cache($cacheKey, [
'total' => $total,
'list' => $list
], 300); // 缓存5分钟
return json([
'code' => 0,
'msg' => 'success',
'total' => $total,
'list' => $list,
'page' => $get['page'],
'limit' => $get['limit']
]);
} catch (\Exception $e) {
$this->app->log->error("获取授权包名列表异常:{$e->getMessage()}");
return json([
'code' => 1,
'msg' => '获取数据失败:' . $e->getMessage()
]);
}
}
/**
* 获取授权包名统计
* @auth true
* @api true
*/
public function getAuthPackageStats()
{
try {
$userId = input('user_id/d', 0);
if (empty($userId)) {
return json(['code' => 1, 'msg' => '用户ID不能为空']);
}
// 获取统计数据
$stats = [
'total' => $this->app->db->name($this->table)
->where('user_id', $userId)
->count(),
'active' => $this->app->db->name('offer_package')
->alias('p')
->join("{$this->table} a", 'p.id = a.package_id')
->where([
'a.user_id' => $userId,
'p.status' => 1
])
->count(),
'latest' => $this->app->db->name($this->table)
->where('user_id', $userId)
->order('create_at desc')
->value('create_at')
];
$stats['latest'] = $stats['latest'] ? format_datetime($stats['latest']) : '';
return json([
'code' => 0,
'msg' => 'success',
'data' => $stats
]);
} catch (\Exception $e) {
$this->app->log->error("获取授权包名统计异常:{$e->getMessage()}");
return json([
'code' => 1,
'msg' => '获取统计失败:' . $e->getMessage()
]);
}
}
}

View File

@@ -0,0 +1,495 @@
<?php
declare(strict_types=1);
namespace app\manager\controller;
use think\admin\Controller;
use think\admin\service\AdminService;
/**
* 包名回传配置管理
* @auth true
* @menu true
*/
class PackageCallback extends Controller
{
/**
* 绑定数据表
* @var string
*/
protected $table = 'offer_package_callback';
/**
* 回传配置列表
* @auth true
* @menu true
*/
public function index()
{
// 设置页面标题
$this->title = '回传配置管理';
$this->fetch();
}
/**
* 获取回传配置列表数据
* @auth true
*/
public function get_list()
{
try {
// 获取请求参数
$page = input('page/d', 1);
$limit = input('limit/d', 15);
$where = [];
// 包名搜索
if ($package_name = input('package_name/s', '')) {
$where[] = ['package_name', 'like', "%{$package_name}%"];
}
// 事件名称搜索
if ($event_name = input('event_name/s', '')) {
$where[] = ['event_name', 'like', "%{$event_name}%"];
}
// 状态筛选
if ($status = input('status/s', '')) {
$where[] = ['status', '=', $status];
}
// 非超级管理员需要进行权限过滤
if (!AdminService::instance()->isSuper()) {
// 检查包名管理模块权限
if (!AdminService::instance()->check('package/index')) {
return json([
'code' => 0,
'msg' => '',
'count' => 0,
'data' => []
]);
}
$userId = AdminService::instance()->getUserId();
// 获取有权限的包名ID列表
$packageIds = $this->app->db->name('offer_package_auth')
->where('user_id', $userId)
->column('package_id');
if (empty($packageIds)) {
return json([
'code' => 0,
'msg' => '',
'count' => 0,
'data' => []
]);
}
$where[] = ['package_id', 'in', $packageIds];
}
// 查询数据
$query = $this->app->db->name($this->table);
// 先获取总数
$total = $query->where($where)->count();
// 分页查询数据
$list = $query->where($where)
->field('*')
->order('id desc')
->limit(($page - 1) * $limit, $limit)
->select()
->toArray();
return json([
'code' => 0,
'msg' => '',
'count' => $total,
'data' => $list
]);
} catch (\Exception $e) {
trace("获取回传配置列表异常:" . $e->getMessage());
return json([
'code' => 1,
'msg' => '获取数据失败,请稍后重试',
'count' => 0,
'data' => []
]);
}
}
/**
* 添加回传配置
* @auth true
*/
public function add()
{
if ($this->request->isGet()) {
// 设置页面标题
$this->title = '添加回传配置';
$this->fetch('add'); // 明确指定渲染 add.html
}
if ($this->request->isPost()) {
try {
$data = $this->_vali([
'package_id.require' => '包名ID不能为空',
'package_name.require' => '包名不能为空!',
'event_name.require' => '事件名称不能为空!',
'callback_url.require' => '回传地址不能为空!',
'status.in:0,1' => '状态值不正确!'
]);
// 检查权限
if (!$this->checkPackageAuth($data['package_id'])) {
return json(['code' => 0, 'info' => '您没有操作此包名的权限!']);
}
// 检查是否已存在相同配置
$exists = $this->app->db->name($this->table)
->where([
'package_id' => $data['package_id'],
'event_name' => $data['event_name']
])
->find();
if ($exists) {
return json(['code' => 0, 'info' => '该包名下已存在相同事件名称的配置!']);
}
// 添加创建信息
$data['create_by'] = AdminService::instance()->getUserId();
$data['create_at'] = date('Y-m-d H:i:s');
$data['update_at'] = date('Y-m-d H:i:s');
$result = $this->app->db->name($this->table)->insert($data);
if ($result === false) {
return json(['code' => 0, 'info' => '添加失败,请重试!']);
}
sysoplog('积分墙包名回传', '添加成功!');
return json(['code' => 1, 'info' => '添加成功!']);
} catch (\Exception $e) {
trace("添加回传配置异常:" . $e->getMessage());
return json(['code' => 0, 'info' => '系统异常,请稍后重试!']);
}
}
}
/**
* 编辑回传配置
* @auth true
*/
public function edit()
{
if ($this->request->isGet()) {
$id = $this->request->get('id/d', 0);
if (empty($id)) {
$this->error('参数错误');
}
// 获取配置信息
$vo = $this->app->db->name($this->table)->where('id', $id)->find();
if (empty($vo)) {
$this->error('配置不存在');
}
// 检查权限
if (!$this->checkPackageAuth($vo['package_id'])) {
$this->error('您没有操作此包名的权限!');
}
$this->assign('vo', $vo);
$this->fetch(); // 直接渲染 edit.html
}
if ($this->request->isPost()) {
try {
$data = $this->_vali([
'id.require' => '配置ID不能为空',
'package_id.require' => '包名ID不能为空',
'package_name.require' => '包名不能为空!',
'event_name.require' => '事件名称不能为空!',
'callback_url.require' => '回传地址不能为空!',
'status.in:0,1' => '状态值不正确!'
]);
// 检查权限
if (!$this->checkPackageAuth($data['package_id'])) {
return json(['code' => 0, 'info' => '您没有操作此包名的权限!']);
}
// 检查是否已存在相同配置(排除自身)
$exists = $this->app->db->name($this->table)
->where([
['package_id', '=', $data['package_id']],
['event_name', '=', $data['event_name']],
['id', '<>', $data['id']]
])
->find();
if ($exists) {
return json(['code' => 0, 'info' => '该包名下已存在相同事件名称的配置!']);
}
// 更新时间
$data['update_at'] = date('Y-m-d H:i:s');
$result = $this->app->db->name($this->table)
->where(['id' => $data['id']])
->update($data);
if ($result === false) {
return json(['code' => 0, 'info' => '更新失败,请重试!']);
}
sysoplog('积分墙包名回传', '更新成功!');
return json(['code' => 1, 'info' => '更新成功!']);
} catch (\Exception $e) {
trace("编辑回传配置异常:" . $e->getMessage());
return json(['code' => 0, 'info' => '系统异常,请稍后重试!']);
}
}
}
/**
* 删除回传配置
* @auth true
*/
public function remove()
{
if ($this->request->isPost()) {
try {
$id = $this->request->param('id/d');
if (empty($id)) {
return json(['code' => 0, 'info' => '参数错误!']);
}
// 获取配置信息
$config = $this->app->db->name($this->table)->where('id', $id)->find();
if (empty($config)) {
return json(['code' => 0, 'info' => '配置不存在!']);
}
// 检查权限
if (!$this->checkPackageAuth($config['package_id'])) {
return json(['code' => 0, 'info' => '您没有操作此包名的权限!']);
}
$result = $this->app->db->name($this->table)->where('id', $id)->delete();
if ($result === false) {
return json(['code' => 0, 'info' => '删除失败,请重试!']);
}
sysoplog('积分墙包名回传', '删除成功!');
return json(['code' => 1, 'info' => '删除成功!']);
} catch (\Exception $e) {
trace("删除回传配置异常:" . $e->getMessage());
return json(['code' => 0, 'info' => '系统异常,请稍后重试!']);
}
}
}
/**
* 检查包名权限
* @param int $packageId 包名ID
* @return bool
*/
protected function checkPackageAuth($packageId)
{
// 超级管理员直接放行
if (AdminService::instance()->isSuper()) {
return true;
}
// 检查用户是否有包名管理模块的权限
if (!AdminService::instance()->check('package/index')) {
return false;
}
// 检查具体包名权限
$userId = AdminService::instance()->getUserId();
$exists = $this->app->db->name('offer_package_auth')
->where(['package_id' => $packageId, 'user_id' => $userId])
->find();
return !empty($exists);
}
/**
* 修改状态
* @auth true
*/
public function state()
{
if ($this->request->isPost()) {
try {
$data = $this->_vali([
'id.require' => '配置ID不能为空',
'status.in:0,1' => '状态值不正确!'
]);
// 获取配置信息
$config = $this->app->db->name($this->table)->where('id', $data['id'])->find();
if (empty($config)) {
return json(['code' => 0, 'info' => '配置不存在!']);
}
// 检查权限
if (!$this->checkPackageAuth($config['package_id'])) {
return json(['code' => 0, 'info' => '您没有操作此包名的权限!']);
}
// 更新状态
$result = $this->app->db->name($this->table)
->where(['id' => $data['id']])
->update([
'status' => $data['status'],
'update_at' => date('Y-m-d H:i:s')
]);
if ($result === false) {
return json(['code' => 0, 'info' => '状态更新失败!']);
}
sysoplog('积分墙包名回传', '状态更新成功!');
return json(['code' => 1, 'info' => '状态更新成功!']);
} catch (\Exception $e) {
trace("修改状态异常:" . $e->getMessage());
return json(['code' => 0, 'info' => '系统异常,请稍后重试!']);
}
}
}
/**
* 搜索包名
* @auth true
*/
public function searchPackages()
{
try {
$keyword = input('keyword/s', '');
$init = input('init/d', 0); // 是否是初始化加载
$event_name = input('event_name/s', ''); // 事件名称,用于排除已配置的包名
$edit_id = input('edit_id/d', 0); // 编辑时的ID用于排除自身
// 构建查询
$query = $this->app->db->name('offer_package')
->alias('p')
->where('p.status', 1);
// 非超级管理员需要进行权限过滤
if (!AdminService::instance()->isSuper()) {
$userId = AdminService::instance()->getUserId();
$query->join('offer_package_auth a', 'p.id = a.package_id')
->where('a.user_id', $userId);
}
// 排除已经配置过此事件的包名
if (!empty($event_name)) {
$existsQuery = $this->app->db->name($this->table)
->where('event_name', $event_name);
// 编辑时排除自身
if ($edit_id > 0) {
$existsQuery->where('id', '<>', $edit_id);
}
$existsPackageIds = $existsQuery->column('package_id');
if (!empty($existsPackageIds)) {
$query->whereNotIn('p.id', $existsPackageIds);
}
}
// 如果是搜索则添加搜索条件
if (!$init && !empty($keyword)) {
$query->where(function($query) use ($keyword) {
$query->whereOr([
['p.package_name', 'like', "%{$keyword}%"],
['p.name', 'like', "%{$keyword}%"]
]);
});
}
// 限制返回数量
$list = $query->field(['p.id', 'p.package_name', 'p.name']) // 明确指定字段
->group('p.id') // 防止重复
->order('p.id desc') // 按ID倒序
->limit(20)
->select()
->toArray();
return json(['code' => 1, 'msg' => '获取成功', 'data' => $list]);
} catch (\Exception $e) {
trace("搜索包名异常:" . $e->getMessage() . "\n" . $e->getTraceAsString());
return json(['code' => 0, 'msg' => '系统异常,请稍后重试']);
}
}
/**
* 检查配置是否存在
* @auth true
*/
public function checkExists()
{
if ($this->request->isPost()) {
try {
$data = $this->_vali([
'package_id.require' => '包名ID不能为空',
'event_name.require' => '事件名称不能为空!'
]);
// 检查是否已存在相同配置
$exists = $this->app->db->name($this->table)
->where([
'package_id' => $data['package_id'],
'event_name' => $data['event_name']
])
->find();
return json([
'code' => 1,
'msg' => '检查成功',
'data' => !empty($exists)
]);
} catch (\Exception $e) {
return json(['code' => 0, 'msg' => '检查失败']);
}
}
}
/**
* 获取已存在的配置详情
* @auth true
*/
public function getExistingConfig()
{
if ($this->request->isPost()) {
try {
$data = $this->_vali([
'package_id.require' => '包名ID不能为空',
'event_name.require' => '事件名称不能为空!'
]);
// 获取已存在的配置
$config = $this->app->db->name($this->table)
->where([
'package_id' => $data['package_id'],
'event_name' => $data['event_name']
])
->find();
return json([
'code' => 1,
'msg' => '获取成功',
'data' => $config
]);
} catch (\Exception $e) {
return json(['code' => 0, 'msg' => '获取失败']);
}
}
}
}

View File

@@ -0,0 +1,125 @@
<?php
declare(strict_types=1);
namespace app\manager\controller;
use think\admin\Controller;
use think\admin\service\AdminService;
/**
* 回传记录管理
* @auth true
* @menu true
*/
class PackageCallbackRecords extends Controller
{
/**
* 绑定数据表
* @var string
*/
protected $table = 'offer_package_callback_records';
/**
* 回传记录列表
* @auth true
* @menu true
*/
public function index()
{
$this->title = '回传记录列表';
$this->fetch();
}
/**
* 获取回传记录列表
* @auth true
*/
public function get_list()
{
try {
$page = input('page/d', 1);
$limit = input('limit/d', 15);
$where = [];
// 包名搜索
if ($package_name = input('package_name/s', '')) {
$where[] = ['package_name', 'like', "%{$package_name}%"];
}
// 事件名称搜索
if ($event_name = input('event_name/s', '')) {
$where[] = ['event_name', 'like', "%{$event_name}%"];
}
// 推送ID搜索
if ($push_record_id = input('push_record_id/d', '')) {
$where[] = ['push_record_id', '=', $push_record_id];
}
// 状态筛选
if ($status = input('status/s', '')) {
$where[] = ['status', '=', $status];
}
// 时间范围筛选
if ($start_time = input('start_time/s', '')) {
$where[] = ['callback_time', '>=', $start_time];
}
if ($end_time = input('end_time/s', '')) {
$where[] = ['callback_time', '<=', $end_time];
}
// 查询数据
$query = $this->app->db->name($this->table)
->alias('a')
->join('offer_package_push_records b', 'a.push_record_id = b.id', 'left')
->field('a.*, b.source_type');
// 获取总数
$total = $query->where($where)->count();
// 分页查询数据
$list = $query->where($where)
->order('a.id desc')
->limit(($page - 1) * $limit, $limit)
->select()
->toArray();
return json([
'code' => 0,
'msg' => '',
'count' => $total,
'data' => $list
]);
} catch (\Exception $e) {
trace("获取回传记录列表异常:" . $e->getMessage());
return json([
'code' => 1,
'msg' => '获取数据失败,请稍后重试',
'count' => 0,
'data' => []
]);
}
}
/**
* 查看详情
* @auth true
*/
public function detail()
{
$id = input('id/d', 0);
if (empty($id)) {
$this->error('参数错误');
}
$vo = $this->app->db->name($this->table)->where('id', $id)->find();
if (empty($vo)) {
$this->error('记录不存在');
}
$this->vo = $vo;
$this->fetch();
}
}

View File

@@ -0,0 +1,118 @@
<?php
declare(strict_types=1);
namespace app\manager\controller;
use think\admin\Controller;
use think\admin\service\AdminService;
/**
* 推送记录管理
* @auth true
* @menu true
*/
class PackagePushRecords extends Controller
{
/**
* 绑定数据表
* @var string
*/
protected $table = 'offer_package_push_records';
/**
* 推送记录列表
* @auth true
* @menu true
*/
public function index()
{
$this->title = '推送记录列表';
$this->fetch();
}
/**
* 获取推送记录列表
* @auth true
*/
public function get_list()
{
try {
$page = input('page/d', 1);
$limit = input('limit/d', 15);
$where = [];
// 包名搜索
if ($package_name = input('package_name/s', '')) {
$where[] = ['package_name', 'like', "%{$package_name}%"];
}
// 事件名称搜索
if ($event_name = input('event_name/s', '')) {
$where[] = ['event_name', 'like', "%{$event_name}%"];
}
// 来源筛选
if ($source_type = input('source_type/s', '')) {
$where[] = ['source_type', '=', $source_type];
}
// 时间范围筛选
if ($start_time = input('start_time/s', '')) {
$where[] = ['receive_time', '>=', $start_time];
}
if ($end_time = input('end_time/s', '')) {
$where[] = ['receive_time', '<=', $end_time];
}
// 查询数据
$query = $this->app->db->name($this->table);
// 获取总数
$total = $query->where($where)->count();
// 分页查询数据
$list = $query->where($where)
->field('*')
->order('id desc')
->limit(($page - 1) * $limit, $limit)
->select()
->toArray();
return json([
'code' => 0,
'msg' => '',
'count' => $total,
'data' => $list
]);
} catch (\Exception $e) {
trace("获取推送记录列表异常:" . $e->getMessage());
return json([
'code' => 1,
'msg' => '获取数据失败,请稍后重试',
'count' => 0,
'data' => []
]);
}
}
/**
* 查看详情
* @auth true
*/
public function detail()
{
$id = input('id/d', 0);
if (empty($id)) {
$this->error('参数错误');
}
$vo = $this->app->db->name($this->table)->where('id', $id)->find();
if (empty($vo)) {
$this->error('记录不存在');
}
$this->vo = $vo;
$this->fetch();
}
}