232 lines
7.7 KiB
PHP
232 lines
7.7 KiB
PHP
<?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);
|
||
}
|
||
|
||
|
||
} |