57 lines
1.8 KiB
Plaintext
57 lines
1.8 KiB
Plaintext
result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid', 'type': 'error'}}
|
|
|
|
try:
|
|
product_id = params_kw.get('product_id', '')
|
|
if not product_id:
|
|
raise ValueError('缺少产品ID')
|
|
|
|
userid = await get_user()
|
|
if not userid:
|
|
raise ValueError('请先登录')
|
|
|
|
userorgid = await get_userorgid()
|
|
dbname = get_module_dbname('product_management')
|
|
|
|
async with DBPools().sqlorContext(dbname) as sor:
|
|
recs = await sor.sqlExe(
|
|
"SELECT * FROM product WHERE id = ${id}$ AND status = '1'",
|
|
{'id': product_id}
|
|
)
|
|
if not recs:
|
|
raise ValueError('产品不存在或已下架')
|
|
|
|
p = recs[0]
|
|
order_id = getID()
|
|
now = timestampstr()
|
|
order = {
|
|
'id': order_id,
|
|
'product_id': p.id,
|
|
'product_code': p.product_code or '',
|
|
'product_name': p.product_name or '',
|
|
'buyer_id': userid,
|
|
'buyer_org_id': userorgid or '0',
|
|
'quantity': 1,
|
|
'unit_price': float(p.price or 0),
|
|
'total_price': float(p.price or 0),
|
|
'currency': p.currency or 'CNY',
|
|
'status': 'pending',
|
|
'purchase_data': '{}',
|
|
'created_at': now,
|
|
'updated_at': now
|
|
}
|
|
await sor.C('purchase_orders', order)
|
|
|
|
result = {
|
|
'widgettype': 'Message',
|
|
'options': {
|
|
'title': '购买成功',
|
|
'message': f"已提交购买订单: {p.product_name},订单号: {order_id}",
|
|
'type': 'success',
|
|
'timeout': 5
|
|
}
|
|
}
|
|
except Exception as e:
|
|
result['options'] = {'title': '购买失败', 'message': str(e), 'type': 'error', 'timeout': 5}
|
|
|
|
return json.dumps(result, ensure_ascii=False)
|