All checks were successful
Egg Server MIflow / build-image (push) Successful in 1m5s
80 lines
1.9 KiB
TypeScript
80 lines
1.9 KiB
TypeScript
import { Context } from "../../types"
|
|
import { makeCheckPathTool } from "../../utils/pathTools"
|
|
|
|
/**
|
|
* 处理登录请求
|
|
* @param req
|
|
* @returns
|
|
*/
|
|
const manageLogin = async (ctx: Context.Data) => {
|
|
const { req, larkService, genResp, logger } = ctx
|
|
logger.info("micro app login")
|
|
const url = new URL(req.url)
|
|
const code = url.searchParams.get("code")
|
|
const appName = url.searchParams.get("app_name") || undefined
|
|
if (!code) {
|
|
return genResp.badRequest("code not found")
|
|
}
|
|
const {
|
|
code: resCode,
|
|
data,
|
|
message,
|
|
} = await larkService.child(appName).user.code2Login(code)
|
|
|
|
logger.debug(`get user session: ${JSON.stringify(data)}`)
|
|
|
|
if (resCode !== 0) {
|
|
return genResp.serverError(message)
|
|
}
|
|
|
|
return genResp.ok(data)
|
|
}
|
|
|
|
/**
|
|
* 处理批量获取用户信息请求
|
|
* @param req
|
|
* @returns
|
|
*/
|
|
const manageBatchUser = async (ctx: Context.Data) => {
|
|
const { body, genResp, larkService, logger } = ctx
|
|
logger.info("batch get user info")
|
|
if (!body) return genResp.badRequest("req body is empty")
|
|
const { user_ids, user_id_type, app_name } = body
|
|
if (!user_ids) {
|
|
return genResp.badRequest("user_ids not found")
|
|
}
|
|
if (!user_id_type) {
|
|
return genResp.badRequest("user_id_type not found")
|
|
}
|
|
|
|
const { code, data, message } = await larkService
|
|
.child(app_name)
|
|
.user.batchGet(user_ids, user_id_type)
|
|
|
|
logger.debug(`batch get user info: ${JSON.stringify(data)}`)
|
|
|
|
if (code !== 0) {
|
|
return genResp.serverError(message)
|
|
}
|
|
|
|
return genResp.ok(data)
|
|
}
|
|
|
|
/**
|
|
* 处理小程序请求
|
|
* @param req
|
|
* @returns
|
|
*/
|
|
export const manageMicroAppReq = async (ctx: Context.Data) => {
|
|
const { exactCheck } = makeCheckPathTool(ctx.req.url, "/micro_app")
|
|
// 处理登录请求
|
|
if (exactCheck("/login")) {
|
|
return manageLogin(ctx)
|
|
}
|
|
// 处理批量获取用户信息请求
|
|
if (exactCheck("/batch_user")) {
|
|
return manageBatchUser(ctx)
|
|
}
|
|
return ctx.genResp.ok()
|
|
}
|