# 促销码/折扣二维码 客户端入口 # 参数: code=促销码id 或 id=discount_qr id # 已登录: 直接绑定; 未登录: 提示登录/注册 from ahserver.serverenv import ServerEnv code = params_kw.get('code', '') qr_id = params_kw.get('id', '') env = ServerEnv() user = await env.get_user() if not user: # 未登录 - 显示登录/注册引导 back_url = '' if code: back_url = env.entire_url('./promote') + f'?code={code}' elif qr_id: back_url = env.entire_url('./promote') + f'?id={qr_id}' result = { "widgettype": "VBox", "options": {"width": "100%", "height": "100%", "spacing": 16, "padding": "20px"}, "subwidgets": [ { "widgettype": "Title", "options": {"text": "请先登录或注册", "level": 2, "i18n": True} }, { "widgettype": "Text", "options": {"otext": "使用促销码需要先登录,登录后将自动为您激活。", "i18n": True} }, { "widgettype": "HBox", "options": {"spacing": 10, "cheight": 3}, "subwidgets": [ { "widgettype": "Button", "options": { "label": "登录", "css": "primary", "actiontype": "link", "url": env.entire_url('/rbac/user/login.ui') + f"?back={back_url}" if back_url else env.entire_url('/rbac/user/login.ui') } }, { "widgettype": "Button", "options": { "label": "注册", "css": "secondary", "actiontype": "link", "url": env.entire_url('/rbac/user/register') + f"?back={back_url}" if back_url else env.entire_url('/rbac/user/register') } } ] } ] } elif code: # 已登录 + 促销码参数 - 绑定促销码 try: # bind_customer expects promo_code_id bind_params = params_kw.copy() bind_params['promo_code_id'] = code ret = await bind_customer(request, bind_params) home_url = env.entire_url('/') if ret.get('status') == 'success': result = { "widgettype": "VBox", "options": {"width": "100%", "height": "100%", "spacing": 16, "padding": "20px"}, "subwidgets": [ { "widgettype": "Title", "options": {"otext": "激活成功", "level": 2, "i18n": True} }, { "widgettype": "Text", "options": {"text": f"促销码 {code} 已成功激活,即将跳转首页..."} }, { "widgettype": "Button", "options": {"label": "返回首页", "css": "primary"}, "binds": [{ "wid": "self", "event": "click", "actiontype": "script", "target": "self", "script": f"window.location.href='{home_url}';" }] } ] } else: result = { "widgettype": "Message", "options": {"title": "提示", "message": ret.get('message', '绑定失败'), "type": "warning"} } except Exception as e: result = { "widgettype": "Message", "options": {"title": "激活失败", "message": str(e), "type": "error"} } else: # 已登录 + 折扣二维码参数 - 走旧流程 ret = await set_promote_discount(request, params_kw) result = { "widgettype": "Message", "options": {"title": "成功", "message": f"获得{ret}折扣", "type": "success"} }