This commit is contained in:
parent
159926dac4
commit
7d70397b70
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
@ -1,5 +1,6 @@
|
||||
{
|
||||
"cSpell.words": [
|
||||
"cloudml",
|
||||
"commitlint",
|
||||
"tseslint",
|
||||
"unapproval",
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user