product_management/wwwroot/api/product_purchase.dspy
yumoqing 4fd136bf53 refactor: reseller org_id isolation for product_management module
- product_category: org_id scoped tree, product_table_name -> product_type
- product: org_id scoped, added extra_json for custom attributes, product_type field
- product_type_config: org_id + operator_id dual isolation, unique key on (org_id, operator_id, category_id, config_name)
- All 18 API endpoints enforce org_id filtering via ServerEnv
- core.py: all methods accept optional org_id, default to current user's org
- CRUD definitions: logined_userorgid set to org_id on all lists
- init/data.json: removed hardcoded global categories (managed per reseller)
- Rebuilt mysql.ddl.sql and all CRUD UI files
2026-05-25 17:03:08 +08:00

85 lines
2.8 KiB
Python

#!/usr/bin/env python3
"""
产品购买标准化接口 (按机构隔离)
参数:
product_id: 产品ID
quantity: 购买数量 (默认1)
purchase_data: 购买附加数据 (JSON字符串)
org_id: 机构ID (可选)
返回:
{success, order_id, message}
"""
import json, time
from appPublic.uniqueID import getID
result = {'success': False, 'order_id': '', 'message': ''}
try:
user_id = await get_user()
from ahserver.serverenv import ServerEnv
env = ServerEnv()
org_id = params_kw.get('org_id', None) or getattr(env, 'orgid', None) or getattr(env, 'org_id', '0')
now = time.strftime('%Y-%m-%d %H:%M:%S')
product_id = params_kw.get('product_id', '')
quantity = int(params_kw.get('quantity', '1'))
purchase_data = params_kw.get('purchase_data', '{}')
if not product_id:
result['message'] = '缺少product_id参数'
return json.dumps(result, ensure_ascii=False)
dbname = get_module_dbname('product_management')
sql = """SELECT * FROM product WHERE id = ${product_id}$ AND status = '1' AND org_id = ${org_id}$"""
async with DBPools().sqlorContext(dbname) as sor:
rows = await sor.sqlExe(sql, {'product_id': product_id, 'org_id': org_id})
if not rows:
result['message'] = '产品不存在或已禁用'
return json.dumps(result, ensure_ascii=False)
product = dict(rows[0])
import datetime
today = datetime.date.today().isoformat()
enabled = str(product.get('enabled_date', '') or '')
expired = str(product.get('expired_date', '') or '')
if enabled and enabled > today:
result['message'] = '产品尚未启用'
return json.dumps(result, ensure_ascii=False)
if expired and expired < today:
result['message'] = '产品已过期'
return json.dumps(result, ensure_ascii=False)
order_id = getID()
order_data = {
'id': order_id,
'product_id': product_id,
'product_code': product.get('product_code', ''),
'product_name': product.get('product_name', ''),
'buyer_id': user_id,
'buyer_org_id': org_id,
'quantity': quantity,
'unit_price': float(product.get('price', 0)),
'total_price': float(product.get('price', 0)) * quantity,
'currency': product.get('currency', 'CNY'),
'purchase_data': purchase_data,
'status': 'pending',
'created_at': now,
'updated_at': now
}
try:
await sor.C('purchase_orders', order_data)
except Exception:
pass
result['success'] = True
result['order_id'] = order_id
result['message'] = '购买请求已提交'
except Exception as e:
result['message'] = '购买失败: ' + str(e)
return json.dumps(result, ensure_ascii=False)