import { LarkServer } from "../../types/larkServer" import LarkBaseService from "./base" class LarkUserService extends LarkBaseService { /** * 登录凭证校验 * @param code 登录凭证 * @returns */ async code2Login(code: string) { const path = `/mina/v2/tokenLoginValidate` return this.post(path, { code }) } /** * 获取用户信息 * @param userId 用户ID * @param userIdType 用户ID类型 * @returns */ async getOne(userId: string, userIdType: "open_id" | "user_id") { const path = `/contact/v3/users/${userId}` return this.get(path, { user_id_type: userIdType, }) } /** * 批量获取用户信息 * @param userIds 用户ID数组 * @param userIdType 用户ID类型 * @returns */ async batchGet( userIds: string[], userIdType: "open_id" | "user_id" = "open_id" ) { const path = `/contact/v3/users/batch` // 如果user_id长度超出50,需要分批请求, const userCount = userIds.length const maxLen = 50 const requestMap = Array.from( { length: Math.ceil(userCount / maxLen) }, (_, index) => { const start = index * maxLen const getParams = `${userIds .slice(start, start + maxLen) .map((id) => `user_ids=${id}`) .join("&")}&user_id_type=${userIdType}` return this.get(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