diff --git a/Server/application/superadmin/config/route.php b/Server/application/superadmin/config/route.php index 977fb2a1..24741243 100644 --- a/Server/application/superadmin/config/route.php +++ b/Server/application/superadmin/config/route.php @@ -34,5 +34,7 @@ Route::group('', function () { // 公司路由 Route::group('company', function () { Route::post('create', 'app\\superadmin\\controller\\CompanyController@create'); + Route::get('list', 'app\\superadmin\\controller\\CompanyController@getList'); + Route::get('detail/:id', 'app\\superadmin\\controller\\CompanyController@getDetail'); }); })->middleware(['app\\superadmin\\middleware\\AdminAuth']); \ No newline at end of file diff --git a/Server/application/superadmin/controller/CompanyController.php b/Server/application/superadmin/controller/CompanyController.php index 485094a4..d979e86c 100644 --- a/Server/application/superadmin/controller/CompanyController.php +++ b/Server/application/superadmin/controller/CompanyController.php @@ -102,11 +102,226 @@ class CompanyController extends Controller } /** - * 检查登录状态 - * @return bool + * 获取项目列表 + * @return \think\response\Json */ - protected function checkLogin() + public function getList() { - return Session::has('admin_id'); + // 获取分页参数 + $page = $this->request->param('page/d', 1); + $limit = $this->request->param('limit/d', 10); + $keyword = $this->request->param('keyword/s', ''); + + // 构建查询条件 + $where = []; + if (!empty($keyword)) { + $where[] = ['name', 'like', "%{$keyword}%"]; + } + + // 查询项目数据 + $total = companyModel::where($where)->count(); + $list = companyModel::where($where) + ->field('id, name, status, tenantId, companyId, memo, createTime') + ->order('id', 'desc') + ->page($page, $limit) + ->select(); + + // 获取每个项目的子账号数量 + $data = []; + foreach ($list as $item) { + // 查询该项目下的子账号数量 + $userCount = Users::where('companyId', $item['companyId']) + ->where('deleteTime', 0) + ->count(); + + $data[] = [ + 'id' => $item['id'], + 'name' => $item['name'], + 'status' => $item['status'], + 'tenantId' => $item['tenantId'], + 'companyId' => $item['companyId'], + 'memo' => $item['memo'], + 'userCount' => $userCount, + 'createTime' => date('Y-m-d H:i:s', $item['createTime']) + ]; + } + + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => [ + 'list' => $data, + 'total' => $total, + 'page' => $page, + 'limit' => $limit + ] + ]); + } + + /** + * 获取项目详情 + * @param int $id 项目ID + * @return \think\response\Json + */ + public function getDetail($id) + { + $company = companyModel::get($id); + if (!$company) { + return json(['code' => 404, 'msg' => '项目不存在']); + } + + // 获取项目下的子账号数量 + $userCount = Users::where('companyId', $id) + ->where('deleteTime', 0) + ->count(); + + $data = [ + 'id' => $company->id, + 'name' => $company->name, + 'status' => $company->status, + 'tenantId' => $company->tenantId, + 'companyId' => $company->companyId, + 'memo' => $company->memo, + 'userCount' => $userCount, + 'createTime' => date('Y-m-d H:i:s', $company->createTime) + ]; + + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => $data + ]); + } + + /** + * 更新项目信息 + * @return \think\response\Json + */ + public function update() + { + if (!$this->request->isPost()) { + return json(['code' => 405, 'msg' => '请求方法不允许']); + } + + // 获取请求参数 + $id = $this->request->post('id/d', 0); + $name = $this->request->post('name/s', ''); + $status = $this->request->post('status/d'); + $tenantId = $this->request->post('tenantId/d'); + $companyId = $this->request->post('companyId/d'); + $memo = $this->request->post('memo/s', ''); + + // 参数验证 + if (empty($id) || empty($name)) { + return json(['code' => 400, 'msg' => '请填写必要参数']); + } + + // 查询项目 + $company = companyModel::get($id); + if (!$company) { + return json(['code' => 404, 'msg' => '项目不存在']); + } + + // 检查项目名称是否已存在(排除自身) + $exists = companyModel::where('name', $name) + ->where('id', '<>', $id) + ->find(); + if ($exists) { + return json(['code' => 400, 'msg' => '项目名称已存在']); + } + + // 更新数据 + $company->name = $name; + if (isset($status)) $company->status = $status; + if (isset($tenantId)) $company->tenantId = $tenantId; + if (isset($companyId)) $company->companyId = $companyId; + $company->memo = $memo; + $company->updateTime = time(); + + if ($company->save()) { + return json([ + 'code' => 200, + 'msg' => '更新成功' + ]); + } + + return json(['code' => 500, 'msg' => '更新失败']); + } + + /** + * 删除项目 + * @return \think\response\Json + */ + public function delete() + { + if (!$this->request->isPost()) { + return json(['code' => 405, 'msg' => '请求方法不允许']); + } + + $id = $this->request->post('id/d', 0); + if (empty($id)) { + return json(['code' => 400, 'msg' => '请指定要删除的项目']); + } + + // 查询项目 + $company = companyModel::get($id); + if (!$company) { + return json(['code' => 404, 'msg' => '项目不存在']); + } + + // 检查是否有关联的子账号 + $userCount = Users::where('companyId', $id) + ->where('deleteTime', 0) + ->count(); + if ($userCount > 0) { + return json(['code' => 400, 'msg' => '该项目下还有关联的子账号,无法删除']); + } + + // 执行删除 + if ($company->delete()) { + return json([ + 'code' => 200, + 'msg' => '删除成功' + ]); + } + + return json(['code' => 500, 'msg' => '删除失败']); + } + + /** + * 更新项目状态 + * @return \think\response\Json + */ + public function updateStatus() + { + if (!$this->request->isPost()) { + return json(['code' => 405, 'msg' => '请求方法不允许']); + } + + $id = $this->request->post('id/d', 0); + $status = $this->request->post('status/d'); + + if (empty($id) || !isset($status)) { + return json(['code' => 400, 'msg' => '参数不完整']); + } + + // 查询项目 + $company = companyModel::get($id); + if (!$company) { + return json(['code' => 404, 'msg' => '项目不存在']); + } + + // 更新状态 + $company->status = $status; + $company->updateTime = time(); + + if ($company->save()) { + return json([ + 'code' => 200, + 'msg' => '状态更新成功' + ]); + } + + return json(['code' => 500, 'msg' => '状态更新失败']); } } \ No newline at end of file diff --git a/Server/application/superadmin/model/Company.php b/Server/application/superadmin/model/Company.php index b37b408b..c31a58ef 100644 --- a/Server/application/superadmin/model/Company.php +++ b/Server/application/superadmin/model/Company.php @@ -4,7 +4,7 @@ namespace app\superadmin\model; use think\Model; /** - * 公司(项目)模型 + * 项目模型 */ class Company extends Model { diff --git a/SuperAdmin/app/dashboard/projects/page.tsx b/SuperAdmin/app/dashboard/projects/page.tsx index c487c3d5..cd179a62 100644 --- a/SuperAdmin/app/dashboard/projects/page.tsx +++ b/SuperAdmin/app/dashboard/projects/page.tsx @@ -1,63 +1,63 @@ "use client" -import { useState } from "react" +import { useState, useEffect } from "react" import Link from "next/link" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu" -import { Plus, Search, MoreHorizontal, Edit, Eye, Trash } from "lucide-react" +import { Plus, Search, MoreHorizontal, Edit, Eye, Trash, ChevronLeft, ChevronRight } from "lucide-react" +import { toast } from "sonner" -// Sample project data -const projectsData = [ - { - id: "1", - name: "电商平台项目", - phone: "13800138000", - accountCount: 5, - deviceCount: 12, - wechatFriends: 245, - }, - { - id: "2", - name: "社交媒体营销", - phone: "13900139000", - accountCount: 8, - deviceCount: 20, - wechatFriends: 567, - }, - { - id: "3", - name: "企业官网推广", - phone: "13700137000", - accountCount: 3, - deviceCount: 8, - wechatFriends: 120, - }, - { - id: "4", - name: "教育平台项目", - phone: "13600136000", - accountCount: 10, - deviceCount: 25, - wechatFriends: 780, - }, - { - id: "5", - name: "金融服务推广", - phone: "13500135000", - accountCount: 6, - deviceCount: 15, - wechatFriends: 320, - }, -] +interface Project { + id: number + name: string + status: number + tenantId: number + companyId: number + memo: string | null + userCount: number + createTime: string +} export default function ProjectsPage() { const [searchTerm, setSearchTerm] = useState("") + const [projects, setProjects] = useState([]) + const [isLoading, setIsLoading] = useState(true) + const [currentPage, setCurrentPage] = useState(1) + const [totalPages, setTotalPages] = useState(1) + const [pageSize] = useState(10) - const filteredProjects = projectsData.filter( - (project) => project.name.toLowerCase().includes(searchTerm.toLowerCase()) || project.phone.includes(searchTerm), - ) + // 获取项目列表 + useEffect(() => { + const fetchProjects = async () => { + setIsLoading(true) + try { + const response = await fetch(`http://yishi.com/company/list?page=${currentPage}&limit=${pageSize}`) + const data = await response.json() + + if (data.code === 200) { + setProjects(data.data.list) + setTotalPages(Math.ceil(data.data.total / pageSize)) + } else { + toast.error(data.msg || "获取项目列表失败") + } + } catch (error) { + toast.error("获取项目列表失败") + } finally { + setIsLoading(false) + } + } + + fetchProjects() + }, [currentPage, pageSize]) + + // 切换页码 + const handlePageChange = (page: number) => { + if (page >= 1 && page <= totalPages) { + setCurrentPage(page) + } + } return (
@@ -75,7 +75,7 @@ export default function ProjectsPage() { setSearchTerm(e.target.value)} @@ -88,22 +88,26 @@ export default function ProjectsPage() { 项目名称 - 手机号 - 关联设备数 - 子账号数 - 微信好友总数 + 状态 + 用户数量 + 创建时间 操作 - {filteredProjects.length > 0 ? ( - filteredProjects.map((project) => ( + {isLoading ? ( + + + 加载中... + + + ) : projects.length > 0 ? ( + projects.map((project) => ( {project.name} - {project.phone} - {project.deviceCount} - {project.accountCount} - {project.wechatFriends} + {project.status === 1 ? '启用' : '禁用'} + {project.userCount} + {project.createTime} @@ -133,7 +137,7 @@ export default function ProjectsPage() { )) ) : ( - + 未找到项目 @@ -141,6 +145,40 @@ export default function ProjectsPage() {
+ + {/* 分页控件 */} + {!isLoading && projects.length > 0 && ( +
+ + + {Array.from({ length: totalPages }, (_, i) => i + 1).map((page) => ( + + ))} + + +
+ )} ) }