zhaoyingbo 09e352a9c1
All checks were successful
Egg Server MIflow / build-image (push) Successful in 1m5s
feat: 抽象网络请求类 & 内容转为ctx向内传递
2024-08-16 09:12:11 +00:00

70 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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