diff --git a/xian_favor/api.py b/xian_favor/api.py index 4ef9561..e3959d9 100644 --- a/xian_favor/api.py +++ b/xian_favor/api.py @@ -134,6 +134,71 @@ def reopen_item(item_id): return jsonify({'success': True, 'data': item}) +@app.route('/api/items//convert', methods=['POST']) +def convert_item(item_id): + """将收藏转换为待办""" + item = db.get_item(item_id) + if not item: + return jsonify({'success': False, 'error': '条目不存在'}), 404 + + # 不能转换已经是待办的 + if item['type'] == 'todo': + return jsonify({'success': False, 'error': '已经是待办事项'}), 400 + + data = request.get_json() or {} + mode = data.get('mode', 'convert') # convert 或 copy + + if mode not in ['convert', 'copy']: + return jsonify({'success': False, 'error': '无效转换模式'}), 400 + + # 构建待办数据 + todo_data = { + 'type': 'todo', + 'title': data.get('title') or item['title'] or f'{item["type"]}收藏', + 'status': data.get('status', 'pending'), + 'priority': data.get('priority', 'medium'), + 'due_date': data.get('due_date'), + 'tags': item['tags'], # 继承原标签 + } + + # 根据原类型处理内容 + if item['type'] == 'text': + # 文本:content 放到 note + todo_data['note'] = item['content'] or '' + if item['note']: + todo_data['note'] += '\n\n' + item['note'] + elif item['type'] == 'link': + # 链接:url 放到 note,保留链接可点击 + todo_data['note'] = f'链接: {item["url"]}\n\n' + if item['content']: + todo_data['note'] += item['content'] + '\n\n' + if item['note']: + todo_data['note'] += item['note'] + # url 字段也保留(方便后续操作) + todo_data['content'] = item['url'] + elif item['type'] == 'column': + # 专栏:url + source 放到 note + todo_data['note'] = f'专栏: {item["url"]}\n' + if item['source']: + todo_data['note'] += f'来源: {item["source"]}\n\n' + if item['content']: + todo_data['note'] += item['content'] + '\n\n' + if item['note']: + todo_data['note'] += item['note'] + todo_data['content'] = item['url'] + + if mode == 'convert': + # 直接转换:更新原条目 + db.update_item(item_id, **todo_data) + result = db.get_item(item_id) + return jsonify({'success': True, 'data': result, 'mode': 'convert'}) + else: + # 复制创建:新建待办,原条目保留 + new_id = db.create_item(**todo_data) + result = db.get_item(new_id) + return jsonify({'success': True, 'data': result, 'mode': 'copy', 'original_id': item_id}) + + @app.route('/api/tags', methods=['GET']) def list_tags(): """列出标签""" @@ -691,6 +756,9 @@ INDEX_TEMPLATE = ''' + + +