add
This commit is contained in:
28
app/admin/controller/sys/Config.php
Normal file
28
app/admin/controller/sys/Config.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\sys;
|
||||
|
||||
use app\admin\model\SysMenu;
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\DatabaseRoute;
|
||||
use think\facade\Db;
|
||||
|
||||
class Config extends Backend
|
||||
{
|
||||
|
||||
public function list()
|
||||
{
|
||||
$get = $this->request->get();
|
||||
$db = Db::connect(get_slave_connect_name());
|
||||
$count = $db->name('sys_config')->count();
|
||||
$config_list= $db->name('sys_config')->where(['status' => 1])->limit(page($get['page'], $get['limit']), $get['limit'])->select()->toArray();
|
||||
$this->n_success(['page' => [
|
||||
'totalCount' => $count,
|
||||
'pageSize' => $get['limit'],
|
||||
'totalPage' => ceil($count / $get['limit']),
|
||||
'currPage' => $get['page'],
|
||||
'list' => $config_list,
|
||||
]]);
|
||||
}
|
||||
|
||||
}
|
||||
24
app/admin/controller/sys/Log.php
Normal file
24
app/admin/controller/sys/Log.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\sys;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\DatabaseRoute;
|
||||
use think\facade\Db;
|
||||
|
||||
class Log extends Backend
|
||||
{
|
||||
protected array $noNeedLogin = ['*'];
|
||||
|
||||
// 用户信息
|
||||
public function list()
|
||||
{
|
||||
$params = $this->request->param();
|
||||
$this->result('成功', DatabaseRoute::paginateDb('sys_log', function ($query) {
|
||||
return $query->order('id', 'desc');
|
||||
}, $params['page'], $params['limit']), 0, null, [], [], 'page');
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
128
app/admin/controller/sys/Menu.php
Normal file
128
app/admin/controller/sys/Menu.php
Normal file
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\sys;
|
||||
|
||||
use app\admin\model\SysMenu;
|
||||
use app\common\controller\Backend;
|
||||
use think\facade\Db;
|
||||
|
||||
class Menu extends Backend
|
||||
{
|
||||
protected array $noNeedLogin = ['*'];
|
||||
|
||||
// 菜单列表
|
||||
public function nav()
|
||||
{
|
||||
$admin = $this->auth->getAdmin();
|
||||
$return = SysMenu::getMenuList($this->auth->getUserMenus($admin['user_id']));
|
||||
|
||||
$this->n_success([
|
||||
'menuList' => convertToCamelCase($return['menuList']),
|
||||
'permissions' => $return['permissions'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function list()
|
||||
{
|
||||
$menuList = Db::name('sys_menu')->order('order_num', 'asc')->select()->toArray();
|
||||
|
||||
// 收集所有 parent_id
|
||||
$parentIds = array_column($menuList, 'parent_id');
|
||||
$parentIds = array_unique(array_filter($parentIds));
|
||||
|
||||
// 批量查询父级菜单
|
||||
$parentMap = [];
|
||||
if (!empty($parentIds)) {
|
||||
$parents = Db::name('sys_menu')->whereIn('menu_id', $parentIds)->column('name', 'menu_id');
|
||||
$parentMap = $parents;
|
||||
}
|
||||
|
||||
// 设置 parentName
|
||||
foreach ($menuList as &$menu) {
|
||||
$menu['menu_id'] = (string) $menu['menu_id'];
|
||||
$menu['parent_id'] = (string) $menu['parent_id'];
|
||||
|
||||
$menu['parent_name'] = $parentMap[$menu['parent_id']] ?? '';
|
||||
}
|
||||
unset($menu);
|
||||
|
||||
return $this->ApiDataReturn(convertToCamelCase($menuList));
|
||||
}
|
||||
|
||||
public function info()
|
||||
{
|
||||
$data = Db::name('sys_menu')->where([
|
||||
'menu_id' => $this->request->param('id')
|
||||
])->find();
|
||||
$data['menu_id'] = (string) $data['menu_id'];
|
||||
$data['parent_id'] = (string) $data['parent_id'];
|
||||
$data = convertToCamelCase($data);
|
||||
$this->n_success(['menu' => $data]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function save()
|
||||
{
|
||||
$menu = $this->request->post();
|
||||
SysMenu::verifyForm($menu);
|
||||
$menu = convertKeysCamelToSnakeRecursive($menu);
|
||||
Db::connect(get_master_connect_name())->name('sys_menu')->insert($menu);
|
||||
$this->success();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function selectInfo()
|
||||
{
|
||||
$list = Db::name('sys_menu')->where([
|
||||
'type' => ['!=', 2]
|
||||
])->order('order_num')->select()->toArray();
|
||||
|
||||
$list[] = [
|
||||
'menu_id' => 0,
|
||||
'name' => '一级菜单',
|
||||
'parent_id' => -1,
|
||||
'open' => true,
|
||||
];
|
||||
$list = convertToCamelCase($list);
|
||||
$this->n_success(['menuList' => $list]);
|
||||
// $this->successWithData([
|
||||
// 'menuList' => $list
|
||||
// ]);
|
||||
}
|
||||
|
||||
public function update()
|
||||
{
|
||||
$params = $this->request->post();
|
||||
$params = convertKeysCamelToSnakeRecursive($params);
|
||||
Db::name('sys_menu')->where([
|
||||
'menu_id' => $params['menu_id']
|
||||
])->update($params);
|
||||
$this->success();
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
$post = $this->request->post();
|
||||
if(empty($post['menuId'])) {
|
||||
$this->error('参数错误');
|
||||
}
|
||||
$menuId = $post['menuId'];
|
||||
if($menuId <= 31) {
|
||||
$this->error('系统菜单,不能删除');
|
||||
}
|
||||
|
||||
// 判断是否有子菜单或按钮
|
||||
$menuList = Db::connect(get_slave_connect_name())->name('sys_menu')->where(['parent_id' => $menuId])->order('order_num', 'asc')->select()->toArray();
|
||||
if($menuList) {
|
||||
$this->error('请先删除子菜单或按钮');
|
||||
}
|
||||
|
||||
if(Db::name('sys_menu')->where(['menu_id' => $menuId])->delete()) {
|
||||
$this->success('删除成功');
|
||||
}
|
||||
$this->error('操作失败');
|
||||
}
|
||||
|
||||
}
|
||||
43
app/admin/controller/sys/Oss.php
Normal file
43
app/admin/controller/sys/Oss.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\sys;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\DatabaseRoute;
|
||||
use think\facade\Db;
|
||||
|
||||
class Oss extends Backend
|
||||
{
|
||||
|
||||
public function list()
|
||||
{
|
||||
$params = $this->request->param();
|
||||
$data = DatabaseRoute::paginateDb('sys_oss', function ($query) {
|
||||
return $query->order('id', 'desc');
|
||||
}, $params['page'], $params['limit']);
|
||||
$this->n_success(['page' => $data]);
|
||||
}
|
||||
|
||||
public function config()
|
||||
{
|
||||
$data = Db::connect(get_slave_connect_name())->name('sys_config')->where(['param_key' => 'CLOUD_STORAGE_CONFIG_KEY'])->column('param_value')[0];
|
||||
$data = json_decode($data, true);
|
||||
$this->n_success(['config' => $data]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function saveConfig()
|
||||
{
|
||||
$post = $this->request->post();
|
||||
if(is_array($post)) {
|
||||
$post = json_encode($post);
|
||||
}else {
|
||||
$this->error('参数错误');
|
||||
}
|
||||
Db::name('sys_config')->where(['param_key' => 'CLOUD_STORAGE_CONFIG_KEY'])->update(['param_value' => $post]);
|
||||
$this->success();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
110
app/admin/controller/sys/Role.php
Normal file
110
app/admin/controller/sys/Role.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\sys;
|
||||
|
||||
use app\admin\model\SysMenu;
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\DatabaseRoute;
|
||||
use think\facade\Db;
|
||||
|
||||
class Role extends Backend
|
||||
{
|
||||
protected array $noNeedLogin = ['*'];
|
||||
|
||||
public function list()
|
||||
{
|
||||
$prams = $this->request->param();
|
||||
$data = DatabaseRoute::paginateDb('sys_role', function ($query) {
|
||||
return $query;
|
||||
}, $prams['page'], $prams['limit']);
|
||||
$this->n_success(['page' => $data]);
|
||||
}
|
||||
|
||||
private function saveRoleMenu($roleId, $params)
|
||||
{
|
||||
if (!empty($params['menu_id_list'])) {
|
||||
Db::name('sys_role_menu')->where([
|
||||
'role_id' => $roleId
|
||||
])->delete();
|
||||
|
||||
foreach ($params['menu_id_list'] as $menuId) {
|
||||
Db::name('sys_role_menu')->insert([
|
||||
'role_id' => $roleId,
|
||||
'menu_id' => $menuId
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
$params = $this->request->post();
|
||||
$params = convertKeysCamelToSnakeRecursive($params);
|
||||
|
||||
$id = Db::name('sys_role')->insert([
|
||||
'role_name' => $params['role_name'],
|
||||
'create_time' => getNormalDate(),
|
||||
'remark' => $params['remark'] ?? ''
|
||||
]);
|
||||
|
||||
$this->saveRoleMenu($id, $params);
|
||||
$this->success();
|
||||
}
|
||||
|
||||
public function update()
|
||||
{
|
||||
$params = $this->request->post();
|
||||
$params = convertKeysCamelToSnakeRecursive($params);
|
||||
Db::name('sys_role')->where([
|
||||
'role_id' => $params['role_id']
|
||||
])->update([
|
||||
'role_name' => $params['role_name'],
|
||||
'remark' => $params['remark'] ?? ''
|
||||
]);
|
||||
|
||||
$this->saveRoleMenu($params['role_id'], $params);
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
$params = $this->request->post();
|
||||
Db::name('sys_role')->where([
|
||||
['role_id', 'in', $params]
|
||||
])->delete();
|
||||
$this->success();
|
||||
}
|
||||
|
||||
public function selects()
|
||||
{
|
||||
|
||||
$this->n_success(['list' => convertToCamelCase(array_map(function ($item) {
|
||||
$item['role_id'] = (string)$item['role_id'];
|
||||
return $item;
|
||||
}, Db::connect(get_slave_connect_name())->name('sys_role')->select()->toArray()))]);
|
||||
}
|
||||
|
||||
|
||||
// 角色信息
|
||||
public function info()
|
||||
{
|
||||
$get = $this->request->get();
|
||||
if(empty($get['roleId'])) {
|
||||
$this->error('roleId 不能为空');
|
||||
}
|
||||
$db = Db::connect(get_slave_connect_name());
|
||||
|
||||
$role = $db->name('sys_role')->where(['role_id' => $get['roleId']])->find();
|
||||
if (!$role) {
|
||||
$this->error('角色不存在');
|
||||
}
|
||||
$role = apiconvertToCamelCase($role);
|
||||
$menuIdList = $db->name('sys_role_menu')->where(['role_id' => $get['roleId']])->column('menu_id');
|
||||
|
||||
$role['menuIdList'] = $menuIdList;
|
||||
$this->n_success(['role' => $role]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
232
app/admin/controller/sys/User.php
Normal file
232
app/admin/controller/sys/User.php
Normal file
@@ -0,0 +1,232 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\sys;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\DatabaseRoute;
|
||||
use app\InvitationCodeUtil;
|
||||
use ba\Random;
|
||||
use think\facade\Db;
|
||||
|
||||
class User extends Backend
|
||||
{
|
||||
protected array $noNeedLogin = ['*'];
|
||||
|
||||
// 用户信息
|
||||
public function info()
|
||||
{
|
||||
$get = $this->request->get();
|
||||
if (empty($get['userId'])) {
|
||||
$info = $this->auth->getAdmin();
|
||||
} else {
|
||||
$info = DatabaseRoute::getDb('sys_user', $get['userId'])->find();
|
||||
}
|
||||
$info = convertToCamelCase($info);
|
||||
$info['password'] = null;
|
||||
// $info['salt'] = null;
|
||||
$roleIdList = array_column(Db::name('sys_user_role')->where([
|
||||
'user_id' => $info['userId']
|
||||
])->select()->toArray(), 'role_id');
|
||||
foreach ($roleIdList as $k => &$v) {
|
||||
$v = (string) $v;
|
||||
}
|
||||
$info['roleIdList'] = $roleIdList;
|
||||
$this->n_success(['user' => $info]);
|
||||
}
|
||||
|
||||
public function list()
|
||||
{
|
||||
$params = $this->request->param();
|
||||
$data = DatabaseRoute::paginateAllDb('sys_user', function ($query) use ($params) {
|
||||
if (!empty($params['username'])) {
|
||||
$query->whereLike('username', '%' . $params['username'] . '%');
|
||||
}
|
||||
|
||||
if (!empty($params['isChannel'])) {
|
||||
$query->where('is_channel', $params['isChannel']);
|
||||
}
|
||||
return $query;
|
||||
}, $params['page'], $params['limit']);
|
||||
$data['list'] = convertToCamelCase($data['list']);
|
||||
$this->n_success(['page' => $data]);
|
||||
|
||||
}
|
||||
|
||||
public function detail()
|
||||
{
|
||||
$id = $this->request->param('id');
|
||||
$sysInfo = DatabaseRoute::getDb('sys_user', $id)->find();
|
||||
if (!$sysInfo) {
|
||||
$this->error('用户不存在');
|
||||
}
|
||||
$userRoleList = Db::name('sys_user_role')->where([
|
||||
'user_id' => $sysInfo['user_id']
|
||||
])->select()->toArray();
|
||||
$sysInfo = convertToCamelCase($sysInfo);
|
||||
$sysInfo['roleIdList'] =array_map('strval', array_column($userRoleList, 'role_id'));
|
||||
$this->successWithData([
|
||||
'user' => $sysInfo
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
public function updateRoleInfo($params)
|
||||
{
|
||||
if (!empty($params['role_id_list'])) {
|
||||
Db::name('sys_user_role')->where([
|
||||
'user_id' => $params['user_id']
|
||||
])->delete();
|
||||
|
||||
foreach ($params['role_id_list'] as $roleId) {
|
||||
Db::name('sys_user_role')->insert([
|
||||
'user_id' => $params['user_id'],
|
||||
'role_id' => $roleId
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function update()
|
||||
{
|
||||
$params = $this->request->post();
|
||||
if (empty($params['userId'])) {
|
||||
$this->error('参数错误');
|
||||
}
|
||||
$params = convertKeysCamelToSnakeRecursive($params);
|
||||
if (isset($params['password']) && $params['password'] == '') {
|
||||
unset($params['password']);
|
||||
} else if (!empty($params['password'])) {
|
||||
$params['password'] = shiro_simple_hash_hex_salt('sha256', $params['password'], $params['salt']);
|
||||
}
|
||||
$this->updateRoleInfo($params);
|
||||
unset($params['role_id_list']);
|
||||
|
||||
DatabaseRoute::getDb('sys_user', $params['user_id'], true, true)->where([
|
||||
'user_id' => $params['user_id']
|
||||
])->update($params);
|
||||
|
||||
$this->success();
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
$params = $this->request->post();
|
||||
|
||||
$params = convertKeysCamelToSnakeRecursive($params);
|
||||
$params['create_time'] = getNormalDate();
|
||||
$params['salt'] = Random::generateRandomPrefixedId(20);
|
||||
$params['password'] = shiro_simple_hash_hex_salt('sha256', $params['password'], $params['salt']);
|
||||
$params['user_id'] = Random::generateRandomPrefixedId();
|
||||
|
||||
$this->updateRoleInfo($params);
|
||||
|
||||
unset($params['role_id_list']);
|
||||
unset($params['money']);
|
||||
DatabaseRoute::getDb('sys_user', $params['user_id'], true)->insert($params);
|
||||
if (!empty($params['is_channel']) && $params['is_channel'] == 1 && empty($params['qd_code'])) {
|
||||
$params['qd_code'] = InvitationCodeUtil::toRegisteredCode($params['user_id']);
|
||||
DatabaseRoute::getDb('sys_user', $params['user_id'], true, true)->update($params);
|
||||
}
|
||||
|
||||
$this->success();
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
$params = $this->request->post();
|
||||
if (empty($params)) {
|
||||
$this->error('参数有误');
|
||||
}
|
||||
|
||||
foreach ($params as $id) {
|
||||
DatabaseRoute::getDb('sys_user', $id, true, true)->delete();
|
||||
}
|
||||
|
||||
$this->success();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function selectInviteUserList()
|
||||
{
|
||||
$params = $this->request->param();
|
||||
$params = convertKeysCamelToSnakeRecursive($params);
|
||||
|
||||
// 1. 分页查询被邀请人分组:按 inviter_code 聚合
|
||||
$inviteListPageInfo = DatabaseRoute::paginateAllDb('tb_user', function ($query) use ($params) {
|
||||
if (!empty($params['user_name'])) {
|
||||
$query->where('user_name', 'like', '%' . $params['user_name'] . '%');
|
||||
}
|
||||
|
||||
if (!empty($params['phone'])) {
|
||||
$query->where('phone', 'like', '%' . $params['phone'] . '%');
|
||||
}
|
||||
|
||||
return $query->fieldRaw('ANY_VALUE(inviter_code) AS inviter_code, COUNT(*) AS counts')
|
||||
->group('inviter_code')
|
||||
->order('counts', 'desc');
|
||||
}, $params['page'], $params['limit'], 'counts', null, true);
|
||||
|
||||
$records = $inviteListPageInfo['records'];
|
||||
if (empty($records)) {
|
||||
$this->successWithData($inviteListPageInfo); // 无数据
|
||||
}
|
||||
|
||||
$inviteCodeList = array_column($records, 'inviter_code');
|
||||
$countMap = array_column($records, 'counts', 'inviter_code');
|
||||
|
||||
// 2. 查询邀请人信息(部分 code 可能查不到 => 被删)
|
||||
$userInfoList = DatabaseRoute::getAllDbData('tb_user', function ($query) use ($inviteCodeList) {
|
||||
return $query->whereIn('invitation_code', $inviteCodeList);
|
||||
})->select()->toArray();
|
||||
|
||||
// 3. 查询金额信息(注意先判断 user_id 是否存在)
|
||||
$userIdList = array_column($userInfoList, 'user_id');
|
||||
$userMoneyList = [];
|
||||
if (!empty($userIdList)) {
|
||||
$userMoneyList = DatabaseRoute::getAllDbData('user_money', function ($query) use ($userIdList) {
|
||||
return $query->whereIn('user_id', $userIdList);
|
||||
})->select()->toArray();
|
||||
}
|
||||
|
||||
$userMoneyMap = [];
|
||||
foreach ($userMoneyList as $money) {
|
||||
$userMoneyMap[$money['user_id']] = $money;
|
||||
}
|
||||
|
||||
// 4. 构建 inviter_code => user 映射
|
||||
$inviterMap = [];
|
||||
foreach ($userInfoList as $user) {
|
||||
$code = $user['invitation_code'];
|
||||
$uid = $user['user_id'];
|
||||
|
||||
$user['money'] = $userMoneyMap[$uid]['invite_income_money'] ?? 0;
|
||||
$user['counts'] = $countMap[$code] ?? 0;
|
||||
|
||||
$inviterMap[$code] = $user;
|
||||
}
|
||||
|
||||
// 5. 最终组装记录:保留所有 inviter_code,即使用户不存在
|
||||
$finalRecords = [];
|
||||
foreach ($inviteCodeList as $code) {
|
||||
if (isset($inviterMap[$code])) {
|
||||
$finalRecords[] = $inviterMap[$code];
|
||||
} else {
|
||||
// 🛠️ 如果邀请人被删,构造一条匿名信息
|
||||
$finalRecords[] = [
|
||||
'user_id' => null,
|
||||
'user_name' => '已删除',
|
||||
'phone' => '-',
|
||||
'money' => 0,
|
||||
'counts' => $countMap[$code],
|
||||
'invitation_code' => $code,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$inviteListPageInfo['records'] = $finalRecords;
|
||||
$this->successWithData($inviteListPageInfo);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user