|
|
@ -17,6 +17,8 @@ use app\model\UserAccessLogModel; |
|
|
|
use app\model\UserChatGroupModel; |
|
|
|
use app\model\UserChatLinkModel; |
|
|
|
use app\model\UserModel; |
|
|
|
use app\model\VipCodeModel; |
|
|
|
use itinysun\model\helper\Service; |
|
|
|
use think\facade\Cache; |
|
|
|
use think\exception\ValidateException; |
|
|
|
use app\utility\UnqId; |
|
|
@ -113,7 +115,6 @@ class AdminService extends AdminBaseService |
|
|
|
} catch (\Exception $exception) { |
|
|
|
return $this->toData('100500', '系统繁忙.', [$exception->getMessage()]); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -232,7 +233,6 @@ class AdminService extends AdminBaseService |
|
|
|
} catch (\Exception $exception) { |
|
|
|
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -273,7 +273,6 @@ class AdminService extends AdminBaseService |
|
|
|
} catch (\Exception $exception) { |
|
|
|
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public function updateAccountStauts($accountId, $param): array |
|
|
@ -300,7 +299,6 @@ class AdminService extends AdminBaseService |
|
|
|
} catch (\Exception $exception) { |
|
|
|
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -336,7 +334,6 @@ class AdminService extends AdminBaseService |
|
|
|
} catch (\Exception $exception) { |
|
|
|
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public function del($id): array |
|
|
@ -352,7 +349,6 @@ class AdminService extends AdminBaseService |
|
|
|
} catch (\Exception $exception) { |
|
|
|
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -404,7 +400,6 @@ class AdminService extends AdminBaseService |
|
|
|
} catch (\Exception $exception) { |
|
|
|
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public function logList($param) |
|
|
@ -442,7 +437,6 @@ class AdminService extends AdminBaseService |
|
|
|
$inviteCode = (new BaseHomeService())->getUniqInviteCode(); |
|
|
|
AdminModel::where('id', $v->id)->update(['invite_code' => $inviteCode]); |
|
|
|
} |
|
|
|
|
|
|
|
} catch (\Exception $exception) { |
|
|
|
return $this->toData('500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]); |
|
|
|
} |
|
|
@ -665,6 +659,118 @@ class AdminService extends AdminBaseService |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 用户访问页面的记录汇总 |
|
|
|
public function getUserAccessLogSummary($adminId, $param) |
|
|
|
{ |
|
|
|
try { |
|
|
|
if (empty($param['page']) || empty($param['limit'])) { |
|
|
|
return $this->toData('400', '缺少分页参数'); |
|
|
|
} |
|
|
|
|
|
|
|
// 获取账号信息 |
|
|
|
$account = AdminModel::where('id', $adminId)->find(); |
|
|
|
if (empty($account)) { |
|
|
|
return $this->toData('400', '当前账号数据为空'); |
|
|
|
} |
|
|
|
|
|
|
|
// 获取账号角色信息 |
|
|
|
$role = AuthRoleModel::where(['id' => $account->role_id])->find(); |
|
|
|
if (empty($role)) { |
|
|
|
return $this->toData('400', '当前账号分配的角色数据为空'); |
|
|
|
} |
|
|
|
|
|
|
|
// 构建可查看的用户ID筛选条件 |
|
|
|
$whereUser = [ |
|
|
|
['ual.user_id', '>', 0] |
|
|
|
]; |
|
|
|
switch ($role->name) { |
|
|
|
case AuthRoleModel::NAME_ADMIN: |
|
|
|
if (!empty($param['user_id'])) { |
|
|
|
$whereUser[] = ['user_id', '=', $param['user_id']]; |
|
|
|
} |
|
|
|
break; |
|
|
|
case AuthRoleModel::NAME_AGENT: |
|
|
|
$userIds = UserModel::where('agent_id', $adminId)->column('user_id'); |
|
|
|
if (!empty($param['user_id']) && in_array($param['user_id'], $userIds)) { |
|
|
|
$whereUser[] = ['ual.user_id', '=', $param['user_id']]; |
|
|
|
} else { |
|
|
|
$whereUser[] = ['ual.user_id', 'in', $userIds]; |
|
|
|
} |
|
|
|
break; |
|
|
|
case AuthRoleModel::NAME_DIRECTOR: |
|
|
|
$teamHeaders = AdminModel::where('parent_id', $adminId)->column('id'); |
|
|
|
$userIds = []; |
|
|
|
if (!empty($teamHeaders)) { |
|
|
|
$customer = AdminModel::whereIn('parent_id', $teamHeaders)->column('id'); |
|
|
|
if (!empty($customer)) { |
|
|
|
$userIds = UserModel::whereIn('customer_id', $customer)->column('user_id'); |
|
|
|
} |
|
|
|
} |
|
|
|
if (!empty($param['user_id']) && in_array($param['user_id'], $userIds)) { |
|
|
|
$whereUser[] = ['ual.user_id', '=', $param['user_id']]; |
|
|
|
} else { |
|
|
|
$whereUser[] = ['ual.user_id', 'in', $userIds]; |
|
|
|
} |
|
|
|
break; |
|
|
|
case AuthRoleModel::NAME_TEAM_HEADER: |
|
|
|
$customer = AdminModel::where('parent_id', $adminId)->column('id'); |
|
|
|
$userIds = []; |
|
|
|
if (!empty($customer)) { |
|
|
|
$userIds = UserModel::whereIn('customer_id', $customer)->column('user_id'); |
|
|
|
} |
|
|
|
if (!empty($param['user_id']) && in_array($param['user_id'], $userIds)) { |
|
|
|
$whereUser[] = ['ual.user_id', '=', $param['user_id']]; |
|
|
|
} else { |
|
|
|
$whereUser[] = ['ual.user_id', 'in', $userIds]; |
|
|
|
} |
|
|
|
break; |
|
|
|
case AuthRoleModel::NAME_CUSTOMER: |
|
|
|
$userIds = UserModel::where(['customer_id' => $adminId])->column('user_id'); |
|
|
|
if (!empty($param['user_id']) && in_array($param['user_id'], $userIds)) { |
|
|
|
$whereUser[] = ['ual.user_id', '=', $param['user_id']]; |
|
|
|
} else { |
|
|
|
$whereUser[] = ['ual.user_id', 'in', $userIds]; |
|
|
|
} |
|
|
|
break; |
|
|
|
default: |
|
|
|
return $this->toData('500', '普通角色没有关联用户账号,不能查看用户数'); |
|
|
|
} |
|
|
|
|
|
|
|
// 组装主查询条件 |
|
|
|
$where = []; |
|
|
|
if (!empty($param['module'])) { |
|
|
|
$where[] = ['module', '=', $param['module']]; |
|
|
|
} |
|
|
|
if (!empty($param['ip'])) { |
|
|
|
$where['ip'] = $param['ip']; |
|
|
|
} |
|
|
|
// 构建查询(按 user_id 分组统计访问次数) |
|
|
|
$query = Db::name('user_access_log') |
|
|
|
->alias('ual') |
|
|
|
->field('ual.user_id, u.user_no, COUNT(*) as access_count, MAX(ual.created_at) as created_at') |
|
|
|
->join('users u', 'ual.user_id = u.user_id', 'LEFT') |
|
|
|
->where($where) |
|
|
|
->where($whereUser) |
|
|
|
->group('ual.user_id') |
|
|
|
->order('access_count', 'desc'); |
|
|
|
|
|
|
|
$list = $query->paginate([ |
|
|
|
'list_rows' => $param['limit'], |
|
|
|
'page' => $param['page'], |
|
|
|
]); |
|
|
|
|
|
|
|
return $this->toData('0', 'SUCCESS', [ |
|
|
|
'list' => $list->items(), |
|
|
|
'page' => $list->currentPage(), |
|
|
|
'total' => $list->total(), |
|
|
|
'last_page' => $list->lastPage(), |
|
|
|
]); |
|
|
|
} catch (\Exception $exception) { |
|
|
|
return $this->toData('500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 用户访问页面的记录 |
|
|
|
public function getUserAccessLog($adminId, $param) |
|
|
|
{ |
|
|
@ -683,7 +789,9 @@ class AdminService extends AdminBaseService |
|
|
|
return $this->toData('400', '当前账号分配的角色数据为空'); |
|
|
|
} |
|
|
|
// 根据账号角色限制数据查看范围, 超级管理员可以查看所有用户数据,代理可以查看自己所属用户的数据,总监、组长以此类推 |
|
|
|
$whereUser = []; |
|
|
|
$whereUser = [ |
|
|
|
['user_id', '>', 0] |
|
|
|
]; |
|
|
|
switch ($role->name) { |
|
|
|
case AuthRoleModel::NAME_ADMIN: // 超级管理员可以查看所有数据 |
|
|
|
if (!empty($param['user_id'])) { |
|
|
@ -741,6 +849,98 @@ class AdminService extends AdminBaseService |
|
|
|
if (!empty($param['module'])) { |
|
|
|
$where['module'] = $param['module']; |
|
|
|
} |
|
|
|
if (!empty($param['ip'])) { |
|
|
|
$where['ip'] = $param['ip']; |
|
|
|
} |
|
|
|
if (!empty($param['start_time']) && !empty($param['end_time'])) { |
|
|
|
$whereUser[] = ['created_at', 'between time', [$param['start_time'], $param['end_time']]]; |
|
|
|
} |
|
|
|
if ($param['export'] ?? false) { |
|
|
|
//太多了 只取10000条 |
|
|
|
$list = UserAccessLogModel::with(['user'])->where($where)->where($whereUser)->order('id', 'desc')->limit(10000)->select(); |
|
|
|
|
|
|
|
$exportData = [['ID', '用户id', '用户编号', '访问时间', 'IP地址', '访问页面', '模块', '备注']]; |
|
|
|
foreach ($list as $item) { |
|
|
|
$exportData[] = [$item->id, $item->user_id, $item->user->user_no ?? '', $item->created_at, $item->ip, $item->page_url, $item->module, $item->remark]; |
|
|
|
} |
|
|
|
|
|
|
|
return ExportService::exportCsv($exportData, '用户浏览信息导出.csv'); |
|
|
|
} else { |
|
|
|
$list = UserAccessLogModel::with(['user'])->where($where)->where($whereUser)->order('id', 'desc')->paginate([ |
|
|
|
'list_rows' => $param['limit'], |
|
|
|
'page' => $param['page'], |
|
|
|
]); |
|
|
|
|
|
|
|
foreach ($list->items() as $item) { |
|
|
|
$data[] = [ |
|
|
|
'id' => $item->id, |
|
|
|
'user_id' => $item->user_id, |
|
|
|
'created_at' => $item->created_at, |
|
|
|
'ip' => $item->ip, |
|
|
|
'page_url' => $item->page_url, |
|
|
|
'module' => $item->module, |
|
|
|
'remark' => $item->remark, |
|
|
|
'updated_at' => $item->updated_at, |
|
|
|
// 关联字段 |
|
|
|
'user_no' => $item->user->user_no ?? '', |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return $this->toData('0', 'SUCCESS', [ |
|
|
|
'list' => $data, |
|
|
|
'page' => $list->currentPage(), |
|
|
|
'total' => $list->total(), |
|
|
|
'last_page' => $list->lastPage(), |
|
|
|
]); |
|
|
|
} |
|
|
|
} catch (\Exception $exception) { |
|
|
|
return $this->toData('500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 游客访问页面的记录 |
|
|
|
public function getVisitorAccessLog($adminId, $param) |
|
|
|
{ |
|
|
|
try { |
|
|
|
if (empty($param['page']) || empty($param['limit'])) { |
|
|
|
return $this->toData('400', '缺少分页参数'); |
|
|
|
} |
|
|
|
// 获取账号信息 |
|
|
|
$account = AdminModel::where('id', $adminId)->find(); |
|
|
|
if (empty($account)) { |
|
|
|
return $this->toData('400', '当前账号数据为空'); |
|
|
|
} |
|
|
|
// 获取账号角色信息 |
|
|
|
$role = AuthRoleModel::where(['id' => $account->role_id])->find(); |
|
|
|
if (empty($role)) { |
|
|
|
return $this->toData('400', '当前账号分配的角色数据为空'); |
|
|
|
} |
|
|
|
// 根据账号角色限制数据查看范围, 超级管理员可以查看所有用户数据,代理可以查看自己所属用户的数据,总监、组长以此类推 |
|
|
|
$whereUser = [ |
|
|
|
['user_id', '=', null] |
|
|
|
]; |
|
|
|
|
|
|
|
$where = []; |
|
|
|
if (!empty($param['module'])) { |
|
|
|
$where['module'] = $param['module']; |
|
|
|
} |
|
|
|
if (!empty($param['ip'])) { |
|
|
|
$where['ip'] = $param['ip']; |
|
|
|
} |
|
|
|
if (!empty($param['start_time']) && !empty($param['end_time'])) { |
|
|
|
$whereUser[] = ['created_at', 'between time', [$param['start_time'], $param['end_time']]]; |
|
|
|
} |
|
|
|
if ($param['export'] ?? false) { |
|
|
|
//太多了 只取10000条 |
|
|
|
$list = UserAccessLogModel::where($where)->where($whereUser)->order('id', 'desc')->limit(10000)->select(); |
|
|
|
$exportData = [['ID', '访问时间', 'IP地址', '访问页面', '模块', '备注']]; |
|
|
|
foreach ($list as $item) { |
|
|
|
$exportData[] = [$item->id, $item->created_at, $item->ip, $item->page_url, $item->module, $item->remark]; |
|
|
|
} |
|
|
|
return ExportService::exportCsv($exportData, '访客浏览信息导出.csv'); |
|
|
|
} else { |
|
|
|
|
|
|
|
$list = UserAccessLogModel::where($where)->where($whereUser)->order('id', 'desc')->paginate([ |
|
|
|
'list_rows' => $param['limit'], |
|
|
|
'page' => $param['page'], |
|
|
@ -751,11 +951,71 @@ class AdminService extends AdminBaseService |
|
|
|
'total' => $list->total(), |
|
|
|
'last_page' => $list->lastPage(), |
|
|
|
]); |
|
|
|
} |
|
|
|
} catch (\Exception $exception) { |
|
|
|
return $this->toData('500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]); |
|
|
|
} |
|
|
|
} |
|
|
|
// 访问页面的记录汇总 |
|
|
|
public function getVisitorAccessLogSummary($adminId, $param) |
|
|
|
{ |
|
|
|
try { |
|
|
|
if (empty($param['page']) || empty($param['limit'])) { |
|
|
|
return $this->toData('400', '缺少分页参数'); |
|
|
|
} |
|
|
|
|
|
|
|
// 获取账号信息 |
|
|
|
$account = AdminModel::where('id', $adminId)->find(); |
|
|
|
if (empty($account)) { |
|
|
|
return $this->toData('400', '当前账号数据为空'); |
|
|
|
} |
|
|
|
|
|
|
|
// 获取账号角色信息 |
|
|
|
$role = AuthRoleModel::where(['id' => $account->role_id])->find(); |
|
|
|
if (empty($role)) { |
|
|
|
return $this->toData('400', '当前账号分配的角色数据为空'); |
|
|
|
} |
|
|
|
|
|
|
|
// 构建可查看的用户ID筛选条件 |
|
|
|
$whereUser = [ |
|
|
|
['ual.user_id', '=', null], |
|
|
|
]; |
|
|
|
|
|
|
|
// 组装主查询条件 |
|
|
|
$where = []; |
|
|
|
if (!empty($param['module'])) { |
|
|
|
$where[] = ['module', '=', $param['module']]; |
|
|
|
} |
|
|
|
|
|
|
|
if (!empty($param['ip'])) { |
|
|
|
$where[] = ['ip', '=', $param['ip']]; |
|
|
|
} |
|
|
|
|
|
|
|
// 构建查询(按 user_id 分组统计访问次数) |
|
|
|
$query = Db::name('user_access_log') |
|
|
|
->alias('ual') |
|
|
|
// ->field('user_id, COUNT(*) as access_count, MAX(create_time) as last_visit_time') |
|
|
|
->field('ip,COUNT(*) as access_count, MAX(ual.created_at) as created_at') |
|
|
|
->where($where) |
|
|
|
->where($whereUser) |
|
|
|
->group('ip') |
|
|
|
->order('access_count', 'desc'); |
|
|
|
|
|
|
|
$list = $query->paginate([ |
|
|
|
'list_rows' => $param['limit'], |
|
|
|
'page' => $param['page'], |
|
|
|
]); |
|
|
|
|
|
|
|
return $this->toData('0', 'SUCCESS', [ |
|
|
|
'list' => $list->items(), |
|
|
|
'page' => $list->currentPage(), |
|
|
|
'total' => $list->total(), |
|
|
|
'last_page' => $list->lastPage(), |
|
|
|
]); |
|
|
|
} catch (\Exception $exception) { |
|
|
|
return $this->toData('500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]); |
|
|
|
} |
|
|
|
} |
|
|
|
public function getAdminOperationLog($adminId, $param) |
|
|
|
{ |
|
|
|
try { |
|
|
@ -926,6 +1186,65 @@ class AdminService extends AdminBaseService |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 生成vip兑换码 |
|
|
|
public function generateVipCode($param) |
|
|
|
{ |
|
|
|
try { |
|
|
|
if (!isset($param['day']) || $param['day'] <= 0) { |
|
|
|
return $this->toData('400', lang('parameter_error')); |
|
|
|
} |
|
|
|
// 一次性生成10个兑换码 |
|
|
|
$resArr = []; |
|
|
|
for ($i = 1; $i <= 10; $i++) { |
|
|
|
$code = (new UnqId())->generateRandomCode(15); |
|
|
|
$exists = VipCodeModel::where(['vip_code' => $code])->find(); |
|
|
|
if (!empty($exists) && $exists->vip_code == $code) { |
|
|
|
for ($j = 1; $j <= 5; $j++) { // 如果存在相同code,本轮再试5次 |
|
|
|
$code = (new UnqId())->generateRandomCode(15); |
|
|
|
$exists = VipCodeModel::where(['vip_code' => $code])->find(); |
|
|
|
if (empty($exists)) { |
|
|
|
VipCodeModel::create(['vip_code' => $code, 'day' => $param['day'], 'status' => 0, 'uid' => 0]); |
|
|
|
$resArr[] = $code; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
VipCodeModel::create(['vip_code' => $code, 'day' => $param['day'], 'status' => 0, 'uid' => 0]); |
|
|
|
$resArr[] = $code; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return $this->toData('0', 'successful', [$resArr]); |
|
|
|
} catch (\Exception $e) { |
|
|
|
return $this->toData('500', 'The system is busy.', [$e->getMessage(), $e->getTrace()]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public function getVipCode($param) |
|
|
|
{ |
|
|
|
try { |
|
|
|
if (!isset($param['page']) || !isset($param['limit'])) { |
|
|
|
return $this->toData('400', lang('parameter_error')); |
|
|
|
} |
|
|
|
$where = []; |
|
|
|
if (isset($param['status'])) { |
|
|
|
$where['status'] = $param['status']; |
|
|
|
} |
|
|
|
$list = VipCodeModel::where($where)->order('id', 'desc')->paginate([ |
|
|
|
'list_rows' => $param['limit'], |
|
|
|
'page' => $param['page'], |
|
|
|
]); |
|
|
|
return $this->toData('0', 'SUCCESS', [ |
|
|
|
'list' => $list->items(), |
|
|
|
'page' => $list->currentPage(), |
|
|
|
'total' => $list->total(), |
|
|
|
'last_page' => $list->lastPage(), |
|
|
|
]); |
|
|
|
} catch (\Exception $e) { |
|
|
|
return $this->toData('500', 'The system is busy.', [$e->getMessage(), $e->getTrace()]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 单次发送邮件或短信 |
|
|
|
public function sendEmailOrSms($param) |
|
|
|
{ |
|
|
@ -1050,5 +1369,4 @@ class AdminService extends AdminBaseService |
|
|
|
return $this->toData('500', 'The system is busy', [$e->getMessage(), $e->getTrace()]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|