From 64bfb46b9878c79e2d45c96a00524fe82f397fec Mon Sep 17 00:00:00 2001 From: jingrow Date: Thu, 31 Jul 2025 04:34:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=AD=E8=B4=B9=E6=88=96=E5=8D=87=E7=BA=A7?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E5=A5=97=E9=A4=90=E8=AE=A1=E5=88=92?= =?UTF-8?q?=E6=97=B6=E6=94=B9=E4=B8=BA=E6=A0=B9=E6=8D=AE=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E8=AE=A1=E5=88=92=E5=AE=9E=E6=97=B6=E6=9F=A5=E8=AF=A2=E6=9C=80?= =?UTF-8?q?=E6=96=B0=E7=9A=84=E5=A5=97=E9=A4=90=E4=BB=B7=E6=A0=BC=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E5=BD=93=E5=89=8D=E8=AE=A1=E5=88=92=E7=9A=84=E4=BB=B7?= =?UTF-8?q?=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/JsiteServerRenewalDialog.vue | 25 +++- jcloud/api/aliyun_server_light.py | 109 +++++++++++++++--- 2 files changed, 114 insertions(+), 20 deletions(-) diff --git a/dashboard/src2/components/JsiteServerRenewalDialog.vue b/dashboard/src2/components/JsiteServerRenewalDialog.vue index 3fd939e..a61a876 100644 --- a/dashboard/src2/components/JsiteServerRenewalDialog.vue +++ b/dashboard/src2/components/JsiteServerRenewalDialog.vue @@ -249,6 +249,7 @@ export default { paymentQrCode: null, paymentQrCodeImage: null, checkInterval: null, + realPlanPrice: null, // 实时套餐价格 renewalPeriods: [ { months: 1, name: '1个月', discount: 0 }, { months: 3, name: '3个月', discount: 0 }, @@ -264,7 +265,7 @@ export default { if (!this.serverDoc) return null; return { - plan_price: this.serverDoc.plan_price || 0, + plan_price: this.realPlanPrice || 0, instance_id: this.serverDoc.instance_id, region: this.serverDoc.region }; @@ -415,6 +416,28 @@ export default { } } }; + }, + getRealPlanPrice() { + return { + url: 'jcloud.api.aliyun_server_light.get_server_plan_price', + params: { + instance_id: this.serverDoc?.instance_id + }, + onSuccess(data) { + if (data.success) { + this.realPlanPrice = data.plan_price; + } + }, + onError(error) { + console.warn('获取实时套餐价格失败:', error.message); + } + }; + } + }, + async mounted() { + // 获取实时套餐价格 + if (this.serverDoc?.instance_id) { + await this.$resources.getRealPlanPrice.submit(); } }, methods: { diff --git a/jcloud/api/aliyun_server_light.py b/jcloud/api/aliyun_server_light.py index a487a75..f384fe5 100644 --- a/jcloud/api/aliyun_server_light.py +++ b/jcloud/api/aliyun_server_light.py @@ -810,9 +810,22 @@ def get_aliyun_instance_upgrade_plans(instance_id): # 过滤套餐:只返回PlanType值一致的套餐,且价格大于等于当前套餐价格 if result.get('data') and 'plans' in result['data']: - filtered_plans = [] - current_plan_price = server.plan_price or 0 + # 先统一调整所有套餐价格 + for plan in result['data']['plans']: + if 'origin_price' in plan and plan['origin_price'] is not None: + original_price = float(plan['origin_price']) + plan['origin_price'] = int(original_price / (1 - 0.2)) # 确保20%利润率且价格为整数 + # 获取当前套餐价格 + current_plan_price = 0 + if server.planid: + for plan in result['data']['plans']: + if plan.get('plan_id') == server.planid: + current_plan_price = plan['origin_price'] + break + + # 过滤套餐 + filtered_plans = [] for plan in result['data']['plans']: # 检查plan_type是否与服务器的PlanType一致 if plan.get('plan_type') == server.plan_type: @@ -821,15 +834,9 @@ def get_aliyun_instance_upgrade_plans(instance_id): try: support_platform_list = json.loads(support_platform_str) if support_platform_str else [] if server.os_type and server.os_type in support_platform_list: - # 在origin_price上统一增加20%作为利润 - if 'origin_price' in plan and plan['origin_price'] is not None: - original_price = float(plan['origin_price']) - adjusted_price = int(original_price / (1 - 0.2)) # 确保20%利润率且价格为整数 - plan['origin_price'] = adjusted_price - - # 只显示价格大于等于当前套餐价格的套餐 - if adjusted_price >= current_plan_price: - filtered_plans.append(plan) + # 只显示价格大于等于当前套餐价格的套餐 + if plan['origin_price'] > current_plan_price: + filtered_plans.append(plan) except: continue @@ -994,15 +1001,27 @@ def create_server_upgrade_order(**kwargs): if current_team.name != team: jingrow.throw("您没有权限为此服务器创建升级订单") - # 获取当前套餐和新套餐信息 - current_plan_price = server_pg.plan_price or 0 + # 获取套餐列表 + plans_response = get_aliyun_plans(server_pg.region) + if not plans_response or not plans_response.get('success'): + jingrow.throw("获取套餐列表失败") + + plans = plans_response['data'].get('plans', []) + + # 获取当前套餐价格 + current_plan_price = 0 + if server_pg.planid: + for plan in plans: + if plan.get('plan_id') == server_pg.planid: + current_plan_price = float(plan.get('origin_price', 0)) + break # 获取新套餐价格 - plans_response = get_aliyun_plans(server_pg.region) new_plan = None - if plans_response and plans_response.get('success'): - plans = plans_response['data'].get('plans', []) - new_plan = next((p for p in plans if p.get('plan_id') == new_plan_id), None) + for plan in plans: + if plan.get('plan_id') == new_plan_id: + new_plan = plan + break if not new_plan: jingrow.throw("新套餐不存在") @@ -1327,9 +1346,12 @@ def update_server_record(instance_ids): server.disk_size = resource_spec.get('disk_size') server.bandwidth = resource_spec.get('bandwidth') - # 更新套餐价格信息 + # 更新套餐信息 plan_id = instance_info.get('plan_id') if plan_id: + # 更新planid + server.planid = plan_id + # 获取套餐信息来计算价格 plans_result = get_aliyun_plans(region_id) if plans_result and plans_result.get('success') and plans_result.get('data', {}).get('plans'): @@ -1353,7 +1375,8 @@ def update_server_record(instance_ids): "status": server.status, "system": server.system, "os_type": server.os_type, - "plan_price": server.plan_price + "plan_price": server.plan_price, + "planid": server.planid } } @@ -1377,6 +1400,54 @@ def reset_server_key_pair(instance_id): jingrow.log_error("重置密钥对失败", f"重置实例 {instance_id} 密钥对时发生错误: {str(e)}") return {"success": False, "message": str(e)} +@jingrow.whitelist() +def get_server_plan_price(instance_id): + """根据实例ID获取当前套餐的实时价格""" + try: + # 获取服务器记录 + server = jingrow.get_pg("Jsite Server", {"instance_id": instance_id}) + if not server: + return {"success": False, "message": "找不到服务器记录"} + + if not server.planid: + return {"success": False, "message": "服务器记录缺少套餐信息"} + + # 获取套餐列表(使用get_aliyun_plans确保价格调整一致) + plans_result = get_aliyun_plans(server.region) + + if not plans_result or not plans_result.get('success'): + return {"success": False, "message": "获取套餐列表失败"} + + # 查找当前套餐 + plans = plans_result.get('data', {}).get('plans', []) + current_plan = None + for plan in plans: + if plan.get('plan_id') == server.planid: + current_plan = plan + break + + if not current_plan: + return {"success": False, "message": "找不到对应的套餐信息"} + + # 使用已经调整过的价格 + current_plan_price = float(current_plan.get('origin_price', 0)) + + return { + "success": True, + "plan_price": current_plan_price, + "plan_info": { + "plan_id": current_plan.get('plan_id'), + "cpu": current_plan.get('core'), + "memory": current_plan.get('memory'), + "disk_size": current_plan.get('disk_size'), + "bandwidth": current_plan.get('bandwidth') + } + } + + except Exception as e: + jingrow.log_error("获取套餐价格失败", f"获取实例 {instance_id} 套餐价格时发生错误: {str(e)}") + return {"success": False, "error": str(e), "message": "获取套餐价格失败"} + @jingrow.whitelist() def get_password(pagetype, name, fieldname):