diff --git a/discount/init.py b/discount/init.py index 19aa2a2..e069fe3 100644 --- a/discount/init.py +++ b/discount/init.py @@ -535,6 +535,52 @@ async def disable_promo_code(request, params_kw): return {'status': 'success'} +async def generate_promo_qr(request, params_kw): + """Generate QR code for a promo code.""" + env = ServerEnv() + dbname = env.get_module_dbname('discount') + promo_id = params_kw.get('id', '') + + if not promo_id: + return {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Missing promo code id', 'type': 'error'}} + + from appPublic.qr import gen_qr_withlogo + from appPublic.jsonConfig import getConfig + from ahserver.filestorage import FileStorage + from appPublic.uniqueID import getID as _getID + + async with DBPools().sqlorContext(dbname) as sor: + recs = await sor.R('discount_promo_code', {'id': promo_id}) + if not recs: + return {'widgettype': 'Message', 'options': {'title': 'Error', 'message': f'Promo code {promo_id} not found', 'type': 'error'}} + + config = getConfig() + qr_url = env.entire_url('./promote') + f'?code={promo_id}' + fs = FileStorage() + p = fs._name2path(f'{_getID()}.png') + gen_qr_withlogo(qr_url, p, logopath=config.logopath, logoloc='cc') + webp = fs.webpath(p) + qr_file_url = env.entire_url('/idfile') + f'?path={webp}' + + return { + "widgettype": "VBox", + "options": {"width": "100%", "height": "100%", "spacing": 10}, + "subwidgets": [ + {"widgettype": "Image", "options": {"width": "340px", "height": "340px", "url": qr_file_url}}, + {"widgettype": "HBox", "options": {"width": "100%", "cheight": 2}, + "subwidgets": [ + {"widgettype": "Text", "options": {"otext": "促销码:", "i18n": True, "cwidth": 5}}, + {"widgettype": "Text", "options": {"text": promo_id}} + ]}, + {"widgettype": "HBox", "options": {"width": "100%", "cheight": 2}, + "subwidgets": [ + {"widgettype": "Text", "options": {"otext": "链接:", "i18n": True, "cwidth": 5}}, + {"widgettype": "Text", "options": {"text": qr_url, "css": "text-muted"}} + ]} + ] + } + + async def bind_customer(request, params_kw): """Bind customer to reseller+sale via promo code (called during registration/login). First-time binding only. Update discounts if new ones are lower. @@ -727,6 +773,7 @@ def load_discount(): env.list_marketings = list_marketings env.generate_promo_code = generate_promo_code env.disable_promo_code = disable_promo_code + env.generate_promo_qr = generate_promo_qr env.bind_customer = bind_customer env.get_customer_bind = get_customer_bind env.assign_customer_to_sale = assign_customer_to_sale diff --git a/wwwroot/api/promo_code_qr.dspy b/wwwroot/api/promo_code_qr.dspy index e6c5f2d..9745de1 100644 --- a/wwwroot/api/promo_code_qr.dspy +++ b/wwwroot/api/promo_code_qr.dspy @@ -1,76 +1,3 @@ # 为选中的促销码生成二维码 -from ahserver.serverenv import ServerEnv -from appPublic.qr import gen_qr_withlogo -from appPublic.jsonConfig import getConfig -from ahserver.filestorage import FileStorage -from sqlor.dbpools import DBPools -from appPublic.uniqueID import getID - -result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid', 'type': 'error'}} - -try: - env = ServerEnv() - dbname = env.get_module_dbname('discount') - promo_id = params_kw.get('id', '') - - if not promo_id: - raise Exception('Missing promo code id') - - async with DBPools().sqlorContext(dbname) as sor: - recs = await sor.R('discount_promo_code', {'id': promo_id}) - if not recs: - raise Exception(f'Promo code {promo_id} not found') - - # QR code URL: 扫码后进入促销绑定页面 - config = getConfig() - qr_url = env.entire_url('./promote') + f'?code={promo_id}' - fs = FileStorage() - p = fs._name2path(f'{getID()}.png') - gen_qr_withlogo(qr_url, p, logopath=config.logopath, logoloc='cc') - webp = fs.webpath(p) - qr_file_url = env.entire_url('/idfile') + f'?path={webp}' - - result = { - "widgettype": "VBox", - "options": {"width": "100%", "height": "100%", "spacing": 10}, - "subwidgets": [ - { - "widgettype": "Image", - "options": { - "width": "340px", - "height": "340px", - "url": qr_file_url - } - }, - { - "widgettype": "HBox", - "options": {"width": "100%", "cheight": 2}, - "subwidgets": [ - { - "widgettype": "Text", - "options": {"otext": "促销码:", "i18n": True, "cwidth": 5} - }, - { - "widgettype": "Text", - "options": {"text": promo_id} - } - ] - }, - { - "widgettype": "HBox", - "options": {"width": "100%", "cheight": 2}, - "subwidgets": [ - { - "widgettype": "Text", - "options": {"otext": "链接:", "i18n": True, "cwidth": 5} - }, - { - "widgettype": "Text", - "options": {"text": qr_url, "css": "text-muted"} - } - ] - } - ] - } -except Exception as e: - result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': str(e), 'type': 'error'}} +result = await generate_promo_qr(request, params_kw) +return result