chuan 2 months ago
parent
commit
6f887f46c8
  1. 4
      app/admin/controller/Admin.php
  2. 1
      app/admin/controller/Test.php
  3. 148
      app/admin/service/AdminService.php
  4. 46
      app/admin/service/AgentService.php
  5. 3
      app/admin/service/NoticeService.php
  6. 35
      app/admin/service/UserService.php
  7. 8
      app/admin/service/auth/AuthRuleService.php
  8. 10
      app/model/AuthRoleModel.php

4
app/admin/controller/Admin.php

@ -130,14 +130,14 @@ class Admin extends AdminBaseController
// 获取用户访问页面的记录
public function getUserAccessLog()
{
$returnData = (new AdminService())->getUserAccessLog($this->request->param());
$returnData = (new AdminService())->getUserAccessLog($this->request->user_id, $this->request->param());
return json($returnData);
}
// 获取admin的操作记录
public function getAdminOperationLog()
{
$returnData = (new AdminService())->getAdminOperationLog($this->request->param());
$returnData = (new AdminService())->getAdminOperationLog($this->request->user_id,$this->request->param());
return json($returnData);
}

1
app/admin/controller/Test.php

@ -1,5 +1,4 @@
<?php
namespace app\admin\controller;
class Test extends AdminBaseController

148
app/admin/service/AdminService.php

