From f5c808c976b26ed947fd098ea31615f29b9fff53 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Wed, 1 Jul 2026 15:26:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A4=8D=E5=88=B6API=E5=8E=BB=E6=8E=89?= =?UTF-8?q?=E9=80=89=E4=B8=AD=E8=A1=8C=E8=A6=81=E6=B1=82,=E6=BA=90?= =?UTF-8?q?=E5=92=8C=E7=9B=AE=E6=A0=87=E9=83=BD=E6=94=B9=E4=B8=BA=E4=B8=8B?= =?UTF-8?q?=E6=8B=89=E9=80=89=E6=8B=A9;=20dspy=E5=8E=BBjson.dumps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- json/upapp.json | 7 ---- wwwroot/api/copy_uapi.dspy | 74 +++++++++++++++++++------------------- wwwroot/copy_uapi.ui | 4 +-- 3 files changed, 38 insertions(+), 47 deletions(-) diff --git a/json/upapp.json b/json/upapp.json index aa0942e..ea10cf6 100644 --- a/json/upapp.json +++ b/json/upapp.json @@ -14,7 +14,6 @@ "selected_data":true }, { - "selected_row":true, "name":"copy_api", "label":"复制API" } @@ -41,12 +40,6 @@ "height":"auto", "width":"500px" }, - "params_mapping":{ - "mapping":{ - "id":"upappid" - }, - "need_other":false - }, "options":{ "method":"GET", "params":{}, diff --git a/wwwroot/api/copy_uapi.dspy b/wwwroot/api/copy_uapi.dspy index 511489f..a4fadbd 100644 --- a/wwwroot/api/copy_uapi.dspy +++ b/wwwroot/api/copy_uapi.dspy @@ -1,44 +1,37 @@ -ns = params_kw.copy() +result = {'widgettype': 'Error', 'options': {'title': 'Error', 'message': '', 'timeout': 3}} -source_upappid = ns.get('source_upappid', '') -target_upappid = ns.get('target_upappid', '') +try: + source_upappid = params_kw.get('source_upappid', '') + target_upappid = params_kw.get('target_upappid', '') -if not source_upappid: - return json.dumps({'widgettype':'Error','options':{'text':'缺少源上位系统ID'}}, ensure_ascii=False) + if not source_upappid: + raise Exception('请选择源上位系统') + if not target_upappid: + raise Exception('请选择目标上位系统') + if source_upappid == target_upappid: + raise Exception('源和目标上位系统不能相同') -if not target_upappid: - return json.dumps({'widgettype':'Error','options':{'text':'请选择目标上位系统'}}, ensure_ascii=False) + db = DBPools() + dbname = get_module_dbname('uapi') + async with db.sqlorContext(dbname) as sor: + sql = 'select * from uapi where upappid = ${upappid}$' + source_uapis = await sor.sqlExe(sql, {'upappid': source_upappid}) -if source_upappid == target_upappid: - return json.dumps({'widgettype':'Error','options':{'text':'源和目标上位系统不能相同'}}, ensure_ascii=False) + if not source_uapis: + raise Exception('源上位系统中没有API记录') -db = DBPools() -dbname = get_module_dbname('uapi') -async with db.sqlorContext(dbname) as sor: - # 查询源上位系统的所有API - sql = 'select * from uapi where upappid = ${upappid}$' - source_uapis = await sor.sqlExe(sql, {'upappid': source_upappid}) + existing_recs = await sor.sqlExe(sql, {'upappid': target_upappid}) + existing_names = {r['name'] for r in existing_recs} - if not source_uapis: - return json.dumps({'widgettype':'Error','options':{'text':'源上位系统中没有API记录'}}, ensure_ascii=False) + copied = 0 + skipped = 0 + for api in source_uapis: + if api['name'] in existing_names: + skipped += 1 + continue - # 查询目标上位系统已有的API名称 - existing_sql = 'select name from uapi where upappid = ${upappid}$' - existing_recs = await sor.sqlExe(existing_sql, {'upappid': target_upappid}) - existing_names = {r['name'] for r in existing_recs} - - copied = 0 - skipped = 0 - for api in source_uapis: - if api['name'] in existing_names: - skipped += 1 - continue - - new_id = getID() - await sor.sqlExe( - '''insert into uapi (id,upappid,name,title,description,need_auth,stream,path,httpmethod,chunk_match,headers,params,data,response,ioid,callbackurl) - values (${id}$,${upappid}$,${name}$,${title}$,${description}$,${need_auth}$,${stream}$,${path}$,${httpmethod}$,${chunk_match}$,${headers}$,${params}$,${data}$,${response}$,${ioid}$,${callbackurl}$)''', - { + new_id = getID() + await sor.C('uapi', { 'id': new_id, 'upappid': target_upappid, 'name': api.get('name', ''), @@ -55,12 +48,17 @@ async with db.sqlorContext(dbname) as sor: 'response': api.get('response', ''), 'ioid': api.get('ioid', ''), 'callbackurl': api.get('callbackurl', '') - } - ) - copied += 1 + }) + copied += 1 msg = f'成功复制 {copied} 条API记录' if skipped > 0: msg += f',跳过 {skipped} 条(API名称已存在)' - return json.dumps({'widgettype':'Message','options':{'text':msg}}, ensure_ascii=False) + result = {'widgettype': 'Message', 'options': {'title': '复制完成', 'message': msg, 'timeout': 3, 'type': 'success'}} + +except Exception as e: + debug(f'copy_uapi error: {format_exc()}') + result = {'widgettype': 'Error', 'options': {'title': '复制失败', 'message': str(e), 'timeout': 5}} + +return result diff --git a/wwwroot/copy_uapi.ui b/wwwroot/copy_uapi.ui index 233cb3a..e9f3318 100644 --- a/wwwroot/copy_uapi.ui +++ b/wwwroot/copy_uapi.ui @@ -5,14 +5,14 @@ "subwidgets": [ { "widgettype": "Text", - "options": {"text": "选择目标上位系统,将当前上位系统的所有API接口复制到目标系统"} + "options": {"text": "选择源和目标上位系统,将源系统的所有API接口复制到目标系统"} }, { "widgettype": "Form", "options": { "submit_url": "{{entire_url('./api/copy_uapi.dspy')}}", "fields": [ - {"name": "source_upappid", "uitype": "hide", "value": "{{params_kw.get('upappid', '')}}"}, + {"name": "source_upappid", "uitype": "code", "label": "源上位系统", "required": true, "data": {{json.dumps(upapps, ensure_ascii=False)}}}, {"name": "target_upappid", "uitype": "code", "label": "目标上位系统", "required": true, "data": {{json.dumps(upapps, ensure_ascii=False)}}} ] },