Skip to content

Let's use AI to Earn!

feat: 整理主进程api请求

牛文政8315e39779c7
diff --git a/electron/main/api/index.ts b/electron/main/api/index.ts
index 16b8092d..d8869d26 100644
--- a/electron/main/api/index.ts
+++ b/electron/main/api/index.ts
@@ -1,5 +1,81 @@
-export * from './data-center';
-export * from './user';
-export * from './platform';
-export * from './ranking';
-export * from './types';
+import { net } from 'electron';
+
+export interface IRequestNetResult<T> {
+ status: number;
+ headers: Record<any, any>;
+ data: T;
+}
+
+export interface IRequestNetParams {
+ headers?: any;
+ url: string;
+ body?: any;
+ method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
+ isFile?: boolean;
+}
+
+const requestNet = <T = any>({
+ headers,
+ body,
+ method,
+ url,
+ isFile,
+}: IRequestNetParams): Promise<IRequestNetResult<T>> => {
+ return new Promise((resolve, reject) => {
+ const req = net.request({
+ method: method || 'GET',
+ url,
+ });
+
+ // 设置请求头
+ if (headers) {
+ Object.entries(headers).forEach(([key, value]) => {
+ // @ts-ignore
+ req.setHeader(key, value);
+ });
+ }
+
+ // 处理响应
+ req.on('response', (response) => {
+ let data = '';
+ response.on('data', (chunk) => {
+ data += chunk;
+ });
+
+ response.on('end', () => {
+ let parsedData: T;
+ try {
+ parsedData = JSON.parse(data);
+ } catch (e) {
+ parsedData = undefined as any;
+ console.log(e);
+ }
+ resolve({
+ status: response.statusCode,
+ headers: response.headers,
+ data: parsedData,
+ });
+ });
+ });
+
+ // 错误处理
+ req.on('error', (error) => {
+ reject(error);
+ });
+
+ if (isFile) {
+ req.setHeader('Content-Type', 'application/octet-stream');
+ req.write(body);
+ } else {
+ // 发送请求体
+ if (body) {
+ req.setHeader('Content-Type', 'application/json');
+ req.write(typeof body === 'string' ? body : JSON.stringify(body));
+ }
+ }
+
+ req.end();
+ });
+};
+
+export default requestNet;
diff --git a/electron/main/api/tools.ts b/electron/main/api/tools.ts
new file mode 100644
index 00000000..ea72bc02
--- /dev/null
+++ b/electron/main/api/tools.ts
@@ -0,0 +1,58 @@
+import { net } from 'electron';
+import { ResOp } from './types';
+import { Ranking, CreateRankingDto } from './types/ranking';
+
+class ToolsApi {
+ private async request<T>(
+ method: string,
+ path: string,
+ data?: any,
+ token?: string,
+ ): Promise<ResOp<T>> {
+ return new Promise((resolve, reject) => {
+ const request = net.request({
+ method,
+ url: `http://127.0.0.1:3000/api/`,
+ headers: token
+ ? {
+ Authorization: `Bearer ${token}`,
+ }
+ : {},
+ });
+
+ request.on('response', (response) => {
+ let data = '';
+ response.on('data', (chunk) => {
+ data += chunk;
+ });
+ response.on('end', () => {
+ try {
+ resolve(JSON.parse(data));
+ } catch (e) {
+ reject(e);
+ }
+ });
+ });
+
+ request.on('error', reject);
+
+ if (data) {
+ request.write(JSON.stringify(data));
+ }
+ request.end();
+ });
+ }
+
+ // 获取AI的评论回复
+ async createRanking(data: CreateRankingDto, token: string): Promise<Ranking> {
+ const response = await this.request<Ranking>(
+ 'POST',
+ 'tools/ai/recover/review',
+ data,
+ token,
+ );
+ return response.data;
+ }
+}
+
+export const rankingApi = new ToolsApi();
diff --git a/electron/main/reply/controller.ts b/electron/main/reply/controller.ts
index aef7398d..e5bb2ced 100644
--- a/electron/main/reply/controller.ts
+++ b/electron/main/reply/controller.ts
@@ -8,7 +8,7 @@
import { AccountService } from '../account/service';
import { Controller, Icp, Inject } from '../core/decorators';
import platController from '../plat';
-import type { PageInfo } from '../plat/plat.type';
+import type { CommentData, PageInfo } from '../plat/plat.type';
import { ReplyService } from './service';
@Controller()
@@ -75,6 +75,51 @@ export class ReplyController {
return res;
}
+ /**
+ * 作品一键AI评论
+ */
+ @Icp('ICP_REPLY_COMMENT_LIST_BY_AI')
+ async createCommentList(
+ event: Electron.IpcMainInvokeEvent,
+ accountId: number,
+ dataId: string,
+ ): Promise<any> {
+ const account = await this.accountService.getAccountById(accountId);
+ if (!account) return null;
+
+ // 1. 获取作品的评论列表, 如果返回
+ let theHasMore = true;
+ let thePcursor = undefined;
+
+ const list: CommentData[] = [];
+
+ while (theHasMore) {
+ const {
+ list,
+ pageInfo: { pcursor, hasMore },
+ } = await platController.getCommentList(account, dataId, {
+ pcursor: thePcursor,
+ });
+
+ list.forEach((item) => {
+ list.push(item);
+ });
+
+ thePcursor = pcursor;
+ theHasMore = !!hasMore;
+ }
+
+ // 2. 循环AI回复评论
+ for (const element of list) {
+ platController.replyComment(account, element.commentId, '嗨', {
+ dataId,
+ comment: element,
+ });
+ }
+
+ return true;
+ }
+
/**
* 回复评论
*/
diff --git a/src/views/trending/index.tsx b/src/views/trending/index.tsx
index aa2f815c..857e757b 100644
--- a/src/views/trending/index.tsx
+++ b/src/views/trending/index.tsx
@@ -2298,12 +2298,12 @@ const Trending: React.FC = () => {
</div>
<div className="w-24 text-center">
<span className="text-[#a66ae4]">
- {item.shareCount || '1w'}
+ {(item as any).shareCount || '1w'}
</span>
</div>
<div className="w-24 text-center">
<span className="text-[#a66ae4]">
- {item.collectCount || '1w'}
+ {(item as any).collectCount || '1w'}
</span>
</div>
</div>