rbac/wwwroot/user/register.dspy

97 lines
3.0 KiB
Plaintext

debug(f'register.dspy: {params_kw=}')
db = DBPools()
dbname = get_module_dbname('rbac')
# 根据注册域名查找所属分销商
parentid = None
try:
host = request.host
env = request._run_ns
domain_info = await env.get_domain_by_host(request, host)
if domain_info:
parentid = domain_info.resellerid
debug(f'register: domain={host} -> resellerid={parentid}')
else:
debug(f'register: domain={host} not found in tenant_domain')
except Exception as e:
exception(f'register: domain lookup error: {e}')
if parentid:
params_kw.parentid = parentid
async with db.sqlorContext(dbname) as sor:
data = await register_user(sor, params_kw)
data = DictObject(**data)
if data.status == 'error':
debug(f"register error: {data.data.message}")
return {
"widgettype": "Error",
"options": {
"timeout": 5,
"title": "注册失败",
"message": data.data.message
}
}
user = data.data.user
orgid = user.orgid
try:
await openCustomerAccounts(sor, '0', orgid)
debug(f'{orgid} accounts opened')
except Exception as e:
exception(f'{e},{orgid=}')
# 检查是否有促销码,如果有则绑定客户归属关系
promo_code_id = params_kw.get('promo_code_id')
if promo_code_id:
try:
await bind_customer(request, DictObject(promo_code_id=promo_code_id, customerid=orgid))
debug(f'register: customer {orgid} bound via promo_code {promo_code_id}')
except Exception as e:
exception(f'register: bind_customer failed for {orgid} via {promo_code_id}: {e}')
# 注册成功后自动登录
await remember_user(user.id, username=user.username, userorgid=user.orgid)
return {
"widgettype": "Message",
"options": {
"timeout": 3,
"auto_open": True,
"title": "注册成功",
"message": f"{user.username} 注册成功,已自动登录"
},
"binds": [
{
"wid": "self",
"event": "dismissed",
"actiontype": "urlwidget",
"target": "window.user_container",
"options": {
"url": entire_url('/rbac/user/userinfo.ui')
}
},
{
"wid": "self",
"event": "dismissed",
"actiontype": "script",
"target": "body.login_window",
"script": "if(this.destroy) this.destroy()"
},
{
"wid": "self",
"event": "dismissed",
"actiontype": "script",
"target": "self",
"script": "if(bricks.app && bricks.app.dispatch) bricks.app.dispatch('user_logined')"
}
]
}
return {
"widgettype": "Error",
"options": {
"timeout": 5,
"title": "注册失败",
"message": "系统错误,请稍后重试"
}
}