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": [
|
"cSpell.words": [
|
||||||
|
"cloudml",
|
||||||
"commitlint",
|
"commitlint",
|
||||||
"tseslint",
|
"tseslint",
|
||||||
"unapproval",
|
"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 {
|
constructor({
|
||||||
getAk(appId: string, appSecret: string) {
|
appId,
|
||||||
return this.post<{ tenant_access_token: string; code: number }>(
|
appSecret,
|
||||||
"/auth/v3/tenant_access_token/internal",
|
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_id: this.appId,
|
||||||
app_secret: appSecret,
|
app_secret: this.appSecret,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
return tenant_access_token
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import NetToolBase, { NetError } from "../netTool/base"
|
import NetToolBase, { NetError } from "../netTool/base"
|
||||||
|
|
||||||
|
|
||||||
class LarkBaseService extends NetToolBase {
|
class LarkBaseService extends NetToolBase {
|
||||||
constructor(getToken: () => Promise<string>, requestId: string) {
|
constructor(getToken: () => Promise<string>, requestId: string) {
|
||||||
super({
|
super({
|
||||||
@ -25,4 +26,6 @@ class LarkBaseService extends NetToolBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default LarkBaseService
|
export default LarkBaseService
|
||||||
|
@ -14,19 +14,27 @@ class LarkService {
|
|||||||
chat: LarkChatService
|
chat: LarkChatService
|
||||||
requestId: string
|
requestId: string
|
||||||
|
|
||||||
constructor(getToken: () => Promise<string>, requestId: string) {
|
constructor({
|
||||||
this.drive = new LarkDriveService(getToken, requestId)
|
appId,
|
||||||
this.message = new LarkMessageService(getToken, requestId)
|
appSecret,
|
||||||
this.user = new LarkUserService(getToken, requestId)
|
requestId,
|
||||||
this.sheet = new LarkSheetService(getToken, requestId)
|
}: {
|
||||||
this.auth = new LarkAuthService(getToken, requestId)
|
appId?: string
|
||||||
this.chat = new LarkChatService(getToken, requestId)
|
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
|
this.requestId = requestId
|
||||||
}
|
}
|
||||||
|
|
||||||
child(getToken: () => Promise<string>, requestId?: string) {
|
|
||||||
return new LarkService(getToken, requestId || this.requestId)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default LarkService
|
export default LarkService
|
||||||
|
@ -215,7 +215,7 @@ class NetToolBase {
|
|||||||
response: res,
|
response: res,
|
||||||
code: resData.code || res.status,
|
code: resData.code || res.status,
|
||||||
message: resData.message || resData.msg || resText || res.statusText,
|
message: resData.message || resData.msg || resText || res.statusText,
|
||||||
data: resData.data,
|
data: resData.data || resData,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// http 错误码正常,但解析异常
|
// http 错误码正常,但解析异常
|
||||||
@ -232,6 +232,7 @@ class NetToolBase {
|
|||||||
response: res,
|
response: res,
|
||||||
code: resData.code,
|
code: resData.code,
|
||||||
message: resData.message || resData.msg || "网络请求失败",
|
message: resData.message || resData.msg || "网络请求失败",
|
||||||
|
data: resData.data || resData,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return resData as T
|
return resData as T
|
||||||
|
Loading…
x
Reference in New Issue
Block a user