Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
feat: 更改自动任务的存储表
牛文政d1c684466196
diff --git a/electron/db/models/autoRun.ts b/electron/db/models/autoRun.tsindex 145de61b..9105695e 100644--- a/electron/db/models/autoRun.ts+++ b/electron/db/models/autoRun.ts@@ -31,11 +31,11 @@ export class AutoRunModel extends TempModel {accountId!: number;@Column({- type: 'varchar',- nullable: true,- comment: '对应数据的数据ID',+ type: 'text',+ nullable: false,+ comment: '对应数据的数据内容 JSON字符串',})- dataId?: string;+ data!: string;@Column({ type: 'int', nullable: false, comment: '执行次数', default: 0 })runCount!: number;diff --git a/electron/global/cache.ts b/electron/global/cache.tsindex 797ef8e8..8e260735 100644--- a/electron/global/cache.ts+++ b/electron/global/cache.ts@@ -1,40 +1,53 @@import NodeCache from 'node-cache';-const GlobleCache = new NodeCache();--/**- * 缓存数据- * @param key 缓存key- * @param value 缓存值- * @param ttl 缓存时间 秒- */-export const setCache = (key: string, value: any, ttl?: number) => {- if (ttl) {- GlobleCache.set(key, value, ttl);- } else {- GlobleCache.set(key, value);- }-};--export const getCache = (key: string) => {- return GlobleCache.get(key);-};--export const delCache = (key: string) => {- return GlobleCache.del(key);-};--export const clearCache = () => {- return GlobleCache.flushAll();-};--// 更改TTL-export const updateCacheTTL = (key: string, ttl: number) => {- GlobleCache.ttl(key, ttl);-};--// 设置多个缓存-export const setMultiCache = (- list: { key: string; val: any; ttl?: number }[],-) => {- GlobleCache.mset(list);-};++class Cache {+ private static instance: Cache;+ private cache: NodeCache;++ private constructor() {+ this.cache = new NodeCache();+ }++ public static getInstance(): Cache {+ if (!Cache.instance) Cache.instance = new Cache();+ return Cache.instance;+ }++ /**+ * 缓存数据+ * @param key 缓存key+ * @param value 缓存值+ * @param ttl 缓存时间 秒+ */+ public setCache(key: string, value: any, ttl?: number) {+ if (ttl) {+ this.cache.set(key, value, ttl);+ } else {+ this.cache.set(key, value);+ }+ }++ public getCache(key: string) {+ return this.cache.get(key);+ }++ public delCache(key: string) {+ return this.cache.del(key);+ }++ public clearCache() {+ return this.cache.flushAll();+ }++ // 更改TTL+ public updateCacheTTL(key: string, ttl: number) {+ this.cache.ttl(key, ttl);+ }++ // 设置多个缓存+ public setMultiCache(list: { key: string; val: any; ttl?: number }[]) {+ this.cache.mset(list);+ }+}++export const GlobleCache = Cache.getInstance();diff --git a/electron/main/autoRun/controller.ts b/electron/main/autoRun/controller.tsindex 67d4b1f6..16272dd2 100644--- a/electron/main/autoRun/controller.ts+++ b/electron/main/autoRun/controller.ts@@ -24,19 +24,22 @@ export class AutoRunController {@Icp('ICP_AUTO_RUN_CREATE')async createAutoRun(event: Electron.IpcMainInvokeEvent,- data: {+ info: {accountId: number;type: AutoRunType;cycleType: string;- dataId?: string;},+ data: Record<string, any>, // 对象) {const userInfo = getUserInfo();- const autoRun = await this.autoRunService.createAutoRun({- userId: userInfo.id,- ...data,- });+ const autoRun = await this.autoRunService.createAutoRun(+ {+ userId: userInfo.id,+ ...info,+ },+ data,+ );return autoRun;}diff --git a/electron/main/autoRun/service.ts b/electron/main/autoRun/service.tsindex 7d6d2e37..88ba7b7f 100644--- a/electron/main/autoRun/service.ts+++ b/electron/main/autoRun/service.ts@@ -45,19 +45,24 @@ export class AutoRunService {}// 创建进程- async createAutoRun(data: Partial<AutoRunModel>) {+ async createAutoRun(info: Partial<AutoRunModel>, data: Record<string, any>) {if (!(await this.chectAutoRunCount(data.userId!))) return null;- return await this.autoRunRepository.save(data);+ info.data = JSON.stringify(data);+ return await this.autoRunRepository.save(info);}// 根据ID查询进程信息async findAutoRunById(id: number) {- return await this.autoRunRepository.findOne({+ const info = await this.autoRunRepository.findOne({where: {id,},});++ if (info) info.data = JSON.parse(info.data);++ return info;}// 查询进程列表diff --git a/electron/main/reply/controller.ts b/electron/main/reply/controller.tsindex 879e907c..d99587de 100644--- a/electron/main/reply/controller.ts+++ b/electron/main/reply/controller.ts@@ -199,14 +199,14 @@ export class ReplyController {async createCommentList(event: Electron.IpcMainInvokeEvent,accountId: number,- dataId: string,+ data: WorkData,): Promise<any> {const account = await this.accountService.getAccountById(accountId);if (!account) return null;const res = await this.replyService.autorReplyComment(account,- dataId,+ data,(e: {tag: AutorReplyCommentScheduleEvent;status: -1 | 0 | 1;@@ -279,32 +279,42 @@ export class ReplyController {async createReplyCommentAutoRun(event: Electron.IpcMainInvokeEvent,accountId: number,- dataId: string,+ data: WorkData,cycleType: string,): Promise<AutoRunModel | null> {const account = await this.accountService.getAccountById(accountId);if (!account) return null;- const res = await this.autoRunService.createAutoRun({- accountId,- cycleType,- type: AutoRunType.ReplyComment,- userId: account.uid,- dataId,- });+ const res = await this.autoRunService.createAutoRun(+ {+ accountId,+ cycleType,+ type: AutoRunType.ReplyComment,+ userId: account.uid,+ },+ data,+ );return res;}// 运行自动任务@Et('ET_AUTO_RUN_REPLY_COMMENT')- async runAutoReplyComment(autoRunData: AutoRunModel): Promise<any> {- const { accountId, dataId } = autoRunData;- if (!dataId) return null;+ async runAutoReplyComment(autoRunData: AutoRunModel): Promise<boolean> {+ const { accountId, data } = autoRunData;+ if (!data) return false;const account = await this.accountService.getAccountById(accountId);- if (!account) return null;+ if (!account) return false;++ const WorkData = JSON.parse(data) as WorkData;++ const res = await this.replyService.addReplyQueue(+ account,+ WorkData,+ autoRunData,+ );- this.replyService.addReplyQueue(account, dataId, autoRunData);+ return res.status === 1;}}diff --git a/electron/main/reply/service.ts b/electron/main/reply/service.tsindex 39079a11..b6b83307 100644--- a/electron/main/reply/service.ts+++ b/electron/main/reply/service.ts@@ -18,6 +18,17 @@ import { ReplyCommentRecordModel } from '../../db/models/replyCommentRecord';import { AppDataSource } from '../../db';import { getUserInfo } from '../user/comment';import { AutoRunRecordStatus } from '../../db/models/autoRunRecord';+import { WorkData } from '../plat/plat.type';+import { GlobleCache } from '../../global/cache';++/**+ * 获取缓存key+ * @param account+ * @returns+ */+function getCacheKey(account: AccountModel) {+ return `OneKeyReplyCommentCacheKey_${account.id}`;+}@Injectable()export class ReplyService {@@ -76,7 +87,7 @@ export class ReplyService {*/async autorReplyComment(account: AccountModel,- dataId: string,+ data: WorkData,scheduleEvent: (data: {tag: AutorReplyCommentScheduleEvent;status: -1 | 0 | 1; // -1 错误 0 进行中 1 完成@@ -89,6 +100,9 @@ export class ReplyService {let theHasMore = true;let thePcursor = undefined;+ // 设置缓存+ GlobleCache.setCache(getCacheKey(account), data, 60 * 15);+try {scheduleEvent({tag: AutorReplyCommentScheduleEvent.Start,@@ -96,6 +110,9 @@ export class ReplyService {});while (theHasMore) {+ // 重设缓存时间+ GlobleCache.updateCacheTTL(getCacheKey(account), 60 * 15);+scheduleEvent({tag: AutorReplyCommentScheduleEvent.GetCommentListStart,status: 0,@@ -103,7 +120,7 @@ export class ReplyService {const {list,pageInfo: { pcursor, hasMore },- } = await platController.getCommentList(account, dataId, thePcursor);+ } = await platController.getCommentList(account, data, thePcursor);if (list.length === 0) {scheduleEvent({@@ -164,7 +181,7 @@ export class ReplyService {element.commentId,aiRes,{- dataId,+ dataId: data.dataId,comment: element,},);@@ -216,6 +233,9 @@ export class ReplyService {});return false;}++ // 清除缓存+ GlobleCache.delCache(getCacheKey(account));}/**@@ -225,17 +245,26 @@ export class ReplyService {*/async addReplyQueue(account: AccountModel,- dataId: string,+ data: WorkData,autoRun: AutoRunModel,- ) {+ ): Promise<{+ status: 0 | 1;+ message?: string;+ }> {+ // 查看缓存,有的就不执行+ if (GlobleCache.getCache(getCacheKey(account)))+ return {+ status: 0,+ message: '该账户有正在执行的任务,请勿重复执行',+ };+// 创建任务执行记录const recordData = await this.autoRunService.createAutoRunRecord(autoRun);-// 添加到队列this.replyQueue.add(() => {this.autorReplyComment(account,- dataId,+ data,(e: {tag: AutorReplyCommentScheduleEvent;status: -1 | 0 | 1;@@ -263,5 +292,9 @@ export class ReplyService {},);});++ return {+ status: 1,+ };}}diff --git a/src/icp/autoRun.ts b/src/icp/autoRun.tsindex 77778f13..a7af521e 100644--- a/src/icp/autoRun.ts+++ b/src/icp/autoRun.ts@@ -50,14 +50,18 @@ export interface AutoRunRecord {* 创建自动进程* @param data*/-export async function ipcCreateAutoRun(data: {- accountId: number;- dataId: number;- type: AutoRunType;- cycleType: string;-}) {+export async function ipcCreateAutoRun(+ info: {+ accountId: number;+ type: AutoRunType;+ cycleType: string;+ },+ data: Record<string, any>,+) {+ data.data = JSON.stringify(data.data);const res: string = await window.ipcRenderer.invoke('ICP_AUTO_RUN_CREATE',+ info,data,);return res;diff --git a/src/icp/reply.ts b/src/icp/reply.tsindex 0d0cdbd9..bd54fdb0 100644--- a/src/icp/reply.ts+++ b/src/icp/reply.ts@@ -115,11 +115,11 @@ export async function icpReplyComment(/*** 一键作品回复评论*/-export async function icpReplyCommentList(accountId: number, dataId: string) {+export async function icpReplyCommentList(accountId: number, data: WorkData) {const res: boolean = await window.ipcRenderer.invoke('ICP_REPLY_COMMENT_LIST_BY_AI',accountId,- dataId,+ data,);return res;}@@ -130,13 +130,13 @@ export async function icpReplyCommentList(accountId: number, dataId: string) {*/export async function ipcCreateAutoRunOfReply(accountId: number,- dataId: string, // 作品ID+ data: WorkData, // 作品IDcycleType: string,) {const res: string = await window.ipcRenderer.invoke('ICP_AUTO_RUN_CREATE_REPLY',accountId,- dataId,+ data,cycleType,);return res;diff --git a/src/views/reply/components/addAutoRun.tsx b/src/views/reply/components/addAutoRun.tsxindex c25a150b..7ce6014b 100644--- a/src/views/reply/components/addAutoRun.tsx+++ b/src/views/reply/components/addAutoRun.tsx@@ -5,24 +5,25 @@* @LastEditors: nevin* @Description: 添加自动运行*/-import { ipcCreateAutoRunOfReply } from '@/icp/reply';+import { ipcCreateAutoRunOfReply, WorkData } from '@/icp/reply';import { Modal } from 'antd';import { forwardRef, useImperativeHandle, useState } from 'react';import { CronSchedule } from '@/components/CronSchedule';export interface AddAutoRunRef {- init: (accountId: number, dataId: string) => Promise<void>;+ init: (accountId: number, data: WorkData) => Promise<void>;}const Com = forwardRef<AddAutoRunRef>((props: any, ref) => {const [isModalOpen, setIsModalOpen] = useState(false);const [accountId, setAccountId] = useState<number>(0);- const [dataId, setDataId] = useState<string>('');+ const [workData, setWorkData] = useState<WorkData | null>(null);+// '周期类型 天 day-22 (例:每天22时) 周 week-2 (例:每周周二,周日0) 月 month-22 (例:每月22号)',const [cycleType, setCycleType] = useState<string>('');- async function init(accountId: number, dataId: string) {+ async function init(accountId: number, data: WorkData) {setAccountId(accountId);- setDataId(dataId);+ setWorkData(data);setIsModalOpen(true);}@@ -36,8 +37,10 @@ const Com = forwardRef<AddAutoRunRef>((props: any, ref) => {async function handleCron(cron: string) {console.log('生成的定时表达式:----', cron);+ if (!workData) return;+setCycleType(cron);- const res = await ipcCreateAutoRunOfReply(accountId, dataId, cron);+ const res = await ipcCreateAutoRunOfReply(accountId, workData, cron);console.log('-------- res', res);}diff --git a/src/views/reply/components/oneKeyReply.tsx b/src/views/reply/components/oneKeyReply.tsxindex 32c4378d..1dc0c6fe 100644--- a/src/views/reply/components/oneKeyReply.tsx+++ b/src/views/reply/components/oneKeyReply.tsx@@ -35,7 +35,7 @@ const Com = forwardRef<OneKeyReplyRef>((props: any, ref) => {*/async function replyCommentList(inAccountId: number, inWorkData: WorkData) {if (!workData?.dataId) return;- const res = await icpReplyCommentList(inAccountId, inWorkData?.dataId);+ const res = await icpReplyCommentList(inAccountId, inWorkData);console.log('------ res', res);}