542 lines
19 KiB
PHP
542 lines
19 KiB
PHP
<?php
|
||
|
||
namespace app\admin\controller\user;
|
||
|
||
use app\api\model\TbUser;
|
||
use app\common\library\DatabaseRoute;
|
||
use think\facade\Db;
|
||
use Throwable;
|
||
use app\common\controller\Backend;
|
||
use app\admin\model\User as UserModel;
|
||
|
||
class User extends Backend
|
||
{
|
||
/**
|
||
* @var object
|
||
* @phpstan-var UserModel
|
||
*/
|
||
protected object $model;
|
||
|
||
protected array $withJoinTable = ['userGroup'];
|
||
|
||
// 排除字段
|
||
protected string|array $preExcludeFields = ['last_login_time', 'login_failure', 'password', 'salt'];
|
||
|
||
protected string|array $quickSearchField = ['username', 'nickname', 'id'];
|
||
|
||
protected array $noNeedPermission = [
|
||
'courseMessage',
|
||
'userMessage',
|
||
'selectUserOnLineCount',
|
||
'homeMessage',
|
||
'selectUserCountStatisticsByTime',
|
||
'selectUserList',
|
||
'updateUserStatusByUserId',
|
||
'inviteAmount',
|
||
'selectUserByInvitationCode',
|
||
'getuserinfo',
|
||
'deleteUserByUserId',
|
||
];
|
||
protected array $noNeedLogin = ['userListExcel'];
|
||
public function initialize(): void
|
||
{
|
||
parent::initialize();
|
||
// $this->model = new UserModel();
|
||
}
|
||
|
||
/**
|
||
* 查看
|
||
* @throws Throwable
|
||
*/
|
||
public function index(): void
|
||
{
|
||
if ($this->request->param('select')) {
|
||
$this->select();
|
||
}
|
||
|
||
list($where, $alias, $limit, $order) = $this->queryBuilder();
|
||
$res = $this->model
|
||
->withoutField('password,salt')
|
||
->withJoin($this->withJoinTable, $this->withJoinType)
|
||
->alias($alias)
|
||
->where($where)
|
||
->order($order)
|
||
->paginate($limit);
|
||
|
||
$this->success('', [
|
||
'list' => $res->items(),
|
||
'total' => $res->total(),
|
||
'remark' => get_route_remark(),
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 添加
|
||
* @throws Throwable
|
||
*/
|
||
public function add(): void
|
||
{
|
||
if ($this->request->isPost()) {
|
||
$data = $this->request->post();
|
||
if (!$data) {
|
||
$this->error(__('Parameter %s can not be empty', ['']));
|
||
}
|
||
|
||
$result = false;
|
||
$passwd = $data['password']; // 密码将被排除不直接入库
|
||
$data = $this->excludeFields($data);
|
||
|
||
$this->model->startTrans();
|
||
try {
|
||
// 模型验证
|
||
if ($this->modelValidate) {
|
||
$validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
||
if (class_exists($validate)) {
|
||
$validate = new $validate();
|
||
if ($this->modelSceneValidate) $validate->scene('add');
|
||
$validate->check($data);
|
||
}
|
||
}
|
||
$result = $this->model->save($data);
|
||
$this->model->commit();
|
||
|
||
if (!empty($passwd)) {
|
||
$this->model->resetPassword($this->model->id, $passwd);
|
||
}
|
||
} catch (Throwable $e) {
|
||
$this->model->rollback();
|
||
$this->error($e->getMessage());
|
||
}
|
||
if ($result !== false) {
|
||
$this->success(__('Added successfully'));
|
||
} else {
|
||
$this->error(__('No rows were added'));
|
||
}
|
||
}
|
||
|
||
$this->error(__('Parameter error'));
|
||
}
|
||
|
||
/**
|
||
* 编辑
|
||
* @throws Throwable
|
||
*/
|
||
public function edit(): void
|
||
{
|
||
$pk = $this->model->getPk();
|
||
$id = $this->request->param($pk);
|
||
$row = $this->model->find($id);
|
||
if (!$row) {
|
||
$this->error(__('Record not found'));
|
||
}
|
||
|
||
if ($this->request->isPost()) {
|
||
$password = $this->request->post('password', '');
|
||
if ($password) {
|
||
$this->model->resetPassword($id, $password);
|
||
}
|
||
parent::edit();
|
||
}
|
||
|
||
unset($row->salt);
|
||
$row->password = '';
|
||
$this->success('', [
|
||
'row' => $row
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 重写select
|
||
* @throws Throwable
|
||
*/
|
||
public function select(): void
|
||
{
|
||
list($where, $alias, $limit, $order) = $this->queryBuilder();
|
||
$res = $this->model
|
||
->withoutField('password,salt')
|
||
->withJoin($this->withJoinTable, $this->withJoinType)
|
||
->alias($alias)
|
||
->where($where)
|
||
->order($order)
|
||
->paginate($limit);
|
||
|
||
foreach ($res as $re) {
|
||
$re->nickname_text = $re->username . '(ID:' . $re->id . ')';
|
||
}
|
||
|
||
$this->success('', [
|
||
'list' => $res->items(),
|
||
'total' => $res->total(),
|
||
'remark' => get_route_remark(),
|
||
]);
|
||
}
|
||
|
||
// 短剧分析
|
||
public function courseMessage()
|
||
{
|
||
$get = $this->request->get();
|
||
$admin = $this->auth->getAdmin();
|
||
$pageUtils = \app\admin\model\User::queryCourseOrder($get['page'], $get['limit'], $get['type'], completeStartTime($get['date']), $admin['user_id']);
|
||
$this->n_success(['data' => $pageUtils]);
|
||
}
|
||
|
||
// 用户分析
|
||
public function userMessage()
|
||
{
|
||
$get = $this->request->get();
|
||
$admin = $this->auth->getAdmin();
|
||
// 补全开始时间(调用之前实现的函数)
|
||
$date = completeStartTime($get['date']); // 假设已实现该函数
|
||
$qdCode = $admin['qd_code'];
|
||
$sumUserCount = \app\admin\model\User::queryUserCount($get['type'], $date, null, $qdCode);
|
||
$h5Count = \app\admin\model\User::queryUserCount($get['type'], $date, "h5", $qdCode);
|
||
$appCount = \app\admin\model\User::queryUserCount($get['type'], $date, "app", $qdCode);
|
||
$wxCount = \app\admin\model\User::queryUserCount($get['type'], $date, "小程序", $qdCode);
|
||
$dyCount = \app\admin\model\User::queryUserCount($get['type'], $date, "抖音", $qdCode);
|
||
$giveMemberCount = \app\admin\model\User::userMessage($date, $get['type'], $qdCode, 1);
|
||
$moneyMemberCount = \app\admin\model\User::userMessage($date, $get['type'], $qdCode, 2);
|
||
$memberCount = \app\admin\model\User::userMessage($date, $get['type'], $qdCode, null);
|
||
$userCount = $sumUserCount - $memberCount;
|
||
$this->n_success(['data' => [
|
||
'sumUserCount' => $sumUserCount,
|
||
'h5Count' => $h5Count,
|
||
'appCount' => $appCount,
|
||
'wxCount' => $wxCount,
|
||
'dyCount' => $dyCount,
|
||
'memberCount' => $memberCount,
|
||
'giveMemberCount' => $giveMemberCount,
|
||
'moneyMemberCount' => $moneyMemberCount,
|
||
'userCount' => $userCount,
|
||
]]);
|
||
}
|
||
|
||
// 当前在线人数统计
|
||
public function selectUserOnLineCount()
|
||
{
|
||
$admin = $this->auth->getAdmin();
|
||
$qdCode = $admin['qd_code'];
|
||
$selectUserOnLineCount = DatabaseRoute::getAllDbData('tb_user', function($query)use($qdCode) {
|
||
if($query) {
|
||
$query->where(['qd_code' => $qdCode]);
|
||
}
|
||
return $query->where('on_line_time', '>=', Db::raw('DATE_SUB(NOW(), INTERVAL 10 MINUTE)'));
|
||
})->count();
|
||
$this->n_success(['data' => $selectUserOnLineCount]);
|
||
}
|
||
|
||
// 用户统计
|
||
public function homeMessage()
|
||
{
|
||
$admin = $this->auth->getAdmin();
|
||
$qdCode = $admin['qd_code'];
|
||
$data = [];
|
||
$data['totalUsers'] = \app\admin\model\User::queryUserCount(0, null, null, $qdCode);
|
||
$data['newToday'] = \app\admin\model\User::queryUserCount(1, null, null, $qdCode);
|
||
$data['newMonth'] = \app\admin\model\User::queryUserCount(2, null, null, $qdCode);
|
||
$data['newYear'] = \app\admin\model\User::queryUserCount(3, null, null, $qdCode);
|
||
$data['totalRevenue'] = \app\admin\model\User::queryPayMoney(0, $qdCode);
|
||
$data['todayRevenue'] = \app\admin\model\User::queryPayMoney(1, $qdCode);
|
||
$data['monthRevenue'] = \app\admin\model\User::queryPayMoney(2, $qdCode);
|
||
$data['yearRevenue'] = \app\admin\model\User::queryPayMoney(3, $qdCode);
|
||
$map = \app\admin\model\User::queryPayAndExtractInfo();
|
||
$data['todayPayAmount'] = isset($map['payAmount'])
|
||
? round((float)$map['payAmount'], 2, PHP_ROUND_HALF_UP)
|
||
: 0.00;
|
||
$data['todayPayCount'] = isset($map['payCount'])
|
||
? (int)$map['payCount']
|
||
: 0;
|
||
$data['todayExtractAmount'] = isset($map['extractAmount'])
|
||
? round((float)$map['extractAmount'], 2, PHP_ROUND_HALF_UP)
|
||
: 0.00;
|
||
|
||
$data['todayExtractCount'] = isset($map['extractCount'])
|
||
? (int)$map['extractCount']
|
||
: 0;
|
||
$this->n_success(['data' => $data]);
|
||
}
|
||
|
||
// 用户增长折线图
|
||
public function selectUserCountStatisticsByTime()
|
||
{
|
||
$get = $this->request->get();
|
||
$startTime = $get['startTime'];
|
||
$endTime = $get['endTime'];
|
||
|
||
// 初始化结果数组
|
||
$userCountList = [];
|
||
$dateList = [];
|
||
|
||
// 日期处理
|
||
$currentDate = strtotime($startTime);
|
||
$endDate = strtotime($endTime);
|
||
|
||
// 循环遍历日期范围
|
||
while ($currentDate <= $endDate) {
|
||
$date = date('Y-m-d', $currentDate);
|
||
|
||
// 查询当日用户注册数量
|
||
$userCount = \app\admin\model\User::queryUserCount(1, $date, null, null);
|
||
|
||
// 记录数据
|
||
$userCountList[] = $userCount;
|
||
$dateList[] = $date;
|
||
|
||
// 日期加1天
|
||
$currentDate = strtotime('+1 day', $currentDate);
|
||
}
|
||
|
||
// 构建结果数据
|
||
$result = [
|
||
'userCountList' => $userCountList,
|
||
'year' => $dateList // 注:原Java代码中使用year变量,但实际存储的是日期列表
|
||
];
|
||
|
||
$this->n_success(['data' => $result]);
|
||
|
||
}
|
||
|
||
|
||
|
||
// 查询所有用户列表
|
||
public function selectUserList()
|
||
{
|
||
$params = $this->request->get();
|
||
$vipType = $params['vipType'] ?? null;
|
||
$member = $params['member'] ?? null;
|
||
$status = $params['status'] ?? null;
|
||
$page = $params['page'] ?? null;
|
||
$limit = $params['limit'] ?? null;
|
||
$phone = $params['phone'] ?? null;
|
||
$sysUserName = $params['sysUserName'] ?? null;
|
||
$userName = $params['userName'] ?? null;
|
||
$sex = $params['sex'] ?? null;
|
||
$platform = $params['platform'] ?? null;
|
||
$sysPhone = $params['sysPhone'] ?? null;
|
||
$inviterCode = $params['inviterCode'] ?? null;
|
||
$invitationCode = $params['invitationCode'] ?? null;
|
||
$qdCode = $params['qdCode'] ?? null;
|
||
$startTime = $params['startTime'] ?? null;
|
||
$endTime = $params['endTime'] ?? null;
|
||
$delegate = $params['delegate'] ?? null;
|
||
$this->n_success(['data' => \app\admin\model\User::selectUserPage(
|
||
$page, $limit, $phone, $sex, $platform, $sysPhone, $status, $member,
|
||
$inviterCode, $userName, $invitationCode, $startTime, $endTime, $qdCode, $sysUserName, $vipType, $delegate
|
||
)]);
|
||
}
|
||
|
||
// 修改用户状态
|
||
public function updateUserStatusByUserId()
|
||
{
|
||
$get = $this->request->get();
|
||
if(empty($get['userId'])) {
|
||
$this->error('userId 不能为空');
|
||
}
|
||
if(empty($get['status']) && $get['status'] == null) {
|
||
$this->error('status 不能为空');
|
||
}
|
||
$status = $get['status'];
|
||
$userId = $get['userId'];
|
||
$db = Db::connect(DatabaseRoute::getConnection('tb_user', ['user_id' => $userId], true));
|
||
// 查询用户是否存在
|
||
$user = $db->name('tb_user')->where(['user_id' => $userId])->find();
|
||
if (is_null($user)) {
|
||
$this->error('用户不存在');
|
||
}
|
||
|
||
// 根据状态执行不同操作
|
||
switch ($status) {
|
||
case 1:
|
||
// 状态1:设置状态为1,并调用upUserBlack方法(拉黑)
|
||
\app\admin\model\User::upUserBlack($user, 1, $db);
|
||
break;
|
||
case 2:
|
||
// 状态2:直接更新状态为2
|
||
$db->name('tb_user')->where('user_id', $userId)
|
||
->update(['status' => 2]);
|
||
break;
|
||
case 0:
|
||
// 状态0:调用upUserBlack方法(解封)
|
||
\app\admin\model\User::upUserBlack($user, 0, $db);
|
||
break;
|
||
default:
|
||
// 无效状态
|
||
$this->error('状态不正确');
|
||
}
|
||
$this->success();
|
||
}
|
||
|
||
|
||
// 更新用户邀请奖励金额
|
||
public function inviteAmount()
|
||
{
|
||
$userInviteDTO = $this->request->post();
|
||
// 验证用户ID不能为空
|
||
if (empty($userInviteDTO['userId'])) {
|
||
$this->error('用户id不能为空');
|
||
}
|
||
|
||
// 验证邀请奖励金额必须大于0
|
||
$inviteAmount = $userInviteDTO['inviteAmount'] ?? null;
|
||
if (is_null($inviteAmount) || bccomp($inviteAmount, 0) <= 0) {
|
||
$this->error('邀请奖励金额必须大于0');
|
||
}
|
||
|
||
$db = Db::connect(DatabaseRoute::getConnection('tb_user', ['user_id' => $userInviteDTO['userId']], true))->name('tb_user');
|
||
// 查询用户是否存在
|
||
$userEntity = $db->where(['user_id' => $userInviteDTO['userId']])->find($userInviteDTO['userId']);
|
||
if (is_null($userEntity)) {
|
||
$this->error('用户不存在');
|
||
}
|
||
// 更新用户邀请奖励金额
|
||
$db->where(['user_id' => $userInviteDTO['userId']])->update(['invite_amount' => $inviteAmount]);
|
||
$this->success();
|
||
}
|
||
|
||
|
||
public function updatePwd()
|
||
{
|
||
$get = $this->request->get();
|
||
if(empty($get['userId']) || empty($get['pwd'])) {
|
||
$this->error('参数不完整');
|
||
}
|
||
$userId = $get['userId'];
|
||
$pwd = $get['pwd'];
|
||
$db = Db::connect(DatabaseRoute::getConnection('sys_user', ['user_id' => $userId]));
|
||
$user = $db->name('sys_user')->where(['user_id' => $userId])->find();
|
||
if(!$user) {
|
||
$this->error('用户不存在');
|
||
}
|
||
$db = Db::connect(DatabaseRoute::getConnection('sys_user', ['user_id' => $userId], true));
|
||
$user = $db->name('sys_user')->where(['user_id' => $userId])->update(['password' => shiro_simple_hash_hex_salt('sha256', $pwd, $user['salt'])]);
|
||
if($user) {
|
||
$this->success();
|
||
}
|
||
$this->error();
|
||
}
|
||
|
||
public function deleteUserByUserId()
|
||
{
|
||
$post = $this->request->post();
|
||
if(empty($post['userId'])) {
|
||
$this->error('userId 不能为空');
|
||
}
|
||
$userId = $post['userId'];
|
||
$db = Db::connect(DatabaseRoute::getConnection('tb_user', ['user_id' => $userId], true));
|
||
$user = $db->name('tb_user')->where(['user_id' => $userId])->delete();
|
||
if($user) {
|
||
$this->success();
|
||
}
|
||
$this->error('操作失败');
|
||
}
|
||
|
||
|
||
public function getuserinfo()
|
||
{
|
||
$userId = $this->request->get('userId');
|
||
if(empty($userId)) {
|
||
$this->error('userId 不能为空');
|
||
}
|
||
|
||
$db = Db::connect(DatabaseRoute::getConnection('tb_user', ['user_id' => $userId]));
|
||
$user = $db->name('tb_user')->where(['user_id' => $userId])->find();
|
||
if(!$user) {
|
||
$this->error('用户不存在');
|
||
}
|
||
|
||
$inviteMoney = \app\admin\model\User::selectInviteMoneyByUserId($userId, $db);
|
||
if(empty($inviteMoney)) {
|
||
$inviteMoney = [
|
||
'user_id' => $userId,
|
||
'money_sum' => 0.00,
|
||
'money' => 0.00,
|
||
'cash_out' => 0.00,
|
||
];
|
||
Db::connect(DatabaseRoute::getConnection('invite_money', ['user_id' => $userId], true), true)->name('invite_money')->insert($inviteMoney);
|
||
}
|
||
$money = $inviteMoney['money'];
|
||
|
||
// 获取当前日期(格式:YYYY-MM-DD HH:mm:ss)
|
||
$date = date('Y-m-d H:i:s');
|
||
|
||
// 查询本月充值(复用之前的instantselectSumPay方法)
|
||
$consume = \app\admin\model\User::instantselectSumPay(date('Y-m'), $userId, $db);
|
||
|
||
// 查询本月提现(假设monthIncome方法已实现)
|
||
$income = \app\admin\model\User::monthIncome(date('Y-m'), $userId, $db);
|
||
|
||
// 查询邀请人数(复用之前的countUsersByInviterCode方法)
|
||
$count = \app\admin\model\User::queryInviterCount($user['invitation_code']);
|
||
|
||
// 查询VIP信息
|
||
$userVip = \app\admin\model\User::selectUserVipByUserId($userId);
|
||
if ($userVip) {
|
||
$user['member'] = $userVip['is_vip'];
|
||
$user['end_time'] = $userVip['end_time'];
|
||
$user['vip_type'] = $userVip['vip_type'];
|
||
}
|
||
|
||
// 组装结果数据
|
||
$resultData = [
|
||
'userEntity' => $user,
|
||
'money' => $money,
|
||
'consume' => $consume,
|
||
'income' => $income,
|
||
'count' => $count
|
||
];
|
||
$this->n_success(['data' => $resultData]);
|
||
|
||
}
|
||
|
||
public function userListExcel()
|
||
{
|
||
$get = $this->request->get();
|
||
$startTime = $get['startTime'] ?? null;
|
||
$endTime = $get['endTime'] ?? null;
|
||
$this->n_success(\app\admin\model\User::userListExcel($startTime, $endTime));
|
||
}
|
||
|
||
|
||
|
||
// 获取用户详细信息
|
||
public function selectUserByInvitationCode()
|
||
{
|
||
$get = $this->request->get();
|
||
if(empty($get['invitationCode'])) {
|
||
$this->error('参数不完整');
|
||
}
|
||
$invitationCode = $get['invitationCode'];
|
||
$userEntity = TbUser::GetByusername($invitationCode, 'invitation_code');
|
||
if(empty($userEntity)) {
|
||
$this->error('用户信息不存在');
|
||
}
|
||
$userId = $userEntity['user_id'];
|
||
|
||
$db = Db::connect(DatabaseRoute::getConnection('invite_money', ['user_id' => $userId]));
|
||
// 查询用户钱包
|
||
$inviteMoney = \app\admin\model\User::selectInviteMoneyByUserId($userId, $db);
|
||
$money = $inviteMoney['money'];
|
||
// 获取当前时间(格式:Y-m-d H:i:s)
|
||
$currentDate = date('Y-m-d H:i:s');
|
||
// 查询本月充值总额
|
||
$consume = \app\admin\model\User::instantselectSumPay($currentDate, $userId, $db);
|
||
$income = \app\admin\model\User::monthIncome($currentDate, $userId, $db);
|
||
//查询邀请人数
|
||
$count = \app\admin\model\User::queryInviterCount($userEntity['invitation_code']);
|
||
$userVip = \app\admin\model\User::selectUserVipByUserId($userId);
|
||
if ($userVip) {
|
||
$userEntity['member'] = $userVip['is_vip'];
|
||
$userEntity['end_time'] = $userVip['end_time'];
|
||
$userEntity['vip_type'] = $userVip['vip_type'];
|
||
}
|
||
$data = [
|
||
'userEntity' => $userEntity,
|
||
'money' => $money,
|
||
'consume' => $consume,
|
||
'income' => $income,
|
||
'count' => $count
|
||
];
|
||
$this->n_success(['data' => $data]);
|
||
}
|
||
|
||
|
||
} |