Files
Django/jituan/services/default_invite.py
T

70 lines
2.3 KiB
Python

"""俱乐部默认打手邀请码(一键登录无扫码时使用)。"""
import logging
logger = logging.getLogger(__name__)
CONFIG_KEY = 'default_dashou_invite'
# 与 club_migrate.CLUB_MIGRATE_DEFAULT_GUANSHI_INVITE 保持同步(无 config_json 时回落)
_FALLBACK_DEFAULT_INVITE = {
'lxs': '0000008VfJKPu4Spmcjt',
'xq': '0000008RffVgKHMj7kQC',
}
def get_club_default_dashou_invite_raw(club) -> str:
"""只读 config_json 里真正保存的值(后台表单用,不含回落)。"""
if not club:
return ''
cfg = getattr(club, 'config_json', None) or {}
if not isinstance(cfg, dict):
return ''
return (cfg.get(CONFIG_KEY) or '').strip()
def get_club_fallback_dashou_invite(club_id: str) -> str:
"""系统内置回落(仅小程序运行时;后台不应把它当已保存值展示)。"""
cid = (club_id or '').strip()
if not cid:
return ''
try:
from jituan.services.club_migrate import CLUB_MIGRATE_DEFAULT_GUANSHI_INVITE
code = (CLUB_MIGRATE_DEFAULT_GUANSHI_INVITE.get(cid) or '').strip()
if code:
return code
except Exception:
pass
return (_FALLBACK_DEFAULT_INVITE.get(cid) or '').strip()
def get_club_default_dashou_invite(club_id: str) -> str:
"""
小程序用:优先 config_json.default_dashou_invite,否则回落内置码。
"""
cid = (club_id or '').strip()
if not cid:
return ''
try:
from jituan.models import Club
club = Club.query.filter(club_id=cid).first()
if club:
code = get_club_default_dashou_invite_raw(club)
if code:
return code
except Exception:
logger.exception('read default_dashou_invite failed club=%s', cid)
return get_club_fallback_dashou_invite(cid)
def set_club_default_dashou_invite(club, invite_code: str) -> None:
"""写入 club.config_json,调用方负责 save。空字符串表示清空已保存值。"""
cfg = dict(getattr(club, 'config_json', None) or {})
code = (invite_code or '').strip()
if code:
cfg[CONFIG_KEY] = code
else:
# 清空:删掉键,避免再次读到旧固定值
cfg.pop(CONFIG_KEY, None)
# 强制新 dict,避免 JSONField 变更检测失败
club.config_json = dict(cfg)