79 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 larkNetTool from "./larkNetTool"
/**
* 登录凭证校验
* @param code
* @returns
*/
const code2Login = (appName?: string) => async (code: string) => {
const URL = `https://open.f.mioffice.cn/open-apis/mina/v2/tokenLoginValidate`
return larkNetTool.post(appName)<LarkServer.UserSessionRes>(URL, { code })
}
/**
* 获取用户信息
* @param user_id
* @returns
*/
const get =
(appName?: string) =>
async (user_id: string, user_id_type: "open_id" | "user_id") => {
const URL = `https://open.f.mioffice.cn/open-apis/contact/v3/users/${user_id}`
return larkNetTool.get(appName)<LarkServer.UserInfoRes>(URL, {
user_id_type,
})
}
/**
* 批量获取用户信息
* @param user_ids
* @returns
*/
const batchGet =
(appName?: string) =>
async (user_ids: string[], user_id_type: "open_id" | "user_id") => {
const URL = `https://open.f.mioffice.cn/open-apis/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 larkNetTool.get(appName)<LarkServer.BatchUserInfoRes>(
URL,
getParams
)
}
)
const responses = await Promise.all(requestMap)
const items = responses.flatMap((res) => {
return res.data?.items || []
})
return {
code: 0,
data: {
items,
},
msg: "success",
}
}
const user = {
code2Login,
batchGet,
get,
}
export default user