KMP鸿蒙打造员工招聘评估

目录
- 概述
- 功能设计
- Kotlin 实现代码(KMP)
- JavaScript 调用示例
- ArkTS 页面集成与调用
- 数据输入与交互体验
- 编译与自动复制流程
- 总结
概述
本案例在 Kotlin Multiplatform (KMP) 工程中实现了一个 员工招聘评估工具:
- 输入:招聘候选人评估数据(教育背景、工作经验、技能水平、沟通能力、文化适配),使用空格分隔,例如:
85 88 82 90 86。 - 输出:
- 综合评分:各项指标的加权综合评分
- 招聘等级:根据综合评分的等级评估
- 各项评分:详细的各维度评分
- 评估建议:根据分析结果的个性化建议
- 招聘建议:是否推荐录用及理由
- 技术路径:Kotlin → Kotlin/JS → JavaScript 模块 → ArkTS 页面调用。
这个案例展示了 KMP 跨端开发在人力资源招聘领域的应用:
把招聘评估逻辑写在 Kotlin 里,一次实现,多端复用;把评估界面写在 ArkTS 里,专注 UI 和体验。
Kotlin 侧负责解析候选人数据、计算综合评分、评估招聘等级、生成招聘建议;ArkTS 侧只需要把输入字符串传给 Kotlin 函数,并把返回结果原样展示出来即可。借助 KMP 的 Kotlin/JS 能力,这个招聘评估工具可以在 Node.js、Web 前端以及 OpenHarmony 中复用相同的代码逻辑。
功能设计
输入数据格式
员工招聘评估工具采用简单直观的输入格式:
- 使用 空格分隔 各个参数。
- 第一个参数是教育背景(整数或浮点数,范围 0-100)。
- 第二个参数是工作经验(整数或浮点数,范围 0-100)。
- 第三个参数是技能水平(整数或浮点数,范围 0-100)。
- 第四个参数是沟通能力(整数或浮点数,范围 0-100)。
- 第五个参数是文化适配(整数或浮点数,范围 0-100)。
- 输入示例:
85 88 82 90 86
这可以理解为:
- 教育背景:85 分
- 工作经验:88 分
- 技能水平:82 分
- 沟通能力:90 分
- 文化适配:86 分
工具会基于这些数据计算出:
- 综合评分:所有指标的加权平均值
- 招聘等级:根据综合评分的等级评估(强烈推荐、推荐、可考虑、需谨慎、不推荐)
- 最强项:各项指标中表现最好的方面
- 最弱项:各项指标中表现最差的方面
- 个性化建议:根据各项指标的具体情况生成
输出信息结构
为了便于在 ArkTS 页面以及终端中直接展示,Kotlin 函数返回的是一段结构化的多行文本,划分为几个分区:
- 标题区:例如"👤 员工招聘评估",一眼看出工具用途。
- 综合评分:综合得分、招聘等级、最强项、最弱项。
- 各项评分:五个维度的详细评分和等级。
- 评估建议:根据各项指标的个性化建议。
- 招聘建议:是否推荐录用及具体理由。
- 等级说明:各等级的标准定义。
这样的输出结构使得:
- 在 ArkTS 中可以直接把整段文本绑定到
Text组件,配合monospace字体,阅读体验类似终端报告。 - 如果将来想把结果保存到日志或者后端,直接保存字符串即可。
- 需要更精细的 UI 时,也可以在前端根据分隔符进行拆分,再按块展示。
Kotlin 实现代码(KMP)
核心代码在 src/jsMain/kotlin/App.kt 中,通过 @JsExport 导出。以下是完整的 Kotlin 实现:
@OptIn(ExperimentalJsExport::class)
@JsExport
fun recruitmentEvaluator(inputData: String = "85 88 82 90 86"): String {
// 输入格式: 教育背景 工作经验 技能水平 沟通能力 文化适配
val parts = inputData.trim().split(" ").filter { it.isNotEmpty() }
if (parts.size < 5) {
return "❌ 错误: 请输入完整的信息,格式: 教育背景 工作经验 技能水平 沟通能力 文化适配\n例如: 85 88 82 90 86"
}
val education = parts[0].toDoubleOrNull() ?: return "❌ 错误: 教育背景必须是数字"
val experience = parts[1].toDoubleOrNull() ?: return "❌ 错误: 工作经验必须是数字"
val skills = parts[2].toDoubleOrNull() ?: return "❌ 错误: 技能水平必须是数字"
val communication = parts[3].toDoubleOrNull() ?: return "❌ 错误: 沟通能力必须是数字"
val cultureFit = parts[4].toDoubleOrNull() ?: return "❌ 错误: 文化适配必须是数字"
if (education < 0 || education > 100 || experience < 0 || experience > 100 ||
skills < 0 || skills > 100 || communication < 0 || communication > 100 ||
cultureFit < 0 || cultureFit > 100) {
return "❌ 错误: 所有评分必须在 0-100 之间"
}
// 计算加权综合评分
val comprehensiveScore = (education * 0.20 + experience * 0.25 + skills * 0.25 + communication * 0.15 + cultureFit * 0.15)
// 判断招聘等级
val recruitmentLevel = when {
comprehensiveScore >= 90 -> "🟢 强烈推荐"
comprehensiveScore >= 80 -> "🟡 推荐"
comprehensiveScore >= 70 -> "🟠 可考虑"
comprehensiveScore >= 60 -> "🔴 需谨慎"
else -> "🔴 不推荐"
}
// 找出最强和最弱项
val evaluationItems = mapOf(
"教育背景" to education,
"工作经验" to experience,
"技能水平" to skills,
"沟通能力" to communication,
"文化适配" to cultureFit
)
val strongest = evaluationItems.maxByOrNull { it.value }
val weakest = evaluationItems.minByOrNull { it.value }
// 判断各项等级
val getGrade = { score: Double ->
when {
score >= 90 -> "优秀"
score >= 80 -> "良好"
score >= 70 -> "中等"
score >= 60 -> "一般"
else -> "需改进"
}
}
// 生成建议
val suggestions = mutableListOf<String>()
if (education >= 85) suggestions.add("✅ 教育背景符合要求")
if (experience >= 85) suggestions.add("✅ 工作经验丰富")
if (skills >= 85) suggestions.add("✅ 技能水平突出")
if (communication >= 85) suggestions.add("✅ 沟通能力强")
if (cultureFit >= 85) suggestions.add("✅ 文化适配度高")
if (education < 70) suggestions.add("⚠️ 教育背景需关注")
if (experience < 70) suggestions.add("⚠️ 工作经验不足")
if (skills < 70) suggestions.add("⚠️ 技能水平需提升")
if (communication < 70) suggestions.add("⚠️ 沟通能力需改进")
if (cultureFit < 70) suggestions.add("⚠️ 文化适配度低")
if (suggestions.isEmpty()) suggestions.add("✅ 综合评估完成")
return "━━━━━━━━━━━━━━━━━━━━━\n" +
"👤 员工招聘评估\n" +
"━━━━━━━━━━━━━━━━━━━━━\n\n" +
"📊 综合评分\n" +
"综合得分: ${(comprehensiveScore * 10).toInt() / 10.0}/100\n" +
"招聘等级: $recruitmentLevel\n" +
"最强项: ${strongest?.key} (${((strongest?.value ?: 0.0) * 10).toInt() / 10.0}分)\n" +
"最弱项: ${weakest?.key} (${((weakest?.value ?: 0.0) * 10).toInt() / 10.0}分)\n\n" +
"📋 各项评分\n" +
"教育背景: ${(education * 10).toInt() / 10.0}分 (${getGrade(education)})\n" +
"工作经验: ${(experience * 10).toInt() / 10.0}分 (${getGrade(experience)})\n" +
"技能水平: ${(skills * 10).toInt() / 10.0}分 (${getGrade(skills)})\n" +
"沟通能力: ${(communication * 10).toInt() / 10.0}分 (${getGrade(communication)})\n" +
"文化适配: ${(cultureFit * 10).toInt() / 10.0}分 (${getGrade(cultureFit)})\n\n" +
"💡 评估建议\n" +
suggestions.mapIndexed { index, tip -> "${index + 1}. $tip" }.joinToString("\n") +
"\n\n" +
"🎯 招聘建议\n" +
(when {
comprehensiveScore >= 90 -> "强烈推荐录用,该候选人各方面表现优异"
comprehensiveScore >= 80 -> "推荐录用,该候选人符合岗位要求"
comprehensiveScore >= 70 -> "可以考虑录用,但需关注某些方面"
comprehensiveScore >= 60 -> "需谨慎考虑,建议进一步沟通"
else -> "不推荐录用,该候选人不符合岗位要求"
}) +
"\n\n" +
"📌 等级说明\n" +
"强烈推荐: 90-100分\n" +
"推荐: 80-89分\n" +
"可考虑: 70-79分\n" +
"需谨慎: 60-69分\n" +
"不推荐: 0-59分\n\n" +
"━━━━━━━━━━━━━━━━━━━━━\n" +
"✅ 评估完成!"
}
代码说明
这段 Kotlin 代码实现了完整的招聘候选人评估功能。让我详细解释关键部分:
数据验证:首先验证输入的评分数据是否有效,确保数据在 0-100 范围内。
评分计算:采用加权平均方式计算综合评分,其中教育背景占20%,工作经验占25%,技能水平占25%,沟通能力占15%,文化适配占15%。这个权重设置反映了不同岗位对候选人的不同要求。
等级评估:根据综合评分给出相应的招聘等级,从"强烈推荐"到"不推荐",帮助招聘人员快速做出决策。
项目分析:找出最强项和最弱项,计算两者的差距,帮助识别候选人的优势和需要改进的方向。
建议生成:根据各项指标生成个性化的评估建议,包括优点和需要改进的方面,以及最终的招聘建议。
JavaScript 调用示例
编译后的 JavaScript 代码可以在 Node.js 或浏览器中直接调用。以下是 JavaScript 的使用示例:
// 导入编译后的 Kotlin/JS 模块
const { recruitmentEvaluator } = require('./hellokjs.js');
// 示例 1:强烈推荐
const result1 = recruitmentEvaluator("90 92 88 95 91");
console.log("示例 1 - 强烈推荐:");
console.log(result1);
console.log("\n");
// 示例 2:推荐
const result2 = recruitmentEvaluator("85 88 82 90 86");
console.log("示例 2 - 推荐:");
console.log(result2);
console.log("\n");
// 示例 3:可考虑
const result3 = recruitmentEvaluator("75 78 72 80 76");
console.log("示例 3 - 可考虑:");
console.log(result3);
console.log("\n");
// 示例 4:需谨慎
const result4 = recruitmentEvaluator("65 68 62 70 66");
console.log("示例 4 - 需谨慎:");
console.log(result4);
console.log("\n");
// 示例 5:不推荐
const result5 = recruitmentEvaluator("45 48 42 50 46");
console.log("示例 5 - 不推荐:");
console.log(result5);
console.log("\n");
// 示例 6:使用默认参数
const result6 = recruitmentEvaluator();
console.log("示例 6 - 使用默认参数:");
console.log(result6);
// 实际应用场景:从用户输入获取数据
function evaluateCandidate(userInput) {
try {
const result = recruitmentEvaluator(userInput);
return {
success: true,
data: result
};
} catch (error) {
return {
success: false,
error: error.message
};
}
}
// 测试实际应用
const userInput = "88 90 85 92 88";
const evaluation = evaluateCandidate(userInput);
if (evaluation.success) {
console.log("招聘评估结果:");
console.log(evaluation.data);
} else {
console.log("评估失败:", evaluation.error);
}
// 多个候选人对比
function compareCandidates(candidates) {
console.log("\n多个候选人对比:");
console.log("═".repeat(60));
const results = candidates.map((candidate, index) => {
const evaluation = recruitmentEvaluator(candidate);
return {
number: index + 1,
candidate,
evaluation
};
});
results.forEach(result => {
console.log(`\n候选人 ${result.number} (${result.candidate}):`);
console.log(result.evaluation);
});
return results;
}
// 测试多个候选人对比
const candidates = [
"90 92 88 95 91",
"85 88 82 90 86",
"75 78 72 80 76",
"65 68 62 70 66"
];
compareCandidates(candidates);
// 招聘统计分析
function analyzeRecruitmentStats(candidates) {
const data = candidates.map(candidate => {
const parts = candidate.split(' ').map(Number);
return {
education: parts[0],
experience: parts[1],
skills: parts[2],
communication: parts[3],
cultureFit: parts[4]
};
});
console.log("\n招聘统计分析:");
const avgEducation = data.reduce((sum, d) => sum + d.education, 0) / data.length;
const avgExperience = data.reduce((sum, d) => sum + d.experience, 0) / data.length;
const avgSkills = data.reduce((sum, d) => sum + d.skills, 0) / data.length;
const avgCommunication = data.reduce((sum, d) => sum + d.communication, 0) / data.length;
const avgCultureFit = data.reduce((sum, d) => sum + d.cultureFit, 0) / data.length;
console.log(`平均教育背景: ${avgEducation.toFixed(1)}`);
console.log(`平均工作经验: ${avgExperience.toFixed(1)}`);
console.log(`平均技能水平: ${avgSkills.toFixed(1)}`);
console.log(`平均沟通能力: ${avgCommunication.toFixed(1)}`);
console.log(`平均文化适配: ${avgCultureFit.toFixed(1)}`);
console.log(`平均综合评分: ${((avgEducation * 0.20 + avgExperience * 0.25 + avgSkills * 0.25 + avgCommunication * 0.15 + avgCultureFit * 0.15)).toFixed(1)}`);
}
analyzeRecruitmentStats(candidates);
JavaScript 代码说明
这段 JavaScript 代码展示了如何在 Node.js 环境中调用编译后的 Kotlin 函数。关键点包括:
模块导入:使用 require 导入编译后的 JavaScript 模块,获取导出的 recruitmentEvaluator 函数。
多个示例:展示了不同招聘等级的调用方式,包括强烈推荐、推荐、可考虑、需谨慎、不推荐等。
错误处理:在实际应用中,使用 try-catch 块来处理可能的错误。
多候选人对比:compareCandidates 函数展示了如何对比多个候选人的评估结果。
统计分析:analyzeRecruitmentStats 函数演示了如何进行招聘统计分析,计算平均分数。
ArkTS 页面集成与调用
在 OpenHarmony 的 ArkTS 页面中集成这个招聘评估工具。以下是完整的 ArkTS 实现代码:
import { recruitmentEvaluator } from './hellokjs';
@Entry
@Component
struct RecruitmentEvaluatorPage {
@State educationValue: string = "85";
@State experienceValue: string = "88";
@State skillsValue: string = "82";
@State communicationValue: string = "90";
@State cultureFitValue: string = "86";
@State evaluationResult: string = "";
@State isLoading: boolean = false;
build() {
Column() {
// 顶部栏
Row() {
Text("👤 员工招聘评估工具")
.fontSize(24)
.fontWeight(FontWeight.Bold)
.fontColor(Color.White)
}
.width("100%")
.height(60)
.backgroundColor("#512DA8")
.justifyContent(FlexAlign.Center)
.padding({ top: 10, bottom: 10 })
// 主容器
Scroll() {
Column() {
// 教育背景输入
Text("🎓 教育背景 (0-100)")
.fontSize(14)
.fontColor("#333333")
.margin({ top: 20, left: 15 })
TextInput({
placeholder: "例如: 85",
text: this.educationValue
})
.width("90%")
.height(45)
.margin({ top: 8, bottom: 15, left: 15, right: 15 })
.padding({ left: 10, right: 10 })
.backgroundColor("#F3E5F5")
.border({ width: 1, color: "#512DA8" })
.onChange((value: string) => {
this.educationValue = value;
})
// 工作经验输入
Text("💼 工作经验 (0-100)")
.fontSize(14)
.fontColor("#333333")
.margin({ left: 15 })
TextInput({
placeholder: "例如: 88",
text: this.experienceValue
})
.width("90%")
.height(45)
.margin({ top: 8, bottom: 15, left: 15, right: 15 })
.padding({ left: 10, right: 10 })
.backgroundColor("#F3E5F5")
.border({ width: 1, color: "#512DA8" })
.onChange((value: string) => {
this.experienceValue = value;
})
// 技能水平输入
Text("⚡ 技能水平 (0-100)")
.fontSize(14)
.fontColor("#333333")
.margin({ left: 15 })
TextInput({
placeholder: "例如: 82",
text: this.skillsValue
})
.width("90%")
.height(45)
.margin({ top: 8, bottom: 15, left: 15, right: 15 })
.padding({ left: 10, right: 10 })
.backgroundColor("#F3E5F5")
.border({ width: 1, color: "#512DA8" })
.onChange((value: string) => {
this.skillsValue = value;
})
// 沟通能力输入
Text("💬 沟通能力 (0-100)")
.fontSize(14)
.fontColor("#333333")
.margin({ left: 15 })
TextInput({
placeholder: "例如: 90",
text: this.communicationValue
})
.width("90%")
.height(45)
.margin({ top: 8, bottom: 15, left: 15, right: 15 })
.padding({ left: 10, right: 10 })
.backgroundColor("#F3E5F5")
.border({ width: 1, color: "#512DA8" })
.onChange((value: string) => {
this.communicationValue = value;
})
// 文化适配输入
Text("🎭 文化适配 (0-100)")
.fontSize(14)
.fontColor("#333333")
.margin({ left: 15 })
TextInput({
placeholder: "例如: 86",
text: this.cultureFitValue
})
.width("90%")
.height(45)
.margin({ top: 8, bottom: 15, left: 15, right: 15 })
.padding({ left: 10, right: 10 })
.backgroundColor("#F3E5F5")
.border({ width: 1, color: "#512DA8" })
.onChange((value: string) => {
this.cultureFitValue = value;
})
// 按钮区域
Row() {
Button("👤 评估候选人")
.width("45%")
.height(45)
.backgroundColor("#512DA8")
.fontColor(Color.White)
.fontSize(16)
.fontWeight(FontWeight.Bold)
.onClick(() => {
this.isLoading = true;
setTimeout(() => {
const input = `${this.educationValue} ${this.experienceValue} ${this.skillsValue} ${this.communicationValue} ${this.cultureFitValue}`;
this.evaluationResult = recruitmentEvaluator(input);
this.isLoading = false;
}, 300);
})
Blank()
Button("🔄 重置")
.width("45%")
.height(45)
.backgroundColor("#9C27B0")
.fontColor(Color.White)
.fontSize(16)
.fontWeight(FontWeight.Bold)
.onClick(() => {
this.educationValue = "85";
this.experienceValue = "88";
this.skillsValue = "82";
this.communicationValue = "90";
this.cultureFitValue = "86";
this.evaluationResult = "";
this.isLoading = false;
})
}
.width("90%")
.margin({ top: 10, bottom: 20, left: 15, right: 15 })
.justifyContent(FlexAlign.SpaceBetween)
// 加载指示器
if (this.isLoading) {
Row() {
LoadingProgress()
.width(40)
.height(40)
.color("#512DA8")
Text(" 正在评估中...")
.fontSize(14)
.fontColor("#666666")
}
.width("90%")
.height(50)
.margin({ bottom: 15, left: 15, right: 15 })
.justifyContent(FlexAlign.Center)
.backgroundColor("#F3E5F5")
.borderRadius(8)
}
// 结果显示区域
if (this.evaluationResult.length > 0) {
Column() {
Text("📋 评估结果")
.fontSize(16)
.fontWeight(FontWeight.Bold)
.fontColor("#512DA8")
.margin({ bottom: 10 })
Text(this.evaluationResult)
.width("100%")
.fontSize(12)
.fontFamily("monospace")
.fontColor("#333333")
.padding(10)
.backgroundColor("#FAFAFA")
.border({ width: 1, color: "#E0E0E0" })
.borderRadius(8)
}
.width("90%")
.margin({ top: 20, bottom: 30, left: 15, right: 15 })
.padding(15)
.backgroundColor("#F3E5F5")
.borderRadius(8)
.border({ width: 1, color: "#512DA8" })
}
}
.width("100%")
}
.layoutWeight(1)
.backgroundColor("#FFFFFF")
}
.width("100%")
.height("100%")
.backgroundColor("#F5F5F5")
}
}
ArkTS 代码说明
这段 ArkTS 代码实现了完整的用户界面和交互逻辑。关键点包括:
导入函数:从编译后的 JavaScript 模块中导入 recruitmentEvaluator 函数。
状态管理:使用 @State 装饰器管理八个状态:五个输入值、评估结果和加载状态。
UI 布局:包含顶部栏、五个输入框、评估和重置按钮、加载指示器和结果显示区域。
交互逻辑:用户输入各项评分后,点击评估按钮。应用会调用 Kotlin 函数进行评估,显示加载动画,最后展示详细的评估结果。
样式设计:使用蓝紫色主题,与人力资源和招聘相关的主题相符。所有输入框、按钮和结果显示区域都有相应的样式设置。
数据输入与交互体验
输入数据格式规范
为了确保工具能够正确处理用户输入,用户应该遵循以下规范:
- 教育背景:整数或浮点数,范围 0-100。
- 工作经验:整数或浮点数,范围 0-100。
- 技能水平:整数或浮点数,范围 0-100。
- 沟通能力:整数或浮点数,范围 0-100。
- 文化适配:整数或浮点数,范围 0-100。
- 分隔符:使用空格分隔各个参数。
示例输入
- 强烈推荐:
90 92 88 95 91 - 推荐:
85 88 82 90 86 - 可考虑:
75 78 72 80 76 - 需谨慎:
65 68 62 70 66 - 不推荐:
45 48 42 50 46
交互流程
- 用户打开应用,看到输入框和默认数据
- 用户输入五项评分指标
- 点击"评估候选人"按钮,应用调用 Kotlin 函数进行评估
- 应用显示加载动画,表示正在处理
- 评估完成后,显示详细的评估结果,包括综合评分、招聘等级、各项评分、评估建议等
- 用户可以点击"重置"按钮清空数据,重新开始
编译与自动复制流程
编译步骤
-
编译 Kotlin 代码:
./gradlew build -
生成 JavaScript 文件:
编译过程会自动生成hellokjs.d.ts和hellokjs.js文件。 -
复制到 ArkTS 项目:
使用提供的脚本自动复制生成的文件到 ArkTS 项目的 pages 目录:./build-and-copy.bat
文件结构
编译完成后,项目结构如下:
kmp_openharmony/
├── src/
│ └── jsMain/
│ └── kotlin/
│ └── App.kt (包含 recruitmentEvaluator 函数)
├── build/
│ └── js/
│ └── packages/
│ └── hellokjs/
│ ├── hellokjs.d.ts
│ └── hellokjs.js
└── kmp_ceshiapp/
└── entry/
└── src/
└── main/
└── ets/
└── pages/
├── hellokjs.d.ts (复制后)
├── hellokjs.js (复制后)
└── Index.ets (ArkTS 页面)
总结
这个案例展示了如何使用 Kotlin Multiplatform 技术实现一个跨端的员工招聘评估工具。通过将核心逻辑写在 Kotlin 中,然后编译为 JavaScript,最后在 ArkTS 中调用,我们实现了代码的一次编写、多端复用。
核心优势
- 代码复用:Kotlin 代码可以在 JVM、JavaScript 和其他平台上运行,避免重复开发。
- 类型安全:Kotlin 的类型系统确保了代码的安全性和可维护性。
- 性能优化:Kotlin 编译为 JavaScript 后,性能与手写 JavaScript 相当。
- 易于维护:集中管理业务逻辑,使得维护和更新变得更加容易。
- 用户体验:通过 ArkTS 提供的丰富 UI 组件,可以创建美观、易用的用户界面。
扩展方向
- 数据持久化:将评估数据保存到本地存储或云端。
- 数据可视化:使用图表库展示各项指标的分布。
- 多候选人管理:支持多个候选人的评估和对比。
- 评估报告:生成详细的招聘评估报告。
- 集成 HR 系统:与人力资源管理系统集成。
- AI 分析:使用机器学习进行候选人匹配和预测。
- 团队协作:支持招聘团队的协作和讨论。
- 评分模板:支持不同岗位的自定义评分权重。
通过这个案例,开发者可以学到如何在 KMP 项目中实现复杂的招聘评估逻辑,以及如何在 OpenHarmony 平台上构建高效的跨端应用。这个招聘评估工具可以作为人力资源管理平台、招聘系统或决策支持工具的核心模块。
欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net
更多推荐


所有评论(0)