feat(net-tool): 接入黄卓的鉴权功能
All checks were successful
/ release (push) Successful in 26s

This commit is contained in:
zhaoyingbo 2024-11-24 11:05:18 +00:00
parent 159926dac4
commit 7d70397b70
5 changed files with 102 additions and 19 deletions

View File

@ -1,5 +1,6 @@
{
"cSpell.words": [
"cloudml",
"commitlint",
"tseslint",
"unapproval",

View File

@ -1,14 +1,84 @@
import LarkBaseService from "./base"
import NetToolBase, { NetError } from "../netTool/base"
import { Lark } from "../types"
export class LarkAuthService extends NetToolBase {
public appId: string
public appSecret: string
class LarkAuthService extends LarkBaseService {
getAk(appId: string, appSecret: string) {
return this.post<{ tenant_access_token: string; code: number }>(
"/auth/v3/tenant_access_token/internal",
constructor({
appId,
appSecret,
requestId,
}: {
appId?: string
appSecret?: string
requestId: string
}) {
super({
prefix:
process.env.NODE_ENV === "production"
? "http://lark-auth.c5-cloudml.xiaomi.srv/lark_auth"
: "http://lark-auth.staging-cloudml.xiaomi.srv/lark_auth",
requestId,
})
this.appId = appId || process.env.LARK_APP_ID || ""
this.appSecret = appSecret || process.env.LARK_APP_SECRET || ""
}
/**
*
* @param user
* @returns
*/
public async getUserAuth(user: string) {
return this.get<
Lark.BaseRes<{
access_token: string
}>
>("/get_auth", {
app_id: this.appId,
app_secret: this.appSecret,
user_name: user,
})
.then(({ data: { access_token } }) => ({
access_token,
}))
.catch((e: NetError) => {
if (e.response?.status === 400)
return {
auth_url: e.data?.detail?.auth_url,
}
throw e
})
}
/**
*
* @param user
* @returns
*/
public deleteUserAuth(user: string) {
return this.get<{ message: string }>("/delete_auth", {
app_id: this.appId,
app_secret: this.appSecret,
user_name: user,
})
}
/**
*
* @returns
*/
public async getAppAuth() {
const {
data: { tenant_access_token },
} = await this.get<Lark.BaseRes<{ tenant_access_token: string }>>(
"/get_tenant_access_token",
{
app_id: appId,
app_secret: appSecret,
app_id: this.appId,
app_secret: this.appSecret,
}
)
return tenant_access_token
}
}

View File

@ -1,5 +1,6 @@
import NetToolBase, { NetError } from "../netTool/base"
class LarkBaseService extends NetToolBase {
constructor(getToken: () => Promise<string>, requestId: string) {
super({
@ -25,4 +26,6 @@ class LarkBaseService extends NetToolBase {
}
}
export default LarkBaseService

View File

@ -14,19 +14,27 @@ class LarkService {
chat: LarkChatService
requestId: string
constructor(getToken: () => Promise<string>, requestId: string) {
this.drive = new LarkDriveService(getToken, requestId)
this.message = new LarkMessageService(getToken, requestId)
this.user = new LarkUserService(getToken, requestId)
this.sheet = new LarkSheetService(getToken, requestId)
this.auth = new LarkAuthService(getToken, requestId)
this.chat = new LarkChatService(getToken, requestId)
constructor({
appId,
appSecret,
requestId,
}: {
appId?: string
appSecret?: string
requestId: string
}) {
this.auth = new LarkAuthService({
appId,
appSecret,
requestId,
})
this.drive = new LarkDriveService(this.auth.getAppAuth, requestId)
this.message = new LarkMessageService(this.auth.getAppAuth, requestId)
this.user = new LarkUserService(this.auth.getAppAuth, requestId)
this.sheet = new LarkSheetService(this.auth.getAppAuth, requestId)
this.chat = new LarkChatService(this.auth.getAppAuth, requestId)
this.requestId = requestId
}
child(getToken: () => Promise<string>, requestId?: string) {
return new LarkService(getToken, requestId || this.requestId)
}
}
export default LarkService

View File

@ -215,7 +215,7 @@ class NetToolBase {
response: res,
code: resData.code || res.status,
message: resData.message || resData.msg || resText || res.statusText,
data: resData.data,
data: resData.data || resData,
})
}
// http 错误码正常,但解析异常
@ -232,6 +232,7 @@ class NetToolBase {
response: res,
code: resData.code,
message: resData.message || resData.msg || "网络请求失败",
data: resData.data || resData,
})
}
return resData as T