This commit is contained in:
wong
2025-04-23 18:11:05 +08:00
7 changed files with 315 additions and 36 deletions

View File

@@ -17,4 +17,14 @@ class DeviceWechatLogin extends Model
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
}
/**
* 获取设备最新登录记录的id
*
* @param array $deviceIds
* @return array
*/
public static function getDevicesLatestLogin(array $deviceIds): array
{
return self::fieldRaw('max(id) as lastedId,deviceId')->whereIn('deviceId', $deviceIds)->group('deviceId')->select()->toArray();
}
}

View File

@@ -41,5 +41,7 @@ Route::group('', function () {
Route::get('list', 'app\superadmin\controller\company\GetCompanyListController@index');
Route::get('detail/:id', 'app\superadmin\controller\company\GetCompanyDetailForUpdateController@index');
Route::get('profile/:id', 'app\superadmin\controller\company\GetCompanyDetailForProfileController@index');
Route::get('devices', 'app\superadmin\controller\company\GetCompanyDevicesForProfileController@index');
Route::get('subusers', 'app\superadmin\controller\company\GetCompanySubusersForProfileController@index');
});
})->middleware(['app\superadmin\middleware\AdminAuth']);

View File

@@ -0,0 +1,95 @@
<?php
namespace app\superadmin\controller\company;
use app\common\model\Device as DeviceModel;
use app\common\model\DeviceWechatLogin as DeviceWechatLoginModel;
use app\common\model\WechatFriend as WechatFriendModel;
use Eison\Utils\Helper\ArrHelper;
use library\ResponseHelper;
use think\Controller;
/**
* 设备管理控制器
*/
class GetCompanyDevicesForProfileController extends Controller
{
/**
* 获取项目下的所有设备
*
* @return array
*/
protected function getDevicesWithCompanyId(): array
{
$companyId = $this->request->param('companyId/d', 0);
$devices = DeviceModel::where(compact('companyId'))->field('id,memo,imei,phone,model,brand,alive,id deviceId')
->select()
->toArray();
if (empty($devices)) {
throw new \Exception('暂无设备', 200);
}
return $devices;
}
/**
* 查询设备与微信的关联关系.
*
* @return array
*/
protected function getDeviceWechatRelationsByDeviceIds(array $deviceIds): array
{
// 获取设备最新登录记录的id
$latestLogs = DeviceWechatLoginModel::getDevicesLatestLogin($deviceIds);
// 获取最新登录记录id
$latestIds = array_column($latestLogs, 'lastedId');
return DeviceWechatLoginModel::field('deviceId,wechatId,alive wAlive')
->whereIn('id', $latestIds)
->select()
->toArray();
}
/**
* 获取设备的微信好友统计
*
* @param array $ownerWechatId
* @return void
*/
protected function getWechatFriendsCount(array $deviceIds): array
{
// 查询设备与微信的关联关系
$relations = $this->getDeviceWechatRelationsByDeviceIds($deviceIds);
// 统计微信好友数量
$friendCounts = WechatFriendModel::alias('f')->field('ownerWechatId wechatId,count(*) friendCount')
->whereIn('ownerWechatId', array_column($relations, 'wechatId'))
->group('ownerWechatId')
->select()
->toArray();
return ArrHelper::join($relations, $friendCounts, 'wechatId');
}
/**
* 获取公司关联的设备列表
*
* @return \think\response\Json
*/
public function index()
{
try {
$devices = $this->getDevicesWithCompanyId();
$friendCount = $this->getWechatFriendsCount(array_column($devices, 'id'));
$result = ArrHelper::leftJoin($devices, $friendCount, 'deviceId');
return ResponseHelper::success($result);
} catch (\Exception $e) {
return ResponseHelper::error($e->getMessage(), $e->getCode());
}
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace app\superadmin\controller\company;
use app\common\model\User as UserModel;
use library\ResponseHelper;
use think\Controller;
/**
* 设备管理控制器
*/
class GetCompanySubusersForProfileController extends Controller
{
/**
* 获取项目下的所有子账号
*
* @return CompanyModel
* @throws \Exception
*/
protected function getSubusers(): array
{
$where = [
'companyId' => $this->request->param('companyId/d', 0),
'isAdmin' => 0
];
return UserModel::field('id,account,phone,username,avatar,status,createTime,typeId')->where($where)->select()->toArray();
}
/**
* 获取公司关联的设备列表
*
* @return \think\response\Json
*/
public function index()
{
$users = $this->getSubusers();
foreach ($users as &$user) {
$user['createTime'] = date('Y-m-d H:i:s', $user['createTime']);
}
return ResponseHelper::success($users);
}
}

View File

@@ -29,7 +29,7 @@ class ResponseHelper
* @param mixed $data 错误数据
* @return \think\response\Json
*/
public static function error($msg = '操作失败', $code = 400, $data = null)
public static function error($msg = '操作失败', $code = 400, $data = [])
{
return json([
'code' => $code,

View File

@@ -35,6 +35,8 @@ export default function EditProjectPage({ params }: { params: { id: string } })
const [isSubmitting, setIsSubmitting] = useState(false)
const [isLoading, setIsLoading] = useState(true)
const [project, setProject] = useState<Project | null>(null)
const [password, setPassword] = useState("")
const [confirmPassword, setConfirmPassword] = useState("")
const { id } = use(params)
useEffect(() => {

View File

@@ -10,6 +10,7 @@ import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@
import { ArrowLeft, Edit } from "lucide-react"
import { toast } from "sonner"
import { use } from "react"
import Image from "next/image"
interface ProjectProfile {
id: number
@@ -24,15 +25,45 @@ interface ProjectProfile {
userCount: number
}
interface Device {
id: number
memo: string
phone: string
model: string
brand: string
alive: number
deviceId: number
wechatId: string
friendCount: number
wAlive: number
imei: string
}
interface SubUser {
id: number
account: string
username: string
phone: string
avatar: string
status: number
createTime: string
typeId: number
}
export default function ProjectDetailPage({ params }: { params: { id: string } }) {
const router = useRouter()
const [isLoading, setIsLoading] = useState(true)
const [isDevicesLoading, setIsDevicesLoading] = useState(false)
const [isSubUsersLoading, setIsSubUsersLoading] = useState(false)
const [profile, setProfile] = useState<ProjectProfile | null>(null)
const [devices, setDevices] = useState<Device[]>([])
const [subUsers, setSubUsers] = useState<SubUser[]>([])
const [activeTab, setActiveTab] = useState("overview")
const { id } = use(params)
useEffect(() => {
const fetchProjectProfile = async () => {
setIsLoading(true)
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}/company/profile/${id}`)
const data = await response.json()
@@ -52,6 +83,62 @@ export default function ProjectDetailPage({ params }: { params: { id: string } }
fetchProjectProfile()
}, [id])
useEffect(() => {
const fetchDevices = async () => {
if (activeTab === "devices") {
setIsDevicesLoading(true)
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}/company/devices?companyId=${id}`)
const data = await response.json()
if (data.code === 200) {
setDevices(data.data)
} else {
toast.error(data.msg || "获取设备列表失败")
}
} catch (error) {
toast.error("网络错误,请稍后重试")
} finally {
setIsDevicesLoading(false)
}
}
}
fetchDevices()
}, [activeTab, id])
useEffect(() => {
const fetchSubUsers = async () => {
if (activeTab === "accounts") {
setIsSubUsersLoading(true)
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}/company/subusers`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
companyId: parseInt(id)
})
})
const data = await response.json()
if (data.code === 200) {
setSubUsers(data.data)
} else {
toast.error(data.msg || "获取子账号列表失败")
}
} catch (error) {
toast.error("网络错误,请稍后重试")
} finally {
setIsSubUsersLoading(false)
}
}
}
fetchSubUsers()
}, [activeTab, id])
if (isLoading) {
return <div className="flex items-center justify-center min-h-screen">...</div>
}
@@ -151,24 +238,38 @@ export default function ProjectDetailPage({ params }: { params: { id: string } }
<CardDescription></CardDescription>
</CardHeader>
<CardContent>
<Table>
<TableHeader>
<TableRow>
<TableHead></TableHead>
<TableHead className="text-right"></TableHead>
</TableRow>
</TableHeader>
<TableBody>
{/* Assuming devices are fetched from the profile */}
{/* Replace with actual devices data */}
{/* {profile.devices.map((device) => (
<TableRow key={device.id}>
<TableCell className="font-medium">{device.name}</TableCell>
<TableCell className="text-right">{device.wechatFriends}</TableCell>
{isDevicesLoading ? (
<div className="flex items-center justify-center py-8">...</div>
) : devices.length === 0 ? (
<div className="flex items-center justify-center py-8 text-muted-foreground"></div>
) : (
<Table>
<TableHeader>
<TableRow>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead>IMEI</TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead className="text-right"></TableHead>
</TableRow>
))} */}
</TableBody>
</Table>
</TableHeader>
<TableBody>
{devices.map((device) => (
<TableRow key={device.id}>
<TableCell className="font-medium">{device.memo}</TableCell>
<TableCell>{device.model}</TableCell>
<TableCell>{device.brand}</TableCell>
<TableCell>{device.imei}</TableCell>
<TableCell>{device.alive === 1 ? "在线" : "离线"}</TableCell>
<TableCell>{device.wAlive === 1 ? "在线" : device.wAlive === 0 ? "离线" : "未登录微信"}</TableCell>
<TableCell className="text-right">{device.friendCount || 0}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
)}
</CardContent>
</Card>
</TabsContent>
@@ -180,24 +281,48 @@ export default function ProjectDetailPage({ params }: { params: { id: string } }
<CardDescription></CardDescription>
</CardHeader>
<CardContent>
<Table>
<TableHeader>
<TableRow>
<TableHead></TableHead>
<TableHead className="text-right"></TableHead>
</TableRow>
</TableHeader>
<TableBody>
{/* Assuming subAccounts are fetched from the profile */}
{/* Replace with actual subAccounts data */}
{/* {profile.subAccounts.map((account) => (
<TableRow key={account.id}>
<TableCell className="font-medium">{account.username}</TableCell>
<TableCell className="text-right">{account.createdAt}</TableCell>
{isSubUsersLoading ? (
<div className="flex items-center justify-center py-8">...</div>
) : subUsers.length === 0 ? (
<div className="flex items-center justify-center py-8 text-muted-foreground"></div>
) : (
<Table>
<TableHeader>
<TableRow>
<TableHead></TableHead>
<TableHead>ID</TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead className="text-right"></TableHead>
</TableRow>
))} */}
</TableBody>
</Table>
</TableHeader>
<TableBody>
{subUsers.map((user) => (
<TableRow key={user.id}>
<TableCell>
<Image
src={user.avatar}
alt={user.username}
width={32}
height={32}
className="rounded-full"
/>
</TableCell>
<TableCell>{user.id}</TableCell>
<TableCell>{user.account}</TableCell>
<TableCell>{user.username}</TableCell>
<TableCell>{user.phone}</TableCell>
<TableCell>{user.status === 1 ? "登录" : "禁用"}</TableCell>
<TableCell>{user.typeId === 1 ? "操盘手" : "门店顾问"}</TableCell>
<TableCell className="text-right">{user.createTime}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
)}
</CardContent>
</Card>
</TabsContent>