@ -2,6 +2,7 @@
namespace app\admin\service;
use app\admin\controller\Admin;
use app\admin\validate\AdminValidate;
use app\home\service\BaseHomeService;
use app\model\AdminLogModel;
@ -630,20 +631,82 @@ class AdminService extends AdminBaseService
}
// 用户访问页面的记录
public function getUserAccessLog($param)
public function getUserAccessLog($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 = [];
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[] = ['user_id', '=', $userIds];
} else {
$whereUser[] = ['user_id', 'in', $userIds];
}
break;
case AuthRoleModel::NAME_DIRECTOR: // 总监 - 查询总监下可以查看的用户
$userIds = [];
$teamHeaders = AdminModel::where('parent_id', $adminId)->find(); // 组长账号
if (!empty($teamHeaders)) {
$customer = AdminModel::where(['parent_id', 'in', $teamHeaders])->find(); // 客服账号
if (!empty($customer)) {
$userIds = UserModel::where(['customer_id', 'in', $customer])->column('user_id');
}
}
if (!empty($param['user_id']) && in_array($param['user_id'], $userIds)) {
$whereUser[] = ['user_id', '=', $userIds];
} else {
$whereUser[] = ['user_id', 'in', $userIds];
}
break;
case AuthRoleModel::NAME_TEAM_HEADER: // 组长 - 查看组长下可以查看的用户
$userIds = [];
$customer = AdminModel::where('parent_id', $adminId)->find();
if (!empty($customer)) {
$userIds = UserModel::where(['customer_id', 'in', $customer])->column('user_id');
}
if (!empty($param['user_id']) && in_array($param['user_id'], $userIds)) {
$whereUser[] = ['user_id', '=', $userIds];
} else {
$whereUser[] = ['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[] = ['user_id', '=', $userIds];
} else {
$whereUser[] = ['user_id', 'in', $userIds];
}
break;
default:
return $this->toData('500', '普通角色没有关联用户账号,不能查看用户数');
}
$where = [];
if (!empty($param['module'])) {
$where['module'] = $param['module'];
}
if (!empty($param['user_id'])) {
$where['user_id'] = $param['user_id'];
}
$list = UserAccessLogModel::where($where)->order('id', 'desc')->paginate([
$list = UserAccessLogModel::where($whereUser)->where($where)->order('id', 'desc')->paginate([
'list_rows' => $param['limit'],
'page' => $param['page'],
]);
@ -658,16 +721,85 @@ class AdminService extends AdminBaseService
}
}
public function getAdminOperationLog($param)
public function getAdminOperationLog($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', '当前账号分配的角色数据为空');
}
// 根据账号角色限制数据查看范围
$where = [];
if (!empty($param['admin_id'])) {
$where['admin_id'] = $param['admin_id'];
switch ($role->name) {
case AuthRoleModel::NAME_ADMIN: // 超级管理员可以查看所有数据
if (!empty($param['admin_id'])) {
$where[] = ['admin_id', '=', $param['admin_id']];
}
break;
case AuthRoleModel::NAME_AGENT: // 代理 - 查看自己以及下属账号的操作数据
$ids = [$adminId];
$directors = AdminModel::where('parent_id', $adminId)->column('id');
if (!empty($directors)){
$ids = array_merge($ids, $directors);
$teamHeader = AdminModel::where('parent_id', 'in', $directors)->column('id');
if (!empty($teamHeader)) {
$ids = array_merge($ids, $teamHeader);
$customer = AdminModel::where('parent_id','in', $teamHeader)->column('id');
if (!empty($customer)) {
$ids = array_merge($ids, $customer);
}
}
}
if (!empty($param['admin_id']) && in_array($param['admin_id'], $ids)) {
$where[] = ['admin_id', '=', $param['admin_id']];
} else {
$where[] = ['admin_id', 'in', $ids];
}
break;
case AuthRoleModel::NAME_DIRECTOR: // 总监 - 查看自己以及下属账号的操作数据
$ids = [$adminId];
$teamHeader = AdminModel::where('parent_id', $adminId)->column('id');
if (!empty($teamHeader)){
$ids = array_merge($ids, $teamHeader);
$customer = AdminModel::where('parent_id','in', $teamHeader)->column('id');
if (!empty($customer)) {
$ids = array_merge($ids, $customer);
}
}
if (!empty($param['admin_id']) && in_array($param['admin_id'], $ids)) {
$where[] = ['admin_id', '=',$param['admin_id']];
} else {
$where[] = ['admin_id', 'in', $ids];
}
break;
case AuthRoleModel::NAME_TEAM_HEADER: // 组长 - 查看自己以及下属账号的操作数据
$ids = [$adminId];
$customer = AdminModel::where('parent_id', $adminId)->column('id');
if (!empty($customer)) {
$ids = array_merge($ids, $customer);
}
if (!empty($param['admin_id']) && in_array($param['admin_id'], $ids)) {
$where[] = ['admin_id', '=', $param['admin_id']];
} else {
$where[] = ['admin_id', 'in', $ids];
}
break;
case AuthRoleModel::NAME_CUSTOMER: // 客服 - 查看自己操作数据
$where[] = ['admin_id', '=', $adminId];
break;
default:
$where[] = ['admin_id', '=', $adminId]; // 其它角色 - 查看自己的操作数
}
$list = AdminLogModel::where($where)->order('id', 'desc')->paginate([
'list_rows' => $param['limit'],
'page' => $param['page'],

46
app/admin/service/AgentService.php

@ -176,28 +176,43 @@ class AgentService extends AdminBaseService
if (empty($customerRole)) {
return $this->toData('500', '系统中还没有客服角色');
}
$roleId = $customerRole->id;
// 如有指定代理ID, 则查询代理ID下的客服列表,否则查询所有代理下的客服列表
$agentId = 0;
if (isset($param['agent_id']) && is_numeric($param['agent_id'])) {
$agentId = $param['agent_id'];
}
$list = AdminModel::when($agentId, function ($query) use ($agentId) {
$query->where('parent_id', $agentId)->order('id', 'desc'); //查询代理下的客服列表
}, function ($query) use($roleId) {
$query->where('role_id', $roleId)->order('id', 'desc'); //查询所有客服列表
})->paginate([
$where = [];
if (isset($param['agent_id'])) { // 根据代理ID过滤客服列表
$teamHeaderIds = [];
$directorIds = AdminModel::where('parent_id', $param['agent_id'])->column('id'); // 代理下的总监账号
if (!empty($directorIds)) {
$teamHeaderIds = AdminModel::where('parent_id', 'in', $directorIds)->column('id'); // 代理下的组长账号
}
$where[] = ['parent_id', 'in', $teamHeaderIds];
} else { // 查询所有客服列表
$where[] = ['role_id', '=', $customerRole->id];
}
$list = AdminModel::where($where)->order('id', 'desc')->paginate([
'list_rows' => $param['limit'],
'page' => $param['page'],
]);
// 获取客服的chat信息
$resData = [];
if (!empty($list->items())) {
$idArr = array_column($list->items(), 'id');
$chatInfos = UserChatLinkModel::where('user_id', 'in', $idArr)->where('user_type', UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN)->column('user_id,chat_uuid,chat_name', 'user_id');
foreach ($list->items() as $itm) {
$tmp = $itm;
$tmp['chat_uuid'] = isset($chatInfos[$itm['id']]) ? $chatInfos[$itm['id']]['chat_uuid'] : '';
$tmp['chat_name'] = isset($chatInfos[$itm['id']]) ? $chatInfos[$itm['id']]['chat_name'] : '';
$resData[] = $tmp;
}
}
return $this->toData('0', 'success', [
'list' => $list->items(), // 当前页的数据
'list' => $resData, // 当前页的数据
'page' => $list->currentPage(), // 当前页码
'total' => $list->total(), // 总记录数
'last_page' => $list->lastPage(), // 最后一页页码
'agent_id' => $agentId,
]);
} catch (\Exception $exception) {
return $this->toData('500', '系统异常 请稍后重试', [$exception->getMessage(), $exception->getTrace()]);
@ -259,6 +274,11 @@ class AgentService extends AdminBaseService
if (empty($user)) {
return $this->toData('500', '用户数据为空');
}
// 获取旧客服chat信息
$oldCustomerChatInfo = UserChatLinkModel::where(['user_id'=>$user->customer_id, 'user_type'=>UserChatLinkModel::USER_CHAT_LINK_USER_TYPE_ADMIN])->find();
if (empty($oldCustomerChatInfo)) {
return $this->toData('500', '旧客服的聊天数据错误');
}
// 变更的客服必须是同一个代理 $user->agent_id
$newCustomer = AdminModel::where(['id'=>$param['new_customer_id']])->find();
if (empty($newCustomer)) {
@ -300,6 +320,8 @@ class AgentService extends AdminBaseService
$chatFriendsData = [
'UserUuid' => $userChatInfo->chat_uuid,
'CustomerUuid' => $customerChatInfo->chat_uuid,
'Type' => 1, // 1-转移好友聊天记录
'CustomerOldUuid' => $oldCustomerChatInfo->chat_uuid, // 旧客服的uuid
];
$chatFriendsUrl = env('CHAT_SERVER.BASE_URL') . '/api/eachOtherFriends';
$chatFriendsRes = (new \app\utility\RequestChatServer())->ReqChatServer($chatFriendsUrl, $chatFriendsData);

3
app/admin/service/NoticeService.php

@ -21,9 +21,10 @@ class NoticeService extends AdminBaseService
return $this->toData('400', 'user_ids参数格式错误');
}
// 循环向每个用户发送弹窗
$data = ['type'=>'admin-popup', 'content'=>$param['content']];
Gateway::$registerAddress = env('GATEWAY_SERVER.REGISTER');
foreach ($param['user_ids'] as $uid) {
Gateway::sendToUid($uid, $param['content']);
Gateway::sendToUid($uid, json_encode($data));
}
return $this->toData('0', 'ok');
} catch (\Exception $e) {

35
app/admin/service/UserService.php

@ -61,23 +61,38 @@ class UserService extends AdminBaseService
// 角色数据权限过滤: 管理员登录可查看所有用户数据;代理登录查看代理下用户数据;客服登录查看客服关联用户的数据
$account = AdminModel::where(['id'=>$adminId])->find();
if (empty($account)) {
return $this->toData('500', '当前账号数据错误');
return $this->toData('500', '当前账号数据为空');
}
$role = AuthRoleModel::where(['id'=>$account->role_id])->find();
if (empty($role)) {
return $this->toData('500', '当前角色数据错误');
return $this->toData('500', '当前账号分配的角色为空');
}
// 如果是代理,过滤代理下的用户
if ($role->name == AuthRoleModel::NAME_AGENT) {
$where['agent_id'] = $adminId;
}
// 如果是客服,过滤客服下的用户
if ($role->name == AuthRoleModel::NAME_CUSTOMER) {
$where['customer_id'] = $adminId;
switch ($role->name) {
case AuthRoleModel::NAME_ADMIN: // 超级管理员可以查看所有数据
break;
case AuthRoleModel::NAME_AGENT: // 代理可以查看自己下属用户数据
$where['agent_id'] = $adminId;
break;
case AuthRoleModel::NAME_DIRECTOR: // 总监可以查看自己下属用户数
$customerIds = [];
$teamHeaderIds = AdminModel::where(['parent_id', '=', $adminId])->column('id');
if (!empty($teamHeaderIds)) {
$customerIds = AdminModel::where(['parent_id', 'in', $teamHeaderIds])->column('id');
}
$where[] = ['customer_id', 'in', $customerIds];
break;
case AuthRoleModel::NAME_TEAM_HEADER: // 组长可以查看自己下属用户数据
$customerIds = AdminModel::where(['parent_id', '=', $adminId])->find();
$where[] = ['customer_id', 'in', $customerIds];
break;
case AuthRoleModel::NAME_CUSTOMER: // 客服可以查看自己下属数据
$where['customer_id'] = $adminId;
break;
default:
return $this->toData('500', '普通账号无权查看用户数据');
}
// base_label过滤
if (!empty($param['base_label'])) {
$where['base_label'] = trim($param['base_label']);

8
app/admin/service/auth/AuthRuleService.php

@ -148,8 +148,10 @@ class AuthRuleService extends AdminBaseService
public function getSideMenu($user_id){
try {
//获取用户权限id
$user=AdminModel::find($user_id);
$role=AuthRoleModel::find($user->role_id);
$user = AdminModel::find($user_id);
if (empty($user)) {
return $this->toData('500', '当前用户数据为空', ['user_id'=>$user_id]);
}
//查询拥有权限
if($user->role_id == AdminModel::ROLE_ID_ADMIN){
//超级管理员拥有全部权限
@ -190,7 +192,7 @@ class AuthRuleService extends AdminBaseService
$message = $validateException->getError();
return $this->toData('200400', $message);
}catch (\Exception $exception){
return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
return $this->toData('200500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
}
}

10
app/model/AuthRoleModel.php

@ -8,8 +8,14 @@ class AuthRoleModel extends BaseModel
{
const STATUS_ON = 1; // 用户启用状态
const STATUS_FORBID = 2; // 用户禁用状态
const NAME_CUSTOMER = '客服'; //客服角色
const NAME_AGENT = '代理'; //代理角色
const NAME_ADMIN = '超级管理员';
const NAME_AGENT = '代理';
const NAME_DIRECTOR = '总监';
const NAME_TEAM_HEADER = '组长';
const NAME_CUSTOMER = '客服';
const NAME_SELLER = '电销';
const NAME_TRANSLATOR = '翻译员';
protected $name = 'auth_role';

Loading…
Cancel
Save