feat: 抽离 action 公共逻辑

This commit is contained in:
zhangzemeng 2024-02-12 09:13:06 +08:00
parent 43fbcb819a
commit c510f4acdc
12 changed files with 48 additions and 190 deletions

View File

@ -1,6 +1,5 @@
// import { OB11Response } from "./utils";
// import { BaseCheckResult } from "./types";
// import { OB11Return } from '../types'; // import { OB11Return } from '../types';
// import BaseAction from "./BaseAction";
// export type ActionType = '' // export type ActionType = ''
@ -16,25 +15,11 @@
// } // }
// class ActionTemplate { // class ActionTemplate extends BaseAction {
// static ACTION_TYPE: ActionType = '' // static ACTION_TYPE: ActionType = ''
// async check(jsonData: any): Promise<BaseCheckResult> {
// return {
// valid: true,
// }
// }
// async handle(jsonData: any) {
// const result = await this.check(jsonData)
// if (!result.valid) {
// return OB11Response.error(result.message)
// }
// const resData = await this._handle(jsonData)
// return resData
// }
// async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> { // async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> {
// return
// } // }
// } // }

View File

@ -0,0 +1,25 @@
import { BaseCheckResult } from "./types"
import { OB11Response } from "./utils"
class BaseAction {
async check(jsonData: any): Promise<BaseCheckResult> {
return {
valid: true,
}
}
async handle(jsonData: any) {
const result = await this.check(jsonData)
if (!result.valid) {
return OB11Response.error(result.message)
}
const resData = await this._handle(jsonData)
return resData
}
async _handle(payload: unknown): Promise<unknown> {
return
}
}
export default BaseAction

View File

