p2 project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.6 KiB

<?php
namespace app\model;
use think\Env;
class AdminModel extends BaseModel
{
protected $name = 'account';
const STATUS_ON = 1; // 用户启用状态
const STATUS_FORBID = 2; // 用户禁用状态
const ROLE_ID_ADMIN = 1; //超级管理员角色
const ROLE_ID_AGENT = 2; // 代理角色
// 通过代理id 获取代理下面可以访问的所有用户id
public static function getUserIdsByAgentId($agentId)
{
// 查询直接被管理的用户
$firstUserIds = UserModel::where('agent_id', $agentId)->column('user_id');
if(empty($firstUserIds)){
return [];
}
// 查询父级下面的用户
$userIds = UserModel::where('origin_user_id','in',$firstUserIds)->column('user_id');
return array_merge($firstUserIds, $userIds);
// return UserModel::where('origin_user_id','in',$firstUserIds)->column('user_id');
}
// 判断操作用户是否是代理商
public static function checkUserIsAgent($adminId)
{
// 代理角色
$admin = self::where('id', $adminId)->find();
if($admin && $admin['role_id'] == self::ROLE_ID_AGENT){
return true;
}
return false;
// $agentGroupId = env('AGENT.AGENT_GROUP_ID');
// $authGroupAccess = AuthGroupAccessModel::where('uid', $adminId)->find();
// if($authGroupAccess && $authGroupAccess['group_id'] == $agentGroupId){
// return true;
// }
// return false;
}
/**
* @desc 根据用户id 查询指定字段
* @param $fields
* @param $userId
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function getFieldsByUserId($fields, $userId): array
{
$self = self::where('id', $userId)->field($fields)->find();
if(empty($self)){
return [];
}
return $self->toArray();
}
/**
* @desc 获取邀请码用户
* @param $inviteCode
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function getIdByInviteCode($inviteCode): int
{
$self = self::where('invite_code', $inviteCode)->value('id');
if(empty($self)){
return 0;
}
return $self;
}
// 获取默认的代理ID
public static function getDefaultAgentId(): int
{
$role = AuthRoleModel::where(['name'=>'代理'])->find();
if (empty($role)) {
return 0;
}
return $role->id;
}
// 根据代理ID获取其下属的所有客服ID (账号关系:代理 - 总监 - 组长 - 客服)
public static function getCustomerIdsByAgentId($agentId): array
{
// 获取代理下的所有总监ID
$directors = self::where(['parent_id' => $agentId])->column('id');
if (empty($directors)) {
return [];
}
// 获取总监下所有组长ID
$teamHeaders = self::where('parent_id', 'in', $directors)->column('id');
if (empty($teamHeaders)) {
return [];
}
// 获取组长下的客服ID 注意:ext = 1 表示启用中的客服,ext = 2表示客服停用了
$customers = self::where('parent_id', 'in', $teamHeaders)->where('ext', 1)->column('id');
if (empty($customers)) {
return [];
}
return $customers;
}
}