chore: clean code for webapi.ts

This commit is contained in:
Wesley F. Young 2024-08-31 14:17:22 +08:00
parent 4b4a784f56
commit 78452cf6a9

View File

@ -26,8 +26,7 @@ export class NTQQWebApi {
msg_seq: msgSeq, msg_seq: msgSeq,
msg_random: msgRandom, msg_random: msgRandom,
target_group_code: targetGroupCode, target_group_code: targetGroupCode,
}).toString() }).toString()}`;
}`;
try { try {
return RequestUtil.HttpGetText(url, 'GET', '', { 'Cookie': this.cookieToString(cookieObject) }); return RequestUtil.HttpGetText(url, 'GET', '', { 'Cookie': this.cookieToString(cookieObject) });
} catch (e) { } catch (e) {
@ -35,16 +34,12 @@ export class NTQQWebApi {
} }
} }
async getGroupEssenceMsgAll(GroupCode: string) { async getGroupEssenceMsgAll(GroupCode: string) {
let ret: GroupEssenceMsgRet[] = []; const ret: GroupEssenceMsgRet[] = [];
for (let i = 0; i < 4; i++) { for (let i = 0; i < 4; i++) {
let data = await this.getGroupEssenceMsg(GroupCode, i, 50); const data = await this.getGroupEssenceMsg(GroupCode, i, 50);
if (!data) break; if (!data) break;
if (data.data.is_end) {
ret.push(data); ret.push(data);
break; if (data.data.is_end) break;
}
ret.push(data);
} }
return ret; return ret;
} }
@ -55,19 +50,18 @@ export class NTQQWebApi {
page_start: page_start.toString(), page_start: page_start.toString(),
page_limit: page_limit.toString(), page_limit: page_limit.toString(),
group_code: GroupCode, group_code: GroupCode,
}).toString() }).toString()}`;
}`;
let ret;
try { try {
ret = await RequestUtil.HttpGetJson<GroupEssenceMsgRet> const ret = await RequestUtil.HttpGetJson<GroupEssenceMsgRet>(
(url, 'GET', '', { 'Cookie': this.cookieToString(cookieObject) }); url,
'GET',
'',
{ 'Cookie': this.cookieToString(cookieObject) }
);
return ret.retcode === 0 ? ret : undefined;
} catch { } catch {
return undefined; return undefined;
} }
if (ret.retcode !== 0) {
return undefined;
}
return ret;
} }
async getGroupMembers(GroupCode: string, cached: boolean = true): Promise<WebApiGroupMember[]> { async getGroupMembers(GroupCode: string, cached: boolean = true): Promise<WebApiGroupMember[]> {
@ -75,15 +69,18 @@ export class NTQQWebApi {
const memberData: Array<WebApiGroupMember> = new Array<WebApiGroupMember>(); const memberData: Array<WebApiGroupMember> = new Array<WebApiGroupMember>();
const cookieObject = await this.core.apis.UserApi.getCookies('qun.qq.com'); const cookieObject = await this.core.apis.UserApi.getCookies('qun.qq.com');
const retList: Promise<WebApiGroupMemberRet>[] = []; const retList: Promise<WebApiGroupMemberRet>[] = [];
const fastRet = await RequestUtil.HttpGetJson<WebApiGroupMemberRet> const fastRet = await RequestUtil.HttpGetJson<WebApiGroupMemberRet>(
(`https://qun.qq.com/cgi-bin/qun_mgr/search_group_members?${new URLSearchParams({ `https://qun.qq.com/cgi-bin/qun_mgr/search_group_members?${new URLSearchParams({
st: '0', st: '0',
end: '40', end: '40',
sort: '1', sort: '1',
gc: GroupCode, gc: GroupCode,
bkn: this.getBknFromCookie(cookieObject), bkn: this.getBknFromCookie(cookieObject),
}).toString() }).toString()}`,
}`, 'POST', '', { 'Cookie': this.cookieToString(cookieObject) }); 'POST',
'',
{ 'Cookie': this.cookieToString(cookieObject) }
);
if (!fastRet?.count || fastRet?.errcode !== 0 || !fastRet?.mems) { if (!fastRet?.count || fastRet?.errcode !== 0 || !fastRet?.mems) {
return []; return [];
} else { } else {
@ -95,15 +92,18 @@ export class NTQQWebApi {
const PageNum = Math.ceil(fastRet.count / 40); const PageNum = Math.ceil(fastRet.count / 40);
//遍历批量请求 //遍历批量请求
for (let i = 2; i <= PageNum; i++) { for (let i = 2; i <= PageNum; i++) {
const ret = RequestUtil.HttpGetJson<WebApiGroupMemberRet> const ret = RequestUtil.HttpGetJson<WebApiGroupMemberRet>(
(`https://qun.qq.com/cgi-bin/qun_mgr/search_group_members?${new URLSearchParams({ `https://qun.qq.com/cgi-bin/qun_mgr/search_group_members?${new URLSearchParams({
st: ((i - 1) * 40).toString(), st: ((i - 1) * 40).toString(),
end: (i * 40).toString(), end: (i * 40).toString(),
sort: '1', sort: '1',
gc: GroupCode, gc: GroupCode,
bkn: this.getBknFromCookie(cookieObject), bkn: this.getBknFromCookie(cookieObject),
}).toString() }).toString()}`,
}`, 'POST', '', { 'Cookie': this.cookieToString(cookieObject) }); 'POST',
'',
{ 'Cookie': this.cookieToString(cookieObject) }
);
retList.push(ret); retList.push(ret);
} }
//批量等待 //批量等待
@ -135,16 +135,19 @@ export class NTQQWebApi {
const cookieObject = await this.core.apis.UserApi.getCookies('qun.qq.com'); const cookieObject = await this.core.apis.UserApi.getCookies('qun.qq.com');
let ret: any = undefined; let ret: any = undefined;
try { try {
ret = await RequestUtil.HttpGetJson<any> ret = await RequestUtil.HttpGetJson<any>(
(`https://web.qun.qq.com/cgi-bin/announce/add_qun_notice${new URLSearchParams({ `https://web.qun.qq.com/cgi-bin/announce/add_qun_notice${new URLSearchParams({
bkn: this.getBknFromCookie(cookieObject), bkn: this.getBknFromCookie(cookieObject),
qid: GroupCode, qid: GroupCode,
text: Content, text: Content,
pinned: '0', pinned: '0',
type: '1', type: '1',
settings: '{"is_show_edit_card":1,"tip_window_type":1,"confirm_required":1}', settings: '{"is_show_edit_card":1,"tip_window_type":1,"confirm_required":1}',
}).toString() }).toString()}`,
}`, 'GET', '', { 'Cookie': this.cookieToString(cookieObject) }); 'GET',
'',
{ 'Cookie': this.cookieToString(cookieObject) }
);
return ret; return ret;
} catch (e) { } catch (e) {
return undefined; return undefined;
@ -153,16 +156,24 @@ export class NTQQWebApi {
async getGroupNotice(GroupCode: string): Promise<undefined | WebApiGroupNoticeRet> { async getGroupNotice(GroupCode: string): Promise<undefined | WebApiGroupNoticeRet> {
const cookieObject = await this.core.apis.UserApi.getCookies('qun.qq.com'); const cookieObject = await this.core.apis.UserApi.getCookies('qun.qq.com');
let ret: WebApiGroupNoticeRet | undefined = undefined;
try { try {
const url = 'https://web.qun.qq.com/cgi-bin/announce/get_t_list?bkn=' + const ret = await RequestUtil.HttpGetJson<WebApiGroupNoticeRet>(
this.getBknFromCookie(cookieObject) + '&qid=' + GroupCode + '&ft=23&ni=1&n=1&i=1&log_read=1&platform=1&s=-1&n=20'; `https://web.qun.qq.com/cgi-bin/announce/get_t_list?${new URLSearchParams({
bkn: this.getBknFromCookie(cookieObject),
ret = await RequestUtil.HttpGetJson<WebApiGroupNoticeRet>(url, 'GET', '', { 'Cookie': this.cookieToString(cookieObject) }); qid: GroupCode,
if (ret?.ec !== 0) { ft: '23',
return undefined; ni: '1',
} n: '1',
return ret; i: '1',
log_read: '1',
platform: '1',
s: '-1',
}).toString()}&n=20`,
'GET',
'',
{ 'Cookie': this.cookieToString(cookieObject) }
);
return ret?.ec === 0 ? ret : undefined;
} catch (e) { } catch (e) {
return undefined; return undefined;
} }
@ -171,14 +182,17 @@ export class NTQQWebApi {
async getGroupHonorInfo(groupCode: string, getType: WebHonorType) { async getGroupHonorInfo(groupCode: string, getType: WebHonorType) {
const cookieObject = await this.core.apis.UserApi.getCookies('qun.qq.com'); const cookieObject = await this.core.apis.UserApi.getCookies('qun.qq.com');
const getDataInternal = async (Internal_groupCode: string, Internal_type: number) => { const getDataInternal = async (Internal_groupCode: string, Internal_type: number) => {
const url = `https://qun.qq.com/interactive/honorlist?${new URLSearchParams({
gc: Internal_groupCode,
type: Internal_type.toString(),
}).toString()
}`;
let resJson; let resJson;
try { try {
const res = await RequestUtil.HttpGetText(url, 'GET', '', { 'Cookie': this.cookieToString(cookieObject) }); const res = await RequestUtil.HttpGetText(
`https://qun.qq.com/interactive/honorlist?${new URLSearchParams({
gc: Internal_groupCode,
type: Internal_type.toString(),
}).toString()}`,
'GET',
'',
{ 'Cookie': this.cookieToString(cookieObject) }
);
const match = /window\.__INITIAL_STATE__=(.*?);/.exec(res); const match = /window\.__INITIAL_STATE__=(.*?);/.exec(res);
if (match) { if (match) {
resJson = JSON.parse(match[1].trim()); resJson = JSON.parse(match[1].trim());
@ -189,7 +203,7 @@ export class NTQQWebApi {
return resJson?.actorList; return resJson?.actorList;
} }
} catch (e) { } catch (e) {
this.context.logger.logDebug('获取当前群荣耀失败', url, e); this.context.logger.logDebug('获取当前群荣耀失败', e);
} }
return undefined; return undefined;
}; };
@ -268,10 +282,12 @@ export class NTQQWebApi {
this.context.logger.logError('获取快乐源泉失败'); this.context.logger.logError('获取快乐源泉失败');
} }
} }
// 冒尖小春笋好像已经被tx扬了 R.I.P. // 冒尖小春笋好像已经被tx扬了 R.I.P.
if (getType === WebHonorType.EMOTION || getType === WebHonorType.ALL) { if (getType === WebHonorType.EMOTION || getType === WebHonorType.ALL) {
HonorInfo.strong_newbie_list = []; HonorInfo.strong_newbie_list = [];
} }
return HonorInfo; return HonorInfo;
} }