@ -1,8 +1,8 @@
import { OB11Response } from "./utils"; import { OB11Response } from "./utils";
import { BaseCheckResult } from "./types";
import { OB11Return, OB11User } from '../types'; import { OB11Return, OB11User } from '../types';
import { OB11Constructor } from "../constructor"; import { OB11Constructor } from "../constructor";
import { friends } from "../../common/data"; import { friends } from "../../common/data";
import BaseAction from "./BaseAction";
export type ActionType = 'get_friend_list' export type ActionType = 'get_friend_list'
@ -12,24 +12,9 @@ export interface PayloadType {
export type ReturnDataType = OB11User[] export type ReturnDataType = OB11User[]
class GetFriendList { class GetFriendList extends BaseAction {
static ACTION_TYPE: ActionType = 'get_friend_list' static ACTION_TYPE: ActionType = 'get_friend_list'
async check(jsonData: any): Promise<BaseCheckResult> {
return {
valid: true,
}
}
async handle(jsonData: any) {
const result = await this.check(jsonData)
if (!result.valid) {
return OB11Response.error(result.message)
}
const resData = await this._handle(jsonData)
return resData
}
async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> { async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> {
return OB11Response.ok(OB11Constructor.friends(friends)); return OB11Response.ok(OB11Constructor.friends(friends));
} }

View File

@ -1,8 +1,8 @@
import { OB11Response } from "./utils"; import { OB11Response } from "./utils";
import { BaseCheckResult } from "./types";
import { OB11Group, OB11Return } from '../types'; import { OB11Group, OB11Return } from '../types';
import { getGroup, groups } from "../../common/data"; import { getGroup, groups } from "../../common/data";
import { OB11Constructor } from "../constructor"; import { OB11Constructor } from "../constructor";
import BaseAction from "./BaseAction";
export type ActionType = 'get_group_info' export type ActionType = 'get_group_info'
@ -13,24 +13,9 @@ export interface PayloadType {
export type ReturnDataType = OB11Group[] export type ReturnDataType = OB11Group[]
class GetGroupInfo { class GetGroupInfo extends BaseAction {
static ACTION_TYPE: ActionType = 'get_group_info' static ACTION_TYPE: ActionType = 'get_group_info'
async check(jsonData: any): Promise<BaseCheckResult> {
return {
valid: true,
}
}
async handle(jsonData: any) {
const result = await this.check(jsonData)
if (!result.valid) {
return OB11Response.error(result.message)
}
const resData = await this._handle(jsonData)
return resData
}
async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> { async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> {
const group = await getGroup(payload.group_id.toString()) const group = await getGroup(payload.group_id.toString())
if (group) { if (group) {

View File

@ -1,8 +1,8 @@
import { OB11Response } from "./utils"; import { OB11Response } from "./utils";
import { BaseCheckResult } from "./types";
import { OB11Group, OB11Return } from '../types'; import { OB11Group, OB11Return } from '../types';
import { OB11Constructor } from "../constructor"; import { OB11Constructor } from "../constructor";
import { groups } from "../../common/data"; import { groups } from "../../common/data";
import BaseAction from "./BaseAction";
export type ActionType = 'get_group_list' export type ActionType = 'get_group_list'
@ -12,24 +12,9 @@ export interface PayloadType {
export type ReturnDataType = OB11Group[] export type ReturnDataType = OB11Group[]
class GetGroupList { class GetGroupList extends BaseAction {
static ACTION_TYPE: ActionType = 'get_group_list' static ACTION_TYPE: ActionType = 'get_group_list'
async check(jsonData: any): Promise<BaseCheckResult> {
return {
valid: true,
}
}
async handle(jsonData: any) {
const result = await this.check(jsonData)
if (!result.valid) {
return OB11Response.error(result.message)
}
const resData = await this._handle(jsonData)
return resData
}
async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> { async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> {
return OB11Response.ok(OB11Constructor.groups(groups)); return OB11Response.ok(OB11Constructor.groups(groups));
} }

View File

@ -1,8 +1,8 @@
import { OB11Response } from "./utils"; import { OB11Response } from "./utils";
import { BaseCheckResult } from "./types";
import { OB11GroupMember, OB11Return } from '../types'; import { OB11GroupMember, OB11Return } from '../types';
import { getGroupMember } from "../../common/data"; import { getGroupMember } from "../../common/data";
import { OB11Constructor } from "../constructor"; import { OB11Constructor } from "../constructor";
import BaseAction from "./BaseAction";
export type ActionType = 'get_group_member_info' export type ActionType = 'get_group_member_info'
@ -14,24 +14,9 @@ export interface PayloadType {
export type ReturnDataType = OB11GroupMember export type ReturnDataType = OB11GroupMember
class GetGroupMemberInfo { class GetGroupMemberInfo extends BaseAction {
static ACTION_TYPE: ActionType = 'get_group_member_info' static ACTION_TYPE: ActionType = 'get_group_member_info'
async check(jsonData: any): Promise<BaseCheckResult> {
return {
valid: true,
}
}
async handle(jsonData: any) {
const result = await this.check(jsonData)
if (!result.valid) {
return OB11Response.error(result.message)
}
const resData = await this._handle(jsonData)
return resData
}
async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> { async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> {
const member = await getGroupMember(payload.group_id.toString(), payload.user_id.toString()) const member = await getGroupMember(payload.group_id.toString(), payload.user_id.toString())
if (member) { if (member) {

View File

@ -1,9 +1,9 @@
import { OB11Response } from "./utils"; import { OB11Response } from "./utils";
import { BaseCheckResult } from "./types";
import { OB11GroupMember, OB11Return } from '../types'; import { OB11GroupMember, OB11Return } from '../types';
import { getGroup } from "../../common/data"; import { getGroup } from "../../common/data";
import { NTQQApi } from "../../ntqqapi/ntcall"; import { NTQQApi } from "../../ntqqapi/ntcall";
import { OB11Constructor } from "../constructor"; import { OB11Constructor } from "../constructor";
import BaseAction from "./BaseAction";
export type ActionType = 'get_group_member_list' export type ActionType = 'get_group_member_list'
@ -14,24 +14,9 @@ export interface PayloadType {
export type ReturnDataType = OB11GroupMember[] export type ReturnDataType = OB11GroupMember[]
class GetGroupMemberList { class GetGroupMemberList extends BaseAction {
static ACTION_TYPE: ActionType = 'get_group_member_list' static ACTION_TYPE: ActionType = 'get_group_member_list'
async check(jsonData: any): Promise<BaseCheckResult> {
return {
valid: true,
}
}
async handle(jsonData: any) {
const result = await this.check(jsonData)
if (!result.valid) {
return OB11Response.error(result.message)
}
const resData = await this._handle(jsonData)
return resData
}
async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> { async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> {
const group = await getGroup(payload.group_id.toString()); const group = await getGroup(payload.group_id.toString());
if (group) { if (group) {

View File

@ -1,8 +1,8 @@
import { OB11Response } from "./utils"; import { OB11Response } from "./utils";
import { BaseCheckResult } from "./types";
import { OB11Return, OB11User } from '../types'; import { OB11Return, OB11User } from '../types';
import { OB11Constructor } from "../constructor"; import { OB11Constructor } from "../constructor";
import { selfInfo } from "../../common/data"; import { selfInfo } from "../../common/data";
import BaseAction from "./BaseAction";
export type ActionType = 'get_login_info' export type ActionType = 'get_login_info'
@ -12,24 +12,9 @@ export interface PayloadType {
export type ReturnDataType = OB11User export type ReturnDataType = OB11User
class GetLoginInfo { class GetLoginInfo extends BaseAction {
static ACTION_TYPE: ActionType = 'get_login_info' static ACTION_TYPE: ActionType = 'get_login_info'
async check(jsonData: any): Promise<BaseCheckResult> {
return {
valid: true,
}
}
async handle(jsonData: any) {
const result = await this.check(jsonData)
if (!result.valid) {
return OB11Response.error(result.message)
}
const resData = await this._handle(jsonData)
return resData
}
async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> { async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> {
return OB11Response.ok(OB11Constructor.selfInfo(selfInfo)); return OB11Response.ok(OB11Constructor.selfInfo(selfInfo));
} }

View File

@ -1,9 +1,9 @@
import { OB11Response } from "./utils"; import { OB11Response } from "./utils";
import { BaseCheckResult } from "./types";
import { msgHistory } from "../../common/data"; import { msgHistory } from "../../common/data";
import { OB11Message, OB11Return } from '../types'; import { OB11Message, OB11Return } from '../types';
import { OB11Constructor } from "../constructor"; import { OB11Constructor } from "../constructor";
import { log } from "../../common/utils"; import { log } from "../../common/utils";
import BaseAction from "./BaseAction";
export type ActionType = 'get_msg' export type ActionType = 'get_msg'
@ -14,24 +14,9 @@ export interface PayloadType {
export type ReturnDataType = OB11Message export type ReturnDataType = OB11Message
class GetMsg { class GetMsg extends BaseAction {
static ACTION_TYPE: ActionType = 'get_msg' static ACTION_TYPE: ActionType = 'get_msg'
async check(jsonData: any): Promise<BaseCheckResult> {
return {
valid: true,
}
}
async handle(jsonData: any) {
const result = await this.check(jsonData)
if (!result.valid) {
return OB11Response.error(result.message)
}
const resData = await this._handle(jsonData)
return resData
}
async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> { async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> {
log("history msg ids", Object.keys(msgHistory)); log("history msg ids", Object.keys(msgHistory));
const msg = msgHistory[payload.message_id.toString()] const msg = msgHistory[payload.message_id.toString()]

View File

@ -1,7 +1,6 @@
import { OB11PostSendMsg, OB11Return } from '../types'; import { OB11PostSendMsg, OB11Return } from '../types';
import { OB11Response } from "./utils";
import { BaseCheckResult } from "./types";
import SendMsg from "./SendMsg"; import SendMsg from "./SendMsg";
import BaseAction from './BaseAction';
export type ActionType = 'send_group_msg' export type ActionType = 'send_group_msg'
@ -13,24 +12,9 @@ export interface ReturnDataType {
message_id: string message_id: string
} }
class SendGroupMsg { class SendGroupMsg extends BaseAction {
static ACTION_TYPE: ActionType = 'send_group_msg' static ACTION_TYPE: ActionType = 'send_group_msg'
async check(jsonData: any): Promise<BaseCheckResult> {
return {
valid: true,
}
}
async handle(jsonData: any) {
const result = await this.check(jsonData)
if (!result.valid) {
return OB11Response.error(result.message)
}
const resData = await this._handle(jsonData)
return resData
}
async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> { async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> {
// 偷懒借用现有逻辑 // 偷懒借用现有逻辑
return new SendMsg()._handle(payload as any) return new SendMsg()._handle(payload as any)

View File

@ -9,7 +9,7 @@ import { uri2local } from "../utils";
import { OB11Response } from "./utils"; import { OB11Response } from "./utils";
import { v4 as uuid4 } from 'uuid'; import { v4 as uuid4 } from 'uuid';
import { log } from "../../common/utils"; import { log } from "../../common/utils";
import { BaseCheckResult } from "./types"; import BaseAction from "./BaseAction";
export type ActionType = 'send_msg' export type ActionType = 'send_msg'
@ -21,24 +21,9 @@ export interface ReturnDataType {
message_id: string message_id: string
} }
class SendMsg { class SendMsg extends BaseAction {
static ACTION_TYPE: ActionType = 'send_msg' static ACTION_TYPE: ActionType = 'send_msg'
async check(jsonData: any): Promise<BaseCheckResult> {
return {
valid: true,
}
}
async handle(jsonData: any) {
const result = await this.check(jsonData)
if (!result.valid) {
return OB11Response.error(result.message)
}
const resData = await this._handle(jsonData)
return resData
}
async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> { async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> {
const peer: Peer = { const peer: Peer = {
chatType: ChatType.friend, chatType: ChatType.friend,

View File

@ -1,7 +1,6 @@
import { OB11PostSendMsg, OB11Return } from '../types'; import { OB11PostSendMsg, OB11Return } from '../types';
import { OB11Response } from "./utils";
import { BaseCheckResult } from "./types";
import SendMsg from "./SendMsg"; import SendMsg from "./SendMsg";
import BaseAction from './BaseAction';
export type ActionType = 'send_private_msg' export type ActionType = 'send_private_msg'
@ -13,24 +12,9 @@ export interface ReturnDataType {
message_id: string message_id: string
} }
class SendPrivateMsg { class SendPrivateMsg extends BaseAction {
static ACTION_TYPE: ActionType = 'send_private_msg' static ACTION_TYPE: ActionType = 'send_private_msg'
async check(jsonData: any): Promise<BaseCheckResult> {
return {
valid: true,
}
}
async handle(jsonData: any) {
const result = await this.check(jsonData)
if (!result.valid) {
return OB11Response.error(result.message)
}
const resData = await this._handle(jsonData)
return resData
}
async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> { async _handle(payload: PayloadType): Promise<OB11Return<ReturnDataType | null>> {
// 偷懒借用现有逻辑 // 偷懒借用现有逻辑
return new SendMsg()._handle(payload as any) return new SendMsg()._handle(payload as any)