"""小程序频道与可配置图标(按俱乐部)。""" from jituan.services.display_config import _display_club_id ICON_FOLDER = 'beijing/tubiao/chengxutubiao' # icon_key -> 后台展示名(文件名固定为 {icon_key}.png) MINIAPP_ICON_META = [ ('merchant_gold_banner', '商家金牌横幅(抢单/订单列表顶部)'), # 商家首页大图(与「我的」页小图标分开配置) ('merchant_home_regular', '商家首页-常规派单大图'), ('merchant_home_custom', '商家首页-自定义派单大图'), ('merchant_home_link', '商家首页-链接派单大图'), ('merchant_home_kefu_key', '商家首页-客服密钥'), ('merchant_home_kefu_list', '商家首页-客服列表'), ('merchant_home_notice', '商家首页-公告图标'), ('merchant_home_stat_bg', '商家首页-统计卡背景'), # 商家「我的」页功能小图标 ('merchant_regular_dispatch', '我的页-常规派单'), ('merchant_custom_dispatch', '我的页-自定义派单'), ('merchant_pending_settle', '我的页-待清算'), ('merchant_kefu_key', '我的页-客服密钥'), ('merchant_kefu_list', '我的页-客服列表'), ('merchant_link_dispatch', '我的页-链接派单'), ('merchant_punish', '我的页-处罚记录'), ('merchant_rank', '我的页-商家排行'), ('merchant_refresh', '我的页-刷新'), ('fighter_recharge_member', '打手中心-会员充值横幅(左侧)'), ('fighter_recharge_deposit', '打手中心-保证金充值横幅(右侧)'), ('mine_pindao', '频道入口'), ('icon_refresh', '通用刷新'), ('guanshi_poster_bg', '管事推广海报背景'), ('zuzhang_poster_bg', '组长推广海报背景'), ] MINIAPP_ICON_KEYS = [k for k, _ in MINIAPP_ICON_META] MINIAPP_ICON_LABELS = dict(MINIAPP_ICON_META) # 无 DB 记录时的默认相对路径(与种子脚本一致) MINIAPP_ICON_DEFAULTS = { key: f'{ICON_FOLDER}/{key}.png' for key in MINIAPP_ICON_KEYS } # 部分历史资源扩展名不同 MINIAPP_ICON_DEFAULTS['icon_refresh'] = 'beijing/guanshiduan/icon-refresh.png' MINIAPP_ICON_DEFAULTS['merchant_refresh'] = 'beijing/guanshiduan/icon-refresh.png' MINIAPP_ICON_DEFAULTS['merchant_gold_banner'] = 'beijing/tubiao/chengxutubiao/merchant_gold_banner.png' MINIAPP_ICON_DEFAULTS['merchant_home_regular'] = 'beijing/shangjiaduan/home/regular_dispatch.png' MINIAPP_ICON_DEFAULTS['merchant_home_custom'] = 'beijing/shangjiaduan/home/custom_dispatch.png' MINIAPP_ICON_DEFAULTS['merchant_home_link'] = 'beijing/shangjiaduan/home/link_dispatch.png' MINIAPP_ICON_DEFAULTS['merchant_home_kefu_key'] = 'beijing/shangjiaduan/home/kefu_key.png' MINIAPP_ICON_DEFAULTS['merchant_home_kefu_list'] = 'beijing/shangjiaduan/home/kefu_list.png' MINIAPP_ICON_DEFAULTS['merchant_home_notice'] = 'beijing/shangjiaduan/home/notice.png' MINIAPP_ICON_DEFAULTS['merchant_home_stat_bg'] = 'beijing/shangjiaduan/stat_card_bg.png' MINIAPP_ICON_DEFAULTS['fighter_recharge_member'] = f'{ICON_FOLDER}/fighter_recharge_member.png' MINIAPP_ICON_DEFAULTS['fighter_recharge_deposit'] = f'{ICON_FOLDER}/fighter_recharge_deposit.png' MINIAPP_ICON_DEFAULTS['mine_pindao'] = 'beijing/tubiao/grzx_guanzhualong.jpg' MINIAPP_ICON_DEFAULTS['guanshi_poster_bg'] = 'beijing/haibaobeijing.jpg' MINIAPP_ICON_DEFAULTS['zuzhang_poster_bg'] = 'beijing/zuzhangbeijing.jpg' POSTER_BG_FIELDS = { 'guanshi_bg': 'guanshi_poster_bg', 'zuzhang_bg': 'zuzhang_poster_bg', } POSTER_BG_DEFAULTS = { 'guanshi_bg': 'beijing/haibaobeijing.jpg', 'zuzhang_bg': 'beijing/zuzhangbeijing.jpg', } POSTER_FOLDER = 'beijing/haibao' POSTER_ICON_KEYS = frozenset(POSTER_BG_FIELDS.values()) def default_icon_path(icon_key: str) -> str: return MINIAPP_ICON_DEFAULTS.get(icon_key) or f'{ICON_FOLDER}/{icon_key}.png' def get_miniapp_icons_dict(club_id: str) -> dict: from config.models import ClubMiniappIcon rows = ClubMiniappIcon.query.filter(club_id=club_id, is_active=True, is_uploaded=True) return { r.icon_key: (r.image_path or '').strip() for r in rows if r.icon_key and (r.image_path or '').strip() } def get_pindao_payload(club_id: str) -> dict: from config.models import ClubPindao, ClubPindaoImage row = ClubPindao.query.filter(club_id=club_id, is_active=True).first() if not row: return {'channel_no': '', 'title': '频道', 'images': []} images = [ img.image_path for img in ClubPindaoImage.query.filter(club_id=club_id).order_by('sort_order', 'id') if img.image_path ] return { 'channel_no': row.channel_no or '', 'title': row.title or '频道', 'images': images, } def get_poster_payload(club_id: str) -> dict: """各俱乐部推广海报背景相对路径(后台上传后覆盖默认)。""" from config.models import ClubMiniappIcon rows = { r.icon_key: r for r in ClubMiniappIcon.query.filter(club_id=club_id, is_active=True) } result = {} for field, icon_key in POSTER_BG_FIELDS.items(): default = POSTER_BG_DEFAULTS[field] row = rows.get(icon_key) if row and row.is_uploaded and (row.image_path or '').strip(): result[field] = row.image_path.strip() else: result[field] = default return result def build_miniapp_assets_payload(request) -> dict: club_id = _display_club_id(request) return { 'icons': get_miniapp_icons_dict(club_id), 'pindao': get_pindao_payload(club_id), 'poster': get_poster_payload(club_id), } def seed_miniapp_icons_for_club(club_id: str, overwrite: bool = False) -> int: from config.models import ClubMiniappIcon created = 0 for key, label in MINIAPP_ICON_META: path = default_icon_path(key) row = ClubMiniappIcon.query.filter(club_id=club_id, icon_key=key).first() if row: if overwrite: row.label = label row.image_path = path row.is_active = True row.save(update_fields=['label', 'image_path', 'is_active', 'UpdateTime']) continue ClubMiniappIcon.query.create( club_id=club_id, icon_key=key, label=label, image_path=path, is_active=True, ) created += 1 return created def copy_miniapp_assets(template_club_id: str, new_club_id: str): from config.models import ClubMiniappIcon, ClubPindao, ClubPindaoImage for row in ClubMiniappIcon.query.filter(club_id=template_club_id): if ClubMiniappIcon.query.filter(club_id=new_club_id, icon_key=row.icon_key).exists(): continue ClubMiniappIcon.query.create( club_id=new_club_id, icon_key=row.icon_key, label=row.label, image_path=row.image_path, description=row.description, is_active=row.is_active, is_uploaded=row.is_uploaded, ) src_pindao = ClubPindao.query.filter(club_id=template_club_id).first() if not src_pindao or ClubPindao.query.filter(club_id=new_club_id).exists(): return new_pindao = ClubPindao.query.create( club_id=new_club_id, channel_no=src_pindao.channel_no, title=src_pindao.title, is_active=src_pindao.is_active, ) for img in ClubPindaoImage.query.filter(club_id=template_club_id).order_by('sort_order', 'id'): ClubPindaoImage.query.create( club_id=new_club_id, pindao=new_pindao, image_path=img.image_path, sort_order=img.sort_order, )