From 0cd689e6ae70b7f34dbdf83eae630200e74e5b5c Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Fri, 11 Apr 2025 16:15:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=8B=E5=8F=8B=E5=9C=88=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cunkebao/app/components/LayoutWrapper.tsx | 47 +- .../workspace/auto-like/[id]/edit/page.tsx | 235 ++++++++++ Cunkebao/app/workspace/auto-like/page.tsx | 6 +- .../workspace/moments-sync/[id]/edit/page.tsx | 147 +++++- .../app/workspace/moments-sync/[id]/page.tsx | 423 +++++++++++++++--- .../components/basic-settings.tsx | 25 ++ .../content-library-selection-dialog.tsx | 114 +++++ .../app/workspace/moments-sync/new/page.tsx | 95 +++- Cunkebao/app/workspace/moments-sync/page.tsx | 340 ++++++++++---- Cunkebao/components/ui/badge.tsx | 1 + .../api/controller/WechatFriendController.php | 2 +- .../command/WechatFriendCommand.php | 19 +- Server/application/cunkebao/config/route.php | 2 + .../controller/ContentLibraryController.php | 298 ++++++++++++ .../controller/WorkbenchController.php | 155 ++++++- .../cunkebao/model/ContentItem.php | 52 +++ .../cunkebao/model/ContentLibrary.php | 38 ++ .../cunkebao/model/WorkbenchMomentsSync.php | 36 ++ .../cunkebao/validate/Workbench.php | 14 +- Server/application/job/WechatFriendJob.php | 20 +- 20 files changed, 1862 insertions(+), 207 deletions(-) create mode 100644 Cunkebao/app/workspace/auto-like/[id]/edit/page.tsx create mode 100644 Cunkebao/app/workspace/moments-sync/components/content-library-selection-dialog.tsx create mode 100644 Server/application/cunkebao/controller/ContentLibraryController.php create mode 100644 Server/application/cunkebao/model/ContentItem.php create mode 100644 Server/application/cunkebao/model/ContentLibrary.php diff --git a/Cunkebao/app/components/LayoutWrapper.tsx b/Cunkebao/app/components/LayoutWrapper.tsx index 6e12898a..6e2e4353 100644 --- a/Cunkebao/app/components/LayoutWrapper.tsx +++ b/Cunkebao/app/components/LayoutWrapper.tsx @@ -4,22 +4,55 @@ import { usePathname } from "next/navigation" import BottomNav from "./BottomNav" import { VideoTutorialButton } from "@/components/VideoTutorialButton" import type React from "react" +import { createContext, useContext, useState, useEffect } from "react" + +// 创建视图模式上下文 +const ViewModeContext = createContext<{ viewMode: "desktop" | "mobile" }>({ viewMode: "desktop" }) + +// 创建视图模式钩子函数 +export function useViewMode() { + const context = useContext(ViewModeContext) + if (!context) { + throw new Error("useViewMode must be used within a LayoutWrapper") + } + return context +} export default function LayoutWrapper({ children }: { children: React.ReactNode }) { const pathname = usePathname() + const [viewMode, setViewMode] = useState<"desktop" | "mobile">("desktop") + + // 检测视图模式 + useEffect(() => { + const checkViewMode = () => { + setViewMode(window.innerWidth < 768 ? "mobile" : "desktop") + } + + // 初始检测 + checkViewMode() + + // 监听窗口大小变化 + window.addEventListener("resize", checkViewMode) + + return () => { + window.removeEventListener("resize", checkViewMode) + } + }, []) // 只在四个主页显示底部导航栏:首页、场景获客、工作台和我的 const mainPages = ["/", "/scenarios", "/workspace", "/profile"] const showBottomNav = mainPages.includes(pathname) return ( -
-
- {children} - {showBottomNav && } - {showBottomNav && } -
-
+ +
+
+ {children} + {showBottomNav && } + {showBottomNav && } +
+
+
) } diff --git a/Cunkebao/app/workspace/auto-like/[id]/edit/page.tsx b/Cunkebao/app/workspace/auto-like/[id]/edit/page.tsx new file mode 100644 index 00000000..7de7f9e7 --- /dev/null +++ b/Cunkebao/app/workspace/auto-like/[id]/edit/page.tsx @@ -0,0 +1,235 @@ +"use client" + +import { useState, useEffect } from "react" +import { useRouter } from "next/navigation" +import { ChevronLeft, Search } from "lucide-react" +import { Button } from "@/components/ui/button" +import { Input } from "@/components/ui/input" +import { StepIndicator } from "../../components/step-indicator" +import { BasicSettings } from "../../components/basic-settings" +import { DeviceSelectionDialog } from "../../components/device-selection-dialog" +import { TagSelector } from "../../components/tag-selector" +import { api, ApiResponse } from "@/lib/api" +import { showToast } from "@/lib/toast" + +interface TaskConfig { + id: number + workbenchId: number + interval: number + maxLikes: number + startTime: string + endTime: string + contentTypes: string[] + devices: number[] + targetGroups: string[] + tagOperator: number + createTime: string + updateTime: string +} + +interface Task { + id: number + name: string + type: number + status: number + autoStart: number + createTime: string + updateTime: string + config: TaskConfig +} + +export default function EditAutoLikePage({ params }: { params: { id: string } }) { + const router = useRouter() + const [currentStep, setCurrentStep] = useState(1) + const [deviceDialogOpen, setDeviceDialogOpen] = useState(false) + const [loading, setLoading] = useState(true) + const [formData, setFormData] = useState({ + taskName: "", + likeInterval: 5, + maxLikesPerDay: 200, + timeRange: { start: "08:00", end: "22:00" }, + contentTypes: ["text", "image", "video"], + enabled: true, + selectedDevices: [] as number[], + selectedTags: [] as string[], + tagOperator: "and" as "and" | "or", + }) + + useEffect(() => { + fetchTaskDetail() + }, []) + + const fetchTaskDetail = async () => { + const loadingToast = showToast("正在加载任务信息...", "loading", true); + try { + const response = await api.get<{code: number, msg: string, data: Task}>(`/v1/workbench/detail?id=${params.id}`) + + if (response.code === 200 && response.data) { + const task = response.data + setFormData({ + taskName: task.name, + likeInterval: task.config.interval, + maxLikesPerDay: task.config.maxLikes, + timeRange: { + start: task.config.startTime, + end: task.config.endTime + }, + contentTypes: task.config.contentTypes, + enabled: task.status === 1, + selectedDevices: task.config.devices, + selectedTags: task.config.targetGroups, + tagOperator: task.config.tagOperator === 1 ? "and" : "or" + }) + } else { + showToast(response.msg || "获取任务信息失败", "error") + router.back() + } + } catch (error: any) { + console.error("获取任务详情失败:", error) + showToast(error?.message || "请检查网络连接", "error") + router.back() + } finally { + loadingToast.remove() + setLoading(false) + } + } + + const handleUpdateFormData = (data: Partial) => { + setFormData((prev) => ({ ...prev, ...data })) + } + + const handleNext = () => { + setCurrentStep((prev) => Math.min(prev + 1, 3)) + } + + const handlePrev = () => { + setCurrentStep((prev) => Math.max(prev - 1, 1)) + } + + const handleComplete = async () => { + const loadingToast = showToast("正在更新任务...", "loading", true); + try { + const response = await api.post('/v1/workbench/update', { + id: params.id, + type: 1, + name: formData.taskName, + interval: formData.likeInterval, + maxLikes: formData.maxLikesPerDay, + startTime: formData.timeRange.start, + endTime: formData.timeRange.end, + contentTypes: formData.contentTypes, + enabled: formData.enabled, + devices: formData.selectedDevices, + targetGroups: formData.selectedTags, + tagOperator: formData.tagOperator === 'and' ? 1 : 2 + }); + + if (response.code === 200) { + loadingToast.remove(); + showToast(response.msg || "更新成功", "success"); + router.push("/workspace/auto-like"); + } else { + loadingToast.remove(); + showToast(response.msg || "请稍后再试", "error"); + } + } catch (error: any) { + console.error("更新自动点赞任务失败:", error); + loadingToast.remove(); + showToast(error?.message || "请检查网络连接或稍后再试", "error"); + } + }; + + if (loading) { + return null; + } + + return ( +
+
+
+ +

编辑自动点赞

+
+
+ +
+ + +
+ {currentStep === 1 && ( + + )} + + {currentStep === 2 && ( +
+
+ + setDeviceDialogOpen(true)} + readOnly + value={formData.selectedDevices.length > 0 ? `已选择 ${formData.selectedDevices.length} 个设备` : ""} + /> +
+ + {formData.selectedDevices.length > 0 && ( +
已选设备:{formData.selectedDevices.length} 个
+ )} + +
+ + +
+ + { + handleUpdateFormData({ selectedDevices: devices }) + setDeviceDialogOpen(false) + }} + /> +
+ )} + + {currentStep === 3 && ( +
+ handleUpdateFormData({ selectedTags: tags })} + onOperatorChange={(operator) => handleUpdateFormData({ tagOperator: operator })} + onBack={handlePrev} + onComplete={handleComplete} + /> + +
+ + +
+
+ )} +
+
+
+ ) +} \ No newline at end of file diff --git a/Cunkebao/app/workspace/auto-like/page.tsx b/Cunkebao/app/workspace/auto-like/page.tsx index 64be369c..c92f8ea6 100644 --- a/Cunkebao/app/workspace/auto-like/page.tsx +++ b/Cunkebao/app/workspace/auto-like/page.tsx @@ -294,7 +294,11 @@ export default function AutoLikePage() {
执行设备:{task.config.devices.length} 个
-
目标人群:{task.config.targetGroups.join(', ')}
+
目标人群:{ + task.config.targetGroups.length > 2 + ? `${task.config.targetGroups[0]} 等${task.config.targetGroups.length - 1}个标签` + : task.config.targetGroups.join(', ') + }
点赞间隔:{task.config.interval} 秒
diff --git a/Cunkebao/app/workspace/moments-sync/[id]/edit/page.tsx b/Cunkebao/app/workspace/moments-sync/[id]/edit/page.tsx index 5ce87098..e206c499 100644 --- a/Cunkebao/app/workspace/moments-sync/[id]/edit/page.tsx +++ b/Cunkebao/app/workspace/moments-sync/[id]/edit/page.tsx @@ -1,33 +1,90 @@ "use client" -import { useState } from "react" +import { useState, useEffect } from "react" import { useRouter } from "next/navigation" import { ChevronLeft, Search } from "lucide-react" import { Button } from "@/components/ui/button" import { StepIndicator } from "../../components/step-indicator" import { BasicSettings } from "../../components/basic-settings" import { DeviceSelectionDialog } from "../../components/device-selection-dialog" +import { ContentLibrarySelectionDialog } from "../../components/content-library-selection-dialog" import { Input } from "@/components/ui/input" +import { api, ApiResponse } from "@/lib/api" +import { showToast } from "@/lib/toast" -export default function EditMomentsSyncPage() { +// 定义基本设置表单数据类型,与BasicSettings组件的formData类型匹配 +interface BasicSettingsFormData { + taskName: string + startTime: string + endTime: string + syncCount: number + syncInterval: number + accountType: "business" | "personal" + enabled: boolean +} + +export default function EditMomentsSyncPage({ params }: { params: { id: string } }) { const router = useRouter() const [currentStep, setCurrentStep] = useState(1) const [deviceDialogOpen, setDeviceDialogOpen] = useState(false) + const [libraryDialogOpen, setLibraryDialogOpen] = useState(false) + const [isLoading, setIsLoading] = useState(true) const [formData, setFormData] = useState({ - taskName: "同步卡若主号", + taskName: "", startTime: "06:00", endTime: "23:59", syncCount: 5, - accountType: "business" as const, + syncInterval: 30, // 同步间隔,默认30分钟 + accountType: "business" as "business" | "personal", enabled: true, selectedDevices: [] as string[], selectedLibraries: [] as string[], }) + // 获取任务详情 + useEffect(() => { + const fetchTaskDetail = async () => { + setIsLoading(true) + try { + const response = await api.get(`/v1/workbench/detail?id=${params.id}`) + if (response.code === 200 && response.data) { + const taskData = response.data + setFormData({ + taskName: taskData.name || "", + startTime: taskData.startTime || "06:00", + endTime: taskData.endTime || "23:59", + syncCount: taskData.syncCount || 5, + syncInterval: taskData.syncInterval || 30, + accountType: taskData.syncType === 1 ? "business" : "personal", + enabled: !!taskData.enabled, + selectedDevices: taskData.devices || [], + selectedLibraries: taskData.contentLibraries || [], + }) + } else { + showToast(response.msg || "获取任务详情失败", "error") + router.back() + } + } catch (error: any) { + console.error("获取任务详情失败:", error) + showToast(error?.message || "获取任务详情失败", "error") + router.back() + } finally { + setIsLoading(false) + } + } + + fetchTaskDetail() + }, [params.id, router]) + const handleUpdateFormData = (data: Partial) => { setFormData((prev) => ({ ...prev, ...data })) } + // 专门用于基本设置的更新函数 + const handleBasicSettingsUpdate = (data: Partial) => { + setFormData((prev) => ({ ...prev, ...data })) + } + const handleNext = () => { setCurrentStep((prev) => Math.min(prev + 1, 3)) } @@ -36,9 +93,44 @@ export default function EditMomentsSyncPage() { setCurrentStep((prev) => Math.max(prev - 1, 1)) } - const handleComplete = () => { - console.log("Form submitted:", formData) - router.push("/workspace/moments-sync") + const handleComplete = async () => { + try { + const response = await api.post('/v1/workbench/update', { + id: params.id, + type: 2, // 朋友圈同步任务类型为2 + name: formData.taskName, + syncInterval: formData.syncInterval, + syncCount: formData.syncCount, + syncType: formData.accountType === "business" ? 1 : 2, // 业务号为1,人设号为2 + startTime: formData.startTime, + endTime: formData.endTime, + accountType: formData.accountType === "business" ? 1 : 2, + status: formData.enabled ? 1 : 0, // 状态:0=禁用,1=启用 + devices: formData.selectedDevices, + contentLibraries: formData.selectedLibraries + }); + + if (response.code === 200) { + showToast(response.msg || "更新成功", "success"); + router.push("/workspace/moments-sync"); + } else { + showToast(response.msg || "请稍后再试", "error"); + } + } catch (error: any) { + console.error("更新朋友圈同步任务失败:", error); + showToast(error?.message || "请检查网络连接或稍后再试", "error"); + } + }; + + if (isLoading) { + return ( +
+
+
+

加载中...

+
+
+ ) } return ( @@ -57,7 +149,19 @@ export default function EditMomentsSyncPage() {
{currentStep === 1 && ( - + )} {currentStep === 2 && ( @@ -69,6 +173,7 @@ export default function EditMomentsSyncPage() { className="h-12 pl-11 rounded-xl border-gray-200 text-base" onClick={() => setDeviceDialogOpen(true)} readOnly + value={formData.selectedDevices.length > 0 ? `已选择 ${formData.selectedDevices.length} 个设备` : ""} />
@@ -83,6 +188,7 @@ export default function EditMomentsSyncPage() { @@ -104,9 +210,19 @@ export default function EditMomentsSyncPage() {
- + setLibraryDialogOpen(true)} + readOnly + value={formData.selectedLibraries.length > 0 ? `已选择 ${formData.selectedLibraries.length} 个内容库` : ""} + />
+ {formData.selectedLibraries.length > 0 && ( +
已选内容库:{formData.selectedLibraries.length} 个
+ )} +
+ + { + handleUpdateFormData({ selectedLibraries: libraries }) + setLibraryDialogOpen(false) + }} + />
)}
diff --git a/Cunkebao/app/workspace/moments-sync/[id]/page.tsx b/Cunkebao/app/workspace/moments-sync/[id]/page.tsx index a3868672..685c2fb2 100644 --- a/Cunkebao/app/workspace/moments-sync/[id]/page.tsx +++ b/Cunkebao/app/workspace/moments-sync/[id]/page.tsx @@ -2,52 +2,216 @@ import { useState, useEffect } from "react" import { useRouter } from "next/navigation" -import { ChevronLeft } from "lucide-react" +import { ChevronLeft, MoreVertical, Clock, Edit, Trash2, Copy, RefreshCw, FileText, MessageSquare, History } from "lucide-react" import { Card } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Badge } from "@/components/ui/badge" +import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" +import { Avatar } from "@/components/ui/avatar" import { Switch } from "@/components/ui/switch" +import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu" +import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog" +import { api, ApiResponse } from "@/lib/api" +import { showToast } from "@/lib/toast" -interface SyncTask { +// 定义任务详情的接口 +interface TaskDetail { id: string name: string status: "running" | "paused" - deviceCount: number - contentLib: string + syncType: number + accountType: number syncCount: number + syncInterval: number + startTime: string + endTime: string + enabled: boolean + devices: { + id: string + name: string + avatar: string + }[] + contentLibraries: { + id: string + name: string + count: number + }[] lastSyncTime: string createTime: string creator: string } -export default function ViewMomentsSyncTask({ params }: { params: { id: string } }) { +// 定义同步历史的接口 +interface SyncHistory { + id: string + syncTime: string + content: string + contentType: "text" | "image" | "video" + status: "success" | "failed" + errorMessage?: string +} + +export default function MomentsSyncDetailPage({ params }: { params: { id: string } }) { const router = useRouter() - const [task, setTask] = useState(null) - + const [taskDetail, setTaskDetail] = useState(null) + const [syncHistory, setSyncHistory] = useState([]) + const [isLoading, setIsLoading] = useState(true) + const [activeTab, setActiveTab] = useState("overview") + const [showDeleteAlert, setShowDeleteAlert] = useState(false) + + // 获取任务详情 useEffect(() => { - // Fetch task data from API - // For now, we'll use mock data - setTask({ - id: params.id, - name: "同步卡若主号", - deviceCount: 2, - contentLib: "卡若朋友圈", - syncCount: 307, - lastSyncTime: "2025-02-06 13:12:35", - createTime: "2024-11-20 19:04:14", - creator: "karuo", - status: "running", - }) - }, [params.id]) + const fetchTaskDetail = async () => { + setIsLoading(true) + try { + const response = await api.get(`/v1/workbench/detail?id=${params.id}`) + if (response.code === 200 && response.data) { + setTaskDetail(response.data) + + // 获取同步历史 + if (activeTab === "history") { + fetchSyncHistory() + } + } else { + showToast(response.msg || "获取任务详情失败", "error") + router.push("/workspace/moments-sync") + } + } catch (error: any) { + console.error("获取任务详情失败:", error) + showToast(error?.message || "获取任务详情失败", "error") + router.push("/workspace/moments-sync") + } finally { + setIsLoading(false) + } + } - const toggleTaskStatus = () => { - if (task) { - setTask({ ...task, status: task.status === "running" ? "paused" : "running" }) + fetchTaskDetail() + }, [params.id, router]) + + // 获取同步历史 + const fetchSyncHistory = async () => { + try { + const response = await api.get(`/v1/workbench/sync/history?id=${params.id}`) + if (response.code === 200 && response.data) { + setSyncHistory(response.data.list || []) + } else { + setSyncHistory([]) + } + } catch (error) { + console.error("获取同步历史失败:", error) + setSyncHistory([]) } } - if (!task) { - return
Loading...
+ // 切换Tab时加载数据 + const handleTabChange = (value: string) => { + setActiveTab(value) + if (value === "history" && syncHistory.length === 0) { + fetchSyncHistory() + } + } + + // 切换任务状态 + const toggleTaskStatus = async () => { + if (!taskDetail) return + + try { + const newStatus = taskDetail.status === "running" ? "paused" : "running" + const response = await api.post('/v1/workbench/update/status', { + id: params.id, + status: newStatus === "running" ? 1 : 0 + }) + + if (response.code === 200) { + setTaskDetail({ + ...taskDetail, + status: newStatus + }) + showToast(`任务已${newStatus === "running" ? "启用" : "暂停"}`, "success") + } else { + showToast(response.msg || "操作失败", "error") + } + } catch (error: any) { + console.error("更新任务状态失败:", error) + showToast(error?.message || "更新任务状态失败", "error") + } + } + + // 编辑任务 + const handleEdit = () => { + router.push(`/workspace/moments-sync/${params.id}/edit`) + } + + // 确认删除 + const confirmDelete = () => { + setShowDeleteAlert(true) + } + + // 执行删除 + const handleDelete = async () => { + try { + const response = await api.post('/v1/workbench/delete', { + id: params.id + }) + + if (response.code === 200) { + showToast("删除成功", "success") + router.push("/workspace/moments-sync") + } else { + showToast(response.msg || "删除失败", "error") + } + } catch (error: any) { + console.error("删除任务失败:", error) + showToast(error?.message || "删除任务失败", "error") + } finally { + setShowDeleteAlert(false) + } + } + + // 复制任务 + const handleCopy = async () => { + try { + const response = await api.post('/v1/workbench/copy', { + id: params.id + }) + + if (response.code === 200) { + showToast("复制成功,正在跳转到新任务", "success") + // 假设后端返回了新任务的ID + if (response.data?.id) { + router.push(`/workspace/moments-sync/${response.data.id}`) + } else { + router.push("/workspace/moments-sync") + } + } else { + showToast(response.msg || "复制失败", "error") + } + } catch (error: any) { + console.error("复制任务失败:", error) + showToast(error?.message || "复制任务失败", "error") + } + } + + if (isLoading) { + return ( +
+
+
+

加载中...

+
+
+ ) + } + + if (!taskDetail) { + return ( +
+
+

任务不存在或已被删除

+ +
+
+ ) } return ( @@ -55,53 +219,192 @@ export default function ViewMomentsSyncTask({ params }: { params: { id: string }
- -

查看朋友圈同步任务

+

任务详情

+
+
+ + + + + + + + + 编辑 + + + + 复制 + + + + 删除 + + +
-
- -
-
-

{task.name}

- - {task.status === "running" ? "进行中" : "已暂停"} - -
- -
- -
-
-

任务详情

-
-

推送设备:{task.deviceCount} 个

-

内容库:{task.contentLib}

-

已同步:{task.syncCount} 条

-

创建人:{task.creator}

+ +
+
+
+

{taskDetail.name}

+ + {taskDetail.status === "running" ? "进行中" : "已暂停"} +
-
-

时间信息

-
-

创建时间:{task.createTime}

-

上次同步:{task.lastSyncTime}

-
+
+
创建时间:{taskDetail.createTime}
+
创建人:{taskDetail.creator}
+
上次同步:{taskDetail.lastSyncTime}
+
已同步:{taskDetail.syncCount} 条
- -
-

同步内容预览

- {/* Add content preview here */} -

暂无内容预览

-
+ + + + 基本信息 + 设备列表 + 同步历史 + + + + +
+
+
账号类型
+
{taskDetail.accountType === 1 ? "业务号" : "人设号"}
+
+
+
同步类型
+
{taskDetail.syncType === 1 ? "循环同步" : "实时更新"}
+
+
+
同步间隔
+
{taskDetail.syncInterval} 分钟
+
+
+
每日同步数量
+
{taskDetail.syncCount} 条
+
+
+
允许发布时间段
+
{taskDetail.startTime} - {taskDetail.endTime}
+
+
+
内容库
+
+ {taskDetail.contentLibraries.map((lib) => ( + + {lib.name} + + ))} +
+
+
+
+
+ + + + {taskDetail.devices.length === 0 ? ( +
暂无关联设备
+ ) : ( +
+ {taskDetail.devices.map((device) => ( +
+ + {device.avatar ? ( + {device.name} + ) : ( +
+ {device.name.charAt(0)} +
+ )} +
+
+
{device.name}
+
ID: {device.id}
+
+
+ ))} +
+ )} +
+
+ + + +
+

同步历史

+ +
+ + {syncHistory.length === 0 ? ( +
暂无同步历史
+ ) : ( +
+ {syncHistory.map((record) => ( +
+
+
+ {record.contentType === "text" && } + {record.contentType === "image" && 图片} + {record.contentType === "video" && 视频} + + {record.status === "success" ? "成功" : "失败"} + +
+
{record.syncTime}
+
+
{record.content}
+ {record.status === "failed" && record.errorMessage && ( +
+ 错误信息: {record.errorMessage} +
+ )} +
+ ))} +
+ )} +
+
+
+ + {/* 删除确认对话框 */} + + + + 确认删除 + + 删除后,此任务将无法恢复。确定要删除吗? + + + + 取消 + + 删除 + + + +
) } diff --git a/Cunkebao/app/workspace/moments-sync/components/basic-settings.tsx b/Cunkebao/app/workspace/moments-sync/components/basic-settings.tsx index 9ef6cf88..b0c32d6a 100644 --- a/Cunkebao/app/workspace/moments-sync/components/basic-settings.tsx +++ b/Cunkebao/app/workspace/moments-sync/components/basic-settings.tsx @@ -13,6 +13,7 @@ interface BasicSettingsProps { startTime: string endTime: string syncCount: number + syncInterval: number accountType: "business" | "personal" enabled: boolean } @@ -85,6 +86,30 @@ export function BasicSettings({ formData, onChange, onNext }: BasicSettingsProps
+
+
同步间隔
+
+ + {formData.syncInterval} + + 分钟 +
+
+
账号类型
diff --git a/Cunkebao/app/workspace/moments-sync/components/content-library-selection-dialog.tsx b/Cunkebao/app/workspace/moments-sync/components/content-library-selection-dialog.tsx new file mode 100644 index 00000000..51d2bf96 --- /dev/null +++ b/Cunkebao/app/workspace/moments-sync/components/content-library-selection-dialog.tsx @@ -0,0 +1,114 @@ +"use client" + +import { useState } from "react" +import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog" +import { Input } from "@/components/ui/input" +import { Button } from "@/components/ui/button" +import { Search, CheckCircle2, Circle } from "lucide-react" +import { ScrollArea } from "@/components/ui/scroll-area" + +interface ContentLibrarySelectionDialogProps { + open: boolean + onOpenChange: (open: boolean) => void + selectedLibraries: string[] + onSelect: (libraries: string[]) => void +} + +export function ContentLibrarySelectionDialog({ + open, + onOpenChange, + selectedLibraries, + onSelect, +}: ContentLibrarySelectionDialogProps) { + const [searchQuery, setSearchQuery] = useState("") + const [libraries] = useState([ + { id: "1", name: "卡若朋友圈", count: 58 }, + { id: "2", name: "暗黑4代练", count: 422 }, + { id: "3", name: "家装设计", count: 107 }, + { id: "4", name: "美食分享", count: 321 }, + { id: "5", name: "旅游攻略", count: 89 }, + ]) + + const [tempSelectedLibraries, setTempSelectedLibraries] = useState(selectedLibraries) + + const toggleLibrary = (libraryId: string) => { + setTempSelectedLibraries((prev) => + prev.includes(libraryId) ? prev.filter((id) => id !== libraryId) : [...prev, libraryId] + ) + } + + const handleConfirm = () => { + onSelect(tempSelectedLibraries) + onOpenChange(false) + } + + const handleCancel = () => { + setTempSelectedLibraries(selectedLibraries) + onOpenChange(false) + } + + const filteredLibraries = libraries.filter((library) => + library.name.toLowerCase().includes(searchQuery.toLowerCase()) + ) + + return ( + + + + 选择内容库 + + +
+
+ + setSearchQuery(e.target.value)} + /> +
+ + +
+ {filteredLibraries.map((library) => ( +
toggleLibrary(library.id)} + > +
+

{library.name}

+

{library.count}条内容

+
+ {tempSelectedLibraries.includes(library.id) ? ( + + ) : ( + + )} +
+ ))} +
+
+
+ +
+ + +
+
+
+ ) +} \ No newline at end of file diff --git a/Cunkebao/app/workspace/moments-sync/new/page.tsx b/Cunkebao/app/workspace/moments-sync/new/page.tsx index a1f71049..5125010f 100644 --- a/Cunkebao/app/workspace/moments-sync/new/page.tsx +++ b/Cunkebao/app/workspace/moments-sync/new/page.tsx @@ -7,19 +7,34 @@ import { Button } from "@/components/ui/button" import { StepIndicator } from "../components/step-indicator" import { BasicSettings } from "../components/basic-settings" import { DeviceSelectionDialog } from "../components/device-selection-dialog" +import { ContentLibrarySelectionDialog } from "../components/content-library-selection-dialog" import { Input } from "@/components/ui/input" -import { toast } from "@/components/ui/use-toast" +import { api, ApiResponse } from "@/lib/api" +import { showToast } from "@/lib/toast" + +// 定义基本设置表单数据类型,与BasicSettings组件的formData类型匹配 +interface BasicSettingsFormData { + taskName: string + startTime: string + endTime: string + syncCount: number + syncInterval: number + accountType: "business" | "personal" + enabled: boolean +} export default function NewMomentsSyncPage() { const router = useRouter() const [currentStep, setCurrentStep] = useState(1) const [deviceDialogOpen, setDeviceDialogOpen] = useState(false) + const [libraryDialogOpen, setLibraryDialogOpen] = useState(false) const [formData, setFormData] = useState({ taskName: "", startTime: "06:00", endTime: "23:59", syncCount: 5, - accountType: "business" as const, + syncInterval: 30, // 同步间隔,默认30分钟 + accountType: "business" as "business" | "personal", enabled: true, selectedDevices: [] as string[], selectedLibraries: [] as string[], @@ -29,6 +44,11 @@ export default function NewMomentsSyncPage() { setFormData((prev) => ({ ...prev, ...data })) } + // 专门用于基本设置的更新函数 + const handleBasicSettingsUpdate = (data: Partial) => { + setFormData((prev) => ({ ...prev, ...data })) + } + const handleNext = () => { setCurrentStep((prev) => Math.min(prev + 1, 3)) } @@ -38,13 +58,31 @@ export default function NewMomentsSyncPage() { } const handleComplete = async () => { - console.log("Form submitted:", formData) - await new Promise((resolve) => setTimeout(resolve, 1000)) - toast({ - title: "创建成���", - description: "朋友圈同步任务已创建并开始执行", - }) - router.push("/workspace/moments-sync") + try { + const response = await api.post('/v1/workbench/create', { + type: 2, // 朋友圈同步任务类型为2 + name: formData.taskName, + syncInterval: formData.syncInterval, + syncCount: formData.syncCount, + syncType: formData.accountType === "business" ? 1 : 2, // 业务号为1,人设号为2 + startTime: formData.startTime, + endTime: formData.endTime, + accountType: formData.accountType === "business" ? 1 : 2, + status: formData.enabled ? 1 : 0, // 状态:0=禁用,1=启用 + devices: formData.selectedDevices, + contentLibraries: formData.selectedLibraries + }); + + if (response.code === 200) { + showToast(response.msg || "创建成功", "success"); + router.push("/workspace/moments-sync"); + } else { + showToast(response.msg || "请稍后再试", "error"); + } + } catch (error: any) { + console.error("创建朋友圈同步任务失败:", error); + showToast(error?.message || "请检查网络连接或稍后再试", "error"); + } } return ( @@ -63,7 +101,19 @@ export default function NewMomentsSyncPage() {
{currentStep === 1 && ( - + )} {currentStep === 2 && ( @@ -75,6 +125,7 @@ export default function NewMomentsSyncPage() { className="h-12 pl-11 rounded-xl border-gray-200 text-base" onClick={() => setDeviceDialogOpen(true)} readOnly + value={formData.selectedDevices.length > 0 ? `已选择 ${formData.selectedDevices.length} 个设备` : ""} />
@@ -89,6 +140,7 @@ export default function NewMomentsSyncPage() { @@ -110,9 +162,19 @@ export default function NewMomentsSyncPage() {
- + setLibraryDialogOpen(true)} + readOnly + value={formData.selectedLibraries.length > 0 ? `已选择 ${formData.selectedLibraries.length} 个内容库` : ""} + />
+ {formData.selectedLibraries.length > 0 && ( +
已选内容库:{formData.selectedLibraries.length} 个
+ )} +
+ + { + handleUpdateFormData({ selectedLibraries: libraries }) + setLibraryDialogOpen(false) + }} + />
)}
diff --git a/Cunkebao/app/workspace/moments-sync/page.tsx b/Cunkebao/app/workspace/moments-sync/page.tsx index 085756df..5a3a0336 100644 --- a/Cunkebao/app/workspace/moments-sync/page.tsx +++ b/Cunkebao/app/workspace/moments-sync/page.tsx @@ -1,7 +1,7 @@ "use client" -import { useState } from "react" -import { ChevronLeft, Plus, Filter, Search, RefreshCw, MoreVertical, Clock, Edit, Trash2, Eye } from "lucide-react" +import { useState, useEffect } from "react" +import { ChevronLeft, Plus, Filter, Search, RefreshCw, MoreVertical, Clock, Edit, Trash2, Eye, Copy } from "lucide-react" import { Card } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" @@ -10,6 +10,18 @@ import { Badge } from "@/components/ui/badge" import { useRouter } from "next/navigation" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu" import { Switch } from "@/components/ui/switch" +import { api, ApiResponse } from "@/lib/api" +import { showToast } from "@/lib/toast" +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle +} from "@/components/ui/alert-dialog" interface SyncTask { id: string @@ -21,55 +33,146 @@ interface SyncTask { lastSyncTime: string createTime: string creator: string + libraries?: string[] } export default function MomentsSyncPage() { const router = useRouter() - const [tasks, setTasks] = useState([ - { - id: "1", - name: "同步卡若主号", - deviceCount: 2, - contentLib: "卡若朋友圈", - syncCount: 307, - lastSyncTime: "2025-02-06 13:12:35", - createTime: "2024-11-20 19:04:14", - creator: "karuo", - status: "running", - }, - { - id: "2", - name: "暗黑4业务", - deviceCount: 1, - contentLib: "暗黑4代练", - syncCount: 622, - lastSyncTime: "2024-03-04 14:09:35", - createTime: "2024-03-04 14:29:04", - creator: "lkdie", - status: "paused", - }, - ]) + const [tasks, setTasks] = useState([]) + const [isLoading, setIsLoading] = useState(true) + const [searchQuery, setSearchQuery] = useState("") + const [showDeleteAlert, setShowDeleteAlert] = useState(false) + const [taskToDelete, setTaskToDelete] = useState(null) - const handleDelete = (taskId: string) => { - setTasks(tasks.filter((task) => task.id !== taskId)) + // 获取任务列表 + const fetchTasks = async () => { + const loadingToast = showToast("正在加载任务列表...", "loading", true); + setIsLoading(true) + try { + const response = await api.get('/v1/workbench/list?type=2') + if (response.code === 200 && response.data) { + setTasks(response.data.list || []) + } else { + showToast(response.msg || "获取任务列表失败", "error") + } + } catch (error: any) { + console.error("获取朋友圈同步任务列表失败:", error) + showToast(error?.message || "请检查网络连接", "error") + } finally { + loadingToast.remove(); + setIsLoading(false) + } } + // 组件加载时获取任务列表 + useEffect(() => { + fetchTasks() + }, []) + + // 搜索任务 + const handleSearch = () => { + fetchTasks() + } + + // 切换任务状态 + const toggleTaskStatus = async (taskId: string, currentStatus: "running" | "paused") => { + const loadingToast = showToast("正在更新任务状态...", "loading", true); + try { + const newStatus = currentStatus === "running" ? "paused" : "running" + const response = await api.post('/v1/workbench/update-status', { + id: taskId, + status: newStatus === "running" ? 1 : 0 + }) + + if (response.code === 200) { + setTasks( + tasks.map((task) => + task.id === taskId ? { ...task, status: newStatus } : task + ) + ) + loadingToast.remove(); + showToast(`任务已${newStatus === "running" ? "启用" : "暂停"}`, "success") + } else { + loadingToast.remove(); + showToast(response.msg || "操作失败", "error") + } + } catch (error: any) { + console.error("更新任务状态失败:", error) + loadingToast.remove(); + showToast(error?.message || "更新任务状态失败", "error") + } + } + + // 确认删除 + const confirmDelete = (taskId: string) => { + setTaskToDelete(taskId) + setShowDeleteAlert(true) + } + + // 执行删除 + const handleDelete = async () => { + if (!taskToDelete) return + + const loadingToast = showToast("正在删除任务...", "loading", true); + try { + const response = await api.delete(`/v1/workbench/delete?id=${taskToDelete}`) + + if (response.code === 200) { + setTasks(tasks.filter((task) => task.id !== taskToDelete)) + loadingToast.remove(); + showToast("删除成功", "success") + } else { + loadingToast.remove(); + showToast(response.msg || "删除失败", "error") + } + } catch (error: any) { + console.error("删除任务失败:", error) + loadingToast.remove(); + showToast(error?.message || "删除任务失败", "error") + } finally { + setTaskToDelete(null) + setShowDeleteAlert(false) + } + } + + // 编辑任务 const handleEdit = (taskId: string) => { router.push(`/workspace/moments-sync/${taskId}/edit`) } + // 查看任务详情 const handleView = (taskId: string) => { router.push(`/workspace/moments-sync/${taskId}`) } - const toggleTaskStatus = (taskId: string) => { - setTasks( - tasks.map((task) => - task.id === taskId ? { ...task, status: task.status === "running" ? "paused" : "running" } : task, - ), - ) + // 复制任务 + const handleCopy = async (taskId: string) => { + const loadingToast = showToast("正在复制任务...", "loading", true); + try { + const response = await api.post('/v1/workbench/copy', { + id: taskId + }) + + if (response.code === 200) { + loadingToast.remove(); + showToast("复制成功", "success") + fetchTasks() // 重新获取列表 + } else { + loadingToast.remove(); + showToast(response.msg || "复制失败", "error") + } + } catch (error: any) { + console.error("复制任务失败:", error) + loadingToast.remove(); + showToast(error?.message || "复制任务失败", "error") + } } + // 过滤任务 + const filteredTasks = tasks.filter( + (task) => task.name.toLowerCase().includes(searchQuery.toLowerCase()) + ) + return (
@@ -94,75 +197,132 @@ export default function MomentsSyncPage() {
- + setSearchQuery(e.target.value)} + onKeyDown={(e) => e.key === 'Enter' && handleSearch()} + />
- -
-
- {tasks.map((task) => ( - -
-
-

{task.name}

- - {task.status === "running" ? "进行中" : "已暂停"} - + {isLoading ? ( +
+
+ +

加载中...

+
+
+ ) : filteredTasks.length === 0 ? ( +
+
+

暂无数据

+ + + +
+
+ ) : ( +
+ {filteredTasks.map((task) => ( + +
+
+

{task.name}

+ + {task.status === "running" ? "进行中" : "已暂停"} + +
+
+ toggleTaskStatus(task.id, task.status)} + /> + + + + + + handleView(task.id)}> + + 查看 + + handleEdit(task.id)}> + + 编辑 + + handleCopy(task.id)}> + + 复制 + + confirmDelete(task.id)}> + + 删除 + + + +
-
- toggleTaskStatus(task.id)} /> - - - - - - handleView(task.id)}> - - 查看 - - handleEdit(task.id)}> - - 编辑 - - handleDelete(task.id)}> - - 删除 - - - -
-
-
-
-
推送设备:{task.deviceCount} 个
-
内容库:{task.contentLib}
+
+
+
推送设备:{task.deviceCount} 个
+
内容库:{task.contentLib}
+
+
+
已同步:{task.syncCount} 条
+
创建人:{task.creator}
+
-
-
已同步:{task.syncCount} 条
-
创建人:{task.creator}
-
-
-
-
- - 上次同步:{task.lastSyncTime} +
+
+ + 上次同步:{task.lastSyncTime} +
+
创建时间:{task.createTime}
-
创建时间:{task.createTime}
-
- - ))} -
+ + ))} +
+ )}
+ + {/* 删除确认对话框 */} + + + + 确认删除 + + 删除后,此任务将无法恢复。确定要删除吗? + + + + 取消 + + 删除 + + + +
) } diff --git a/Cunkebao/components/ui/badge.tsx b/Cunkebao/components/ui/badge.tsx index 087817b4..b1da6227 100644 --- a/Cunkebao/components/ui/badge.tsx +++ b/Cunkebao/components/ui/badge.tsx @@ -12,6 +12,7 @@ const badgeVariants = cva( secondary: "border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80", destructive: "border-transparent bg-destructive text-destructive-foreground shadow hover:bg-destructive/80", outline: "text-foreground", + success: "border-transparent bg-green-500 text-white shadow hover:bg-green-600", }, }, defaultVariants: { diff --git a/Server/application/api/controller/WechatFriendController.php b/Server/application/api/controller/WechatFriendController.php index 65d873fe..b99e5e86 100644 --- a/Server/application/api/controller/WechatFriendController.php +++ b/Server/application/api/controller/WechatFriendController.php @@ -115,7 +115,7 @@ class WechatFriendController extends BaseController 'additionalPicture' => $item['additionalPicture'], 'desc' => $item['desc'], 'country' => $item['country'], - 'province' => isset($item['province']) ? $item['province'] : '', + 'privince' => isset($item['privince']) ? $item['privince'] : '', 'city' => isset($item['city']) ? $item['city'] : '', 'createTime' =>isset($item['createTime']) ? $item['createTime'] : '', 'updateTime' => time() diff --git a/Server/application/command/WechatFriendCommand.php b/Server/application/command/WechatFriendCommand.php index c4b8cbdd..65cfc4a9 100644 --- a/Server/application/command/WechatFriendCommand.php +++ b/Server/application/command/WechatFriendCommand.php @@ -8,6 +8,7 @@ use think\console\Output; use think\facade\Log; use think\Queue; use app\job\WechatFriendJob; +use think\facade\Cache; class WechatFriendCommand extends Command { @@ -22,12 +23,16 @@ class WechatFriendCommand extends Command $output->writeln('开始处理微信列表任务...'); try { - // 初始页码 - $pageIndex = 0; + // 从缓存获取初始页码和上次处理的好友ID,缓存10分钟有效 + $pageIndex = Cache::get('friendsPage', 0); + $preFriendId = Cache::get('preFriendId', ''); + + $output->writeln('从缓存获取页码:' . $pageIndex . ',上次处理的好友ID:' . ($preFriendId ?: '无')); + $pageSize = 1000; // 每页获取1000条记录 - // 将第一页任务添加到队列 - $this->addToQueue($pageIndex, $pageSize); + // 将任务添加到队列 + $this->addToQueue($pageIndex, $pageSize, $preFriendId); $output->writeln('微信列表任务已添加到队列'); } catch (\Exception $e) { @@ -43,12 +48,14 @@ class WechatFriendCommand extends Command * 添加任务到队列 * @param int $pageIndex 页码 * @param int $pageSize 每页大小 + * @param string $preFriendId 上一个好友ID */ - protected function addToQueue($pageIndex, $pageSize) + protected function addToQueue($pageIndex, $pageSize, $preFriendId = '') { $data = [ 'pageIndex' => $pageIndex, - 'pageSize' => $pageSize + 'pageSize' => $pageSize, + 'preFriendId' => $preFriendId ]; // 添加到队列,设置任务名为 wechat_friends diff --git a/Server/application/cunkebao/config/route.php b/Server/application/cunkebao/config/route.php index 32de68e0..8b42cd95 100644 --- a/Server/application/cunkebao/config/route.php +++ b/Server/application/cunkebao/config/route.php @@ -54,5 +54,7 @@ Route::group('v1/', function () { Route::post('update-status', 'app\\cunkebao\\controller\\WorkbenchController@updateStatus'); // 更新工作台状态 Route::delete('delete', 'app\\cunkebao\\controller\\WorkbenchController@delete'); // 删除工作台 Route::post('copy', 'app\\cunkebao\\controller\\WorkbenchController@copy'); // 拷贝工作台 + Route::get('detail', 'app\\cunkebao\\controller\\WorkbenchController@detail'); // 获取工作台详情 + Route::post('update', 'app\\cunkebao\\controller\\WorkbenchController@update'); // 更新工作台 }); })->middleware(['jwt']); \ No newline at end of file diff --git a/Server/application/cunkebao/controller/ContentLibraryController.php b/Server/application/cunkebao/controller/ContentLibraryController.php new file mode 100644 index 00000000..b4676ceb --- /dev/null +++ b/Server/application/cunkebao/controller/ContentLibraryController.php @@ -0,0 +1,298 @@ +request->param('page', 1); + $limit = $this->request->param('limit', 10); + $keyword = $this->request->param('keyword', ''); + + $where = [ + ['userId', '=', $this->request->userInfo['id']] + ]; + + // 添加名称模糊搜索 + if ($keyword !== '') { + $where[] = ['name', 'like', '%' . $keyword . '%']; + } + + $list = ContentLibrary::where($where) + ->field('id,name,description,createTime,updateTime') + ->order('id', 'desc') + ->page($page, $limit) + ->select(); + + $total = ContentLibrary::where($where)->count(); + + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => [ + 'list' => $list, + 'total' => $total, + 'page' => $page, + 'limit' => $limit + ] + ]); + } + + /** + * 获取内容库详情 + * @param int $id 内容库ID + * @return \think\response\Json + */ + public function detail($id) + { + if (empty($id)) { + return json(['code' => 400, 'msg' => '参数错误']); + } + + $library = ContentLibrary::where([ + ['id', '=', $id], + ['userId', '=', $this->request->userInfo['id']] + ]) + ->field('id,name,description,createTime,updateTime') + ->find(); + + if (empty($library)) { + return json(['code' => 404, 'msg' => '内容库不存在']); + } + + // 获取内容项目 + $items = ContentItem::where('libraryId', $id)->select(); + $library['items'] = $items; + + return json(['code' => 200, 'msg' => '获取成功', 'data' => $library]); + } + + /** + * 创建内容库 + * @return \think\response\Json + */ + public function create() + { + if (!$this->request->isPost()) { + return json(['code' => 400, 'msg' => '请求方式错误']); + } + + // 获取请求参数 + $param = $this->request->post(); + + // 简单验证 + if (empty($param['name'])) { + return json(['code' => 400, 'msg' => '内容库名称不能为空']); + } + + Db::startTrans(); + try { + // 创建内容库 + $library = new ContentLibrary; + $library->name = $param['name']; + $library->description = isset($param['description']) ? $param['description'] : ''; + $library->userId = $this->request->userInfo['id']; + $library->companyId = $this->request->userInfo['companyId']; + $library->save(); + + // 如果有内容项目,也一并创建 + if (!empty($param['items']) && is_array($param['items'])) { + foreach ($param['items'] as $item) { + $contentItem = new ContentItem; + $contentItem->libraryId = $library->id; + $contentItem->type = $item['type']; + $contentItem->title = $item['title'] ?? ''; + $contentItem->contentData = $item['contentData']; + $contentItem->save(); + } + } + + Db::commit(); + return json(['code' => 200, 'msg' => '创建成功', 'data' => ['id' => $library->id]]); + } catch (\Exception $e) { + Db::rollback(); + return json(['code' => 500, 'msg' => '创建失败:' . $e->getMessage()]); + } + } + + /** + * 更新内容库 + * @return \think\response\Json + */ + public function update() + { + if (!$this->request->isPost()) { + return json(['code' => 400, 'msg' => '请求方式错误']); + } + + // 获取请求参数 + $param = $this->request->post(); + + // 简单验证 + if (empty($param['id'])) { + return json(['code' => 400, 'msg' => '参数错误']); + } + + if (empty($param['name'])) { + return json(['code' => 400, 'msg' => '内容库名称不能为空']); + } + + // 查询内容库是否存在 + $library = ContentLibrary::where([ + ['id', '=', $param['id']], + ['userId', '=', $this->request->userInfo['id']] + ])->find(); + + if (!$library) { + return json(['code' => 404, 'msg' => '内容库不存在']); + } + + Db::startTrans(); + try { + // 更新内容库基本信息 + $library->name = $param['name']; + $library->description = isset($param['description']) ? $param['description'] : ''; + $library->save(); + + Db::commit(); + return json(['code' => 200, 'msg' => '更新成功']); + } catch (\Exception $e) { + Db::rollback(); + return json(['code' => 500, 'msg' => '更新失败:' . $e->getMessage()]); + } + } + + /** + * 删除内容库 + * @param int $id 内容库ID + * @return \think\response\Json + */ + public function delete($id) + { + if (empty($id)) { + return json(['code' => 400, 'msg' => '参数错误']); + } + + $library = ContentLibrary::where([ + ['id', '=', $id], + ['userId', '=', $this->request->userInfo['id']] + ])->find(); + + if (!$library) { + return json(['code' => 404, 'msg' => '内容库不存在']); + } + + Db::startTrans(); + try { + // 删除相关内容项目 + ContentItem::where('libraryId', $id)->delete(); + + // 删除内容库 + $library->delete(); + + Db::commit(); + return json(['code' => 200, 'msg' => '删除成功']); + } catch (\Exception $e) { + Db::rollback(); + return json(['code' => 500, 'msg' => '删除失败:' . $e->getMessage()]); + } + } + + /** + * 添加内容项目 + * @return \think\response\Json + */ + public function addItem() + { + if (!$this->request->isPost()) { + return json(['code' => 400, 'msg' => '请求方式错误']); + } + + // 获取请求参数 + $param = $this->request->post(); + + // A简单验证 + if (empty($param['libraryId'])) { + return json(['code' => 400, 'msg' => '内容库ID不能为空']); + } + + if (empty($param['type'])) { + return json(['code' => 400, 'msg' => '内容类型不能为空']); + } + + if (empty($param['contentData'])) { + return json(['code' => 400, 'msg' => '内容数据不能为空']); + } + + // 查询内容库是否存在 + $library = ContentLibrary::where([ + ['id', '=', $param['libraryId']], + ['userId', '=', $this->request->userInfo['id']] + ])->find(); + + if (!$library) { + return json(['code' => 404, 'msg' => '内容库不存在']); + } + + try { + // 创建内容项目 + $item = new ContentItem; + $item->libraryId = $param['libraryId']; + $item->type = $param['type']; + $item->title = $param['title'] ?? ''; + $item->contentData = $param['contentData']; + $item->save(); + + return json(['code' => 200, 'msg' => '添加成功', 'data' => ['id' => $item->id]]); + } catch (\Exception $e) { + return json(['code' => 500, 'msg' => '添加失败:' . $e->getMessage()]); + } + } + + /** + * 删除内容项目 + * @param int $id 内容项目ID + * @return \think\response\Json + */ + public function deleteItem($id) + { + if (empty($id)) { + return json(['code' => 400, 'msg' => '参数错误']); + } + + // 查询内容项目是否存在并检查权限 + $item = ContentItem::alias('i') + ->join('content_library l', 'i.libraryId = l.id') + ->where([ + ['i.id', '=', $id], + ['l.userId', '=', $this->request->userInfo['id']] + ]) + ->find(); + + if (!$item) { + return json(['code' => 404, 'msg' => '内容项目不存在或无权限操作']); + } + + try { + // 删除内容项目 + ContentItem::destroy($id); + + return json(['code' => 200, 'msg' => '删除成功']); + } catch (\Exception $e) { + return json(['code' => 500, 'msg' => '删除失败:' . $e->getMessage()]); + } + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/controller/WorkbenchController.php b/Server/application/cunkebao/controller/WorkbenchController.php index 1f1f79d7..f3982e2a 100644 --- a/Server/application/cunkebao/controller/WorkbenchController.php +++ b/Server/application/cunkebao/controller/WorkbenchController.php @@ -84,8 +84,11 @@ class WorkbenchController extends Controller $config->syncInterval = $param['syncInterval']; $config->syncCount = $param['syncCount']; $config->syncType = $param['syncType']; + $config->startTime = $param['startTime']; + $config->endTime = $param['endTime']; + $config->accountType = $param['accountType']; $config->devices = json_encode($param['devices']); - $config->targetGroups = json_encode($param['targetGroups']); + $config->contentLibraries = json_encode($param['contentLibraries'] ?? []); $config->createTime = time(); $config->updateTime = time(); $config->save(); @@ -155,9 +158,9 @@ class WorkbenchController extends Controller 'autoLike' => function($query) { $query->field('workbenchId,interval,maxLikes,startTime,endTime,contentTypes,devices,targetGroups'); }, - // 'momentsSync' => function($query) { - // $query->field('workbenchId,syncInterval,syncCount,syncType,devices,targetGroups'); - // }, + 'momentsSync' => function($query) { + $query->field('workbenchId,syncInterval,syncCount,syncType,startTime,endTime,accountType,devices,contentLibraries'); + }, // 'groupPush' => function($query) { // $query->field('workbenchId,pushInterval,pushContent,pushTime,devices,targetGroups'); // }, @@ -188,8 +191,9 @@ class WorkbenchController extends Controller if (!empty($item->momentsSync)) { $item->config = $item->momentsSync; $item->config->devices = json_decode($item->config->devices, true); - $item->config->targetGroups = json_decode($item->config->targetGroups, true); + $item->config->contentLibraries = json_decode($item->config->contentLibraries, true); } + unset($item->momentsSync,$item->moments_sync); break; case self::TYPE_GROUP_PUSH: if (!empty($item->groupPush)) { @@ -199,6 +203,7 @@ class WorkbenchController extends Controller $item->config->pushContent = json_decode($item->config->pushContent, true); $item->config->pushTime = json_decode($item->config->pushTime, true); } + unset($item->groupPush,$item->group_push); break; case self::TYPE_GROUP_CREATE: if (!empty($item->groupCreate)) { @@ -206,9 +211,9 @@ class WorkbenchController extends Controller $item->config->devices = json_decode($item->config->devices, true); $item->config->targetGroups = json_decode($item->config->targetGroups, true); } + unset($item->groupCreate,$item->group_create); break; } - unset( $item->momentsSync, $item->groupPush, $item->groupCreate); return $item; }); @@ -231,8 +236,10 @@ class WorkbenchController extends Controller * @param int $id 工作台ID * @return \think\response\Json */ - public function detail($id) + public function detail() { + $id = $this->request->param('id', ''); + if (empty($id)) { return json(['code' => 400, 'msg' => '参数错误']); } @@ -243,14 +250,14 @@ class WorkbenchController extends Controller $query->field('workbenchId,interval,maxLikes,startTime,endTime,contentTypes,devices,targetGroups'); }, 'momentsSync' => function($query) { - $query->field('workbenchId,syncInterval,syncCount,syncType,devices,targetGroups'); + $query->field('workbenchId,syncInterval,syncCount,syncType,startTime,endTime,accountType,devices,contentLibraries'); }, - 'groupPush' => function($query) { - $query->field('workbenchId,pushInterval,pushContent,pushTime,devices,targetGroups'); - }, - 'groupCreate' => function($query) { - $query->field('workbenchId,groupNamePrefix,maxGroups,membersPerGroup,devices,targetGroups'); - } + // 'groupPush' => function($query) { + // $query->field('workbenchId,pushInterval,pushContent,pushTime,devices,targetGroups'); + // }, + // 'groupCreate' => function($query) { + // $query->field('workbenchId,groupNamePrefix,maxGroups,membersPerGroup,devices,targetGroups'); + // } ]; $workbench = Workbench::where([ @@ -273,14 +280,15 @@ class WorkbenchController extends Controller $workbench->config = $workbench->autoLike; $workbench->config->devices = json_decode($workbench->config->devices, true); $workbench->config->targetGroups = json_decode($workbench->config->targetGroups, true); - $workbench->config->contentTypes = explode(',', $workbench->config->contentTypes); + $workbench->config->contentTypes = json_decode($workbench->config->contentTypes, true); + unset($workbench->autoLike,$workbench->auto_like); } break; case self::TYPE_MOMENTS_SYNC: if (!empty($workbench->momentsSync)) { $workbench->config = $workbench->momentsSync; $workbench->config->devices = json_decode($workbench->config->devices, true); - $workbench->config->targetGroups = json_decode($workbench->config->targetGroups, true); + $workbench->config->contentLibraries = json_decode($workbench->config->contentLibraries, true); } break; case self::TYPE_GROUP_PUSH: @@ -305,6 +313,112 @@ class WorkbenchController extends Controller return json(['code' => 200, 'msg' => '获取成功', 'data' => $workbench]); } + /** + * 更新工作台 + * @return \think\response\Json + */ + public function update() + { + if (!$this->request->isPost()) { + return json(['code' => 400, 'msg' => '请求方式错误']); + } + + // 获取请求参数 + $param = $this->request->post(); + + // 验证数据 + $validate = new WorkbenchValidate; + if (!$validate->scene('update')->check($param)) { + return json(['code' => 400, 'msg' => $validate->getError()]); + } + + // 查询工作台是否存在 + $workbench = Workbench::where([ + ['id', '=', $param['id']], + ['userId', '=', $this->request->userInfo['id']], + ['isDel', '=', 0] + ])->find(); + + if (!$workbench) { + return json(['code' => 404, 'msg' => '工作台不存在']); + } + + Db::startTrans(); + try { + // 更新工作台基本信息 + $workbench->name = $param['name']; + $workbench->autoStart = !empty($param['autoStart']) ? 1 : 0; + $workbench->updateTime = time(); + $workbench->save(); + + // 根据类型更新对应的配置 + switch ($workbench->type) { + case self::TYPE_AUTO_LIKE: + $config = WorkbenchAutoLike::where('workbenchId', $param['id'])->find(); + if ($config) { + $config->interval = $param['interval']; + $config->maxLikes = $param['maxLikes']; + $config->startTime = $param['startTime']; + $config->endTime = $param['endTime']; + $config->contentTypes = json_encode($param['contentTypes']); + $config->devices = json_encode($param['devices']); + $config->targetGroups = json_encode($param['targetGroups']); + $config->tagOperator = $param['tagOperator']; + $config->updateTime = time(); + $config->save(); + } + break; + + case self::TYPE_MOMENTS_SYNC: + $config = WorkbenchMomentsSync::where('workbenchId', $param['id'])->find(); + if ($config) { + $config->syncInterval = $param['syncInterval']; + $config->syncCount = $param['syncCount']; + $config->syncType = $param['syncType']; + $config->startTime = $param['startTime']; + $config->endTime = $param['endTime']; + $config->accountType = $param['accountType']; + $config->devices = json_encode($param['devices']); + $config->contentLibraries = json_encode($param['contentLibraries'] ?? []); + $config->updateTime = time(); + $config->save(); + } + break; + + case self::TYPE_GROUP_PUSH: + $config = WorkbenchGroupPush::where('workbenchId', $param['id'])->find(); + if ($config) { + $config->pushInterval = $param['pushInterval']; + $config->pushContent = json_encode($param['pushContent']); + $config->pushTime = json_encode($param['pushTime']); + $config->devices = json_encode($param['devices']); + $config->targetGroups = json_encode($param['targetGroups']); + $config->save(); + } + break; + + case self::TYPE_GROUP_CREATE: + $config = WorkbenchGroupCreate::where('workbenchId', $param['id'])->find(); + if ($config) { + $config->groupNamePrefix = $param['groupNamePrefix']; + $config->maxGroups = $param['maxGroups']; + $config->membersPerGroup = $param['membersPerGroup']; + $config->devices = json_encode($param['devices']); + $config->targetGroups = json_encode($param['targetGroups']); + $config->updateTime = time(); + $config->save(); + } + break; + } + + Db::commit(); + return json(['code' => 200, 'msg' => '更新成功']); + } catch (\Exception $e) { + Db::rollback(); + return json(['code' => 500, 'msg' => '更新失败:' . $e->getMessage()]); + } + } + /** * 更新工作台状态 * @return \think\response\Json @@ -341,8 +455,9 @@ class WorkbenchController extends Controller /** * 删除工作台(软删除) */ - public function delete($id) + public function delete() { + $id = $this->request->param('id'); if (empty($id)) { return json(['code' => 400, 'msg' => '参数错误']); } @@ -399,6 +514,7 @@ class WorkbenchController extends Controller $newWorkbench->status = 1; // 新拷贝的默认启用 $newWorkbench->autoStart = $workbench->autoStart; $newWorkbench->userId = $this->request->userInfo['id']; + $newWorkbench->companyId = $this->request->userInfo['companyId']; $newWorkbench->save(); // 根据类型拷贝对应的配置 @@ -426,8 +542,11 @@ class WorkbenchController extends Controller $newConfig->syncInterval = $config->syncInterval; $newConfig->syncCount = $config->syncCount; $newConfig->syncType = $config->syncType; + $newConfig->startTime = $config->startTime; + $newConfig->endTime = $config->endTime; + $newConfig->accountType = $config->accountType; $newConfig->devices = $config->devices; - $newConfig->targetGroups = $config->targetGroups; + $newConfig->contentLibraries = $config->contentLibraries; $newConfig->save(); } break; diff --git a/Server/application/cunkebao/model/ContentItem.php b/Server/application/cunkebao/model/ContentItem.php new file mode 100644 index 00000000..e4c415df --- /dev/null +++ b/Server/application/cunkebao/model/ContentItem.php @@ -0,0 +1,52 @@ +belongsTo('ContentLibrary', 'libraryId', 'id'); + } + + // 内容类型获取器 + public function getTypeTextAttr($value, $data) + { + $types = [ + self::TYPE_TEXT => '文本', + self::TYPE_IMAGE => '图片', + self::TYPE_VIDEO => '视频', + self::TYPE_LINK => '链接' + ]; + return isset($types[$data['type']]) ? $types[$data['type']] : '未知'; + } + + // 内容数据获取器 + public function getContentDataAttr($value) + { + return json_decode($value, true); + } + + // 内容数据修改器 + public function setContentDataAttr($value) + { + return is_array($value) ? json_encode($value) : $value; + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/model/ContentLibrary.php b/Server/application/cunkebao/model/ContentLibrary.php new file mode 100644 index 00000000..2fbc08e4 --- /dev/null +++ b/Server/application/cunkebao/model/ContentLibrary.php @@ -0,0 +1,38 @@ +belongsTo('User', 'userId', 'id'); + } + + // 定义关联的内容项目 + public function items() + { + return $this->hasMany('ContentItem', 'libraryId', 'id'); + } + + // 根据ID数组获取内容库列表 + public static function getByIds($ids) + { + if (empty($ids)) { + return []; + } + + return self::where('id', 'in', $ids)->select(); + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/model/WorkbenchMomentsSync.php b/Server/application/cunkebao/model/WorkbenchMomentsSync.php index 5a343e2a..6c18ed2c 100644 --- a/Server/application/cunkebao/model/WorkbenchMomentsSync.php +++ b/Server/application/cunkebao/model/WorkbenchMomentsSync.php @@ -9,6 +9,12 @@ class WorkbenchMomentsSync extends Model protected $pk = 'id'; protected $name = 'workbench_moments_sync'; + // 同步类型 + const SYNC_TYPE_TEXT = 1; // 文本 + const SYNC_TYPE_IMAGE = 2; // 图片 + const SYNC_TYPE_VIDEO = 3; // 视频 + const SYNC_TYPE_LINK = 4; // 链接 + // 自动写入时间戳 protected $autoWriteTimestamp = true; protected $createTime = 'createTime'; @@ -19,4 +25,34 @@ class WorkbenchMomentsSync extends Model { return $this->belongsTo('Workbench', 'workbenchId', 'id'); } + + // 定义关联的内容库 + public function contentLibraries() + { + return $this->belongsToMany('ContentLibrary', 'workbench_content_relation', 'contentLibraryId', 'workbenchId'); + } + + // 开始时间获取器 + public function getStartTimeAttr($value) + { + return $value ? date('H:i', strtotime($value)) : ''; + } + + // 结束时间获取器 + public function getEndTimeAttr($value) + { + return $value ? date('H:i', strtotime($value)) : ''; + } + + // 同步类型获取器 + public function getSyncTypeTextAttr($value, $data) + { + $types = [ + self::SYNC_TYPE_TEXT => '文本', + self::SYNC_TYPE_IMAGE => '图片', + self::SYNC_TYPE_VIDEO => '视频', + self::SYNC_TYPE_LINK => '链接' + ]; + return isset($types[$data['syncType']]) ? $types[$data['syncType']] : '未知'; + } } \ No newline at end of file diff --git a/Server/application/cunkebao/validate/Workbench.php b/Server/application/cunkebao/validate/Workbench.php index 63a5d502..9ba771d0 100644 --- a/Server/application/cunkebao/validate/Workbench.php +++ b/Server/application/cunkebao/validate/Workbench.php @@ -25,10 +25,15 @@ class Workbench extends Validate 'startTime' => 'requireIf:type,1|dateFormat:H:i', 'endTime' => 'requireIf:type,1|dateFormat:H:i', 'contentTypes' => 'requireIf:type,1|array|contentTypeEnum:text,image,video', + 'targetGroups' => 'requireIf:type,1|array', // 朋友圈同步特有参数 'syncInterval' => 'requireIf:type,2|number|min:1', 'syncCount' => 'requireIf:type,2|number|min:1', 'syncType' => 'requireIf:type,2|in:1,2,3,4', + 'startTime' => 'requireIf:type,2|dateFormat:H:i', + 'endTime' => 'requireIf:type,2|dateFormat:H:i', + 'accountType' => 'requireIf:type,2|in:1,2', + 'contentLibraries' => 'requireIf:type,2|array', // 群消息推送特有参数 'pushInterval' => 'requireIf:type,3|number|min:1', 'pushContent' => 'requireIf:type,3|array', @@ -39,7 +44,6 @@ class Workbench extends Validate 'membersPerGroup' => 'requireIf:type,4|number|min:1', // 通用参数 'devices' => 'require|array', - 'targetGroups' => 'require|array' ]; /** @@ -75,6 +79,14 @@ class Workbench extends Validate 'syncCount.min' => '同步数量必须大于0', 'syncType.requireIf' => '请选择同步类型', 'syncType.in' => '同步类型错误', + 'startTime.requireIf' => '请设置发布开始时间', + 'startTime.dateFormat' => '发布开始时间格式错误', + 'endTime.requireIf' => '请设置发布结束时间', + 'endTime.dateFormat' => '发布结束时间格式错误', + 'accountType.requireIf' => '请选择账号类型', + 'accountType.in' => '账号类型错误', + 'contentLibraries.requireIf' => '请选择内容库', + 'contentLibraries.array' => '内容库格式错误', // 群消息推送相关提示 'pushInterval.requireIf' => '请设置推送间隔', 'pushInterval.number' => '推送间隔必须为数字', diff --git a/Server/application/job/WechatFriendJob.php b/Server/application/job/WechatFriendJob.php index 4224e59a..bf2dd571 100644 --- a/Server/application/job/WechatFriendJob.php +++ b/Server/application/job/WechatFriendJob.php @@ -6,6 +6,7 @@ use think\queue\Job; use think\facade\Log; use think\Queue; use think\facade\Config; +use think\facade\Cache; use app\api\controller\WechatFriendController; class WechatFriendJob @@ -58,7 +59,7 @@ class WechatFriendJob $pageSize = isset($data['pageSize']) ? $data['pageSize'] : 1000; $preFriendId = isset($data['preFriendId']) ? $data['preFriendId'] : ''; - Log::info('开始获取微信列表,页码:' . $pageIndex . ',页大小:' . $pageSize); + Log::info('开始获取微信列表,页码:' . $pageIndex . ',页大小:' . $pageSize . ',上一好友ID:' . $preFriendId); // 实例化控制器 $wechatFriendController = new WechatFriendController(); @@ -86,10 +87,24 @@ class WechatFriendJob // 判断是否有下一页 if (!empty($data) && count($data) > 0) { + // 获取最后一条记录的ID + $lastFriendId = $data[count($data)-1]['id']; + + // 更新缓存中的页码和最后一个好友ID,设置10分钟过期 + Cache::set('friendsPage', $pageIndex + 1, 600); + Cache::set('preFriendId', $lastFriendId, 600); + + Log::info('更新缓存,下一页页码:' . ($pageIndex + 1) . ',最后好友ID:' . $lastFriendId . ',缓存时间:10分钟'); + // 有下一页,将下一页任务添加到队列 $nextPageIndex = $pageIndex + 1; - $this->addNextPageToQueue($nextPageIndex, $pageSize,$data[count($data)-1]['id']); + $this->addNextPageToQueue($nextPageIndex, $pageSize, $lastFriendId); Log::info('添加下一页任务到队列,页码:' . $nextPageIndex); + } else { + // 没有下一页,重置缓存,设置10分钟过期 + Cache::set('friendsPage', 0, 600); + Cache::set('preFriendId', '', 600); + Log::info('获取完成,重置缓存,缓存时间:10分钟'); } return true; @@ -104,6 +119,7 @@ class WechatFriendJob * 添加下一页任务到队列 * @param int $pageIndex 页码 * @param int $pageSize 每页大小 + * @param string $preFriendId 上一个好友ID */ protected function addNextPageToQueue($pageIndex, $pageSize,$preFriendId) {