Compare commits

...

2 Commits

Author SHA1 Message Date
17b87c2353 chore(release): publish
- @egg/net-tool@1.14.0
2024-11-24 11:05:45 +00:00
7d70397b70 feat(net-tool): 接入黄卓的鉴权功能
All checks were successful
/ release (push) Successful in 26s
2024-11-24 11:05:18 +00:00
8 changed files with 110 additions and 21 deletions

View File

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

2
package-lock.json generated
View File

@ -12394,7 +12394,7 @@
},
"packages/net-tool": {
"name": "@egg/net-tool",
"version": "1.13.0",
"version": "1.14.0",
"license": "ISC",
"dependencies": {
"@egg/logger": "^1.4.4",

View File

@ -3,6 +3,12 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [1.14.0](http://yingbo.im:3000/zhaoyingbo/egg_tools/compare/@egg/net-tool@1.13.0...@egg/net-tool@1.14.0) (2024-11-24)
### Features
- **net-tool:** 接入黄卓的鉴权功能 ([7d70397](http://yingbo.im:3000/zhaoyingbo/egg_tools/commits/7d70397b70888463769e25d993b2802261c08874))
# [1.13.0](http://yingbo.im:3000/zhaoyingbo/egg_tools/compare/@egg/net-tool@1.12.0...@egg/net-tool@1.13.0) (2024-11-24)
### Features

View File

@ -1,6 +1,6 @@
{
"name": "@egg/net-tool",
"version": "1.13.0",
"version": "1.14.0",
"description": "Net Tools for Egg projects",
"type": "module",
"main": "src/index.ts",

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