@ -17,6 +17,8 @@ use app\model\UserAccessLogModel;
use app\model\UserChatGroupModel;
use app\model\UserChatGroupModel;
use app\model\UserChatLinkModel;
use app\model\UserChatLinkModel;
use app\model\UserModel;
use app\model\UserModel;
use app\model\VipCodeModel;
use itinysun\model\helper\Service;
use think\facade\Cache;
use think\facade\Cache;
use think\exception\ValidateException;
use think\exception\ValidateException;
use app\utility\UnqId;
use app\utility\UnqId;
@ -36,12 +38,12 @@ class AdminService extends AdminBaseService
if (!isset($param['role_name'])) {
if (!isset($param['role_name'])) {
return $this->toData('400', '缺少参数');
return $this->toData('400', '缺少参数');
}
}
$role = AuthRoleModel::where(['name'=>$param['role_name']])->find();
$role = AuthRoleModel::where(['name' => $param['role_name']])->find();
if (empty($role)) {
if (empty($role)) {
return $this->toData('500', '没有目标角色信息');
return $this->toData('500', '没有目标角色信息');
}
}
// 根据角色查询账号列表
// 根据角色查询账号列表
$users = AdminModel::where(['role_id'=>$role->id])->select()->toArray();
$users = AdminModel::where(['role_id' => $role->id])->select()->toArray();
return $this->toData('0', 'ok', $users);
return $this->toData('0', 'ok', $users);
} catch (\Exception $e) {
} catch (\Exception $e) {
return $this->toData('500', '系统错误', [$e->getMessage(), $e->getTrace()]);
return $this->toData('500', '系统错误', [$e->getMessage(), $e->getTrace()]);
@ -66,7 +68,7 @@ class AdminService extends AdminBaseService
return $this->toData('1', '添加失败');
return $this->toData('1', '添加失败');
}
}
// 检测一下是否注册过聊天账号,没有的话就注册一下聊天账号
// 检测一下是否注册过聊天账号,没有的话就注册一下聊天账号
$isRegChat = UserChatLinkModel::where(['user_id'=>$resAdmin->id, 'user_type'=>UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find();
$isRegChat = UserChatLinkModel::where(['user_id' => $resAdmin->id, 'user_type' => UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find();
if (empty($isRegChat)) {
if (empty($isRegChat)) {
$chatData = [
$chatData = [
'Username' => $resAdmin->user_name, //用account.id + account.user_name 拼接作为聊天账号注册的Username
'Username' => $resAdmin->user_name, //用account.id + account.user_name 拼接作为聊天账号注册的Username
@ -87,13 +89,13 @@ class AdminService extends AdminBaseService
]);
]);
}
}
// 检测一下是否创建过聊天群组,没有的话创建一个
// 检测一下是否创建过聊天群组,没有的话创建一个
$chatGroup = UserChatGroupModel::where(['user_id'=>$resAdmin->id,'remark'=>UserChatGroupModel::USER_CHAT_GROUP_REMARK_ADMIN_INIT])->find();
$chatGroup = UserChatGroupModel::where(['user_id' => $resAdmin->id, 'remark' => UserChatGroupModel::USER_CHAT_GROUP_REMARK_ADMIN_INIT])->find();
if (empty($chatGroup)) {
if (empty($chatGroup)) {
$groupName = 'ivs-group'.$resAdmin->id;
$groupName = 'ivs-group' . $resAdmin->id;
$chatGroupData = [
$chatGroupData = [
'Name' => $groupName,
'Name' => $groupName,
];
];
$chatGroupUrl = env('CHAT_SERVER.BASE_URL') . '/api/group/'.$regChat['data']['uuid'];
$chatGroupUrl = env('CHAT_SERVER.BASE_URL') . '/api/group/' . $regChat['data']['uuid'];
$chatGroupRes = (new \app\utility\RequestChatServer())->ReqChatServer($chatGroupUrl, $chatGroupData);
$chatGroupRes = (new \app\utility\RequestChatServer())->ReqChatServer($chatGroupUrl, $chatGroupData);
if (!isset($chatGroupRes['data']['group_uuid'])) {
if (!isset($chatGroupRes['data']['group_uuid'])) {
return $this->toData('100400', '创建聊天群组失败');
return $this->toData('100400', '创建聊天群组失败');
@ -113,7 +115,6 @@ class AdminService extends AdminBaseService
} catch (\Exception $exception) {
} catch (\Exception $exception) {
return $this->toData('100500', '系统繁忙.', [$exception->getMessage()]);
return $this->toData('100500', '系统繁忙.', [$exception->getMessage()]);
}
}
}
}
/**
/**
@ -147,7 +148,7 @@ class AdminService extends AdminBaseService
$list[$i]['role_name'] = $roleList[$index]['name'];
$list[$i]['role_name'] = $roleList[$index]['name'];
}
}
$list[$i]['url'] = $host . '/register/index?=agent_code='.$list[$i]['invite_code'];
$list[$i]['url'] = $host . '/register/index?=agent_code=' . $list[$i]['invite_code'];
}
}
}
}
return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $list]);
return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $list]);
@ -183,7 +184,7 @@ class AdminService extends AdminBaseService
}
}
$infoArr['role_name'] = $role->name;
$infoArr['role_name'] = $role->name;
// 获取用户的聊天账号信息
// 获取用户的聊天账号信息
$chatInfo = UserChatLinkModel::where(['user_id'=>$userId, 'user_type'=>UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find();
$chatInfo = UserChatLinkModel::where(['user_id' => $userId, 'user_type' => UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find();
$chat_uuid = 0;
$chat_uuid = 0;
if (!empty($chatInfo)) {
if (!empty($chatInfo)) {
$chat_uuid = $chatInfo->chat_uuid;
$chat_uuid = $chatInfo->chat_uuid;
@ -232,7 +233,6 @@ class AdminService extends AdminBaseService
} catch (\Exception $exception) {
} catch (\Exception $exception) {
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
}
}
}
}
/**
/**
@ -273,7 +273,6 @@ class AdminService extends AdminBaseService
} catch (\Exception $exception) {
} catch (\Exception $exception) {
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
}
}
}
}
public function updateAccountStauts($accountId, $param): array
public function updateAccountStauts($accountId, $param): array
@ -300,7 +299,6 @@ class AdminService extends AdminBaseService
} catch (\Exception $exception) {
} catch (\Exception $exception) {
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
}
}
}
}
@ -336,7 +334,6 @@ class AdminService extends AdminBaseService
} catch (\Exception $exception) {
} catch (\Exception $exception) {
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
}
}
}
}
public function del($id): array
public function del($id): array
@ -352,7 +349,6 @@ class AdminService extends AdminBaseService
} catch (\Exception $exception) {
} catch (\Exception $exception) {
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
}
}
}
}
/**
/**
@ -370,9 +366,9 @@ class AdminService extends AdminBaseService
if ($rules) {
if ($rules) {
$rules = explode(",", $rules);
$rules = explode(",", $rules);
//字符串转换为数字类型
//字符串转换为数字类型
// $rules=array_map(function ($value){
// $rules=array_map(function ($value){
// return (int)$value;
// return (int)$value;
// },$rules);
// },$rules);
}
}
// 返回
// 返回
return $this->toData('0', 'Modification successful.', $rules);
return $this->toData('0', 'Modification successful.', $rules);
@ -404,7 +400,6 @@ class AdminService extends AdminBaseService
} catch (\Exception $exception) {
} catch (\Exception $exception) {
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
}
}
}
}
public function logList($param)
public function logList($param)
@ -421,7 +416,7 @@ class AdminService extends AdminBaseService
$list = $adminLogModel->order('id', 'desc')->page($param['page'], $param['limit'])->select()->toArray();
$list = $adminLogModel->order('id', 'desc')->page($param['page'], $param['limit'])->select()->toArray();
$accountIds = array_column($list, "admin_id");
$accountIds = array_column($list, "admin_id");
$accountList = AdminModel::where('id', 'in', $accountIds)->column('user_name', 'id');
$accountList = AdminModel::where('id', 'in', $accountIds)->column('user_name', 'id');
// $menuList = AuthRuleModel::column('title', 'path');
// $menuList = AuthRuleModel::column('title', 'path');
if (!empty($list)) {
if (!empty($list)) {
foreach ($list as & $v) {
foreach ($list as & $v) {
$v['admin_name'] = $accountList[$v['admin_id']] ?? '-';
$v['admin_name'] = $accountList[$v['admin_id']] ?? '-';
@ -438,11 +433,10 @@ class AdminService extends AdminBaseService
{
{
try {
try {
$user = AdminModel::select();
$user = AdminModel::select();
foreach ($user as $v){
foreach ($user as $v) {
$inviteCode = (new BaseHomeService())->getUniqInviteCode();
$inviteCode = (new BaseHomeService())->getUniqInviteCode();
AdminModel::where('id',$v->id)->update(['invite_code'=>$inviteCode]);
AdminModel::where('id', $v->id)->update(['invite_code' => $inviteCode]);
}
}
} catch (\Exception $exception) {
} catch (\Exception $exception) {
return $this->toData('500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
return $this->toData('500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
}
}
@ -456,14 +450,14 @@ class AdminService extends AdminBaseService
return $this->toData('400', '缺少分页参数');
return $this->toData('400', '缺少分页参数');
}
}
// 查询角色表中翻译角色的id
// 查询角色表中翻译角色的id
$role = AuthRoleModel::where(['name'=>'翻译'])->find();
$role = AuthRoleModel::where(['name' => '翻译'])->find();
if (empty($role)) {
if (empty($role)) {
return $this->toData('400', '系统不存在翻译角色,请先创建');
return $this->toData('400', '系统不存在翻译角色,请先创建');
}
}
// 查询属于翻译角色的所有账号
// 查询属于翻译角色的所有账号
$where = ['role_id'=>$role->id];
$where = ['role_id' => $role->id];
if (!empty($param['user_name'])) {
if (!empty($param['user_name'])) {
$list = AdminModel::where($where)->whereLike('user_name', '%'.$param['user_name'].'%')->order('id', 'desc')->paginate([
$list = AdminModel::where($where)->whereLike('user_name', '%' . $param['user_name'] . '%')->order('id', 'desc')->paginate([
'list_rows' => $param['limit'],
'list_rows' => $param['limit'],
'page' => $param['page'],
'page' => $param['page'],
]);
]);
@ -497,24 +491,24 @@ class AdminService extends AdminBaseService
}
}
// 翻译员的聊天账号信息
// 翻译员的聊天账号信息
$translatorChatInfo = UserChatLinkModel::where(['user_id'=>$param['translator_id'], 'user_type'=>UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find();
$translatorChatInfo = UserChatLinkModel::where(['user_id' => $param['translator_id'], 'user_type' => UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find();
if (empty($translatorChatInfo)) {
if (empty($translatorChatInfo)) {
return $this->toData('500', '翻译员的聊条账号信息错误');
return $this->toData('500', '翻译员的聊条账号信息错误');
}
}
// 翻译员创建的聊天群组信息
// 翻译员创建的聊天群组信息
$chatGroup = UserChatGroupModel::where(['user_id'=>$param['translator_id'], 'remark'=>UserChatGroupModel::USER_CHAT_GROUP_REMARK_ADMIN_INIT])->find();
$chatGroup = UserChatGroupModel::where(['user_id' => $param['translator_id'], 'remark' => UserChatGroupModel::USER_CHAT_GROUP_REMARK_ADMIN_INIT])->find();
if (empty($chatGroup)) {
if (empty($chatGroup)) {
return $this->toData('500', 'The chat group is error.');
return $this->toData('500', 'The chat group is error.');
}
}
foreach ($param['customer_id'] as $customerId) {
foreach ($param['customer_id'] as $customerId) {
// 记录好友关系
// 记录好友关系
$translatorCustomer = TranslatorCustomerModel::where(['translator_id'=>$param['translator_id'], 'customer_id'=>$customerId])->find();
$translatorCustomer = TranslatorCustomerModel::where(['translator_id' => $param['translator_id'], 'customer_id' => $customerId])->find();
if (empty($translatorCustomer)) {
if (empty($translatorCustomer)) {
TranslatorCustomerModel::create(['translator_id'=>$param['translator_id'], 'customer_id'=>$customerId]);
TranslatorCustomerModel::create(['translator_id' => $param['translator_id'], 'customer_id' => $customerId]);
}
}
//查询客服的聊天账号uuid
//查询客服的聊天账号uuid
$customerChatInfo = UserChatLinkModel::where(['user_id'=>$customerId, 'user_type'=>UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find();
$customerChatInfo = UserChatLinkModel::where(['user_id' => $customerId, 'user_type' => UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find();
if (empty($customerChatInfo)) {
if (empty($customerChatInfo)) {
return $this->toData('500', '客服的聊天账号信息错误');
return $this->toData('500', '客服的聊天账号信息错误');
}
}
@ -524,11 +518,11 @@ class AdminService extends AdminBaseService
];
];
$chatFriendsUrl = env('CHAT_SERVER.BASE_URL') . '/api/eachOtherFriends';
$chatFriendsUrl = env('CHAT_SERVER.BASE_URL') . '/api/eachOtherFriends';
$chatFriendsRes = (new \app\utility\RequestChatServer())->ReqChatServer($chatFriendsUrl, $chatFriendsData);
$chatFriendsRes = (new \app\utility\RequestChatServer())->ReqChatServer($chatFriendsUrl, $chatFriendsData);
Log::info("翻译员添加客服好友结果:". json_encode($chatFriendsRes));
Log::info("翻译员添加客服好友结果:" . json_encode($chatFriendsRes));
// 将客服添加到翻译员的聊天群组中
// 将客服添加到翻译员的聊天群组中
$joinChatGroupUrl = env('CHAT_SERVER.BASE_URL') . '/api/group/join/'.$customerChatInfo->chat_uuid.'/'.$chatGroup->group_uuid;
$joinChatGroupUrl = env('CHAT_SERVER.BASE_URL') . '/api/group/join/' . $customerChatInfo->chat_uuid . '/' . $chatGroup->group_uuid;
$joinChatGroupRes = (new \app\utility\RequestChatServer())->ReqChatServer($joinChatGroupUrl, []);
$joinChatGroupRes = (new \app\utility\RequestChatServer())->ReqChatServer($joinChatGroupUrl, []);
Log::info("翻译员添加客服到聊天群组结果:". json_encode($joinChatGroupRes));
Log::info("翻译员添加客服到聊天群组结果:" . json_encode($joinChatGroupRes));
}
}
return $this->toData('0', 'SUCCESS', []);
return $this->toData('0', 'SUCCESS', []);
@ -547,7 +541,7 @@ class AdminService extends AdminBaseService
if (empty($param['page']) || empty($param['limit'])) {
if (empty($param['page']) || empty($param['limit'])) {
return $this->toData('400', '缺少分页参数');
return $this->toData('400', '缺少分页参数');
}
}
$list = TranslatorCustomerModel::where(['translator_id'=>$param['translator_id']])->order('id', 'desc')->paginate([
$list = TranslatorCustomerModel::where(['translator_id' => $param['translator_id']])->order('id', 'desc')->paginate([
'list_rows' => $param['limit'],
'list_rows' => $param['limit'],
'page' => $param['page'],
'page' => $param['page'],
]);
]);
@ -571,19 +565,19 @@ class AdminService extends AdminBaseService
}
}
// 总监及以上角色才有权限分配用户与组长角色绑定
// 总监及以上角色才有权限分配用户与组长角色绑定
$account = AdminModel::where(['id'=>$adminId])->find();
$account = AdminModel::where(['id' => $adminId])->find();
if (empty($account)) {
if (empty($account)) {
return $this->toData('500', '当前账号数据为空');
return $this->toData('500', '当前账号数据为空');
}
}
$role = AuthRoleModel::where(['id'=>$account->role_id])->find();
$role = AuthRoleModel::where(['id' => $account->role_id])->find();
if (empty($role)) {
if (empty($role)) {
return $this->toData('500', '当前账号分配的角色为空');
return $this->toData('500', '当前账号分配的角色为空');
}
}
if (!in_array($role->name, [AuthRoleModel::NAME_ADMIN,AuthRoleModel::NAME_AGENT,AuthRoleModel::NAME_DIRECTOR])) {
if (!in_array($role->name, [AuthRoleModel::NAME_ADMIN, AuthRoleModel::NAME_AGENT, AuthRoleModel::NAME_DIRECTOR])) {
return $this->toData('500', '当前账号无权分配用户与组长角色绑定');
return $this->toData('500', '当前账号无权分配用户与组长角色绑定');
}
}
$relation = GroupLeaderWithUserModel::where(['user_id'=>$param['user_id']])->find();
$relation = GroupLeaderWithUserModel::where(['user_id' => $param['user_id']])->find();
if (empty($relation)) {
if (empty($relation)) {
$res = GroupLeaderWithUserModel::create([
$res = GroupLeaderWithUserModel::create([
'user_id' => $param['user_id'],
'user_id' => $param['user_id'],
@ -594,11 +588,11 @@ class AdminService extends AdminBaseService
}
}
}
}
$adminUserChat = UserChatLinkModel::where(['user_id'=>$param['group_leader_id'], 'user_type'=>UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find();
$adminUserChat = UserChatLinkModel::where(['user_id' => $param['group_leader_id'], 'user_type' => UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find();
if (empty($adminUserChat)) {
if (empty($adminUserChat)) {
return $this->toData('400', '管理端账号缺少chat信息');
return $this->toData('400', '管理端账号缺少chat信息');
}
}
$frontUserChat = UserChatLinkModel::where(['user_id'=>$param['user_id'], 'user_type'=>UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_PC])->find();
$frontUserChat = UserChatLinkModel::where(['user_id' => $param['user_id'], 'user_type' => UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_PC])->find();
if (empty($frontUserChat)) {
if (empty($frontUserChat)) {
return $this->toData('400', '用户账号缺少chat信息');
return $this->toData('400', '用户账号缺少chat信息');
}
}
@ -622,19 +616,19 @@ class AdminService extends AdminBaseService
return $this->toData('400', '缺少参数');
return $this->toData('400', '缺少参数');
}
}
// 组长及以上角色才有权限分配用户与电销角色绑定
// 组长及以上角色才有权限分配用户与电销角色绑定
$account = AdminModel::where(['id'=>$adminId])->find();
$account = AdminModel::where(['id' => $adminId])->find();
if (empty($account)) {
if (empty($account)) {
return $this->toData('500', '当前账号数据为空');
return $this->toData('500', '当前账号数据为空');
}
}
$role = AuthRoleModel::where(['id'=>$account->role_id])->find();
$role = AuthRoleModel::where(['id' => $account->role_id])->find();
if (empty($role)) {
if (empty($role)) {
return $this->toData('500', '当前账号分配的角色为空');
return $this->toData('500', '当前账号分配的角色为空');
}
}
if (!in_array($role->name, [AuthRoleModel::NAME_ADMIN,AuthRoleModel::NAME_AGENT,AuthRoleModel::NAME_DIRECTOR, AuthRoleModel::NAME_TEAM_HEADER])) {
if (!in_array($role->name, [AuthRoleModel::NAME_ADMIN, AuthRoleModel::NAME_AGENT, AuthRoleModel::NAME_DIRECTOR, AuthRoleModel::NAME_TEAM_HEADER])) {
return $this->toData('500', '当前账号无权分配用户与电销角色绑定');
return $this->toData('500', '当前账号无权分配用户与电销角色绑定');
}
}
$relation = SellerWithUserModel::where(['user_id'=>$param['user_id']])->find();
$relation = SellerWithUserModel::where(['user_id' => $param['user_id']])->find();
if (empty($relation)) {
if (empty($relation)) {
$res = SellerWithUserModel::create([
$res = SellerWithUserModel::create([
'user_id' => $param['user_id'],
'user_id' => $param['user_id'],
@ -645,11 +639,11 @@ class AdminService extends AdminBaseService
}
}
}
}
$adminUserChat = UserChatLinkModel::where(['user_id'=>$param['seller_id'], 'user_type'=>UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find();
$adminUserChat = UserChatLinkModel::where(['user_id' => $param['seller_id'], 'user_type' => UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find();
if (empty($adminUserChat)) {
if (empty($adminUserChat)) {
return $this->toData('400', '管理端账号缺少chat信息');
return $this->toData('400', '管理端账号缺少chat信息');
}
}
$frontUserChat = UserChatLinkModel::where(['user_id'=>$param['user_id'], 'user_type'=>UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_PC])->find();
$frontUserChat = UserChatLinkModel::where(['user_id' => $param['user_id'], 'user_type' => UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_PC])->find();
if (empty($frontUserChat)) {
if (empty($frontUserChat)) {
return $this->toData('400', '用户账号缺少chat信息');
return $this->toData('400', '用户账号缺少chat信息');
}
}
@ -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)
public function getUserAccessLog($adminId, $param)
{
{
@ -678,12 +784,14 @@ class AdminService extends AdminBaseService
return $this->toData('400', '当前账号数据为空');
return $this->toData('400', '当前账号数据为空');
}
}
// 获取账号角色信息
// 获取账号角色信息
$role = AuthRoleModel::where(['id'=>$account->role_id])->find();
$role = AuthRoleModel::where(['id' => $account->role_id])->find();
if (empty($role)) {
if (empty($role)) {
return $this->toData('400', '当前账号分配的角色数据为空');
return $this->toData('400', '当前账号分配的角色数据为空');
}
}
// 根据账号角色限制数据查看范围, 超级管理员可以查看所有用户数据,代理可以查看自己所属用户的数据,总监、组长以此类推
// 根据账号角色限制数据查看范围, 超级管理员可以查看所有用户数据,代理可以查看自己所属用户的数据,总监、组长以此类推
$whereUser = [];
$whereUser = [
['user_id', '>', 0]
];
switch ($role->name) {
switch ($role->name) {
case AuthRoleModel::NAME_ADMIN: // 超级管理员可以查看所有数据
case AuthRoleModel::NAME_ADMIN: // 超级管理员可以查看所有数据
if (!empty($param['user_id'])) {
if (!empty($param['user_id'])) {
@ -741,6 +849,98 @@ class AdminService extends AdminBaseService
if (!empty($param['module'])) {
if (!empty($param['module'])) {
$where['module'] = $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 = UserAccessLogModel::where($where)->where($whereUser)->order('id', 'desc')->paginate([
'list_rows' => $param['limit'],
'list_rows' => $param['limit'],
'page' => $param['page'],
'page' => $param['page'],
@ -751,11 +951,71 @@ class AdminService extends AdminBaseService
'total' => $list->total(),
'total' => $list->total(),
'last_page' => $list->lastPage(),
'last_page' => $list->lastPage(),
]);
]);
}
} catch (\Exception $exception) {
} catch (\Exception $exception) {
return $this->toData('500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
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)
public function getAdminOperationLog($adminId, $param)
{
{
try {
try {
@ -768,7 +1028,7 @@ class AdminService extends AdminBaseService
return $this->toData('400', '当前账号数据为空');
return $this->toData('400', '当前账号数据为空');
}
}
// 获取账号角色信息
// 获取账号角色信息
$role = AuthRoleModel::where(['id'=>$account->role_id])->find();
$role = AuthRoleModel::where(['id' => $account->role_id])->find();
if (empty($role)) {
if (empty($role)) {
return $this->toData('400', '当前账号分配的角色数据为空');
return $this->toData('400', '当前账号分配的角色数据为空');
}
}
@ -783,12 +1043,12 @@ class AdminService extends AdminBaseService
case AuthRoleModel::NAME_AGENT: // 代理 - 查看自己以及下属账号的操作数据
case AuthRoleModel::NAME_AGENT: // 代理 - 查看自己以及下属账号的操作数据
$ids = [$adminId];
$ids = [$adminId];
$directors = AdminModel::where('parent_id', $adminId)->column('id');
$directors = AdminModel::where('parent_id', $adminId)->column('id');
if (!empty($directors)){
if (!empty($directors)) {
$ids = array_merge($ids, $directors);
$ids = array_merge($ids, $directors);
$teamHeader = AdminModel::where('parent_id', 'in', $directors)->column('id');
$teamHeader = AdminModel::where('parent_id', 'in', $directors)->column('id');
if (!empty($teamHeader)) {
if (!empty($teamHeader)) {
$ids = array_merge($ids, $teamHeader);
$ids = array_merge($ids, $teamHeader);
$customer = AdminModel::where('parent_id','in', $teamHeader)->column('id');
$customer = AdminModel::where('parent_id', 'in', $teamHeader)->column('id');
if (!empty($customer)) {
if (!empty($customer)) {
$ids = array_merge($ids, $customer);
$ids = array_merge($ids, $customer);
}
}
@ -803,15 +1063,15 @@ class AdminService extends AdminBaseService
case AuthRoleModel::NAME_DIRECTOR: // 总监 - 查看自己以及下属账号的操作数据
case AuthRoleModel::NAME_DIRECTOR: // 总监 - 查看自己以及下属账号的操作数据
$ids = [$adminId];
$ids = [$adminId];
$teamHeader = AdminModel::where('parent_id', $adminId)->column('id');
$teamHeader = AdminModel::where('parent_id', $adminId)->column('id');
if (!empty($teamHeader)){
if (!empty($teamHeader)) {
$ids = array_merge($ids, $teamHeader);
$ids = array_merge($ids, $teamHeader);
$customer = AdminModel::where('parent_id','in', $teamHeader)->column('id');
$customer = AdminModel::where('parent_id', 'in', $teamHeader)->column('id');
if (!empty($customer)) {
if (!empty($customer)) {
$ids = array_merge($ids, $customer);
$ids = array_merge($ids, $customer);
}
}
}
}
if (!empty($param['admin_id']) & & in_array($param['admin_id'], $ids)) {
if (!empty($param['admin_id']) & & in_array($param['admin_id'], $ids)) {
$where[] = ['admin_id', '=',$param['admin_id']];
$where[] = ['admin_id', '=', $param['admin_id']];
} else {
} else {
$where[] = ['admin_id', 'in', $ids];
$where[] = ['admin_id', 'in', $ids];
}
}
@ -857,7 +1117,7 @@ class AdminService extends AdminBaseService
if (empty($param['user_id'])) {
if (empty($param['user_id'])) {
return $this->toData('400', '请指定用户ID');
return $this->toData('400', '请指定用户ID');
}
}
if (empty($param['days']) || $param['days'] < = 0 ) {
if (empty($param['days']) || $param['days'] < = 0) {
return $this->toData('400', '赠送时间错误');
return $this->toData('400', '赠送时间错误');
}
}
$userId = $param['user_id'];
$userId = $param['user_id'];
@ -870,7 +1130,7 @@ class AdminService extends AdminBaseService
// 记录赠送vip信息
// 记录赠送vip信息
Db::transaction(function () use ($userId, $giveDay) {
Db::transaction(function () use ($userId, $giveDay) {
// 查询用户是否开通过vip,更新用户vip到期时间
// 查询用户是否开通过vip,更新用户vip到期时间
$vipLog = PurchaseVipModel::where(['user_id'=>$userId])->find();
$vipLog = PurchaseVipModel::where(['user_id' => $userId])->find();
$expireTimestamp = strtotime("+$giveDay day");
$expireTimestamp = strtotime("+$giveDay day");
$expireTime = date("Y-m-d H:i:s", $expireTimestamp);
$expireTime = date("Y-m-d H:i:s", $expireTimestamp);
if (!empty($vipLog)) {
if (!empty($vipLog)) {
@ -911,7 +1171,7 @@ class AdminService extends AdminBaseService
if (!isset($param['page']) || !isset($param['limit']) || !isset($param['user_id'])) {
if (!isset($param['page']) || !isset($param['limit']) || !isset($param['user_id'])) {
return $this->toData('400', lang('parameter_error'));
return $this->toData('400', lang('parameter_error'));
}
}
$list = PurchaseVipLogModel::where(['user_id'=>$param['user_id']])->order('id', 'desc')->paginate([
$list = PurchaseVipLogModel::where(['user_id' => $param['user_id']])->order('id', 'desc')->paginate([
'list_rows' => $param['limit'],
'list_rows' => $param['limit'],
'page' => $param['page'],
'page' => $param['page'],
]);
]);
@ -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)
public function sendEmailOrSms($param)
{
{
@ -933,11 +1252,11 @@ class AdminService extends AdminBaseService
if (empty($param['user_id']) || empty($param['type']) || empty($param['content'])) {
if (empty($param['user_id']) || empty($param['type']) || empty($param['content'])) {
return $this->toData('400', '参数错误');
return $this->toData('400', '参数错误');
}
}
if (!in_array($param['type'], [1,2])) {
if (!in_array($param['type'], [1, 2])) {
return $this->toData('400', '参数错误');
return $this->toData('400', '参数错误');
}
}
$user = UserModel::where(['user_id'=>$param['user_id']])->find();
$user = UserModel::where(['user_id' => $param['user_id']])->find();
if (empty($user)) {
if (empty($user)) {
return $this->toData('500', '用户不存在');
return $this->toData('500', '用户不存在');
}
}
@ -990,7 +1309,7 @@ class AdminService extends AdminBaseService
{
{
try {
try {
// type: 1-短信,2-邮件
// type: 1-短信,2-邮件
if (!in_array($param['type'], [1,2])) {
if (!in_array($param['type'], [1, 2])) {
return $this->toData('400', '发送类型不在支持范围内');
return $this->toData('400', '发送类型不在支持范围内');
}
}
// content: 发送的消息内容
// content: 发送的消息内容
@ -1015,7 +1334,7 @@ class AdminService extends AdminBaseService
}
}
$jobName = 'app\admin\job\SendSmsFromBackend';
$jobName = 'app\admin\job\SendSmsFromBackend';
Queue::push($jobName, [
Queue::push($jobName, [
'mobile' => $v['country_code'].$v['phone_number'],
'mobile' => $v['country_code'] . $v['phone_number'],
'message' => $param['content'],
'message' => $param['content'],
], 'sendSmsFromBackend');
], 'sendSmsFromBackend');
}
}
@ -1050,5 +1369,4 @@ class AdminService extends AdminBaseService
return $this->toData('500', 'The system is busy', [$e->getMessage(), $e->getTrace()]);
return $this->toData('500', 'The system is busy', [$e->getMessage(), $e->getTrace()]);
}
}
}
}
}
}