try: user_id = await get_user() post_id = params_kw.get('post_id', '') db = DBPools() async with db.sqlorContext('sage') as sor: # 检查作品是否存在且为付费KTV posts = await sor.R('showcase_posts', {"id": post_id}) if not posts: return json.dumps({"status": "error", "message": "作品不存在"}, ensure_ascii=False) post = posts[0] price = float(post.get('price', 0)) if price <= 0: return json.dumps({"status": "error", "message": "该作品免费,无需购买"}, ensure_ascii=False) # 检查是否已购买 existing = await sor.sqlExe( "SELECT id FROM showcase_downloads WHERE post_id = ${post_id}$ AND user_id = ${user_id}$ AND payment_status = '1'", {"post_id": post_id, "user_id": user_id} ) if existing: return json.dumps({"status": "ok", "message": "已购买", "download_url": post.get('content_url', '')}, ensure_ascii=False) # 创建购买记录(模拟支付成功) dl_data = { "post_id": post_id, "user_id": user_id, "price": price, "payment_status": "1", "download_url": post.get('content_url', ''), "download_count": 1, "created_at": curDateString() } result = await create_showcase_download(dl_data) return json.dumps({"status": "ok", "message": "购买成功", "download_url": post.get('content_url', '')}, ensure_ascii=False) except Exception as e: return json.dumps({"status": "error", "message": str(e)}, ensure_ascii=False)