All checks were successful
Egg Server MIflow / build-image (push) Successful in 1m5s
70 lines
1.8 KiB
TypeScript
70 lines
1.8 KiB
TypeScript
import { LarkServer } from "../../types/larkServer"
|
||
import LarkBaseService from "./base"
|
||
|
||
class LarkUserService extends LarkBaseService {
|
||
/**
|
||
* 登录凭证校验
|
||
* @param {string} code 登录凭证
|
||
* @returns
|
||
*/
|
||
async code2Login(code: string) {
|
||
const path = `/mina/v2/tokenLoginValidate`
|
||
return this.post<LarkServer.UserSessionRes>(path, { code })
|
||
}
|
||
|
||
/**
|
||
* 获取用户信息
|
||
* @param {string} user_id 用户ID
|
||
* @param {"open_id" | "user_id"} user_id_type 用户ID类型
|
||
* @returns
|
||
*/
|
||
async getOne(user_id: string, user_id_type: "open_id" | "user_id") {
|
||
const path = `/contact/v3/users/${user_id}`
|
||
return this.get<LarkServer.UserInfoRes>(path, {
|
||
user_id_type,
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 批量获取用户信息
|
||
* @param {string[]} user_ids 用户ID数组
|
||
* @param {"open_id" | "user_id"} user_id_type 用户ID类型
|
||
* @returns
|
||
*/
|
||
async batchGet(user_ids: string[], user_id_type: "open_id" | "user_id") {
|
||
const path = `/contact/v3/users/batch`
|
||
|
||
// 如果user_id长度超出50,需要分批请求,
|
||
const userCount = user_ids.length
|
||
const maxLen = 50
|
||
|
||
const requestMap = Array.from(
|
||
{ length: Math.ceil(userCount / maxLen) },
|
||
(_, index) => {
|
||
const start = index * maxLen
|
||
const user_idsSlice = user_ids.slice(start, start + maxLen)
|
||
const getParams = `${user_idsSlice
|
||
.map((id) => `user_ids=${id}`)
|
||
.join("&")}&user_id_type=${user_id_type}`
|
||
return this.get<LarkServer.BatchUserInfoRes>(path, getParams)
|
||
}
|
||
)
|
||
|
||
const responses = await Promise.all(requestMap)
|
||
|
||
const items = responses.flatMap((res) => {
|
||
return res.data?.items || []
|
||
})
|
||
|
||
return {
|
||
code: 0,
|
||
data: {
|
||
items,
|
||
},
|
||
message: "success",
|
||
}
|
||
}
|
||
}
|
||
|
||
export default LarkUserService
|