chore: 改成实例

This commit is contained in:
手瓜一十雪 2024-08-09 13:01:02 +08:00
parent cd45f7051c
commit 1db27ab0e3
3 changed files with 30 additions and 44 deletions

View File

@ -3,12 +3,11 @@ import {
CacheFileType, CacheFileType,
ChatCacheListItemBasic, ChatCacheListItemBasic,
ChatType, ChatType,
ElementType, IMAGE_HTTP_HOST, IMAGE_HTTP_HOST_NT, Peer, PicElement, RawMessage ElementType, IMAGE_HTTP_HOST, IMAGE_HTTP_HOST_NT, Peer, PicElement
} from '@/core/entities'; } from '@/core/entities';
import path from 'path'; import path from 'path';
import fs from 'fs'; import fs from 'fs';
import fsPromises from 'fs/promises'; import fsPromises from 'fs/promises';
import { log, logDebug, logError } from '@/common/utils/log';
import { GeneralCallResult, napCatCore, OnRichMediaDownloadCompleteParams } from '@/core'; import { GeneralCallResult, napCatCore, OnRichMediaDownloadCompleteParams } from '@/core';
import { calculateFileMD5 } from '@/common/utils/file'; import { calculateFileMD5 } from '@/common/utils/file';
import * as fileType from 'file-type'; import * as fileType from 'file-type';
@ -18,26 +17,25 @@ import { sessionConfig } from '@/core/sessionConfig';
import { rkeyManager } from '../utils/rkey'; import { rkeyManager } from '../utils/rkey';
import { NTEventDispatch } from '@/common/utils/EventTask'; import { NTEventDispatch } from '@/common/utils/EventTask';
import { NodeIKernelSearchService } from '../services/NodeIKernelSearchService'; import { NodeIKernelSearchService } from '../services/NodeIKernelSearchService';
import { selfInfo } from '../data';
export class NTQQFileApi { export class NTQQFileApi {
static async getFileType(filePath: string) { async getFileType(filePath: string) {
return fileType.fileTypeFromFile(filePath); return fileType.fileTypeFromFile(filePath);
} }
static async copyFile(filePath: string, destPath: string) { async copyFile(filePath: string, destPath: string) {
await napCatCore.util.copyFile(filePath, destPath); await napCatCore.util.copyFile(filePath, destPath);
} }
static async getFileSize(filePath: string): Promise<number> { async getFileSize(filePath: string): Promise<number> {
return await napCatCore.util.getFileSize(filePath); return await napCatCore.util.getFileSize(filePath);
} }
static async getVideoUrl(peer: Peer, msgId: string, elementId: string) { async getVideoUrl(peer: Peer, msgId: string, elementId: string) {
return (await napCatCore.session.getRichMediaService().getVideoPlayUrlV2(peer, msgId, elementId, 0, { downSourceType: 1, triggerType: 1 })).urlResult.domainUrl; return (await napCatCore.session.getRichMediaService().getVideoPlayUrlV2(peer, msgId, elementId, 0, { downSourceType: 1, triggerType: 1 })).urlResult.domainUrl;
} }
// 上传文件到QQ的文件夹 // 上传文件到QQ的文件夹
static async uploadFile(filePath: string, elementType: ElementType = ElementType.PIC, elementSubType: number = 0) { async uploadFile(filePath: string, elementType: ElementType = ElementType.PIC, elementSubType: number = 0) {
// napCatCore.wrapper.util. // napCatCore.wrapper.util.
const fileMd5 = await calculateFileMD5(filePath); const fileMd5 = await calculateFileMD5(filePath);
let ext: string = (await NTQQFileApi.getFileType(filePath))?.ext as string || ''; let ext: string = (await NTQQFileApi.getFileType(filePath))?.ext as string || '';
@ -68,10 +66,10 @@ export class NTQQFileApi {
ext ext
}; };
} }
static async downloadMediaByUuid() { async downloadMediaByUuid() {
//napCatCore.session.getRichMediaService().downloadFileForFileUuid(); //napCatCore.session.getRichMediaService().downloadFileForFileUuid();
} }
static async downloadMedia(msgId: string, chatType: ChatType, peerUid: string, elementId: string, thumbPath: string, sourcePath: string, timeout = 1000 * 60 * 2, force: boolean = false) { async downloadMedia(msgId: string, chatType: ChatType, peerUid: string, elementId: string, thumbPath: string, sourcePath: string, timeout = 1000 * 60 * 2, force: boolean = false) {
//logDebug('receive downloadMedia task', msgId, chatType, peerUid, elementId, thumbPath, sourcePath, timeout, force); //logDebug('receive downloadMedia task', msgId, chatType, peerUid, elementId, thumbPath, sourcePath, timeout, force);
// 用于下载收到的消息中的图片等 // 用于下载收到的消息中的图片等
if (sourcePath && fs.existsSync(sourcePath)) { if (sourcePath && fs.existsSync(sourcePath)) {
@ -135,7 +133,7 @@ export class NTQQFileApi {
return filePath; return filePath;
} }
static async getImageSize(filePath: string): Promise<ISizeCalculationResult | undefined> { async getImageSize(filePath: string): Promise<ISizeCalculationResult | undefined> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
imageSize(filePath, (err, dimensions) => { imageSize(filePath, (err, dimensions) => {
if (err) { if (err) {
@ -146,7 +144,7 @@ export class NTQQFileApi {
}); });
}); });
} }
static async addFileCache(peer: Peer, msgId: string, msgSeq: string, senderUid: string, elemId: string, elemType: string, fileSize: string, fileName: string) { async addFileCache(peer: Peer, msgId: string, msgSeq: string, senderUid: string, elemId: string, elemType: string, fileSize: string, fileName: string) {
let GroupData; let GroupData;
let BuddyData; let BuddyData;
if (peer.chatType === ChatType.group) { if (peer.chatType === ChatType.group) {
@ -204,7 +202,7 @@ export class NTQQFileApi {
] ]
}); });
} }
static async searchfile(keys: string[]) { async searchfile(keys: string[]) {
type EventType = NodeIKernelSearchService['searchFileWithKeywords']; type EventType = NodeIKernelSearchService['searchFileWithKeywords'];
interface OnListener { interface OnListener {
searchId: string, searchId: string,
@ -256,7 +254,7 @@ export class NTQQFileApi {
let [ret] = (await Listener); let [ret] = (await Listener);
return ret; return ret;
} }
static async getImageUrl(element: PicElement) { async getImageUrl(element: PicElement) {
if (!element) { if (!element) {
return ''; return '';
} }
@ -291,51 +289,51 @@ export class NTQQFileApi {
} }
export class NTQQFileCacheApi { export class NTQQFileCacheApi {
static async setCacheSilentScan(isSilent: boolean = true) { async setCacheSilentScan(isSilent: boolean = true) {
return ''; return '';
} }
static getCacheSessionPathList() { getCacheSessionPathList() {
return ''; return '';
} }
static clearCache(cacheKeys: Array<string> = ['tmp', 'hotUpdate']) { clearCache(cacheKeys: Array<string> = ['tmp', 'hotUpdate']) {
// 参数未验证 // 参数未验证
return napCatCore.session.getStorageCleanService().clearCacheDataByKeys(cacheKeys); return napCatCore.session.getStorageCleanService().clearCacheDataByKeys(cacheKeys);
} }
static addCacheScannedPaths(pathMap: object = {}) { addCacheScannedPaths(pathMap: object = {}) {
return napCatCore.session.getStorageCleanService().addCacheScanedPaths(pathMap); return napCatCore.session.getStorageCleanService().addCacheScanedPaths(pathMap);
} }
static scanCache(): Promise<GeneralCallResult & { scanCache(): Promise<GeneralCallResult & {
size: string[] size: string[]
}> { }> {
// 需要注册Listener onFinishScan // 需要注册Listener onFinishScan
return napCatCore.session.getStorageCleanService().scanCache(); return napCatCore.session.getStorageCleanService().scanCache();
} }
static getHotUpdateCachePath() { getHotUpdateCachePath() {
// 未实现 // 未实现
return ''; return '';
} }
static getDesktopTmpPath() { getDesktopTmpPath() {
// 未实现 // 未实现
return ''; return '';
} }
static getChatCacheList(type: ChatType, pageSize: number = 1000, pageIndex: number = 0) { getChatCacheList(type: ChatType, pageSize: number = 1000, pageIndex: number = 0) {
return napCatCore.session.getStorageCleanService().getChatCacheInfo(type, pageSize, 1, pageIndex); return napCatCore.session.getStorageCleanService().getChatCacheInfo(type, pageSize, 1, pageIndex);
} }
static getFileCacheInfo(fileType: CacheFileType, pageSize: number = 1000, lastRecord?: CacheFileListItem) { getFileCacheInfo(fileType: CacheFileType, pageSize: number = 1000, lastRecord?: CacheFileListItem) {
const _lastRecord = lastRecord ? lastRecord : { fileType: fileType }; const _lastRecord = lastRecord ? lastRecord : { fileType: fileType };
//需要五个参数 //需要五个参数
//return napCatCore.session.getStorageCleanService().getFileCacheInfo(); //return napCatCore.session.getStorageCleanService().getFileCacheInfo();
} }
static async clearChatCache(chats: ChatCacheListItemBasic[] = [], fileKeys: string[] = []) { async clearChatCache(chats: ChatCacheListItemBasic[] = [], fileKeys: string[] = []) {
return napCatCore.session.getStorageCleanService().clearChatCacheInfo(chats, fileKeys); return napCatCore.session.getStorageCleanService().clearChatCacheInfo(chats, fileKeys);
} }
} }

View File

@ -1,13 +1,7 @@
import { ModifyProfileParams, SelfInfo, User, UserDetailInfoByUin, UserDetailInfoByUinV2 } from '@/core/entities'; import type { ModifyProfileParams, SelfInfo, User, UserDetailInfoByUin, UserDetailInfoByUinV2 } from '@/core/entities';
import { friends, groupMembers, selfInfo } from '@/core/data';
import { CacheClassFuncAsync, CacheClassFuncAsyncExtend } from '@/common/utils/helper';
import { napCatCore, NTQQFriendApi } from '@/core';
import { NodeIKernelProfileListener, ProfileListener } from '@/core/listeners'; import { NodeIKernelProfileListener, ProfileListener } from '@/core/listeners';
import { RequestUtil } from '@/common/utils/request'; import { RequestUtil } from '@/common/utils/request';
import { logWarn } from '@/common/utils/log';
import { NTEventDispatch } from '@/common/utils/EventTask';
import { NodeIKernelProfileService, ProfileBizType, UserDetailSource } from '@/core/services'; import { NodeIKernelProfileService, ProfileBizType, UserDetailSource } from '@/core/services';
import { requireMinNTQQBuild } from '@/common/utils/QQBasicInfo';
export class NTQQUserApi { export class NTQQUserApi {
async getProfileLike(uid: string) { async getProfileLike(uid: string) {
@ -156,14 +150,12 @@ export class NTQQUserApi {
return napCatCore.session.getProfileService().modifyDesktopMiniProfile(param); return napCatCore.session.getProfileService().modifyDesktopMiniProfile(param);
} }
//需要异常处理 //需要异常处理
@CacheClassFuncAsync(1800 * 1000)
async getCookies(domain: string) { async getCookies(domain: string) {
const ClientKeyData = await NTQQUserApi.forceFetchClientKey(); const ClientKeyData = await NTQQUserApi.forceFetchClientKey();
const requestUrl = 'https://ssl.ptlogin2.qq.com/jump?ptlang=1033&clientuin=' + selfInfo.uin + '&clientkey=' + ClientKeyData.clientKey + '&u1=https%3A%2F%2F' + domain + '%2F' + selfInfo.uin + '%2Finfocenter&keyindex=19%27' const requestUrl = 'https://ssl.ptlogin2.qq.com/jump?ptlang=1033&clientuin=' + selfInfo.uin + '&clientkey=' + ClientKeyData.clientKey + '&u1=https%3A%2F%2F' + domain + '%2F' + selfInfo.uin + '%2Finfocenter&keyindex=19%27'
let cookies: { [key: string]: string; } = await RequestUtil.HttpsGetCookies(requestUrl); let cookies: { [key: string]: string; } = await RequestUtil.HttpsGetCookies(requestUrl);
return cookies; return cookies;
} }
@CacheClassFuncAsync(1800 * 1000)
async getPSkey(domainList: string[]) { async getPSkey(domainList: string[]) {
return await napCatCore.session.getTipOffService().getPskey(domainList, true); return await napCatCore.session.getTipOffService().getPskey(domainList, true);
} }
@ -178,7 +170,7 @@ export class NTQQUserApi {
return robotUinRanges?.response?.robotUinRanges; return robotUinRanges?.response?.robotUinRanges;
} }
//需要异常处理 //需要异常处理
@CacheClassFuncAsync(1800 * 1000)
async getQzoneCookies() { async getQzoneCookies() {
const ClientKeyData = await NTQQUserApi.forceFetchClientKey(); const ClientKeyData = await NTQQUserApi.forceFetchClientKey();
const requestUrl = 'https://ssl.ptlogin2.qq.com/jump?ptlang=1033&clientuin=' + selfInfo.uin + '&clientkey=' + ClientKeyData.clientKey + '&u1=https%3A%2F%2Fuser.qzone.qq.com%2F' + selfInfo.uin + '%2Finfocenter&keyindex=19%27' const requestUrl = 'https://ssl.ptlogin2.qq.com/jump?ptlang=1033&clientuin=' + selfInfo.uin + '&clientkey=' + ClientKeyData.clientKey + '&u1=https%3A%2F%2Fuser.qzone.qq.com%2F' + selfInfo.uin + '%2Finfocenter&keyindex=19%27'
@ -186,7 +178,7 @@ export class NTQQUserApi {
return cookies; return cookies;
} }
//需要异常处理 //需要异常处理
@CacheClassFuncAsync(1800 * 1000)
async getSkey(): Promise<string | undefined> { async getSkey(): Promise<string | undefined> {
const ClientKeyData = await NTQQUserApi.forceFetchClientKey(); const ClientKeyData = await NTQQUserApi.forceFetchClientKey();
if (ClientKeyData.result !== 0) { if (ClientKeyData.result !== 0) {

View File

@ -1,8 +1,4 @@
import { selfInfo } from '@/core/data';
import { log, logDebug } from '@/common/utils/log';
import { NTQQUserApi } from './user';
import { RequestUtil } from '@/common/utils/request'; import { RequestUtil } from '@/common/utils/request';
import { CacheClassFuncAsync } from '@/common/utils/helper';
export enum WebHonorType { export enum WebHonorType {
ALL = 'all', ALL = 'all',
TALKACTIVE = 'talkative', TALKACTIVE = 'talkative',
@ -117,7 +113,7 @@ export class WebApi {
async shareDigest(groupCode: string, msgSeq: string, msgRandom: string, targetGroupCode: string) { async shareDigest(groupCode: string, msgSeq: string, msgRandom: string, targetGroupCode: string) {
const CookiesObject = await NTQQUserApi.getCookies('qun.qq.com'); const CookiesObject = await NTQQUserApi.getCookies('qun.qq.com');
const CookieValue = Object.entries(CookiesObject).map(([key, value]) => `${key}=${value}`).join('; '); const CookieValue = Object.entries(CookiesObject).map(([key, value]) => `${key}=${value}`).join('; ');
const Bkn = WebApi.genBkn(CookiesObject.skey); const Bkn = this.genBkn(CookiesObject.skey);
let ret: any = undefined; let ret: any = undefined;
const data = 'group_code=' + groupCode + '&msg_seq=' + msgSeq + '&msg_random=' + msgRandom + '&target_group_code=' + targetGroupCode; const data = 'group_code=' + groupCode + '&msg_seq=' + msgSeq + '&msg_random=' + msgRandom + '&target_group_code=' + targetGroupCode;
const url = 'https://qun.qq.com/cgi-bin/group_digest/share_digest?bkn=' + Bkn + "&" + data; const url = 'https://qun.qq.com/cgi-bin/group_digest/share_digest?bkn=' + Bkn + "&" + data;
@ -134,7 +130,7 @@ export class WebApi {
async getGroupEssenceMsg(GroupCode: string, page_start: string) { async getGroupEssenceMsg(GroupCode: string, page_start: string) {
const CookiesObject = await NTQQUserApi.getCookies('qun.qq.com'); const CookiesObject = await NTQQUserApi.getCookies('qun.qq.com');
const CookieValue = Object.entries(CookiesObject).map(([key, value]) => `${key}=${value}`).join('; '); const CookieValue = Object.entries(CookiesObject).map(([key, value]) => `${key}=${value}`).join('; ');
const Bkn = WebApi.genBkn(CookiesObject.skey); const Bkn = this.genBkn(CookiesObject.skey);
const url = 'https://qun.qq.com/cgi-bin/group_digest/digest_list?bkn=' + Bkn + '&group_code=' + GroupCode + '&page_start=' + page_start + '&page_limit=20'; const url = 'https://qun.qq.com/cgi-bin/group_digest/digest_list?bkn=' + Bkn + '&group_code=' + GroupCode + '&page_start=' + page_start + '&page_limit=20';
let ret; let ret;
try { try {
@ -155,7 +151,7 @@ export class WebApi {
try { try {
const CookiesObject = await NTQQUserApi.getCookies('qun.qq.com'); const CookiesObject = await NTQQUserApi.getCookies('qun.qq.com');
const CookieValue = Object.entries(CookiesObject).map(([key, value]) => `${key}=${value}`).join('; '); const CookieValue = Object.entries(CookiesObject).map(([key, value]) => `${key}=${value}`).join('; ');
const Bkn = WebApi.genBkn(CookiesObject.skey); const Bkn = this.genBkn(CookiesObject.skey);
const retList: Promise<WebApiGroupMemberRet>[] = []; const retList: Promise<WebApiGroupMemberRet>[] = [];
const fastRet = await RequestUtil.HttpGetJson<WebApiGroupMemberRet>('https://qun.qq.com/cgi-bin/qun_mgr/search_group_members?st=0&end=40&sort=1&gc=' + GroupCode + '&bkn=' + Bkn, 'POST', '', { 'Cookie': CookieValue }); const fastRet = await RequestUtil.HttpGetJson<WebApiGroupMemberRet>('https://qun.qq.com/cgi-bin/qun_mgr/search_group_members?st=0&end=40&sort=1&gc=' + GroupCode + '&bkn=' + Bkn, 'POST', '', { 'Cookie': CookieValue });
if (!fastRet?.count || fastRet?.errcode !== 0 || !fastRet?.mems) { if (!fastRet?.count || fastRet?.errcode !== 0 || !fastRet?.mems) {
@ -204,7 +200,7 @@ export class WebApi {
const CookiesObject = await NTQQUserApi.getCookies('qun.qq.com'); const CookiesObject = await NTQQUserApi.getCookies('qun.qq.com');
const CookieValue = Object.entries(CookiesObject).map(([key, value]) => `${key}=${value}`).join('; '); const CookieValue = Object.entries(CookiesObject).map(([key, value]) => `${key}=${value}`).join('; ');
const Bkn = WebApi.genBkn(CookiesObject.skey); const Bkn = this.genBkn(CookiesObject.skey);
let ret: any = undefined; let ret: any = undefined;
const data = 'qid=' + GroupCode + '&bkn=' + Bkn + '&text=' + Content + '&pinned=0&type=1&settings={"is_show_edit_card":1,"tip_window_type":1,"confirm_required":1}'; const data = 'qid=' + GroupCode + '&bkn=' + Bkn + '&text=' + Content + '&pinned=0&type=1&settings={"is_show_edit_card":1,"tip_window_type":1,"confirm_required":1}';
const url = 'https://web.qun.qq.com/cgi-bin/announce/add_qun_notice?bkn=' + Bkn; const url = 'https://web.qun.qq.com/cgi-bin/announce/add_qun_notice?bkn=' + Bkn;
@ -219,7 +215,7 @@ export class WebApi {
async getGrouptNotice(GroupCode: string): Promise<undefined | WebApiGroupNoticeRet> { async getGrouptNotice(GroupCode: string): Promise<undefined | WebApiGroupNoticeRet> {
const CookiesObject = await NTQQUserApi.getCookies('qun.qq.com'); const CookiesObject = await NTQQUserApi.getCookies('qun.qq.com');
const CookieValue = Object.entries(CookiesObject).map(([key, value]) => `${key}=${value}`).join('; '); const CookieValue = Object.entries(CookiesObject).map(([key, value]) => `${key}=${value}`).join('; ');
const Bkn = WebApi.genBkn(CookiesObject.skey); const Bkn = this.genBkn(CookiesObject.skey);
let ret: WebApiGroupNoticeRet | undefined = undefined; let ret: WebApiGroupNoticeRet | undefined = undefined;
//console.log(CookieValue); //console.log(CookieValue);
const url = 'https://web.qun.qq.com/cgi-bin/announce/get_t_list?bkn=' + Bkn + '&qid=' + GroupCode + '&ft=23&ni=1&n=1&i=1&log_read=1&platform=1&s=-1&n=20'; const url = 'https://web.qun.qq.com/cgi-bin/announce/get_t_list?bkn=' + Bkn + '&qid=' + GroupCode + '&ft=23&ni=1&n=1&i=1&log_read=1&platform=1&s=-1&n=20';