前言
前面的迭代中,我们已经完成备忘录基础能力:登录注册、ACL数据隔离、笔记增删改查、云端搜索、一键复制剪贴板。 基础功能稳定之后,我们继续增加AIGC能力,让备忘录不再只做文本存储,还可以智能处理已有笔记内容。
功能需求

- 选中一条已存在的笔记,提供两个操作:AI摘要 / AI润色
- 操作前校验用户权限,只能处理自己创建的笔记,防止越权访问
- AI生成的内容新建一条笔记存入Bmob,不覆盖原始笔记,方便对比回溯
- 完善异常捕获:笔记不存在、无权限、AI接口超时、接口返回异常都做兜底
整体业务流程
- 用户前端选中笔记,点击【AI摘要】或【AI润色】按钮
- 前端向后端传入笔记ID、操作类型
- 后端调用鉴权函数,校验笔记归属
- 鉴权通过,读取笔记标题和内容,组装提示词请求AI接口
- 解析AI返回结果,提取新标题、新正文
- 将AI结果作为全新笔记写入Bmob后端云,绑定当前登录用户
- 返回结果给前端,刷新笔记列表展示
核心Python代码
python
import requests
def ai_note_process(note_id: str, current_user, op_type: str):
"""
笔记AI处理:summary摘要 / polish润色
:param note_id: 目标笔记ID
:param current_user: 当前登录用户对象
:param op_type: 操作类型
:return: dict,统一返回ok、msg、data
"""
# 权限校验,判断笔记是否存在+归属用户
auth_res = get_note_with_auth(note_id, current_user)
if not auth_res["ok"]:
return auth_res
note_info = auth_res["data"]
origin_title = note_info["title"]
origin_content = note_info["content"]
# 配置两种场景的系统提示词
prompt_config = {
"summary": {
"system": "你是文本摘要助手,提炼笔记核心信息精简总结,输出严格按照格式:\n标题:xxx\n内容:xxx"
},
"polish": {
"system": "你是文本润色助手,优化语句通顺度,保留全部核心信息,输出严格按照格式:\n标题:xxx\n内容:xxx"
}
}
if op_type not in prompt_config:
return {"ok": False, "msg": "非法操作类型"}
ai_api_url = "替换你的AI接口地址"
api_key = "替换你的API Key"
payload = {
"messages": [
{"role": "system", "content": prompt_config[op_type]["system"]},
{"role": "user", "content": f"原笔记标题:{origin_title}\n原笔记内容:{origin_content}"}
],
"temperature": 0.6
}
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
try:
resp = requests.post(ai_api_url, json=payload, headers=headers, timeout=30)
resp.raise_for_status()
ai_result = resp.json()
ai_output = ai_result["choices"][0]["message"]["content"]
except Exception as e:
return {"ok": False, "msg": "AI接口调用异常", "error": str(e)}
# 解析AI返回文本
new_title = ai_output.split("标题:")[1].split("内容:")[0].strip()
new_content = ai_output.split("内容:")[1].strip()
# 在Bmob新建笔记,不覆盖原始笔记
new_note_data = {
"title": new_title,
"content": new_content,
"user": current_user
}
new_note = BmobQuery("Note").create(new_note_data)
return {
"ok": True,
"msg": "AI处理完成,已生成新笔记",
"data": {
"new_note": new_note,
"new_title": new_title,
"new_content": new_content
}
}
配套鉴权函数
python
def get_note_with_auth(note_id, current_user):
note = BmobQuery("Note").get_by_id(note_id)
if not note:
return {"ok": False, "msg": "笔记不存在"}
if note["user"]["objectId"] != current_user["objectId"]:
return {"ok": False, "msg": "无权限访问该笔记"}
return {"ok": True, "data": note}
前端JS调用代码
javascript
async function handleAiNoteOperate(noteId, opType) {
if (!noteId) return;
const res = await fetch("/api/ai-note-process", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ noteId, opType })
})
const result = await res.json();
if (result.ok) {
showToast("AI处理完成");
refreshNoteList();
} else {
showToast("操作失败:" + result.msg);
}
}
关键设计思考
- 不覆盖原文:AI处理结果新建笔记,用户随时对比原文,不会丢失数据。
- 前置鉴权:AI请求之前先校验权限,避免无效AI调用,节省接口费用,保障ACL数据隔离。
- Bmob优势:不用自己部署后端服务器、不用写数据库CRUD,直接使用Bmob后端云,快速落地项目。
结尾
这个功能把基础备忘录升级成带AIGC能力的应用。项目包含权限校验、AI接口对接、异常处理,放到简历里,比单纯CRUD项目更有竞争力。